Class: Gekko::LimitOrder

Inherits:
Order
  • Object
show all
Defined in:
lib/gekko/limit_order.rb

Overview

Represents a limit order. These order must specify a price.

Instance Attribute Summary collapse

Attributes inherited from Order

#created_at, #expiration, #id, #remaining, #side, #size, #uid

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Order

#ask?, #bid?, #crosses?, #expired?, #fill_or_kill?, #message, #to_hash

Methods included from Serialization

included, #serialize

Constructor Details

#initialize(side, id, uid, size, price, expiration = nil) ⇒ LimitOrder

Returns a new instance of LimitOrder.



10
11
12
13
14
15
# File 'lib/gekko/limit_order.rb', line 10

def initialize(side, id, uid, size, price, expiration = nil)
  super(side, id, uid, size, expiration)
  @price = price

  raise 'Price must be a positive integer' if @price.nil? || (!@price.is_a?(Fixnum) || (@price <= 0))
end

Instance Attribute Details

#priceObject

Returns the value of attribute price.



8
9
10
# File 'lib/gekko/limit_order.rb', line 8

def price
  @price
end

Class Method Details

.from_hash(hsh) ⇒ Gekko::LimitOrder

Initializes a Gekko::LimitOrder subclass from a Hash instance

Parameters:

  • hsh (Hash)

    The order data

Returns:



47
48
49
50
51
52
# File 'lib/gekko/limit_order.rb', line 47

def self.from_hash(hsh)
  order = LimitOrder.new(hsh[:side], UUID.parse(hsh[:id]), UUID.parse(hsh[:uid]), hsh[:size], hsh[:price], hsh[:expiration])
  order.remaining   = hsh[:remaining] || hsh[:size]
  order.created_at  = hsh[:created_at] if hsh[:created_at]
  order
end

Instance Method Details

#<=>(other) ⇒ Fixnum

LimitOrders are sorted by ASC price for asks, DESC price for bids, if prices are equal then creation timestamp is used, and older orders get priority.

Returns:

  • (Fixnum)

    1 if self < other, -1 if not, 0 if equivalent



35
36
37
38
39
# File 'lib/gekko/limit_order.rb', line 35

def <=>(other)
  cmp = (ask? ? 1 : -1) * (price <=> other.price)
  cmp = (created_at <=> other.created_at) if cmp.zero?
  cmp
end

#done?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/gekko/limit_order.rb', line 24

def done?
  filled?
end

#filled?Boolean

Returns true if the order is filled

Returns:

  • (Boolean)


20
21
22
# File 'lib/gekko/limit_order.rb', line 20

def filled?
  remaining.zero?
end