Move next_trade to matcher implementation

This commit is contained in:
Tim Kächele 2024-01-20 11:11:59 +01:00
parent 188f45d4c4
commit 0ba5914b9c
3 changed files with 9 additions and 14 deletions

View File

@ -8,7 +8,6 @@ module Rex
@sell_side = RBTree.new @sell_side = RBTree.new
@buy_side = RBTree.new @buy_side = RBTree.new
@order_ids = {} # order_id => order @order_ids = {} # order_id => order
@current_trade_id = 0
@current_order_id = 0 @current_order_id = 0
end end
@ -68,10 +67,6 @@ module Rex
sell_side.first&.[](0) sell_side.first&.[](0)
end end
def next_trade_id
@current_trade_id += 1
end
private private
attr_reader( attr_reader(

View File

@ -1,6 +1,10 @@
module Rex module Rex
module Book module Book
class Matcher class Matcher
def initialize
@current_trade_id = 0
end
def match(order_book) def match(order_book)
trades = [] trades = []
highest_buy_order = order_book.highest_buy_order highest_buy_order = order_book.highest_buy_order
@ -10,7 +14,7 @@ module Rex
while highest_buy_order.price >= lowest_sell_order.price while highest_buy_order.price >= lowest_sell_order.price
max_quantity = min(highest_buy_order.remaining_quantity, lowest_sell_order.remaining_quantity) max_quantity = min(highest_buy_order.remaining_quantity, lowest_sell_order.remaining_quantity)
trade = Trade.new( trade = Trade.new(
id: order_book.next_trade_id, id: next_trade_id,
buy_order: highest_buy_order, buy_order: highest_buy_order,
sell_order: lowest_sell_order, sell_order: lowest_sell_order,
quantity: max_quantity, quantity: max_quantity,
@ -32,6 +36,10 @@ module Rex
private private
def next_trade_id
@current_trade_id += 1
end
def min(a, b) def min(a, b)
return a if a < b return a if a < b
b b

View File

@ -150,12 +150,4 @@ RSpec.describe Rex::Book::LimitOrderBook do
end end
end end
end end
describe "#next_trade_id" do
it "returns an increasing trade id" do
expect(instance.next_trade_id).to eq(1)
expect(instance.next_trade_id).to eq(2)
expect(instance.next_trade_id).to eq(3)
end
end
end end