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) ⇒ BookSide

Returns a new instance of BookSide.



12
13
14
15
# File 'lib/gekko/book_side.rb', line 12

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

Instance Attribute Details

#sideObject

TODO: Insert orders more smartly by using a dichotomy search



10
11
12
# File 'lib/gekko/book_side.rb', line 10

def side
  @side
end

Instance Method Details

#ask_side?Boolean

Returns +true if this is the ask side

Returns:

  • (Boolean)


44
45
46
# File 'lib/gekko/book_side.rb', line 44

def ask_side?
  side == :ask
end

#bid_side?Boolean

Returns true if this is the bid side

Returns:

  • (Boolean)


51
52
53
# File 'lib/gekko/book_side.rb', line 51

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



23
24
25
26
27
28
29
30
31
32
# File 'lib/gekko/book_side.rb', line 23

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

#topObject

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



37
38
39
# File 'lib/gekko/book_side.rb', line 37

def top
  first && first.price
end