Class: RMarket::OrderBook

Inherits:
Object
  • Object
show all
Defined in:
lib/rmarket/order_book.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(asset_label = "") ⇒ OrderBook

Returns a new instance of OrderBook.



5
6
7
8
9
# File 'lib/rmarket/order_book.rb', line 5

def initialize(asset_label="")
  @buy_ledger = OrderLedger.new("buy", asset_label)
  @sell_ledger = OrderLedger.new("sell", asset_label)
  @asset_label = asset_label
end

Instance Attribute Details

#asset_labelObject (readonly)

Returns the value of attribute asset_label.



3
4
5
# File 'lib/rmarket/order_book.rb', line 3

def asset_label
  @asset_label
end

Instance Method Details

#askObject



36
# File 'lib/rmarket/order_book.rb', line 36

def ask; @sell_ledger.ask; end

#ask_quantityObject



39
# File 'lib/rmarket/order_book.rb', line 39

def ask_quantity; @sell_ledger.ask_quantity; end

#bidObject



35
# File 'lib/rmarket/order_book.rb', line 35

def bid; @buy_ledger.bid; end

#bid_quantityObject



38
# File 'lib/rmarket/order_book.rb', line 38

def bid_quantity; @buy_ledger.bid_quantity; end

#matches?(order) ⇒ Boolean

Returns:

  • (Boolean)


11
12
13
14
15
16
17
18
19
# File 'lib/rmarket/order_book.rb', line 11

def matches?(order)
  return false if order.asset_label != @asset_label
  if order.type == "buy"
    return false if @sell_ledger.outstanding_order_count == 0 || ask > order.price
  else
    return false if @buy_ledger.outstanding_order_count == 0 || bid < order.price
  end
  true
end

#outstanding_ask_countObject



33
# File 'lib/rmarket/order_book.rb', line 33

def outstanding_ask_count; @sell_ledger.outstanding_order_count; end

#outstanding_bid_countObject



32
# File 'lib/rmarket/order_book.rb', line 32

def outstanding_bid_count; @buy_ledger.outstanding_order_count; end

#remove_prior_orders(trader) ⇒ Object



41
42
43
44
# File 'lib/rmarket/order_book.rb', line 41

def remove_prior_orders(trader)
  @buy_ledger.remove_prior_orders(trader)
  @sell_ledger.remove_prior_orders(trader)
end

#snapshotObject



46
47
48
# File 'lib/rmarket/order_book.rb', line 46

def snapshot
  {"asset_label" => @asset_label, "bid" => bid, "bid_quantity" => bid_quantity, "ask" => ask, "ask_quantity" => ask_quantity}
end

#submit_order(order) ⇒ Object



21
22
23
24
25
26
27
28
29
30
# File 'lib/rmarket/order_book.rb', line 21

def submit_order(order)
  raise "Asset label of order does not match OrderBook" if order.asset_label != @asset_label
  if order.type == "buy"
    order = @sell_ledger.transact(order) if matches?(order)
    @buy_ledger.submit_order(order) if order != nil
  else
    order = @buy_ledger.transact(order) if matches?(order)
    @sell_ledger.submit_order(order) if order != nil
  end
end