Class: Gekko::Order
- Inherits:
-
Object
- Object
- Gekko::Order
- Includes:
- Serialization
- 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, and must specify a size, and an optional expiration timestamp.
Direct Known Subclasses
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.
-
#uid ⇒ Object
Returns the value of attribute uid.
Class Method Summary collapse
-
.from_hash(hsh) ⇒ Gekko::Order
Initializes a
Gekko::Order
subclass from aHash
instance.
Instance Method Summary collapse
-
#ask? ⇒ Boolean
Returns
true
if this order is a sell order. -
#bid? ⇒ Boolean
Returns
true
if this order is a buy order. -
#crosses?(limit_order) ⇒ Boolean
Returns
true
if this order can execute againstlimit_order
. -
#expired? ⇒ Boolean
Returns
true
if this order is expired. -
#fill_or_kill? ⇒ Boolean
Returns
true
if this order isn’t supposed to stick around in the order book. -
#initialize(side, id, uid, size, 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.
-
#to_hash ⇒ Hash
Returns a
Hash
representation of thisOrder
instance.
Methods included from Serialization
Constructor Details
#initialize(side, id, uid, size, expiration = nil) ⇒ Order
Returns a new instance of Order.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/gekko/order.rb', line 14 def initialize(side, id, uid, size, expiration = nil) @id = id @uid = uid @side = side && side.to_sym @size = size @remaining = @size @expiration = expiration @created_at = Time.now.to_f raise 'Orders must have an UUID' unless @id && @id.is_a?(UUID) raise 'Orders must have a user ID' unless @uid && @uid.is_a?(UUID) raise 'Side must be either :bid or :ask' unless [:bid, :ask].include?(@side) 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.
12 13 14 |
# File 'lib/gekko/order.rb', line 12 def created_at @created_at end |
#expiration ⇒ Object
Returns the value of attribute expiration.
12 13 14 |
# File 'lib/gekko/order.rb', line 12 def expiration @expiration end |
#id ⇒ Object
Returns the value of attribute id.
12 13 14 |
# File 'lib/gekko/order.rb', line 12 def id @id end |
#price ⇒ Object
Returns the value of attribute price.
12 13 14 |
# File 'lib/gekko/order.rb', line 12 def price @price end |
#remaining ⇒ Object
Returns the value of attribute remaining.
12 13 14 |
# File 'lib/gekko/order.rb', line 12 def remaining @remaining end |
#side ⇒ Object
Returns the value of attribute side.
12 13 14 |
# File 'lib/gekko/order.rb', line 12 def side @side end |
#size ⇒ Object
Returns the value of attribute size.
12 13 14 |
# File 'lib/gekko/order.rb', line 12 def size @size end |
#uid ⇒ Object
Returns the value of attribute uid.
12 13 14 |
# File 'lib/gekko/order.rb', line 12 def uid @uid end |
Class Method Details
.from_hash(hsh) ⇒ Gekko::Order
Initializes a Gekko::Order
subclass from a Hash
instance
137 138 139 |
# File 'lib/gekko/order.rb', line 137 def self.from_hash(hsh) (hsh[:price] ? LimitOrder : MarketOrder).from_hash(hsh) end |
Instance Method Details
#ask? ⇒ Boolean
Returns true
if this order is a sell order
86 87 88 |
# File 'lib/gekko/order.rb', line 86 def ask? !bid? end |
#bid? ⇒ Boolean
Returns true
if this order is a buy order
79 80 81 |
# File 'lib/gekko/order.rb', line 79 def bid? side == :bid end |
#crosses?(limit_order) ⇒ Boolean
Returns true
if this order can execute against limit_order
37 38 39 40 41 42 43 44 45 |
# File 'lib/gekko/order.rb', line 37 def crosses?(limit_order) if limit_order raise 'Can not test againt a market order' unless limit_order.is_a?(LimitOrder) if bid? ^ limit_order.bid? is_a?(MarketOrder) || (bid? && (price >= limit_order.price)) || (ask? && (price <= limit_order.price)) end end end |
#expired? ⇒ Boolean
Returns true
if this order is expired
101 102 103 |
# File 'lib/gekko/order.rb', line 101 def expired? expiration && (expiration <= Time.now.to_i) end |
#fill_or_kill? ⇒ Boolean
Returns true
if this order isn’t supposed to stick around in the order book
94 95 96 |
# File 'lib/gekko/order.rb', line 94 def fill_or_kill? is_a?(Gekko::MarketOrder) end |
#message(type, extra_attrs = {}) ⇒ Hash
Creates a message in order to print it ont the tape
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/gekko/order.rb', line 56 def (type, extra_attrs = {}) hsh = { type: type, order_id: id.to_s, side: side, size: size, remaining: remaining, price: price, expiration: expiration }.merge(extra_attrs) if is_a?(Gekko::MarketOrder) hsh.delete(:price) hsh[:quote_margin] = quote_margin hsh[:remaining_quote_margin] = remaining_quote_margin end hsh end |
#to_hash ⇒ Hash
Returns a Hash
representation of this Order
instance
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/gekko/order.rb', line 110 def to_hash hsh = { id: id.to_s, uid: uid.to_s, side: side, size: size, price: price, remaining: remaining, expiration: expiration, created_at: created_at } if is_a?(Gekko::MarketOrder) hsh.delete(:price) hsh[:quote_margin] = quote_margin hsh[:remaining_quote_margin] = remaining_quote_margin end hsh end |