def test_run_trade_ticks_through_aggregator_results_in_expected_bars(self): # Arrange bar_store = ObjectStorer() handler = bar_store.store bar_spec = BarSpecification(10000, BarAggregation.VALUE, PriceType.LAST) bar_type = BarType(ETHUSDT_BINANCE.id, bar_spec) aggregator = ValueBarAggregator(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(7962, len(bar_store.get_store())) self.assertEqual(Price("426.86"), last_bar.open) self.assertEqual(Price("426.94"), last_bar.high) self.assertEqual(Price("426.83"), last_bar.low) self.assertEqual(Price("426.94"), last_bar.close) self.assertEqual(Quantity(23), last_bar.volume)
def test_handle_trade_tick_when_value_below_threshold_updates(self): # Arrange bar_store = ObjectStorer() handler = bar_store.store instrument_id = TestStubs.audusd_id() bar_spec = BarSpecification(100000, BarAggregation.VALUE, PriceType.LAST) bar_type = BarType(instrument_id, bar_spec) aggregator = ValueBarAggregator(bar_type, handler, TestLogger(TestClock())) tick1 = TradeTick( instrument_id=AUDUSD_SIM.id, price=Price("15000.00"), size=Quantity("3.5"), side=OrderSide.BUY, match_id=TradeMatchId("123456"), timestamp=UNIX_EPOCH, ) # Act aggregator.handle_trade_tick(tick1) # Assert self.assertEqual(0, len(bar_store.get_store())) self.assertEqual(Decimal("52500.000"), aggregator.cum_value)
def test_handle_trade_tick_when_value_below_threshold_updates(self): # Arrange bar_store = ObjectStorer() handler = bar_store.store instrument_id = TestStubs.audusd_id() bar_spec = BarSpecification(100000, BarAggregation.VALUE, PriceType.LAST) bar_type = BarType(instrument_id, bar_spec) aggregator = ValueBarAggregator( AUDUSD_SIM, bar_type, handler, Logger(TestClock()), ) tick1 = TradeTick( instrument_id=AUDUSD_SIM.id, price=Price.from_str("15000.00"), size=Quantity.from_str("3.5"), 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())) self.assertEqual(Decimal("52500.000"), aggregator.get_cumulative_value())
def test_run_trade_ticks_through_aggregator_results_in_expected_bars(self): # Arrange bar_store = ObjectStorer() handler = bar_store.store bar_spec = BarSpecification(10000, BarAggregation.VALUE, PriceType.LAST) bar_type = BarType(ETHUSDT_BINANCE.id, bar_spec) aggregator = ValueBarAggregator( ETHUSDT_BINANCE, bar_type, handler, Logger(TestClock()), ) wrangler = TradeTickDataWrangler(instrument=ETHUSDT_BINANCE) provider = TestDataProvider() ticks = wrangler.process( provider.read_csv_ticks("binance-ethusdt-trades.csv")[:1000]) # Act for tick in ticks: aggregator.handle_trade_tick(tick) # Assert last_bar = bar_store.get_store()[-1] assert len(bar_store.get_store()) == 109 assert last_bar.open == Price.from_str("423.19") assert last_bar.high == Price.from_str("423.25") assert last_bar.low == Price.from_str("423.19") assert last_bar.close == Price.from_str("423.25") assert last_bar.volume == Quantity.from_int(24)
def test_handle_trade_tick_when_volume_beyond_threshold_sends_bars_to_handler( self): # Arrange bar_store = ObjectStorer() handler = bar_store.store instrument_id = TestStubs.audusd_id() bar_spec = BarSpecification(100000, BarAggregation.VALUE, PriceType.LAST) bar_type = BarType(instrument_id, bar_spec) aggregator = ValueBarAggregator(bar_type, handler, TestLogger(TestClock())) tick1 = TradeTick( instrument_id=AUDUSD_SIM.id, price=Price("20.00001"), size=Quantity("3000.00"), side=OrderSide.BUY, match_id=TradeMatchId("123456"), timestamp=UNIX_EPOCH, ) tick2 = TradeTick( instrument_id=AUDUSD_SIM.id, price=Price("20.00002"), size=Quantity("4000.00"), side=OrderSide.BUY, match_id=TradeMatchId("123457"), timestamp=UNIX_EPOCH, ) tick3 = TradeTick( instrument_id=AUDUSD_SIM.id, price=Price("20.00000"), size=Quantity("5000.00"), 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(2, len(bar_store.get_store())) self.assertEqual(Price("20.00001"), bar_store.get_store()[0].bar.open) self.assertEqual(Price("20.00002"), bar_store.get_store()[0].bar.high) self.assertEqual(Price("20.00001"), bar_store.get_store()[0].bar.low) self.assertEqual(Price('20.00002'), bar_store.get_store()[0].bar.close) self.assertEqual(Quantity("5000"), bar_store.get_store()[0].bar.volume) self.assertEqual(Price("20.00002"), bar_store.get_store()[1].bar.open) self.assertEqual(Price("20.00002"), bar_store.get_store()[1].bar.high) self.assertEqual(Price("20.00000"), bar_store.get_store()[1].bar.low) self.assertEqual(Price('20.00000'), bar_store.get_store()[1].bar.close) self.assertEqual(Quantity("5000.00"), bar_store.get_store()[1].bar.volume) self.assertEqual(Decimal("40000.00000"), aggregator.cum_value)
def test_run_trade_ticks_through_aggregator_results_in_expected_bars(self): # Arrange bar_store = ObjectStorer() handler = bar_store.store bar_spec = BarSpecification(10000, BarAggregation.VALUE, PriceType.LAST) bar_type = BarType(ETHUSDT_BINANCE.id, bar_spec) aggregator = ValueBarAggregator(bar_type, handler, Logger(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)
def test_handle_trade_tick_when_volume_beyond_threshold_sends_bars_to_handler( self): # Arrange bar_store = ObjectStorer() handler = bar_store.store instrument_id = TestStubs.audusd_id() bar_spec = BarSpecification(100000, BarAggregation.VALUE, PriceType.LAST) bar_type = BarType(instrument_id, bar_spec) aggregator = ValueBarAggregator( AUDUSD_SIM, bar_type, handler, Logger(TestClock()), ) tick1 = TradeTick( instrument_id=AUDUSD_SIM.id, price=Price.from_str("20.00001"), size=Quantity.from_str("3000.00"), aggressor_side=AggressorSide.BUY, match_id="123456", ts_event_ns=0, ts_recv_ns=0, ) tick2 = TradeTick( instrument_id=AUDUSD_SIM.id, price=Price.from_str("20.00002"), size=Quantity.from_str("4000.00"), aggressor_side=AggressorSide.BUY, match_id="123457", ts_event_ns=0, ts_recv_ns=0, ) tick3 = TradeTick( instrument_id=AUDUSD_SIM.id, price=Price.from_str("20.00000"), size=Quantity.from_str("5000.00"), aggressor_side=AggressorSide.BUY, match_id="123458", ts_event_ns=0, ts_recv_ns=0, ) # Act aggregator.handle_trade_tick(tick1) aggregator.handle_trade_tick(tick2) aggregator.handle_trade_tick(tick3) # Assert self.assertEqual(2, len(bar_store.get_store())) self.assertEqual(Price.from_str("20.00001"), bar_store.get_store()[0].open) self.assertEqual(Price.from_str("20.00002"), bar_store.get_store()[0].high) self.assertEqual(Price.from_str("20.00001"), bar_store.get_store()[0].low) self.assertEqual(Price.from_str("20.00002"), bar_store.get_store()[0].close) self.assertEqual(Quantity.from_str("5000.00"), bar_store.get_store()[0].volume) self.assertEqual(Price.from_str("20.00002"), bar_store.get_store()[1].open) self.assertEqual(Price.from_str("20.00002"), bar_store.get_store()[1].high) self.assertEqual(Price.from_str("20.00000"), bar_store.get_store()[1].low) self.assertEqual(Price.from_str("20.00000"), bar_store.get_store()[1].close) self.assertEqual(Quantity.from_str("5000.00"), bar_store.get_store()[1].volume) self.assertEqual( Decimal("40000.11000"), aggregator.get_cumulative_value()) # TODO: WIP - Should be 40000
def test_handle_trade_tick_when_volume_beyond_threshold_sends_bars_to_handler( self): # Arrange bar_store = ObjectStorer() handler = bar_store.store instrument_id = TestIdStubs.audusd_id() bar_spec = BarSpecification(100000, BarAggregation.VALUE, PriceType.LAST) bar_type = BarType(instrument_id, bar_spec) aggregator = ValueBarAggregator( AUDUSD_SIM, bar_type, handler, Logger(TestClock()), ) tick1 = TradeTick( instrument_id=AUDUSD_SIM.id, price=Price.from_str("20.00001"), size=Quantity.from_str("3000.00"), aggressor_side=AggressorSide.BUY, trade_id=TradeId("123456"), ts_event=0, ts_init=0, ) tick2 = TradeTick( instrument_id=AUDUSD_SIM.id, price=Price.from_str("20.00002"), size=Quantity.from_str("4000.00"), aggressor_side=AggressorSide.BUY, trade_id=TradeId("123457"), ts_event=0, ts_init=0, ) tick3 = TradeTick( instrument_id=AUDUSD_SIM.id, price=Price.from_str("20.00000"), size=Quantity.from_str("5000.00"), aggressor_side=AggressorSide.BUY, trade_id=TradeId("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()) == 2 assert bar_store.get_store()[0].open == Price.from_str("20.00001") assert bar_store.get_store()[0].high == Price.from_str("20.00002") assert bar_store.get_store()[0].low == Price.from_str("20.00001") assert bar_store.get_store()[0].close == Price.from_str("20.00002") assert bar_store.get_store()[0].volume == Quantity.from_str("5000.00") assert bar_store.get_store()[1].open == Price.from_str("20.00002") assert bar_store.get_store()[1].high == Price.from_str("20.00002") assert bar_store.get_store()[1].low == Price.from_str("20.00000") assert bar_store.get_store()[1].close == Price.from_str("20.00000") assert bar_store.get_store()[1].volume == Quantity.from_str("5000.00") assert aggregator.get_cumulative_value() == Decimal("40000.11000")