Sha256: 8856105c0f925e2bd5911396f5557ce97d69953c040dee0869a34bfbcec73029

Contents?: true

Size: 1.28 KB

Versions: 3

Compression:

Stored size: 1.28 KB

Contents

require 'observer'

class Order
  include Observable

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

  def initialize id, local_id, side, price, size, owner
    @id, @local_id, @side, @price, @size, @owner = id, local_id, side, price, size, owner
    @initial_size = @size
    @sent_at = Time.now
    @status = :pending_accept

    # normalize the price to be within cents
    @price = @price.round 2
  end

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

  def fill! price, amount
    changed

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

    @size -= amount
    @status = status
    
    notify_observers status, self, price, amount
  end

  def cancel!
    @status = :cancelled
    changed
    notify_observers :cancel, self
  end

  def valid?
    errors.length > 0
  end

  def status= new_status
    @status = new_status
  end

  def errors
    errors = []

    errors << "Price must be a number" unless @price.is_a? Integer
    errors << "Price must be strictly positive" unless @price > 0
    errors << "Unknown side '#{@side}'" unless ["buy", "sell"].include? @side
    errors << "Size must be strictly positive" unless @size > 0

    errors
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
ruby-trade-0.4 server/order.rb
ruby-trade-0.3 server/order.rb
ruby-trade-0.2 server/order.rb