Sha256: e590234788fec10cc8d17b5af855d21f35756b256b38e88c3233b2d28e1f20c7

Contents?: true

Size: 842 Bytes

Versions: 1

Compression:

Stored size: 842 Bytes

Contents

require 'observer'

class Order
  include Observable

  attr_reader :id, :local_id, :side, :price, :size, :sent_at, :status

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

  def status= new_status
    @status = new_status
  end

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

  def cancelled?
    @cancelled
  end

  def fill! amount
    changed

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

    @size -= amount
    
    notify_observers status, self
  end

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

    @cancelled = true
    changed
    notify_observers :cancel, self
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ruby-trade-0.1 lib/order.rb