Sha256: 829516227d5ce9741f023c9c0b8985692f1b4a6f39c61b6721523fc3d504ed37

Contents?: true

Size: 1.11 KB

Versions: 1

Compression:

Stored size: 1.11 KB

Contents

require_relative 'account'
require_relative 'order-book'
require_relative 'order'
require_relative 'server'

STARTING_EQUITY = 0
STARTING_CASH = 10_000

class Exchange
  def initialize
    @accounts = {}
    @orders = {}
    @order_no = 0
    @book = OrderBook.new
  end

  def identify data
    account = @accounts[data["peer_name"]] || Account.new(data["peer_name"], data["name"], STARTING_EQUITY, STARTING_CASH)

    account.ai = data["ai"]
    account.update_name data["name"]

    @accounts[account.name] = account

    account
  end

  def new_order account, data
    id = @order_no += 1
    order = Order.new id, data["local_id"], data["side"], data["price"],
      data["size"], account.id

    if not order.valid?
      return order.errors, order
    else
      @orders[id] = order
      return nil, order
    end
  end

  # Send an order
  def send_order order
    order.status = :accepted
    @book.send_order order
  end

  def level1
    {
      bid: @book.bid,
      ask: @book.ask,
      last: @book.last
    }
  end

  def cancel_order order_id
    order = @orders[order_id]
    @book.cancel_order order if order
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ruby-trade-0.1 server/exchange.rb