Class: BitexBot::OrderBookSimulator

Inherits:
Object
  • Object
show all
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

Class Method Details

.best_price(currency, target, price) ⇒ Object



67
68
69
# File 'lib/bitex_bot/models/order_book_simulator.rb', line 67

def self.best_price(currency, target, price)
  price.tap { Robot.log(:debug, "Best price to get #{currency} #{target} is #{Robot.taker.quote.upcase} #{price}") }
end

.best_price?(volume, target, seen) ⇒ Boolean



63
64
65
# File 'lib/bitex_bot/models/order_book_simulator.rb', line 63

def self.best_price?(volume, target, seen)
  volume >= (target - seen)
end

.estimate_quantity_to_skip(volatility, transactions) ⇒ Object

private class methods



55
56
57
58
59
60
61
# File 'lib/bitex_bot/models/order_book_simulator.rb', line 55

def self.estimate_quantity_to_skip(volatility, transactions)
  threshold = transactions.first.timestamp - volatility
  transactions
    .select { |t| t.timestamp > threshold }
    .map(&:amount)
    .sum
end

.run(volatility, taker_transactions, taker_orderbook, amount_target, quantity_target, fx_rate = 1) ⇒ 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
50
# File 'lib/bitex_bot/models/order_book_simulator.rb', line 20

def self.run(volatility, taker_transactions, taker_orderbook, amount_target, quantity_target, fx_rate = 1)
  to_skip = estimate_quantity_to_skip(volatility, taker_transactions)
  Robot.log(:debug, "Skipping #{to_skip} #{Robot.taker.base.upcase}")
  seen = 0

  taker_orderbook.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?
      dropped = [quantity, to_skip].min
      to_skip -= dropped
      quantity -= dropped
      Robot.log(:debug, "Skipped #{dropped} #{Robot.taker.base.upcase} @ #{Robot.taker.quote.upcase} #{price}")
      next if quantity.zero?
    end

    if quantity_target.present?
      return best_price(Robot.maker.base.upcase, quantity_target, price) if best_price?(quantity, quantity_target, seen)

      seen += quantity
    elsif amount_target.present?
      amount = price * quantity
      return best_price(Robot.maker.quote.upcase, amount_target * fx_rate, price) if best_price?(amount, amount_target, seen)

      seen += amount
    end
  end
  taker_orderbook.last.price
end