Class: Gekko::Order
- Inherits:
-
Object
- Object
- Gekko::Order
- Defined in:
- lib/gekko/order.rb
Overview
Represents a trade order. Trade orders can be either buy (bid) or sell (ask) orders. All orders are identified by an UUID, must specify a size, a price, and an optional expiration timestamp.
Instance Attribute Summary collapse
-
#created_at ⇒ Object
Returns the value of attribute created_at.
-
#expiration ⇒ Object
Returns the value of attribute expiration.
-
#id ⇒ Object
Returns the value of attribute id.
-
#price ⇒ Object
Returns the value of attribute price.
-
#remaining ⇒ Object
Returns the value of attribute remaining.
-
#side ⇒ Object
Returns the value of attribute side.
-
#size ⇒ Object
Returns the value of attribute size.
Instance Method Summary collapse
-
#ask? ⇒ Boolean
Returns
trueif this order is a sell order. -
#bid? ⇒ Boolean
Returns
trueif this order is a buy order. -
#crosses?(other) ⇒ Boolean
Returns
trueif this order can execute againstother_order. -
#filled? ⇒ Boolean
Returns
trueif the order is filled. -
#initialize(side, id, size, price, expiration = nil) ⇒ Order
constructor
A new instance of Order.
-
#message(type, extra_attrs = {}) ⇒ Hash
Creates a message in order to print it ont the tape.
Constructor Details
#initialize(side, id, size, price, expiration = nil) ⇒ Order
Returns a new instance of Order.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/gekko/order.rb', line 12 def initialize(side, id, size, price, expiration = nil) @id = id @side = side @size = size @remaining = @size @price = price @expiration = expiration @created_at = Time.now.to_f raise 'Orders must have an UUID' unless @id && @id.is_a?(UUID) raise 'Side must be either :bid or :ask' unless [:bid, :ask].include?(side) raise 'Price must be a positive integer or be omitted' if (@price && (!@price.is_a?(Fixnum) || (@price <= 0))) raise 'Size must be a positive integer' if (@size && (!@size.is_a?(Fixnum) || @size <= 0)) raise 'Expiration must be omitted or be an integer' unless (@expiration.nil? || (@expiration.is_a?(Fixnum) && @expiration > 0)) raise 'The order creation timestamp can''t be nil' if !@created_at end |
Instance Attribute Details
#created_at ⇒ Object
Returns the value of attribute created_at.
10 11 12 |
# File 'lib/gekko/order.rb', line 10 def created_at @created_at end |
#expiration ⇒ Object
Returns the value of attribute expiration.
10 11 12 |
# File 'lib/gekko/order.rb', line 10 def expiration @expiration end |
#id ⇒ Object
Returns the value of attribute id.
10 11 12 |
# File 'lib/gekko/order.rb', line 10 def id @id end |
#price ⇒ Object
Returns the value of attribute price.
10 11 12 |
# File 'lib/gekko/order.rb', line 10 def price @price end |
#remaining ⇒ Object
Returns the value of attribute remaining.
10 11 12 |
# File 'lib/gekko/order.rb', line 10 def remaining @remaining end |
#side ⇒ Object
Returns the value of attribute side.
10 11 12 |
# File 'lib/gekko/order.rb', line 10 def side @side end |
#size ⇒ Object
Returns the value of attribute size.
10 11 12 |
# File 'lib/gekko/order.rb', line 10 def size @size end |
Instance Method Details
#ask? ⇒ Boolean
Returns true if this order is a sell order
78 79 80 |
# File 'lib/gekko/order.rb', line 78 def ask? !bid? end |
#bid? ⇒ Boolean
Returns true if this order is a buy order
71 72 73 |
# File 'lib/gekko/order.rb', line 71 def bid? side == :bid end |
#crosses?(other) ⇒ Boolean
Returns true if this order can execute against other_order
35 36 37 38 39 |
# File 'lib/gekko/order.rb', line 35 def crosses?(other) if other && (bid? ^ other.bid?) (bid? && (price >= other.price)) || (ask? && (price <= other.price)) end end |
#filled? ⇒ Boolean
Returns true if the order is filled
44 45 46 |
# File 'lib/gekko/order.rb', line 44 def filled? remaining.zero? end |
#message(type, extra_attrs = {}) ⇒ Hash
Creates a message in order to print it ont the tape
57 58 59 60 61 62 63 64 65 66 |
# File 'lib/gekko/order.rb', line 57 def (type, extra_attrs = {}) { type: type, order_id: id, side: side, size: size, remaining: remaining, price: price }.merge(extra_attrs) end |