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
Returns the value of attribute side.
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, opts = {}) ⇒ 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.
-
#to_hash ⇒ Hash
Returns a
Hash
representation of thisBookSide
instance. -
#top ⇒ Object
Returns the first order price, or
nil
if there’s no order.
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
#side ⇒ Object
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
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
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.
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_hash ⇒ Hash
Returns a Hash
representation of this BookSide
instance
25 26 27 |
# File 'lib/gekko/book_side.rb', line 25 def to_hash map(&:to_hash) end |
#top ⇒ Object
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 |