Class: ItbitApiWrapper

Inherits:
ApiWrapper show all
Defined in:
lib/bitex_bot/models/api_wrappers/itbit/itbit_api_wrapper.rb

Overview

Wrapper implementation for Itbit API. api.itbit.com/docs

Constant Summary

Constants inherited from ApiWrapper

ApiWrapper::MIN_AMOUNT

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from ApiWrapper

#base, #base_quote, #cancel, #enough_order_size?, #name, #place_order, #quote

Constructor Details

#initialize(settings) ⇒ ItbitApiWrapper

Returns a new instance of ItbitApiWrapper.



6
7
8
9
10
11
12
13
14
# 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

Instance Attribute Details

#client_keyObject

Returns the value of attribute client_key.



4
5
6
# File 'lib/bitex_bot/models/api_wrappers/itbit/itbit_api_wrapper.rb', line 4

def client_key
  @client_key
end

#default_wallet_idObject

Returns the value of attribute default_wallet_id.



4
5
6
# File 'lib/bitex_bot/models/api_wrappers/itbit/itbit_api_wrapper.rb', line 4

def default_wallet_id
  @default_wallet_id
end

#sandboxObject

Returns the value of attribute sandbox.



4
5
6
# File 'lib/bitex_bot/models/api_wrappers/itbit/itbit_api_wrapper.rb', line 4

def sandbox
  @sandbox
end

#secretObject

Returns the value of attribute secret.



4
5
6
# File 'lib/bitex_bot/models/api_wrappers/itbit/itbit_api_wrapper.rb', line 4

def secret
  @secret
end

#user_idObject

Returns the value of attribute user_id.



4
5
6
# File 'lib/bitex_bot/models/api_wrappers/itbit/itbit_api_wrapper.rb', line 4

def user_id
  @user_id
end

Instance Method Details

#amount_and_quantity(order_id) ⇒ Object



26
27
28
29
30
31
32
# 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

#balanceObject



34
35
36
# 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) ⇒ Object



95
96
97
98
99
100
101
102
# 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) ⇒ Object

[

{ 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 }

]



87
88
89
# 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 = '') ⇒ Object



139
140
141
142
143
144
145
# 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) ⇒ Object



38
39
40
# 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) ⇒ Object



104
105
106
# 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

#marketObject



135
136
137
# 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_bookObject



42
43
44
# 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) ⇒ Object

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]]



112
113
114
# 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) ⇒ Object

<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'

>



126
127
128
# 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) ⇒ Object



116
117
118
# 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

#ordersObject



46
47
48
# 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) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# 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

#setupObject



16
17
18
19
20
21
22
23
24
# 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) ⇒ Object

{ tid: 601855, price: 0.41814e3, amount: 0.19e-1, date: 1460161126 }



131
132
133
# 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

#transactionsObject



72
73
74
# 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_transactionsObject

We don’t need to fetch the list of transaction for itbit since we wont actually use them later.



77
78
79
# File 'lib/bitex_bot/models/api_wrappers/itbit/itbit_api_wrapper.rb', line 77

def user_transactions
  []
end

#walletObject



91
92
93
# 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