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.



9
10
11
12
13
14
15
16
# File 'lib/order.rb', line 9

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

  @price = @price.round 2
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

#priceObject

Returns the value of attribute price.



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

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



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

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

#cancel!Object



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

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

  @cancelled = true
  changed
  notify_observers :cancel, self
end

#cancelled?Boolean

Returns:

  • (Boolean)


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

def cancelled?
  @cancelled
end

#fill!(amount) ⇒ Object



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

def fill! amount
  changed

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

  @size -= amount
  
  notify_observers status, self
end