Remove limit volume tracking from order book

It is not super relevant for the current matching engine,
we are going to solve this in the server implementation
This commit is contained in:
Tim Kächele 2023-10-30 20:43:26 +01:00
parent 79be3b534c
commit 0efb6b0e88
3 changed files with 1 additions and 44 deletions

View File

@ -2,10 +2,6 @@ require "rbtree"
module Rex module Rex
class OrderBook class OrderBook
attr_reader(
:buy_limit_volumes,
:sell_limit_volumes
)
def initialize(matcher: Matcher.new) def initialize(matcher: Matcher.new)
@matcher = matcher @matcher = matcher
@sell_side = RBTree.new @sell_side = RBTree.new
@ -13,19 +9,15 @@ module Rex
@order_ids = {} # order_id => order @order_ids = {} # order_id => order
@current_trade_id = 0 @current_trade_id = 0
@current_order_id = 0 @current_order_id = 0
@buy_limit_volumes = {} # price => volume
@sell_limit_volumes = {} # price => volume
end end
def add_order(order) def add_order(order)
side = side_for_order(order) side = side_for_order(order)
limit_volumes = limit_volume_for_order_side(order)
order.id = next_order_id order.id = next_order_id
side[order.price] ||= Limit.new(order.price) side[order.price] ||= Limit.new(order.price)
side[order.price].add_order(order) side[order.price].add_order(order)
limit_volumes[order.price] ||= 0
limit_volumes[order.price] += order.remaining_quantity
order_ids[order.id] = order order_ids[order.id] = order
end end
@ -46,9 +38,6 @@ module Rex
side.delete(limit.price) side.delete(limit.price)
end end
limit_volumes = limit_volume_for_order_side(order)
limit_volumes[order.price] -= order.remaining_quantity
order_ids.delete(order.id) order_ids.delete(order.id)
end end
@ -58,9 +47,6 @@ module Rex
trade.buy_order.remaining_quantity -= trade.quantity trade.buy_order.remaining_quantity -= trade.quantity
trade.sell_order.remaining_quantity -= trade.quantity trade.sell_order.remaining_quantity -= trade.quantity
buy_limit_volumes[trade.buy_order.price] -= trade.quantity
sell_limit_volumes[trade.sell_order.price] -= trade.quantity
remove_order(trade.buy_order.id) if trade.buy_order.filled? remove_order(trade.buy_order.id) if trade.buy_order.filled?
remove_order(trade.sell_order.id) if trade.sell_order.filled? remove_order(trade.sell_order.id) if trade.sell_order.filled?
end end
@ -97,14 +83,6 @@ module Rex
@current_order_id += 1 @current_order_id += 1
end end
def limit_volume_for_order_side(order)
if order.is_buy
@buy_limit_volumes
else
@sell_limit_volumes
end
end
def side_for_order(order) def side_for_order(order)
if order.is_buy if order.is_buy
buy_side buy_side

View File

@ -41,13 +41,6 @@ RSpec.describe Rex::Matcher do
expect(order_book.lowest_sell_order).to eq(pricier_sell_order) expect(order_book.lowest_sell_order).to eq(pricier_sell_order)
expect(order_book.lowest_sell_order.remaining_quantity).to eq(20) expect(order_book.lowest_sell_order.remaining_quantity).to eq(20)
end end
it "adjusts the limit volumes" do
instance.match(order_book)
expect(order_book.buy_limit_volumes).to eq({100 => 0})
expect(order_book.sell_limit_volumes).to eq({99 => 0, 100 => 20})
end
end end
context "when order book is empty" do context "when order book is empty" do

View File

@ -13,13 +13,6 @@ RSpec.describe Rex::OrderBook do
expect(instance.best_buy_price).to eq(100) expect(instance.best_buy_price).to eq(100)
end end
it "adjusts the limit volume on the correct side" do
order = build(:order, is_buy: true, quantity: 100, remaining_quantity: 99)
instance.add_order(order)
expect(instance.buy_limit_volumes[order.price]).to eq(99)
end
it "assigns an order id" do it "assigns an order id" do
expect { instance.add_order(order_a) }.to change(order_a, :id).from(nil).to(1) expect { instance.add_order(order_a) }.to change(order_a, :id).from(nil).to(1)
expect { instance.add_order(order_b) }.to change(order_b, :id).from(nil).to(2) expect { instance.add_order(order_b) }.to change(order_b, :id).from(nil).to(2)
@ -150,13 +143,6 @@ RSpec.describe Rex::OrderBook do
instance.add_order(sell_order) instance.add_order(sell_order)
end end
it "adjusts the limit's volume" do
expect do
instance.cancel_order(sell_order.id)
end.to(change { instance.sell_limit_volumes[sell_order.price] }
.by(-sell_order.remaining_quantity))
end
it "does not affect the buy side" do it "does not affect the buy side" do
instance.cancel_order(sell_order.id) instance.cancel_order(sell_order.id)