Class: KrakenApiWrapper
- Inherits:
-
ApiWrapper
- Object
- ApiWrapper
- KrakenApiWrapper
- Defined in:
- lib/bitex_bot/models/api_wrappers/kraken/kraken_api_wrapper.rb
Overview
Wrapper implementation for Kraken API. www.kraken.com/en-us/help/api
Constant Summary collapse
- MIN_AMOUNT =
0.002
Instance Attribute Summary collapse
-
#api_key ⇒ Object
Returns the value of attribute api_key.
-
#api_secret ⇒ Object
Returns the value of attribute api_secret.
-
#client ⇒ Object
Returns the value of attribute client.
Instance Method Summary collapse
- #amount_and_quantity(order_id) ⇒ Object
- #balance ⇒ Object
-
#balance_parser(balances, currency, reserved) ⇒ Object
rubocop:enable Metrics/AbcSize.
-
#balance_summary_parser(balances) ⇒ Object
{ ZEUR: ‘1433.0939’, XXBT: ‘0.0000000000’, ‘XETH’: ‘99.7497224800’ } rubocop:disable Metrics/AbcSize.
- #crypto_reserved(open_orders) ⇒ Object
-
#currency_pair ⇒ Object
{ ‘XBTUSD’ => { ‘altname’ => ‘XBTUSD’, ‘aclass_base’ => ‘currency’, ‘base’ => ‘XXBT’, ‘aclass_quote’ => ‘currency’, ‘quote’ => ‘ZUSD’, ‘lot’ => ‘unit’, ‘pair_decimals’ => 1, ‘lot_decimals’ => 8, ‘lot_multiplier’ => 1, ‘leverage_buy’ => [2, 3, 4, 5], ‘leverage_sell’ => [2, 3, 4, 5], ‘fees’ => [[0, 0.26], .., [250_000, 0.2]], ‘fees_maker’ => [[0, 0.16], .., [250_000, 0.1]], ‘fee_volume_currency’ => ‘ZUSD’, ‘margin_call’ => 80, ‘margin_stop’ => 40 } }.
- #enough_order_size?(quantity, _price) ⇒ Boolean
- #fiat_reserved(open_orders) ⇒ Object
- #find_lost(type, price, quantity) ⇒ Object
-
#initialize(settings) ⇒ KrakenApiWrapper
constructor
A new instance of KrakenApiWrapper.
- #order_book ⇒ Object
-
#order_book_parser(book) ⇒ Object
{ ‘asks’: [[‘204.52893’, ‘0.010’, 1440291148], [‘204.78790’, ‘0.312’, 1440291132]], ‘bids’: [[‘204.24000’, ‘0.100’, 1440291016], [‘204.23010’, ‘0.312’, 1440290699]] }.
-
#order_parser(order) ⇒ Object
<KrakenOrder:0x007faf255382d0 @id=“OGZ3HI-5I322-OIOV52”, @type=:sell, @datetime=1546971756, @amount=0.248752e-2, @executed_amount=0.248752e-2, @price=0.40025e4, @avg_price=0.40074e4>.
- #order_summary_parser(stock_market) ⇒ Object
- #orders ⇒ Object
- #orders_by(open_orders, order_type) ⇒ Object
- #send_order(type, price, quantity) ⇒ Object
- #setup ⇒ Object
-
#transaction_parser(transaction) ⇒ Object
- [‘price’, ‘amount’, ‘timestamp’, ‘buy/sell’, ‘market/limit’, ‘miscellaneous’
- ‘202.51626’, ‘0.01440000’, 1440277319.1922, ‘b’, ‘l’, ”], [‘202.54000’, ‘0.10000000’, 1440277322.8993, ‘b’, ‘l’, ”
-
].
- #transactions ⇒ Object
-
#user_transactions ⇒ Object
We don’t need to fetch the list of transactions for Kraken.
Methods inherited from ApiWrapper
#base, #base_quote, #cancel, #name, #place_order, #quote
Constructor Details
#initialize(settings) ⇒ KrakenApiWrapper
Returns a new instance of KrakenApiWrapper.
8 9 10 11 12 |
# File 'lib/bitex_bot/models/api_wrappers/kraken/kraken_api_wrapper.rb', line 8 def initialize(settings) self.api_key = settings.api_key self.api_secret = settings.api_secret setup end |
Instance Attribute Details
#api_key ⇒ Object
Returns the value of attribute api_key.
4 5 6 |
# File 'lib/bitex_bot/models/api_wrappers/kraken/kraken_api_wrapper.rb', line 4 def api_key @api_key end |
#api_secret ⇒ Object
Returns the value of attribute api_secret.
4 5 6 |
# File 'lib/bitex_bot/models/api_wrappers/kraken/kraken_api_wrapper.rb', line 4 def api_secret @api_secret end |
#client ⇒ Object
Returns the value of attribute client.
4 5 6 |
# File 'lib/bitex_bot/models/api_wrappers/kraken/kraken_api_wrapper.rb', line 4 def client @client end |
Instance Method Details
#amount_and_quantity(order_id) ⇒ Object
20 21 22 |
# File 'lib/bitex_bot/models/api_wrappers/kraken/kraken_api_wrapper.rb', line 20 def amount_and_quantity(order_id) KrakenOrder.amount_and_quantity(order_id) end |
#balance ⇒ Object
24 25 26 27 28 |
# File 'lib/bitex_bot/models/api_wrappers/kraken/kraken_api_wrapper.rb', line 24 def balance balance_summary_parser(client.private.balance) rescue KrakenClient::ErrorResponse, Net::ReadTimeout retry end |
#balance_parser(balances, currency, reserved) ⇒ Object
rubocop:enable Metrics/AbcSize
77 78 79 |
# File 'lib/bitex_bot/models/api_wrappers/kraken/kraken_api_wrapper.rb', line 77 def balance_parser(balances, currency, reserved) Balance.new(balances[currency].to_d, reserved, balances[currency].to_d - reserved) end |
#balance_summary_parser(balances) ⇒ Object
{ ZEUR: ‘1433.0939’, XXBT: ‘0.0000000000’, ‘XETH’: ‘99.7497224800’ } rubocop:disable Metrics/AbcSize
67 68 69 70 71 72 73 74 |
# File 'lib/bitex_bot/models/api_wrappers/kraken/kraken_api_wrapper.rb', line 67 def balance_summary_parser(balances) open_orders = KrakenOrder.open BalanceSummary.new( balance_parser(balances, currency_pair[:base], crypto_reserved(open_orders)), balance_parser(balances, currency_pair[:quote], fiat_reserved(open_orders)), client.private.trade_volume(pair: currency_pair[:altname])[:fees][currency_pair[:name]][:fee].to_d ) end |
#crypto_reserved(open_orders) ⇒ Object
81 82 83 |
# File 'lib/bitex_bot/models/api_wrappers/kraken/kraken_api_wrapper.rb', line 81 def crypto_reserved(open_orders) orders_by(open_orders, :sell).sum { |o| (o.amount - o.executed_amount).to_d } end |
#currency_pair ⇒ Object
{
'XBTUSD' => {
'altname' => 'XBTUSD',
'aclass_base' => 'currency',
'base' => 'XXBT',
'aclass_quote' => 'currency',
'quote' => 'ZUSD',
'lot' => 'unit',
'pair_decimals' => 1,
'lot_decimals' => 8,
'lot_multiplier' => 1,
'leverage_buy' => [2, 3, 4, 5],
'leverage_sell' => [2, 3, 4, 5],
'fees' => [[0, 0.26], .., [250_000, 0.2]],
'fees_maker' => [[0, 0.16], .., [250_000, 0.1]],
'fee_volume_currency' => 'ZUSD',
'margin_call' => 80,
'margin_stop' => 40
}
}
140 141 142 143 144 |
# File 'lib/bitex_bot/models/api_wrappers/kraken/kraken_api_wrapper.rb', line 140 def currency_pair @currency_pair ||= client.public.asset_pairs.map do |currency_pair, data| [data['altname'], data.merge(name: currency_pair).with_indifferent_access] end.to_h[BitexBot::Settings.taker_settings.order_book.upcase] end |
#enough_order_size?(quantity, _price) ⇒ Boolean
30 31 32 |
# File 'lib/bitex_bot/models/api_wrappers/kraken/kraken_api_wrapper.rb', line 30 def enough_order_size?(quantity, _price) quantity >= MIN_AMOUNT end |
#fiat_reserved(open_orders) ⇒ Object
85 86 87 |
# File 'lib/bitex_bot/models/api_wrappers/kraken/kraken_api_wrapper.rb', line 85 def fiat_reserved(open_orders) orders_by(open_orders, :buy).sum { |o| (o.amount - o.executed_amount) * o.price.to_d } end |
#find_lost(type, price, quantity) ⇒ Object
34 35 36 37 |
# File 'lib/bitex_bot/models/api_wrappers/kraken/kraken_api_wrapper.rb', line 34 def find_lost(type, price, quantity) order = KrakenOrder.find_lost(type, price, quantity) order_parser(order) if order.present? end |
#order_book ⇒ Object
39 40 41 42 43 |
# File 'lib/bitex_bot/models/api_wrappers/kraken/kraken_api_wrapper.rb', line 39 def order_book order_book_parser(client.public.order_book(currency_pair[:altname])[currency_pair[:name]]) rescue NoMethodError retry end |
#order_book_parser(book) ⇒ Object
'asks': [['204.52893', '0.010', 1440291148], ['204.78790', '0.312', 1440291132]],
'bids': [['204.24000', '0.100', 1440291016], ['204.23010', '0.312', 1440290699]]
97 98 99 |
# File 'lib/bitex_bot/models/api_wrappers/kraken/kraken_api_wrapper.rb', line 97 def order_book_parser(book) OrderBook.new(Time.now.to_i, order_summary_parser(book[:bids]), order_summary_parser(book[:asks])) end |
#order_parser(order) ⇒ Object
<KrakenOrder:0x007faf255382d0 @id=“OGZ3HI-5I322-OIOV52”, @type=:sell, @datetime=1546971756, @amount=0.248752e-2,
@executed_amount=0.248752e-2, @price=0.40025e4, @avg_price=0.40074e4>
107 108 109 |
# File 'lib/bitex_bot/models/api_wrappers/kraken/kraken_api_wrapper.rb', line 107 def order_parser(order) Order.new(order.id, order.type, order.price, order.amount, order.datetime, order) end |
#order_summary_parser(stock_market) ⇒ Object
101 102 103 |
# File 'lib/bitex_bot/models/api_wrappers/kraken/kraken_api_wrapper.rb', line 101 def order_summary_parser(stock_market) stock_market.map { |stock| OrderSummary.new(stock[0].to_d, stock[1].to_d) } end |
#orders ⇒ Object
45 46 47 |
# File 'lib/bitex_bot/models/api_wrappers/kraken/kraken_api_wrapper.rb', line 45 def orders KrakenOrder.open.map { |o| order_parser(o) } end |
#orders_by(open_orders, order_type) ⇒ Object
89 90 91 |
# File 'lib/bitex_bot/models/api_wrappers/kraken/kraken_api_wrapper.rb', line 89 def orders_by(open_orders, order_type) open_orders.select { |o| o.type == order_type } end |
#send_order(type, price, quantity) ⇒ Object
49 50 51 52 |
# File 'lib/bitex_bot/models/api_wrappers/kraken/kraken_api_wrapper.rb', line 49 def send_order(type, price, quantity) order = KrakenOrder.create!(type, price, quantity) order_parser(order) if order.present? end |
#setup ⇒ Object
14 15 16 17 18 |
# File 'lib/bitex_bot/models/api_wrappers/kraken/kraken_api_wrapper.rb', line 14 def setup KrakenOrder.api_wrapper = self self.client ||= KrakenClient.load(api_key: api_key, api_secret: api_secret) HTTParty::Basement.headers('User-Agent' => BitexBot.user_agent) end |
#transaction_parser(transaction) ⇒ Object
[
['price', 'amount', 'timestamp', 'buy/sell', 'market/limit', 'miscellaneous']
['202.51626', '0.01440000', 1440277319.1922, 'b', 'l', ''],
['202.54000', '0.10000000', 1440277322.8993, 'b', 'l', '']
]
116 117 118 |
# File 'lib/bitex_bot/models/api_wrappers/kraken/kraken_api_wrapper.rb', line 116 def transaction_parser(transaction) Transaction.new(transaction[2].to_i, transaction[0].to_d, transaction[1].to_d, transaction[2].to_i) end |
#transactions ⇒ Object
54 55 56 57 58 |
# File 'lib/bitex_bot/models/api_wrappers/kraken/kraken_api_wrapper.rb', line 54 def transactions client.public.trades(currency_pair[:altname])[currency_pair[:name]].reverse.map { |t| transaction_parser(t) } rescue NoMethodError retry end |
#user_transactions ⇒ Object
We don’t need to fetch the list of transactions for Kraken
61 62 63 |
# File 'lib/bitex_bot/models/api_wrappers/kraken/kraken_api_wrapper.rb', line 61 def user_transactions [] end |