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
- .best_price(currency, target, price) ⇒ Object
- .best_price?(volume, target, seen) ⇒ Boolean
-
.estimate_quantity_to_skip(volatility, transactions) ⇒ Object
private class methods.
-
.run(volatility, transactions, order_book, amount_target, quantity_target) ⇒ Decimal
rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity.
Class Method Details
.best_price(currency, target, price) ⇒ Object
66 67 68 |
# File 'lib/bitex_bot/models/order_book_simulator.rb', line 66 def self.best_price(currency, target, price) price.tap { Robot.log(:debug, "Best price to get #{currency} #{target} is $#{price}") } end |
.best_price?(volume, target, seen) ⇒ Boolean
62 63 64 |
# File 'lib/bitex_bot/models/order_book_simulator.rb', line 62 def self.best_price?(volume, target, seen) volume >= (target - seen) end |
.estimate_quantity_to_skip(volatility, transactions) ⇒ Object
private class methods
54 55 56 57 58 59 60 |
# File 'lib/bitex_bot/models/order_book_simulator.rb', line 54 def self.estimate_quantity_to_skip(volatility, transactions) threshold = transactions.first. - volatility transactions .select { |t| t. > threshold } .map(&:amount) .sum end |
.run(volatility, transactions, order_book, amount_target, quantity_target) ⇒ Decimal
rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
20 21 22 23 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 |
# File 'lib/bitex_bot/models/order_book_simulator.rb', line 20 def self.run(volatility, transactions, order_book, amount_target, quantity_target) to_skip = estimate_quantity_to_skip(volatility, transactions) Robot.log(:debug, "Skipping #{to_skip} BTC") seen = 0 order_book.each do |order_summary| price = order_summary.price quantity = order_summary.quantity # An order may be partially or completely skipped due to volatility. if to_skip.positive? [quantity, to_skip].min.tap do |dropped| to_skip -= dropped quantity -= dropped Robot.log(:debug, "Skipped #{dropped} BTC @ $#{price}") end next if quantity.zero? end if quantity_target.present? return best_price('BTC', quantity_target, price) if best_price?(quantity, quantity_target, seen) seen += quantity elsif amount_target.present? amount = price * quantity return best_price('$', amount_target, price) if best_price?(amount, amount_target, seen) seen += amount end end order_book.last.price end |