class Gekko::BookSide

A side of the order book

Attributes

side[RW]
stops[RW]

Public Class Methods

from_hash(side, h) click to toggle source

Returns a Gekko::BookSide instance from a hash

@param side [Symbol] Either :bid or :ask @param h [Hash] The hash representation of a book side @return [Gekko::BookSide] The represented book side

# File lib/gekko/book_side.rb, line 43
def self.from_hash(side, h)
  h = symbolize_keys(h)
  bs = new(side, orders: h[:orders])
  bs.stops = h[:stops].map { |o| Gekko::Order.from_hash(o) }
  bs
end
new(side, opts = {}) click to toggle source
Calls superclass method
# File lib/gekko/book_side.rb, line 12
def initialize(side, opts = {})
  super()

  raise "Incorrect side <#{side}>" unless [:bid, :ask].include?(side)
  @side   = side
  @stops  = []

  if opts[:orders]
    opts[:orders].each_with_index { |obj, idx| self[idx] = Order.from_hash(obj) }
    sort!
  end

  opts[:stops].each_with_index { |obj, idx| stops[idx] = Order.from_hash(obj) } if opts[:stops]
end

Public Instance Methods

ask_side?() click to toggle source

Returns +true if this is the ask side

# File lib/gekko/book_side.rb, line 77
def ask_side?
  side == :ask
end
bid_side?() click to toggle source

Returns true if this is the bid side

# File lib/gekko/book_side.rb, line 84
def bid_side?
  side == :bid
end
insert_order(order) click to toggle source

Inserts an order in the order book so that it remains sort by ascending or descending price, depending on what side of the complete book this is.

@param order [Order] The order to insert

# File lib/gekko/book_side.rb, line 56
def insert_order(order)
  raise "Can't insert a #{order.side} order on the #{side} side" unless (side == order.side)

  idx = find_index do |ord|
    (bid_side? && (ord.price < order.price)) ||
      (ask_side? && (ord.price > order.price))
  end

  insert((idx || -1), order)
end
remove_expired!() { |message(:done, reason: :expired)| ... } click to toggle source

Removes the expired book orders and STOPs from this side

# File lib/gekko/book_side.rb, line 91
def remove_expired!
  [self, stops].each do |orders|
    orders.reject! do |order|
      if order.expired?
        yield(order.message(:done, reason: :expired))
        true
      end
    end
  end
end
to_hash() click to toggle source

Returns a Hash representation of this BookSide instance

@return [Hash] The serializable representation

# File lib/gekko/book_side.rb, line 32
def to_hash
  { orders: map(&:to_hash), stops: stops.map(&:to_hash) }
end
top() click to toggle source

Returns the first order price, or nil if there’s no order

# File lib/gekko/book_side.rb, line 70
def top
  first && first.price
end