Class: RMarket::OrderLedger

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

Instance Method Summary collapse

Constructor Details

#initialize(type = nil, asset_label = "") ⇒ OrderLedger

Returns a new instance of OrderLedger.



4
5
6
7
8
# File 'lib/rmarket/order_ledger.rb', line 4

def initialize(type=nil, asset_label="")
  @orders = []
  @type = type
  @asset_label = asset_label
end

Instance Method Details

#askObject



71
72
73
74
# File 'lib/rmarket/order_ledger.rb', line 71

def ask
  raise "OrderLedger is of type \"buy\" and cannot supply an \"ask\"" if @type == "buy"
  @orders == [] ? 1/0.0 : @orders.first.price
end

#ask_quantityObject



76
77
78
79
# File 'lib/rmarket/order_ledger.rb', line 76

def ask_quantity
  raise "OrderLedger is of type \"buy\" and cannot supply an \"ask\"" if @type == "buy"
  @orders == [] ? 0 : @orders.first.quantity
end

#bidObject



61
62
63
64
# File 'lib/rmarket/order_ledger.rb', line 61

def bid
  raise "OrderLedger is of type \"sell\" and cannot supply a \"bid\"" if @type == "sell"
  @orders == [] ? 0 : @orders.last.price
end

#bid_quantityObject



66
67
68
69
# File 'lib/rmarket/order_ledger.rb', line 66

def bid_quantity
  raise "OrderLedger is of type \"sell\" and cannot supply a \"bid\"" if @type == "sell"
  @orders == [] ? 0 : @orders.last.quantity
end

#outstanding_order_countObject



59
# File 'lib/rmarket/order_ledger.rb', line 59

def outstanding_order_count; @orders.size; end

#remove_prior_orders(trader) ⇒ Object



81
# File 'lib/rmarket/order_ledger.rb', line 81

def remove_prior_orders(trader);  @orders.reject!{|x| x.trader == trader}; end

#submit_order(order) ⇒ Object



53
54
55
56
57
# File 'lib/rmarket/order_ledger.rb', line 53

def submit_order(order)
  @type ||= order.type
  index = @orders.index{|i| i.price > order.price}
  index == nil ? @orders << order : @orders.insert(index, order)
end

#transact(order) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/rmarket/order_ledger.rb', line 10

def transact(order)
  while order.quantity > 0 && price != 0 && price != 1/0.0
    if @type == "buy" && order.type == "sell"
      if bid > order.price
        if @orders.last.quantity > order.quantity
          @orders.last.trader.(-bid*order.quantity, order.quantity, @asset_label)
          order.trader.(bid*order.quantity, -order.quantity, @asset_label)
          @orders.last.quantity -= order.quantity
          return nil
        else
          @orders.last.trader.(-bid*@orders.last.quantity, @orders.last.quantity, @asset_label)
          order.trader.(bid*@orders.last.quantity, -@orders.last.quantity, @asset_label)
          order.quantity -= @orders.last.quantity
          @orders.pop
          return nil if order.quantity == 0
        end
      else
        return order
      end
    elsif @type == "sell" && order.type == "buy"
      if ask < order.price
        if @orders.first.quantity > order.quantity
          @orders.first.trader.(ask*order.quantity, -order.quantity, @asset_label)
          order.trader.(-ask*order.quantity, order.quantity, @asset_label)
          @orders.first.quantity -= order.quantity
          return nil
        else
          @orders.first.trader.(ask*@orders.first.quantity, -@orders.first.quantity, @asset_label)
          order.trader.(-ask*@orders.first.quantity, @orders.first.quantity, @asset_label)
          order.quantity -= @orders.first.quantity
          @orders.delete_at(0)
          return nil if order.quantity == 0
        end
      else
        return order
      end
    else
      raise "Orders of the same type cannot transact"
    end
  end
  return order
end