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



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

Returns:

  • (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.timestamp - volatility
  transactions
    .select { |t| t.timestamp > threshold }
    .map(&:amount)
    .sum
end

.run(volatility, transactions, order_book, amount_target, quantity_target) ⇒ Decimal

rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity

Parameters:

  • volatility (Integer)

    How many seconds of recent volume we need to skip from the start of the order book to be more certain that our order will get executed.

  • transactions (Hash)

    a list of hashes representing all transactions in the other exchange: Each hash contains ‘date’, ‘tid’, ‘price’ and ‘amount’, where ‘amount’ is the BTC transacted.

  • order_book ([price, quantity])

    a list of lists representing the order book to dig in.

  • amount_target (BigDecimal)

    stop when this amount has been reached, leave as nil if looking for a quantity_target.

  • quantity_target (BigDecimal)

    stop when this quantity has been reached, leave as nil if looking for an amount_target.

Returns:

  • (Decimal)

    Returns the price that we’re more likely to get when executing an order for the given amount or quantity.



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