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
88 89 90 91 92 93 |
# File 'lib/bitex_bot/models/bitfinex_api_wrapper.rb', line 88 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
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/bitex_bot/models/bitfinex_api_wrapper.rb', line 58 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
50 51 52 53 54 55 56 |
# File 'lib/bitex_bot/models/bitfinex_api_wrapper.rb', line 50 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
76 77 78 79 80 |
# File 'lib/bitex_bot/models/bitfinex_api_wrapper.rb', line 76 def self.orders with_retry 'orders' do Bitfinex::Client.new.orders.collect{|o| BitfinexOrder.new(o) } end end |
.place_order(type, price, quantity) ⇒ Object
95 96 97 98 99 100 101 |
# File 'lib/bitex_bot/models/bitfinex_api_wrapper.rb', line 95 def self.place_order(type, price, quantity) with_retry "place order #{type} #{price} #{quantity}" do order_data = Bitfinex::Client.new .new_order('btcusd', quantity.round(4), 'exchange limit', type.to_s, price.round(2)) BitfinexOrder.new(order_data) end end |
.setup(settings) ⇒ Object
19 20 21 22 23 24 |
# File 'lib/bitex_bot/models/bitfinex_api_wrapper.rb', line 19 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
37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/bitex_bot/models/bitfinex_api_wrapper.rb', line 37 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
84 85 86 |
# File 'lib/bitex_bot/models/bitfinex_api_wrapper.rb', line 84 def self.user_transactions [] end |
.with_retry(action, &block) ⇒ Object
26 27 28 29 30 31 32 33 34 35 |
# File 'lib/bitex_bot/models/bitfinex_api_wrapper.rb', line 26 def self.with_retry(action, &block) begin block.call rescue StandardError, Bitfinex::ClientError => e raise e if BitexBot::Robot.test_mode BitexBot::Robot.logger.info("Bitfinex #{action} failed. Retrying in 5 seconds.") sleep 5 retry end end |