Class: BitexApiWrapper

Inherits:
ApiWrapper show all
Defined in:
lib/bitex_bot/models/api_wrappers/bitex/bitex_api_wrapper.rb

Overview

Wrapper implementation for Bitex API. bitex.la/developers

Defined Under Namespace

Classes: Order

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) ⇒ BitexApiWrapper

Returns a new instance of BitexApiWrapper.



23
24
25
26
27
28
29
30
# 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

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



21
22
23
# File 'lib/bitex_bot/models/api_wrappers/bitex/bitex_api_wrapper.rb', line 21

def api_key
  @api_key
end

#debugObject

Returns the value of attribute debug.



21
22
23
# File 'lib/bitex_bot/models/api_wrappers/bitex/bitex_api_wrapper.rb', line 21

def debug
  @debug
end

#sandboxObject

Returns the value of attribute sandbox.



21
22
23
# File 'lib/bitex_bot/models/api_wrappers/bitex/bitex_api_wrapper.rb', line 21

def sandbox
  @sandbox
end

#ssl_versionObject

Returns the value of attribute ssl_version.



21
22
23
# File 'lib/bitex_bot/models/api_wrappers/bitex/bitex_api_wrapper.rb', line 21

def ssl_version
  @ssl_version
end

Instance Method Details

#amount_and_quantity(order_id) ⇒ Object

rubocop:disable Metrics/AbcSize



44
45
46
47
48
49
50
# 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

#balanceObject

rubocop:enable Metrics/AbcSize



53
54
55
# 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) ⇒ Object



100
101
102
103
104
105
106
# 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) ⇒ Object

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.



92
93
94
95
96
97
98
# 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 = '_') ⇒ Object



172
173
174
175
176
177
178
# 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) ⇒ Object



57
58
59
# 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) ⇒ Object



108
109
110
# 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

#marketObject



168
169
170
# 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) ⇒ Object



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



61
62
63
# 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) ⇒ 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]]



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

<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

>



128
129
130
# 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) ⇒ Object



120
121
122
# 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) ⇒ Object



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

#ordersObject



65
66
67
# 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

#profileObject



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



69
70
71
72
# 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

#setupObject



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



163
164
165
166
# 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) ⇒ Object

[

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

]



144
145
146
# 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

#transactionsObject



74
75
76
# 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) ⇒ Object

<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

>



157
158
159
160
161
# 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_transactionsObject



78
79
80
# 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