Class: Gekko::BookSide

Inherits:
Array
  • Object
show all
Defined in:
lib/gekko/book_side.rb

Overview

A side of the order book

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(side, opts = {}) ⇒ BookSide

Returns a new instance of BookSide.



10
11
12
13
14
15
16
17
18
# File 'lib/gekko/book_side.rb', line 10

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

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

Instance Attribute Details

#sideObject

Returns the value of attribute side.



8
9
10
# File 'lib/gekko/book_side.rb', line 8

def side
  @side
end

Instance Method Details

#ask_side?Boolean

Returns +true if this is the ask side

Returns:

  • (Boolean)


56
57
58
# File 'lib/gekko/book_side.rb', line 56

def ask_side?
  side == :ask
end

#bid_side?Boolean

Returns true if this is the bid side

Returns:

  • (Boolean)


63
64
65
# File 'lib/gekko/book_side.rb', line 63

def bid_side?
  side == :bid
end

#insert_order(order) ⇒ Object

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.

Parameters:

  • order (Order)

    The order to insert



35
36
37
38
39
40
41
42
43
44
# File 'lib/gekko/book_side.rb', line 35

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

#to_hashHash

Returns a Hash representation of this BookSide instance

Returns:

  • (Hash)

    The serializable representation



25
26
27
# File 'lib/gekko/book_side.rb', line 25

def to_hash
  map(&:to_hash)
end

#topObject

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



49
50
51
# File 'lib/gekko/book_side.rb', line 49

def top
  first && first.price
end