Class: BitexApiWrapper
- Inherits:
-
ApiWrapper
- Object
- ApiWrapper
- BitexApiWrapper
- 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
Instance Attribute Summary collapse
-
#api_key ⇒ Object
Returns the value of attribute api_key.
-
#debug ⇒ Object
Returns the value of attribute debug.
-
#sandbox ⇒ Object
Returns the value of attribute sandbox.
-
#ssl_version ⇒ Object
Returns the value of attribute ssl_version.
Instance Method Summary collapse
-
#amount_and_quantity(order_id) ⇒ Object
rubocop:disable Metrics/AbcSize.
-
#balance ⇒ Object
rubocop:enable Metrics/AbcSize.
- #balance_parser(balances, currency) ⇒ Object
-
#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. }.
- #currency_pair(order_book = '_') ⇒ Object
- #find_lost(type, price, _quantity) ⇒ Object
-
#initialize(settings) ⇒ BitexApiWrapper
constructor
A new instance of BitexApiWrapper.
- #last_order_by(price) ⇒ Object
- #market ⇒ Object
- #order_amount(order) ⇒ Object
- #order_book ⇒ Object
-
#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]] }.
-
#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 >.
- #order_summary_parser(orders) ⇒ Object
- #order_type(order) ⇒ Object
- #orders ⇒ Object
- #profile ⇒ Object
- #send_order(type, price, quantity, wait = false) ⇒ Object
- #setup ⇒ Object
- #trade_type(trade) ⇒ Object
-
#transaction_parser(transaction) ⇒ Object
- [1492795215, 80310, 1243.51657154, 4.60321971], [UNIX timestamp, Transaction ID, Price Paid, Amound Sold
-
].
- #transactions ⇒ Object
-
#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 >.
- #user_transactions ⇒ Object
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_key ⇒ Object
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 |
#debug ⇒ Object
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 |
#sandbox ⇒ Object
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_version ⇒ Object
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 |
#balance ⇒ Object
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. >= 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. - Time.now.to_i).abs < 500 }.first end |
#market ⇒ Object
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_book ⇒ Object
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 |
#orders ⇒ Object
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 |
#profile ⇒ Object
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 |
#setup ⇒ Object
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 |
#transactions ⇒ Object
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_transactions ⇒ Object
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 |