Allow iteration of limit volume tracker entries

This commit is contained in:
Tim Kächele 2024-02-03 20:29:20 +01:00
parent 4b48904f1e
commit 16028989e4
2 changed files with 29 additions and 0 deletions

View File

@ -1,6 +1,7 @@
module Rex module Rex
module Book module Book
class LimitVolumeTracker class LimitVolumeTracker
include Enumerable
LimitVolumeChange = Struct.new(:side, :price, :quantity) LimitVolumeChange = Struct.new(:side, :price, :quantity)
attr_reader :volumes attr_reader :volumes
@ -24,6 +25,18 @@ module Rex
] ]
end end
def each
volumes.each do |side, limits|
limits.each do |limit, volume|
yield LimitVolumeChange.new(
side ? :buy : :sell,
limit,
volume
)
end
end
end
def remove_order(order) def remove_order(order)
volumes[order.is_buy][order.price] -= order.remaining_quantity volumes[order.is_buy][order.price] -= order.remaining_quantity

View File

@ -37,6 +37,22 @@ RSpec.describe Rex::Book::LimitVolumeTracker do
end end
end end
describe "#each" do
before do
instance.add_order(sell_order)
end
it "retuns all limit volumes" do
expect(instance.to_a).to eq([
Rex::Book::LimitVolumeTracker::LimitVolumeChange.new(
:sell,
100,
250
)
])
end
end
describe "#process_trade" do describe "#process_trade" do
let(:trade) do let(:trade) do
Rex::Book::Trade.new( Rex::Book::Trade.new(