class ItbitApiWrapper
Wrapper implementation for Itbit API. api.itbit.com/docs
Attributes
client_key[RW]
default_wallet_id[RW]
sandbox[RW]
secret[RW]
user_id[RW]
Public Class Methods
new(settings)
click to toggle source
# File lib/bitex_bot/models/api_wrappers/itbit/itbit_api_wrapper.rb, line 6 def initialize(settings) self.client_key = settings.client_key self.secret = settings.secret self.user_id = settings.user_id self.default_wallet_id = settings.default_wallet_id self.sandbox = settings.sandbox currency_pair(settings.order_book) setup end
Public Instance Methods
amount_and_quantity(order_id)
click to toggle source
# File lib/bitex_bot/models/api_wrappers/itbit/itbit_api_wrapper.rb, line 26 def amount_and_quantity(order_id) order = Itbit::Order.find(order_id) amount = order.volume_weighted_average_price * order.amount_filled quantity = order.amount_filled [amount, quantity] end
balance()
click to toggle source
# File lib/bitex_bot/models/api_wrappers/itbit/itbit_api_wrapper.rb, line 34 def balance balance_summary_parser(wallet[:balances]) end
balance_parser(balances, currency)
click to toggle source
# File lib/bitex_bot/models/api_wrappers/itbit/itbit_api_wrapper.rb, line 95 def balance_parser(balances, currency) currency_balance = balances.find { |balance| balance[:currency] == currency } Balance.new( currency_balance[:total_balance].to_d, currency_balance[:total_balance].to_d - currency_balance[:available_balance].to_d, currency_balance[:available_balance].to_d ) end
balance_summary_parser(balances)
click to toggle source
[
{ total_balance: 0.2e2, currency: :usd, available_balance: 0.1e2 }, { total_balance: 0.0, currency: :xbt, available_balance: 0.0 }, { total_balance: 0.0, currency: :eur, available_balance: 0.0 }, { total_balance: 0.0, currency: :sgd, available_balance: 0.0 }
]
# File lib/bitex_bot/models/api_wrappers/itbit/itbit_api_wrapper.rb, line 87 def balance_summary_parser(balances) BalanceSummary.new(balance_parser(balances, base.to_sym), balance_parser(balances, quote.to_sym), 0.5.to_d) end
currency_pair(order_book = '')
click to toggle source
# File lib/bitex_bot/models/api_wrappers/itbit/itbit_api_wrapper.rb, line 139 def currency_pair(order_book = '') @currency_pair ||= { name: order_book, base: order_book.slice(0..2), quote: order_book.slice(3..6) } end
find_lost(type, price, _quantity)
click to toggle source
# File lib/bitex_bot/models/api_wrappers/itbit/itbit_api_wrapper.rb, line 38 def find_lost(type, price, _quantity) orders.find { |o| o.type == type && o.price == price && o.timestamp >= 5.minutes.ago.to_i } end
last_order_by(price)
click to toggle source
# File lib/bitex_bot/models/api_wrappers/itbit/itbit_api_wrapper.rb, line 104 def last_order_by(price) Itbit::Order.all.select { |o| o.price == price && (o.created_time - Time.now.to_i).abs < 500 }.first end
market()
click to toggle source
# File lib/bitex_bot/models/api_wrappers/itbit/itbit_api_wrapper.rb, line 135 def market "Itbit::#{currency_pair[:name].upcase}MarketData".constantize end
order_book()
click to toggle source
# File lib/bitex_bot/models/api_wrappers/itbit/itbit_api_wrapper.rb, line 42 def order_book order_book_parser(market.orders) end
order_book_parser(book)
click to toggle source
{
bids: [[0.63921e3, 0.195e1], [0.637e3, 0.47e0], [0.63e3, 0.158e1]], asks: [[0.6424e3, 0.4e0], [0.6433e3, 0.95e0], [0.6443e3, 0.25e0]]
}
# File lib/bitex_bot/models/api_wrappers/itbit/itbit_api_wrapper.rb, line 112 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)
click to toggle source
<Itbit::Order:
@id='8fd820d3-baff-4d6f-9439-ff03d816c7ce', @wallet_id='b440efce-a83c-4873-8833-802a1022b476', @side=:buy, @instrument=:xbtusd, @type=:limit, @amount=0.1005e1, @display_amount=0.1005e1, @price=0.1e3, @volume_weighted_average_price=0.0, @amount_filled=0.0, @created_time=1415290187, @status=:open, @metadata={foo: 'bar'}, @client_order_identifier='o'
>
# File lib/bitex_bot/models/api_wrappers/itbit/itbit_api_wrapper.rb, line 126 def order_parser(order) Order.new(order.id, order.side, order.price, order.amount, order.created_time, order) end
order_summary_parser(orders)
click to toggle source
# File lib/bitex_bot/models/api_wrappers/itbit/itbit_api_wrapper.rb, line 116 def order_summary_parser(orders) orders.map { |order| OrderSummary.new(order[0], order[1]) } end
orders()
click to toggle source
# File lib/bitex_bot/models/api_wrappers/itbit/itbit_api_wrapper.rb, line 46 def orders Itbit::Order.all(instrument: currency_pair[:name], status: :open).map { |o| order_parser(o) } end
send_order(type, price, quantity)
click to toggle source
# File lib/bitex_bot/models/api_wrappers/itbit/itbit_api_wrapper.rb, line 50 def send_order(type, price, quantity) order = Itbit::Order.create!( type, currency_pair[:name], quantity.round(4), price.round(2), wait: true, currency: currency_pair[:base] ) order_parser(order) if order.present? rescue RestClient::RequestTimeout => e # On timeout errors, we still look for the latest active closing order that may be available. # We have a magic threshold of 5 minutes and also use the price to recognize an order as the current one. # TODO: Maybe we can identify the order using metadata instead of price. BitexBot::Robot.log(:error, 'Captured Timeout on itbit') latest = last_order_by(price) return latest if latest.present? BitexBot::Robot.log(:error, 'Could not find my order') raise e end
setup()
click to toggle source
# File lib/bitex_bot/models/api_wrappers/itbit/itbit_api_wrapper.rb, line 16 def setup Itbit.tap do |conf| conf.client_key = client_key conf.secret = secret conf.user_id = user_id conf.default_wallet_id = default_wallet_id conf.sandbox = sandbox end end
transaction_parser(transaction)
click to toggle source
{ tid: 601855, price: 0.41814e3, amount: 0.19e-1, date: 1460161126 }
# File lib/bitex_bot/models/api_wrappers/itbit/itbit_api_wrapper.rb, line 131 def transaction_parser(transaction) Transaction.new(transaction[:tid], transaction[:price], transaction[:amount], transaction[:date], transaction) end
transactions()
click to toggle source
# File lib/bitex_bot/models/api_wrappers/itbit/itbit_api_wrapper.rb, line 72 def transactions market.trades.map { |t| transaction_parser(t.symbolize_keys) } end
user_transactions()
click to toggle source
We don't need to fetch the list of transaction for itbit since we wont actually use them later.
# File lib/bitex_bot/models/api_wrappers/itbit/itbit_api_wrapper.rb, line 77 def user_transactions [] end
wallet()
click to toggle source
# File lib/bitex_bot/models/api_wrappers/itbit/itbit_api_wrapper.rb, line 91 def wallet Itbit::Wallet.all.find { |w| w[:id] == Itbit.default_wallet_id } end