diff --git a/lib/rex/book/limit_order_book.rb b/lib/rex/book/limit_order_book.rb index 8e953ba..dea472b 100644 --- a/lib/rex/book/limit_order_book.rb +++ b/lib/rex/book/limit_order_book.rb @@ -8,7 +8,6 @@ module Rex @sell_side = RBTree.new @buy_side = RBTree.new @order_ids = {} # order_id => order - @current_trade_id = 0 @current_order_id = 0 end @@ -68,10 +67,6 @@ module Rex sell_side.first&.[](0) end - def next_trade_id - @current_trade_id += 1 - end - private attr_reader( diff --git a/lib/rex/book/matcher.rb b/lib/rex/book/matcher.rb index bbd9c07..338a95f 100644 --- a/lib/rex/book/matcher.rb +++ b/lib/rex/book/matcher.rb @@ -1,6 +1,10 @@ module Rex module Book class Matcher + def initialize + @current_trade_id = 0 + end + def match(order_book) trades = [] highest_buy_order = order_book.highest_buy_order @@ -10,7 +14,7 @@ module Rex while highest_buy_order.price >= lowest_sell_order.price max_quantity = min(highest_buy_order.remaining_quantity, lowest_sell_order.remaining_quantity) trade = Trade.new( - id: order_book.next_trade_id, + id: next_trade_id, buy_order: highest_buy_order, sell_order: lowest_sell_order, quantity: max_quantity, @@ -32,6 +36,10 @@ module Rex private + def next_trade_id + @current_trade_id += 1 + end + def min(a, b) return a if a < b b diff --git a/spec/book/limit_order_book_spec.rb b/spec/book/limit_order_book_spec.rb index 8f017bb..2a7ddce 100644 --- a/spec/book/limit_order_book_spec.rb +++ b/spec/book/limit_order_book_spec.rb @@ -150,12 +150,4 @@ RSpec.describe Rex::Book::LimitOrderBook do 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