class BitexApiWrapper

Wrapper implementation for Bitex API. bitex.la/developers

Constants

Order

Attributes

api_key[RW]
debug[RW]
sandbox[RW]
ssl_version[RW]

Public Class Methods

new(settings) click to toggle source
# File lib/bitex_bot/models/api_wrappers/bitex/bitex_api_wrapper.rb, line 23
def initialize(settings)
  self.api_key = settings.api_key
  self.ssl_version = settings.ssl_version
  self.debug = settings.debug
  self.sandbox = settings.sandbox
  currency_pair(settings.order_book)
  setup
end

Public Instance Methods

amount_and_quantity(order_id) click to toggle source

rubocop:disable Metrics/AbcSize

# File lib/bitex_bot/models/api_wrappers/bitex/bitex_api_wrapper.rb, line 44
def amount_and_quantity(order_id)
  closes = user_transactions.select { |t| t.order_id.to_s == order_id }
  amount = closes.map { |c| c.send(currency[:quote]).to_d }.sum.abs
  quantity = closes.map { |c| c.send(currency[:base]).to_d }.sum.abs

  [amount, quantity]
end
balance() click to toggle source

rubocop:enable Metrics/AbcSize

# File lib/bitex_bot/models/api_wrappers/bitex/bitex_api_wrapper.rb, line 53
def balance
  balance_summary_parser(profile)
end
balance_parser(balances, currency) click to toggle source
# File lib/bitex_bot/models/api_wrappers/bitex/bitex_api_wrapper.rb, line 100
def balance_parser(balances, currency)
  Balance.new(
    balances["#{currency}_balance".to_sym].to_d,
    balances["#{currency}_reserved".to_sym].to_d,
    balances["#{currency}_available".to_sym].to_d
  )
end
balance_summary_parser(balances) click to toggle source

{

usd_balance:               10000.00, # Total USD balance.
usd_reserved:               2000.00, # USD reserved in open orders.
usd_available:              8000.00, # USD available for trading.
btc_balance:            20.00000000, # Total BTC balance.
btc_reserved:            5.00000000, # BTC reserved in open orders.
btc_available:          15.00000000, # BTC available for trading.
fee:                            0.5, # Your trading fee (0.5 means 0.5%).
btc_deposit_address: "1XXXXXXXX..."  # Your BTC deposit address.

}

# File lib/bitex_bot/models/api_wrappers/bitex/bitex_api_wrapper.rb, line 92
def balance_summary_parser(balances)
  BalanceSummary.new(
    balance_parser(balances, currency_pair[:base]),
    balance_parser(balances, currency_pair[:quote]),
    balances[:fee].to_d
  )
end
currency_pair(order_book = '_') click to toggle source
# File lib/bitex_bot/models/api_wrappers/bitex/bitex_api_wrapper.rb, line 172
def currency_pair(order_book = '_')
  @currency_pair ||= {
    name: order_book,
    base: order_book.split('_').first,
    quote: order_book.split('_').last
  }
end
find_lost(type, price, _quantity) click to toggle source
# File lib/bitex_bot/models/api_wrappers/bitex/bitex_api_wrapper.rb, line 57
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/bitex/bitex_api_wrapper.rb, line 108
def last_order_by(price)
  orders.select { |o| o.price == price && (o.timestamp - Time.now.to_i).abs < 500 }.first
end
market() click to toggle source
# File lib/bitex_bot/models/api_wrappers/bitex/bitex_api_wrapper.rb, line 168
def market
  @market ||= { btc: Bitex::BitcoinMarketData, bch: Bitex::BitcoinCashMarketData }[currency_pair[:base].to_sym]
end
order_amount(order) click to toggle source
# File lib/bitex_bot/models/api_wrappers/bitex/bitex_api_wrapper.rb, line 136
def order_amount(order)
  order.is_a?(Bitex::Bid) ? order.amount : order.quantity
end
order_book() click to toggle source
# File lib/bitex_bot/models/api_wrappers/bitex/bitex_api_wrapper.rb, line 61
def order_book
  order_book_parser(market.order_book)
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/bitex/bitex_api_wrapper.rb, line 116
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

<Bitex::Bid

@id=12345678, @created_at=1999-12-31 21:10:00 -0300, @order_book=:btc_usd, @price=0.1e4, @status=:executing, @reason=nil,
@issuer=nil, @amount=0.1e3, @remaining_amount=0.1e2, @produced_quantity=0.0

>

# File lib/bitex_bot/models/api_wrappers/bitex/bitex_api_wrapper.rb, line 128
def order_parser(order)
  Order.new(order.id.to_s, order_type(order), order.price, order_amount(order), order.created_at.to_i, order)
end
order_summary_parser(orders) click to toggle source
# File lib/bitex_bot/models/api_wrappers/bitex/bitex_api_wrapper.rb, line 120
def order_summary_parser(orders)
  orders.map { |order| OrderSummary.new(order[0].to_d, order[1].to_d) }
end
order_type(order) click to toggle source
# File lib/bitex_bot/models/api_wrappers/bitex/bitex_api_wrapper.rb, line 132
def order_type(order)
  order.is_a?(Bitex::Bid) ? :buy : :sell
end
orders() click to toggle source
# File lib/bitex_bot/models/api_wrappers/bitex/bitex_api_wrapper.rb, line 65
def orders
  Bitex::Order.all.map { |o| order_parser(o) }
end
profile() click to toggle source
# File lib/bitex_bot/models/api_wrappers/bitex/bitex_api_wrapper.rb, line 39
def profile
  Bitex::Profile.get
end
send_order(type, price, quantity, wait = false) click to toggle source
# File lib/bitex_bot/models/api_wrappers/bitex/bitex_api_wrapper.rb, line 69
def send_order(type, price, quantity, wait = false)
  order = { sell: Bitex::Ask, buy: Bitex::Bid }[type].create!(base_quote.to_sym, quantity, price, wait)
  order_parser(order) if order.present?
end
setup() click to toggle source
# File lib/bitex_bot/models/api_wrappers/bitex/bitex_api_wrapper.rb, line 32
def setup
  Bitex.api_key = api_key
  Bitex.ssl_version = ssl_version
  Bitex.debug = debug
  Bitex.sandbox = sandbox
end
trade_type(trade) click to toggle source
# File lib/bitex_bot/models/api_wrappers/bitex/bitex_api_wrapper.rb, line 163
def trade_type(trade)
  # ask: 0, bid: 1
  trade.is_a?(Bitex::Buy) ? 1 : 0
end
transaction_parser(transaction) click to toggle source

[

[1492795215, 80310, 1243.51657154, 4.60321971],
[UNIX timestamp, Transaction ID, Price Paid, Amound Sold]

]

# File lib/bitex_bot/models/api_wrappers/bitex/bitex_api_wrapper.rb, line 144
def transaction_parser(transaction)
  Transaction.new(transaction.id, transaction.price.to_d, transaction.amount.to_d, transaction.created_at.to_i, transaction)
end
transactions() click to toggle source
# File lib/bitex_bot/models/api_wrappers/bitex/bitex_api_wrapper.rb, line 74
def transactions
  Bitex::Trade.all.map { |t| transaction_parser(t) }
end
user_transaction_parser(trade) click to toggle source

<Bitex::Buy:0x007ff9a2979390

@id=12345678, @created_at=1999-12-31 21:10:00 -0300, @order_book=:btc_usd, @quantity=0.2e1, @amount=0.6e3, @fee=0.5e-1,
@price=0.3e3, @bid_id=123

>

<Bitex::Sell:0x007ff9a2978710

@id=12345678, @created_at=1999-12-31 21:10:00 -0300, @order_book=:btc_usd, @quantity=0.2e1, @amount=0.6e3, @fee=0.5e-1,
@price=0.3e3, @ask_id=456i

>

# File lib/bitex_bot/models/api_wrappers/bitex/bitex_api_wrapper.rb, line 157
def user_transaction_parser(trade)
  UserTransaction.new(
    trade.id, trade.amount, trade.quantity, trade.price, trade.fee, trade_type(trade), trade.created_at.to_i
  )
end
user_transactions() click to toggle source
# File lib/bitex_bot/models/api_wrappers/bitex/bitex_api_wrapper.rb, line 78
def user_transactions
  Bitex::Trade.all.map { |trade| user_transaction_parser(trade) }
end