Class: BitfinexApiWrapper
- Inherits:
-
Object
- Object
- BitfinexApiWrapper
- Defined in:
- lib/bitex_bot/models/bitfinex_api_wrapper.rb
Class Method Summary collapse
- .amount_and_quantity(order_id, transactions) ⇒ Object
- .balance ⇒ Object
- .order_book ⇒ Object
- .orders ⇒ Object
- .place_order(type, price, quantity) ⇒ Object
- .setup(settings) ⇒ Object
- .transactions ⇒ Object
-
.user_transactions ⇒ Object
We don’t need to fetch the list of transactions for bitfinex.
- .with_retry(action, &block) ⇒ Object
Class Method Details
.amount_and_quantity(order_id, transactions) ⇒ Object
73 74 75 76 77 78 |
# File 'lib/bitex_bot/models/bitfinex_api_wrapper.rb', line 73 def self.amount_and_quantity(order_id, transactions) with_retry "find order #{order_id}" do order = Bitfinex::Client.new.order_status(order_id) [order['avg_execution_price'].to_d * order['executed_amount'].to_d, order['executed_amount'].to_d] end end |
.balance ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/bitex_bot/models/bitfinex_api_wrapper.rb', line 43 def self.balance with_retry 'balance' do balances = Bitfinex::Client.new.balances(type: 'exchange') sleep 1 # Sleep to avoid sending two consecutive requests to bitfinex. fee = Bitfinex::Client.new.account_info.first['taker_fees'] btc = balances.find{|b| b['currency'] == 'btc' } || {} usd = balances.find{|b| b['currency'] == 'usd' } || {} { "btc_balance" => btc['amount'].to_d, "btc_reserved" => btc['amount'].to_d - btc['available'].to_d, "btc_available" => btc['available'].to_d, "usd_balance" => usd['amount'].to_d, "usd_reserved" => usd['amount'].to_d - usd['available'].to_d, "usd_available" => usd['available'].to_d, "fee" => fee.to_d } end end |
.order_book ⇒ Object
35 36 37 38 39 40 41 |
# File 'lib/bitex_bot/models/bitfinex_api_wrapper.rb', line 35 def self.order_book with_retry 'order_book' do book = Bitfinex::Client.new.orderbook { 'bids' => book['bids'].collect{|b| [b['price'], b['amount']] }, 'asks' => book['asks'].collect{|a| [a['price'], a['amount']] } } end end |
.orders ⇒ Object
61 62 63 64 65 |
# File 'lib/bitex_bot/models/bitfinex_api_wrapper.rb', line 61 def self.orders with_retry 'orders' do Bitfinex::Client.new.orders.collect{|o| BitfinexOrder.new(o) } end end |
.place_order(type, price, quantity) ⇒ Object
80 81 82 83 84 85 86 |
# File 'lib/bitex_bot/models/bitfinex_api_wrapper.rb', line 80 def self.place_order(type, price, quantity) with_retry "place order #{type} #{price} #{quantity}" do order_data = Bitfinex::Client.new .new_order('btcusd', quantity, 'exchange limit', type.to_s, price) BitfinexOrder.new(order_data) end end |
.setup(settings) ⇒ Object
5 6 7 8 9 10 |
# File 'lib/bitex_bot/models/bitfinex_api_wrapper.rb', line 5 def self.setup(settings) Bitfinex::Client.configure do |conf| conf.api_key = settings.bitfinex.api_key conf.secret = settings.bitfinex.api_secret end end |
.transactions ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/bitex_bot/models/bitfinex_api_wrapper.rb', line 22 def self.transactions with_retry 'transactions' do Bitfinex::Client.new.trades.collect do |t| Hashie::Mash.new({ tid: t['tid'].to_i, price: t['price'], amount: t['amount'], date: t['timestamp'] }) end end end |
.user_transactions ⇒ Object
We don’t need to fetch the list of transactions for bitfinex
69 70 71 |
# File 'lib/bitex_bot/models/bitfinex_api_wrapper.rb', line 69 def self.user_transactions [] end |
.with_retry(action, &block) ⇒ Object
12 13 14 15 16 17 18 19 20 |
# File 'lib/bitex_bot/models/bitfinex_api_wrapper.rb', line 12 def self.with_retry(action, &block) begin block.call rescue StandardError, Bitfinex::ClientError => e BitexBot::Robot.logger.info("Bitfinex #{action} failed. Retrying in 5 seconds.") sleep 5 retry end end |