Class: Gekko::BookSide
- Inherits:
-
Array
- Object
- Array
- Gekko::BookSide
- Defined in:
- lib/gekko/book_side.rb
Overview
A side of the order book
Instance Attribute Summary collapse
-
#side ⇒ Object
TODO: Insert orders more smartly by using a dichotomy search.
Instance Method Summary collapse
-
#ask_side? ⇒ Boolean
Returns +true if this is the ask side.
-
#bid_side? ⇒ Boolean
Returns true if this is the bid side.
-
#initialize(side) ⇒ BookSide
constructor
A new instance of BookSide.
-
#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.
-
#top ⇒ Object
Returns the first order price, or
nilif there’s no order.
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
#side ⇒ Object
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
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
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.
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 |
#top ⇒ Object
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 |