Implement basic capped trade tracker
This commit is contained in:
parent
16028989e4
commit
1bdb619f6c
@ -6,6 +6,7 @@ require_relative "book/order"
|
||||
require_relative "book/limit_order_book"
|
||||
require_relative "book/matcher"
|
||||
require_relative "book/limit_volume_tracker"
|
||||
require_relative "book/trade_tracker"
|
||||
|
||||
module Rex
|
||||
module Book
|
||||
|
29
lib/rex/book/trade_tracker.rb
Normal file
29
lib/rex/book/trade_tracker.rb
Normal 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
|
24
spec/book/trade_tracker_spec.rb
Normal file
24
spec/book/trade_tracker_spec.rb
Normal 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
|
Loading…
Reference in New Issue
Block a user