Class: BitexBot::OrderBookSimulator
- Inherits:
-
Object
- Object
- BitexBot::OrderBookSimulator
- Defined in:
- lib/bitex_bot/models/order_book_simulator.rb
Overview
Simulates hitting an order-book to find a price at which an order can be assumed to get executed completely. It essentially drops the start of the order book, to account for price volatility (assuming those orders may be taken by someone else), and then digs until the given USD amount or BTC quantity are reached, finally returning the last price seen, which is the ‘safest’ price at which we can expect this order to get executed quickly.
Class Method Summary collapse
-
.run(volatility, transactions, order_book, amount_target, quantity_target) ⇒ Decimal
Returns the price that we’re more likely to get when executing an order for the given amount or quantity.
Class Method Details
.run(volatility, transactions, order_book, amount_target, quantity_target) ⇒ Decimal
Returns the price that we’re more likely to get when executing an order for the given amount or quantity.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/bitex_bot/models/order_book_simulator.rb', line 24 def self.run(volatility, transactions, order_book, amount_target, quantity_target) to_skip = estimate_quantity_to_skip(volatility, transactions) BitexBot::Robot.logger.debug("Skipping #{to_skip} BTC") seen = 0 safest_price = 0 order_book.each do |price, quantity| price = price.to_d quantity = quantity.to_d # An order may be partially or completely skipped due to volatility. if to_skip > 0 dropped = [quantity, to_skip].min to_skip -= dropped quantity -= dropped BitexBot::Robot.logger.debug("Skipped #{dropped} BTC @ $#{price}") next if quantity == 0 end if quantity_target if quantity >= (quantity_target - seen) BitexBot::Robot.logger.debug("Best price to get "\ "#{quantity_target} BTC is $#{price}") return price else seen += quantity end elsif amount_target amount = price * quantity if amount >= (amount_target - seen) BitexBot::Robot.logger.debug("Best price to get "\ "$#{amount_target} is $#{price}") return price else seen += amount end end end return order_book.last.first.to_d end |