Implement basic capped trade tracker

This commit is contained in:
Tim Kächele 2024-02-03 21:04:09 +01:00
parent 16028989e4
commit 1bdb619f6c
3 changed files with 54 additions and 0 deletions

View File

@ -6,6 +6,7 @@ require_relative "book/order"
require_relative "book/limit_order_book" require_relative "book/limit_order_book"
require_relative "book/matcher" require_relative "book/matcher"
require_relative "book/limit_volume_tracker" require_relative "book/limit_volume_tracker"
require_relative "book/trade_tracker"
module Rex module Rex
module Book module Book

View File

@ -0,0 +1,29 @@
module Rex
module Book
class TradeTracker
DEFAULT_TRADE_HISTORY_LIMIT = 200
attr_reader :trades
def initialize(limit: DEFAULT_TRADE_HISTORY_LIMIT)
@limit = limit
@trades = []
end
def add(trade)
@trades.push(trade)
cap
end
def fetch_trades(limit)
@trades.last(limit)
end
private
def cap
@trades = @trades.last(@limit)
end
end
end
end

View File

@ -0,0 +1,24 @@
# frozen_string_literal: true
RSpec.describe Rex::Book::TradeTracker do
let(:n) { 1 }
subject(:instance) { described_class.new(limit: n) }
before do
instance.add(10)
instance.add(11)
end
describe "#trades" do
it "returns only the last n trades" do
expect(instance.trades).to eq([11])
end
end
describe "#fetch_trades" do
let(:n) { 3 }
it "returns only the last n trades" do
expect(instance.fetch_trades(1)).to eq([11])
end
end
end