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 Book
class LimitVolumeTracker
include Enumerable
LimitVolumeChange = Struct.new(:side, :price, :quantity)
attr_reader :volumes
@ -24,6 +25,18 @@ module Rex
]
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)
volumes[order.is_buy][order.price] -= order.remaining_quantity

View File

@ -37,6 +37,22 @@ RSpec.describe Rex::Book::LimitVolumeTracker do
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
let(:trade) do
Rex::Book::Trade.new(