Class: Gekko::Order

Inherits:
Object
  • Object
show all
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

LimitOrder, MarketOrder

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(side, id, size, expiration = nil) ⇒ Order

Returns a new instance of Order.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/gekko/order.rb', line 12

def initialize(side, id, size, expiration = nil)
  @id         = id
  @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 '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_atObject

Returns the value of attribute created_at.



10
11
12
# File 'lib/gekko/order.rb', line 10

def created_at
  @created_at
end

#expirationObject

Returns the value of attribute expiration.



10
11
12
# File 'lib/gekko/order.rb', line 10

def expiration
  @expiration
end

#idObject

Returns the value of attribute id.



10
11
12
# File 'lib/gekko/order.rb', line 10

def id
  @id
end

#priceObject

Returns the value of attribute price.



10
11
12
# File 'lib/gekko/order.rb', line 10

def price
  @price
end

#remainingObject

Returns the value of attribute remaining.



10
11
12
# File 'lib/gekko/order.rb', line 10

def remaining
  @remaining
end

#sideObject

Returns the value of attribute side.



10
11
12
# File 'lib/gekko/order.rb', line 10

def side
  @side
end

#sizeObject

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

Returns:

  • (Boolean)


73
74
75
# File 'lib/gekko/order.rb', line 73

def ask?
  !bid?
end

#bid?Boolean

Returns true if this order is a buy order

Returns:

  • (Boolean)


66
67
68
# File 'lib/gekko/order.rb', line 66

def bid?
  side == :bid
end

#crosses?(limit_order) ⇒ Boolean

Returns true if this order can execute against limit_order

Parameters:

  • limit_order (LimitOrder)

    The limit order against which we want to know if an execution is possible

Returns:

  • (Boolean)


33
34
35
36
37
38
39
40
41
# File 'lib/gekko/order.rb', line 33

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

Returns:

  • (Boolean)


88
89
90
# File 'lib/gekko/order.rb', line 88

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

Returns:

  • (Boolean)


81
82
83
# File 'lib/gekko/order.rb', line 81

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

Parameters:

  • type (Symbol)

    The type of message we’re printing

  • extra_attrs (Hash) (defaults to: {})

    The extra attributes we’re including in the message

Returns:

  • (Hash)

    The message we’ll print on the tape



52
53
54
55
56
57
58
59
60
61
# File 'lib/gekko/order.rb', line 52

def message(type, extra_attrs = {})
  {
    type:       type,
    order_id:   id.to_s,
    side:       side,
    size:       size,
    remaining:  remaining,
    price:      price
  }.merge(extra_attrs)
end