Class: ItbitApiWrapper

Inherits:
Object
  • Object
show all
Defined in:
lib/bitex_bot/models/itbit_api_wrapper.rb

Class Method Summary collapse

Class Method Details

.amount_and_quantity(order_id, transactions) ⇒ Object



43
44
45
46
# File 'lib/bitex_bot/models/itbit_api_wrapper.rb', line 43

def self.amount_and_quantity(order_id, transactions)
  order = Itbit::Order.find(order_id)
  [order.volume_weighted_average_price * order.amount_filled, order.amount_filled]
end

.balanceObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/bitex_bot/models/itbit_api_wrapper.rb', line 18

def self.balance
  balances = Itbit::Wallet.all
    .find{|i| i[:id] == Itbit.default_wallet_id }[:balances]
  usd = balances.find{|x| x[:currency] == :usd }
  btc = balances.find{|x| x[:currency] == :xbt }
  { "btc_balance" => btc[:total_balance],
    "btc_reserved" => btc[:total_balance] - btc[:available_balance],
    "btc_available" => btc[:available_balance],
    "usd_balance" => usd[:total_balance],
    "usd_reserved" => usd[:total_balance] - usd[:available_balance],
    "usd_available" => usd[:available_balance],
    "fee" => 0.5
  }
end

.order_bookObject



14
15
16
# File 'lib/bitex_bot/models/itbit_api_wrapper.rb', line 14

def self.order_book
  Itbit::XBTUSDMarketData.orders.stringify_keys
end

.ordersObject



33
34
35
# File 'lib/bitex_bot/models/itbit_api_wrapper.rb', line 33

def self.orders
  Itbit::Order.all(status: :open)
end

.place_order(type, price, quantity) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/bitex_bot/models/itbit_api_wrapper.rb', line 48

def self.place_order(type, price, quantity)
  begin
    return Itbit::Order.create!(type, :xbtusd, quantity.round(4), price.round(2), wait: true)
  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.logger.error("Captured Timeout on itbit")
    latest = Itbit::Order.all.select do |x|
      x.price == price && (x.created_time - Time.now.to_i).abs < 500
    end.first
    if latest
      return latest
    else
      BitexBot::Robot.logger.error("Could not find my order")
      raise e
    end
  end
end

.setup(settings) ⇒ Object



2
3
4
5
6
7
8
# File 'lib/bitex_bot/models/itbit_api_wrapper.rb', line 2

def self.setup(settings)
  Itbit.client_key = settings.itbit.client_key
  Itbit.secret = settings.itbit.secret
  Itbit.user_id = settings.itbit.user_id
  Itbit.default_wallet_id = settings.itbit.default_wallet_id
  Itbit.sandbox = settings.sandbox
end

.transactionsObject



10
11
12
# File 'lib/bitex_bot/models/itbit_api_wrapper.rb', line 10

def self.transactions
  Itbit::XBTUSDMarketData.trades.collect{|t| Hashie::Mash.new(t) }
end

.user_transactionsObject

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



39
40
41
# File 'lib/bitex_bot/models/itbit_api_wrapper.rb', line 39

def self.user_transactions
  []
end