def test_run_trade_ticks_through_aggregator_results_in_expected_bars(self): # Arrange bar_store = ObjectStorer() handler = bar_store.store bar_spec = BarSpecification(1000, BarAggregation.TICK, PriceType.LAST) bar_type = BarType(ETHUSDT_BINANCE.id, bar_spec) aggregator = TickBarAggregator(bar_type, handler, TestLogger(TestClock())) wrangler = TradeTickDataWrangler( instrument=ETHUSDT_BINANCE, data=TestDataProvider.ethusdt_trades(), ) wrangler.pre_process(0) ticks = wrangler.build_ticks() # Act for tick in ticks: aggregator.handle_trade_tick(tick) # Assert last_bar = bar_store.get_store()[-1].bar self.assertEqual(69, len(bar_store.get_store())) self.assertEqual(Price("426.72"), last_bar.open) self.assertEqual(Price("427.01"), last_bar.high) self.assertEqual(Price("426.46"), last_bar.low) self.assertEqual(Price("426.67"), last_bar.close) self.assertEqual(Quantity(2281), last_bar.volume)
def test_handle_trade_tick_when_count_below_threshold_updates(self): # Arrange bar_store = ObjectStorer() handler = bar_store.store instrument = AUDUSD_SIM bar_spec = BarSpecification(3, BarAggregation.TICK, PriceType.LAST) bar_type = BarType(instrument.id, bar_spec) aggregator = TickBarAggregator( instrument, bar_type, handler, Logger(TestClock()), ) tick1 = TradeTick( instrument_id=AUDUSD_SIM.id, price=Price.from_str("1.00001"), size=Quantity.from_int(1), aggressor_side=AggressorSide.BUY, match_id="123456", ts_event_ns=0, ts_recv_ns=0, ) # Act aggregator.handle_trade_tick(tick1) # Assert self.assertEqual(0, len(bar_store.get_store()))
def test_run_quote_ticks_through_aggregator_results_in_expected_bars(self): # Arrange bar_store = ObjectStorer() handler = bar_store.store instrument_id = TestStubs.audusd_id() bar_spec = BarSpecification(100, BarAggregation.TICK, PriceType.MID) bar_type = BarType(instrument_id, bar_spec) aggregator = TickBarAggregator(bar_type, handler, TestLogger(TestClock())) wrangler = QuoteTickDataWrangler( instrument=AUDUSD_SIM, data_quotes=TestDataProvider.audusd_ticks(), ) wrangler.pre_process(instrument_indexer=0) ticks = wrangler.build_ticks() # Act for tick in ticks: aggregator.handle_quote_tick(tick) # Assert last_bar = bar_store.get_store()[-1].bar self.assertEqual(999, len(bar_store.get_store())) self.assertEqual(Price("0.66939"), last_bar.open) self.assertEqual(Price("0.66947"), last_bar.high) self.assertEqual(Price("0.669355"), last_bar.low) self.assertEqual(Price("0.66945"), last_bar.close) self.assertEqual(Quantity(100), last_bar.volume)
def test_run_trade_ticks_through_aggregator_results_in_expected_bars(self): # Arrange bar_store = ObjectStorer() handler = bar_store.store instrument = ETHUSDT_BINANCE bar_spec = BarSpecification(1000, BarAggregation.TICK, PriceType.LAST) bar_type = BarType(instrument.id, bar_spec) aggregator = TickBarAggregator( instrument, bar_type, handler, Logger(TestClock()), ) wrangler = TradeTickDataWrangler(instrument=ETHUSDT_BINANCE) provider = TestDataProvider() ticks = wrangler.process( provider.read_csv_ticks("binance-ethusdt-trades.csv")[:10000]) # Act for tick in ticks: aggregator.handle_trade_tick(tick) # Assert last_bar = bar_store.get_store()[-1] assert len(bar_store.get_store()) == 10 assert last_bar.open == Price.from_str("424.70") assert last_bar.high == Price.from_str("425.25") assert last_bar.low == Price.from_str("424.51") assert last_bar.close == Price.from_str("425.15") assert last_bar.volume == Quantity.from_int(3075)
def test_handle_quote_tick_when_count_below_threshold_updates(self): # Arrange bar_store = ObjectStorer() handler = bar_store.store instrument = AUDUSD_SIM bar_spec = BarSpecification(3, BarAggregation.TICK, PriceType.MID) bar_type = BarType(instrument.id, bar_spec) aggregator = TickBarAggregator( instrument, bar_type, handler, Logger(TestClock()), ) tick1 = QuoteTick( instrument_id=instrument.id, bid=Price.from_str("1.00001"), ask=Price.from_str("1.00004"), bid_size=Quantity.from_int(1), ask_size=Quantity.from_int(1), ts_event_ns=0, ts_recv_ns=0, ) # Act aggregator.handle_quote_tick(tick1) # Assert self.assertEqual(0, len(bar_store.get_store()))
def test_run_quote_ticks_through_aggregator_results_in_expected_bars(self): # Arrange bar_store = ObjectStorer() handler = bar_store.store instrument = AUDUSD_SIM bar_spec = BarSpecification(100, BarAggregation.TICK, PriceType.MID) bar_type = BarType(instrument.id, bar_spec) aggregator = TickBarAggregator( instrument, bar_type, handler, Logger(TestClock()), ) # Setup data wrangler = QuoteTickDataWrangler(instrument) provider = TestDataProvider() ticks = wrangler.process( provider.read_csv_ticks("truefx-audusd-ticks.csv")[:1000]) # Act for tick in ticks: aggregator.handle_quote_tick(tick) # Assert last_bar = bar_store.get_store()[-1] assert len(bar_store.get_store()) == 10 assert last_bar.open == Price.from_str("0.670335") assert last_bar.high == Price.from_str("0.670345") assert last_bar.low == Price.from_str("0.670225") assert last_bar.close == Price.from_str("0.670230") assert last_bar.volume == Quantity.from_int(100000000)
def test_handle_trade_tick_when_count_below_threshold_updates(self): # Arrange bar_store = ObjectStorer() handler = bar_store.store instrument_id = TestStubs.audusd_id() bar_spec = BarSpecification(3, BarAggregation.TICK, PriceType.LAST) bar_type = BarType(instrument_id, bar_spec) aggregator = TickBarAggregator(bar_type, handler, Logger(TestClock())) tick1 = TradeTick( instrument_id=AUDUSD_SIM.id, price=Price("1.00001"), size=Quantity(1), side=OrderSide.BUY, match_id=TradeMatchId("123456"), timestamp_ns=0, ) # Act aggregator.handle_trade_tick(tick1) # Assert self.assertEqual(0, len(bar_store.get_store()))
def test_handle_quote_tick_when_count_below_threshold_updates(self): # Arrange bar_store = ObjectStorer() handler = bar_store.store symbol = TestStubs.symbol_audusd_fxcm() bar_spec = BarSpecification(3, BarAggregation.TICK, PriceType.MID) bar_type = BarType(symbol, bar_spec) aggregator = TickBarAggregator(bar_type, handler, TestLogger(TestClock())) tick1 = QuoteTick( symbol=AUDUSD_SIM.symbol, bid=Price("1.00001"), ask=Price("1.00004"), bid_size=Quantity(1), ask_size=Quantity(1), timestamp=UNIX_EPOCH, ) # Act aggregator.handle_quote_tick(tick1) # Assert self.assertEqual(0, len(bar_store.get_store()))
def test_handle_trade_tick_when_count_at_threshold_sends_bar_to_handler( self): # Arrange bar_store = ObjectStorer() handler = bar_store.store instrument = AUDUSD_SIM bar_spec = BarSpecification(3, BarAggregation.TICK, PriceType.LAST) bar_type = BarType(instrument.id, bar_spec) aggregator = TickBarAggregator( instrument, bar_type, handler, Logger(TestClock()), ) tick1 = TradeTick( instrument_id=instrument.id, price=Price.from_str("1.00001"), size=Quantity.from_int(1), aggressor_side=AggressorSide.BUY, trade_id="123456", ts_event=0, ts_init=0, ) tick2 = TradeTick( instrument_id=instrument.id, price=Price.from_str("1.00002"), size=Quantity.from_int(1), aggressor_side=AggressorSide.BUY, trade_id="123457", ts_event=0, ts_init=0, ) tick3 = TradeTick( instrument_id=instrument.id, price=Price.from_str("1.00000"), size=Quantity.from_int(1), aggressor_side=AggressorSide.BUY, trade_id="123458", ts_event=0, ts_init=0, ) # Act aggregator.handle_trade_tick(tick1) aggregator.handle_trade_tick(tick2) aggregator.handle_trade_tick(tick3) # Assert assert len(bar_store.get_store()) == 1 assert bar_store.get_store()[0].open == Price.from_str("1.00001") assert bar_store.get_store()[0].high == Price.from_str("1.00002") assert bar_store.get_store()[0].low == Price.from_str("1.00000") assert bar_store.get_store()[0].close == Price.from_str("1.00000") assert bar_store.get_store()[0].volume == Quantity.from_int(3)
def test_handle_quote_tick_when_count_at_threshold_sends_bar_to_handler( self): # Arrange bar_store = ObjectStorer() handler = bar_store.store instrument = AUDUSD_SIM bar_spec = BarSpecification(3, BarAggregation.TICK, PriceType.MID) bar_type = BarType(instrument.id, bar_spec) aggregator = TickBarAggregator( instrument, bar_type, handler, Logger(TestClock()), ) tick1 = QuoteTick( instrument_id=instrument.id, bid=Price.from_str("1.00001"), ask=Price.from_str("1.00004"), bid_size=Quantity.from_int(1), ask_size=Quantity.from_int(1), ts_event=0, ts_init=0, ) tick2 = QuoteTick( instrument_id=instrument.id, bid=Price.from_str("1.00002"), ask=Price.from_str("1.00005"), bid_size=Quantity.from_int(1), ask_size=Quantity.from_int(1), ts_event=0, ts_init=0, ) tick3 = QuoteTick( instrument_id=instrument.id, bid=Price.from_str("1.00000"), ask=Price.from_str("1.00003"), bid_size=Quantity.from_int(1), ask_size=Quantity.from_int(1), ts_event=0, ts_init=0, ) # Act aggregator.handle_quote_tick(tick1) aggregator.handle_quote_tick(tick2) aggregator.handle_quote_tick(tick3) # Assert assert len(bar_store.get_store()) == 1 assert bar_store.get_store()[0].open == Price.from_str("1.000025") assert bar_store.get_store()[0].high == Price.from_str("1.000035") assert bar_store.get_store()[0].low == Price.from_str("1.000015") assert bar_store.get_store()[0].close == Price.from_str("1.000015") assert bar_store.get_store()[0].volume == Quantity.from_int(3)
def test_handle_trade_tick_when_count_at_threshold_sends_bar_to_handler( self): # Arrange bar_store = ObjectStorer() handler = bar_store.store instrument_id = TestStubs.audusd_id() bar_spec = BarSpecification(3, BarAggregation.TICK, PriceType.LAST) bar_type = BarType(instrument_id, bar_spec) aggregator = TickBarAggregator(bar_type, handler, Logger(TestClock())) tick1 = TradeTick( instrument_id=AUDUSD_SIM.id, price=Price.from_str("1.00001"), size=Quantity.from_int(1), aggressor_side=AggressorSide.BUY, match_id=TradeMatchId("123456"), timestamp_origin_ns=0, timestamp_ns=0, ) tick2 = TradeTick( instrument_id=AUDUSD_SIM.id, price=Price.from_str("1.00002"), size=Quantity.from_int(1), aggressor_side=AggressorSide.BUY, match_id=TradeMatchId("123457"), timestamp_origin_ns=0, timestamp_ns=0, ) tick3 = TradeTick( instrument_id=AUDUSD_SIM.id, price=Price.from_str("1.00000"), size=Quantity.from_int(1), aggressor_side=AggressorSide.BUY, match_id=TradeMatchId("123458"), timestamp_origin_ns=0, timestamp_ns=0, ) # Act aggregator.handle_trade_tick(tick1) aggregator.handle_trade_tick(tick2) aggregator.handle_trade_tick(tick3) # Assert self.assertEqual(1, len(bar_store.get_store())) self.assertEqual(Price.from_str("1.00001"), bar_store.get_store()[0].open) self.assertEqual(Price.from_str("1.00002"), bar_store.get_store()[0].high) self.assertEqual(Price.from_str("1.00000"), bar_store.get_store()[0].low) self.assertEqual(Price.from_str("1.00000"), bar_store.get_store()[0].close) self.assertEqual(Quantity.from_int(3), bar_store.get_store()[0].volume)
def test_handle_quote_tick_when_count_at_threshold_sends_bar_to_handler( self): # Arrange bar_store = ObjectStorer() handler = bar_store.store instrument_id = TestStubs.audusd_id() bar_spec = BarSpecification(3, BarAggregation.TICK, PriceType.MID) bar_type = BarType(instrument_id, bar_spec) aggregator = TickBarAggregator(bar_type, handler, Logger(TestClock())) tick1 = QuoteTick( instrument_id=AUDUSD_SIM.id, bid=Price.from_str("1.00001"), ask=Price.from_str("1.00004"), bid_size=Quantity.from_int(1), ask_size=Quantity.from_int(1), timestamp_origin_ns=0, timestamp_ns=0, ) tick2 = QuoteTick( instrument_id=AUDUSD_SIM.id, bid=Price.from_str("1.00002"), ask=Price.from_str("1.00005"), bid_size=Quantity.from_int(1), ask_size=Quantity.from_int(1), timestamp_origin_ns=0, timestamp_ns=0, ) tick3 = QuoteTick( instrument_id=AUDUSD_SIM.id, bid=Price.from_str("1.00000"), ask=Price.from_str("1.00003"), bid_size=Quantity.from_int(1), ask_size=Quantity.from_int(1), timestamp_origin_ns=0, timestamp_ns=0, ) # Act aggregator.handle_quote_tick(tick1) aggregator.handle_quote_tick(tick2) aggregator.handle_quote_tick(tick3) # Assert self.assertEqual(1, len(bar_store.get_store())) self.assertEqual(Price.from_str("1.000025"), bar_store.get_store()[0].open) self.assertEqual(Price.from_str("1.000035"), bar_store.get_store()[0].high) self.assertEqual(Price.from_str("1.000015"), bar_store.get_store()[0].low) self.assertEqual(Price.from_str("1.000015"), bar_store.get_store()[0].close) self.assertEqual(Quantity.from_int(3), bar_store.get_store()[0].volume)
def test_handle_trade_tick_when_count_at_threshold_sends_bar_to_handler( self): # Arrange bar_store = ObjectStorer() handler = bar_store.store symbol = TestStubs.symbol_audusd() bar_spec = BarSpecification(3, BarAggregation.TICK, PriceType.LAST) bar_type = BarType(symbol, bar_spec) aggregator = TickBarAggregator(bar_type, handler, TestLogger(TestClock())) tick1 = TradeTick( symbol=AUDUSD_SIM.symbol, price=Price("1.00001"), size=Quantity(1), side=OrderSide.BUY, match_id=TradeMatchId("123456"), timestamp=UNIX_EPOCH, ) tick2 = TradeTick( symbol=AUDUSD_SIM.symbol, price=Price("1.00002"), size=Quantity(1), side=OrderSide.BUY, match_id=TradeMatchId("123457"), timestamp=UNIX_EPOCH, ) tick3 = TradeTick( symbol=AUDUSD_SIM.symbol, price=Price("1.00000"), size=Quantity(1), side=OrderSide.BUY, match_id=TradeMatchId("123458"), timestamp=UNIX_EPOCH, ) # Act aggregator.handle_trade_tick(tick1) aggregator.handle_trade_tick(tick2) aggregator.handle_trade_tick(tick3) # Assert self.assertEqual(1, len(bar_store.get_store())) self.assertEqual(Price("1.00001"), bar_store.get_store()[0].bar.open) self.assertEqual(Price("1.00002"), bar_store.get_store()[0].bar.high) self.assertEqual(Price("1.00000"), bar_store.get_store()[0].bar.low) self.assertEqual(Price("1.00000"), bar_store.get_store()[0].bar.close) self.assertEqual(Quantity(3), bar_store.get_store()[0].bar.volume)
def test_handle_quote_tick_when_count_at_threshold_sends_bar_to_handler( self): # Arrange bar_store = ObjectStorer() handler = bar_store.store symbol = TestStubs.symbol_audusd() bar_spec = BarSpecification(3, BarAggregation.TICK, PriceType.MID) bar_type = BarType(symbol, bar_spec) aggregator = TickBarAggregator(bar_type, handler, TestLogger(TestClock())) tick1 = QuoteTick( symbol=AUDUSD_SIM.symbol, bid=Price("1.00001"), ask=Price("1.00004"), bid_size=Quantity(1), ask_size=Quantity(1), timestamp=UNIX_EPOCH, ) tick2 = QuoteTick( symbol=AUDUSD_SIM.symbol, bid=Price("1.00002"), ask=Price("1.00005"), bid_size=Quantity(1), ask_size=Quantity(1), timestamp=UNIX_EPOCH, ) tick3 = QuoteTick( symbol=AUDUSD_SIM.symbol, bid=Price("1.00000"), ask=Price("1.00003"), bid_size=Quantity(1), ask_size=Quantity(1), timestamp=UNIX_EPOCH, ) # Act aggregator.handle_quote_tick(tick1) aggregator.handle_quote_tick(tick2) aggregator.handle_quote_tick(tick3) # Assert self.assertEqual(1, len(bar_store.get_store())) self.assertEqual(Price("1.000025"), bar_store.get_store()[0].bar.open) self.assertEqual(Price("1.000035"), bar_store.get_store()[0].bar.high) self.assertEqual(Price("1.000015"), bar_store.get_store()[0].bar.low) self.assertEqual(Price("1.000015"), bar_store.get_store()[0].bar.close) self.assertEqual(Quantity(3), bar_store.get_store()[0].bar.volume)