Class: Order

Inherits:
Object
  • Object
show all
Includes:
Observable
Defined in:
lib/order.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, side, price, size) ⇒ Order

Returns a new instance of Order.



8
9
10
11
12
13
# File 'lib/order.rb', line 8

def initialize id, side, price, size
  @id, @side, @price, @size = id, side, price, size
  @sent_at = Time.now
  @cancelled = false
  @status = :pending_accept
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



6
7
8
# File 'lib/order.rb', line 6

def id
  @id
end

#local_idObject (readonly)

Returns the value of attribute local_id.



6
7
8
# File 'lib/order.rb', line 6

def local_id
  @local_id
end

#priceObject (readonly)

Returns the value of attribute price.



6
7
8
# File 'lib/order.rb', line 6

def price
  @price
end

#sent_atObject (readonly)

Returns the value of attribute sent_at.



6
7
8
# File 'lib/order.rb', line 6

def sent_at
  @sent_at
end

#sideObject (readonly)

Returns the value of attribute side.



6
7
8
# File 'lib/order.rb', line 6

def side
  @side
end

#sizeObject (readonly)

Returns the value of attribute size.



6
7
8
# File 'lib/order.rb', line 6

def size
  @size
end

#statusObject

Returns the value of attribute status.



6
7
8
# File 'lib/order.rb', line 6

def status
  @status
end

Instance Method Details

#<=>(order) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/order.rb', line 19

def <=> order
  if order.price == price
    @sent_at <=> order.sent_at
  else
    price <=> order.price
  end
end

#cancel!Object



42
43
44
45
46
47
48
# File 'lib/order.rb', line 42

def cancel!
  return if @cancelled  # can only cancel an order once

  @cancelled = true
  changed
  notify_observers :cancel, self
end

#cancelled?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/order.rb', line 27

def cancelled?
  @cancelled
end

#fill!(amount) ⇒ Object



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

def fill! amount
  changed

  status = :fill
  status = :partial_fill if amount < @size

  @size -= amount
  
  notify_observers status, self
end