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

.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.

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.



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