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(1000, BarAggregation.VALUE, PriceType.MID) bar_type = BarType(instrument_id, bar_spec) aggregator = ValueBarAggregator(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(67, len(bar_store.get_store())) self.assertEqual(Price("0.66921"), last_bar.open) self.assertEqual(Price("0.669485"), last_bar.high) self.assertEqual(Price("0.669205"), last_bar.low) self.assertEqual(Price("0.669475"), last_bar.close) self.assertEqual(Quantity(1494), last_bar.volume)
class BulkTickBarBuilderTests(unittest.TestCase): def test_given_list_of_ticks_aggregates_tick_bars(self): # Arrange tick_data = TestDataProvider.usdjpy_ticks() bid_data = TestDataProvider.usdjpy_1min_bid() ask_data = TestDataProvider.usdjpy_1min_ask() self.wrangler = QuoteTickDataWrangler( instrument=TestInstrumentProvider.default_fx_ccy("USD/JPY"), data_quotes=tick_data, data_bars_bid={BarAggregation.MINUTE: bid_data}, data_bars_ask={BarAggregation.MINUTE: ask_data}, ) self.wrangler.pre_process(0) bar_store = ObjectStorer() handler = bar_store.store_2 instrument_id = TestStubs.usdjpy_id() bar_spec = BarSpecification(3, BarAggregation.TICK, PriceType.MID) bar_type = BarType(instrument_id, bar_spec) clock = TestClock() logger = TestLogger(clock) ticks = self.wrangler.build_ticks() builder = BulkTickBarBuilder(bar_type, logger, handler) # Act builder.receive(ticks) # Assert self.assertEqual(333, len(bar_store.get_store()[0][1]))
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(1000, BarAggregation.VOLUME, PriceType.MID) bar_type = BarType(instrument.id, bar_spec) aggregator = VolumeBarAggregator( instrument, bar_type, handler, Logger(TestClock()), ) wrangler = QuoteTickDataWrangler( instrument=instrument, data_quotes=TestDataProvider.audusd_ticks(), ) wrangler.pre_process(instrument_indexer=0, default_volume=1) ticks = wrangler.build_ticks() # Act for tick in ticks: aggregator.handle_quote_tick(tick) # Assert last_bar = bar_store.get_store()[-1] self.assertEqual(99, len(bar_store.get_store())) self.assertEqual(Price.from_str("0.669325"), last_bar.open) self.assertEqual(Price.from_str("0.669485"), last_bar.high) self.assertEqual(Price.from_str("0.66917"), last_bar.low) self.assertEqual(Price.from_str("0.66935"), last_bar.close) self.assertEqual(Quantity.from_int(1000), last_bar.volume)
class TardisQuoteDataWranglerTests(unittest.TestCase): def setUp(self): # Fixture Setup self.clock = TestClock() def test_tick_data(self): # Arrange # Act ticks = TestDataProvider.tardis_quotes() # Assert self.assertEqual(9999, len(ticks)) def test_pre_process_with_tick_data(self): # Arrange tick_data = TestDataProvider.tardis_quotes() self.tick_builder = QuoteTickDataWrangler( instrument=TestInstrumentProvider.btcusdt_binance(), data_quotes=tick_data, data_bars_bid=None, data_bars_ask=None, ) # Act self.tick_builder.pre_process(0) ticks = self.tick_builder.processed_data # Assert self.assertEqual(BarAggregation.TICK, self.tick_builder.resolution) self.assertEqual(9999, len(ticks)) self.assertEqual( Timestamp("2020-02-22 00:00:03.522418+0000", tz="UTC"), ticks.iloc[1].name) self.assertEqual("0.670000", ticks.bid_size[0]) self.assertEqual("0.840000", ticks.ask_size[0]) self.assertEqual("9681.92", ticks.bid[0]) self.assertEqual("9682.00", ticks.ask[0]) self.assertEqual( sorted([ "ask", "ask_size", "bid", "bid_size", "instrument_id", "symbol" ]), sorted(ticks.columns), )
class QuoteTickDataWranglerTests(unittest.TestCase): def setUp(self): # Fixture Setup self.clock = TestClock() def test_tick_data(self): # Arrange # Act ticks = TestDataProvider.usdjpy_ticks() # Assert self.assertEqual(1000, len(ticks)) def test_pre_process_with_tick_data(self): # Arrange tick_data = TestDataProvider.usdjpy_ticks() self.tick_builder = QuoteTickDataWrangler( instrument=TestInstrumentProvider.default_fx_ccy( TestStubs.symbol_usdjpy_fxcm()), data_quotes=tick_data, data_bars_bid=None, data_bars_ask=None, ) # Act self.tick_builder.pre_process(0) ticks = self.tick_builder.processed_data # Assert self.assertEqual(BarAggregation.TICK, self.tick_builder.resolution) self.assertEqual(1000, len(ticks)) self.assertEqual(Timestamp("2013-01-01 22:02:35.907000", tz="UTC"), ticks.iloc[1].name) def test_pre_process_with_bar_data(self): # Arrange bid_data = TestDataProvider.usdjpy_1min_bid() ask_data = TestDataProvider.usdjpy_1min_ask() self.tick_builder = QuoteTickDataWrangler( instrument=TestInstrumentProvider.default_fx_ccy( TestStubs.symbol_usdjpy_fxcm()), data_quotes=None, data_bars_bid={BarAggregation.MINUTE: bid_data}, data_bars_ask={BarAggregation.MINUTE: ask_data}, ) # Act self.tick_builder.pre_process(0) tick_data = self.tick_builder.processed_data # Assert self.assertEqual(BarAggregation.MINUTE, self.tick_builder.resolution) self.assertEqual(115044, len(tick_data)) self.assertEqual( Timestamp("2013-01-31 23:59:59.700000+0000", tz="UTC"), tick_data.iloc[0].name) self.assertEqual( Timestamp("2013-01-31 23:59:59.800000+0000", tz="UTC"), tick_data.iloc[1].name) self.assertEqual( Timestamp("2013-01-31 23:59:59.900000+0000", tz="UTC"), tick_data.iloc[2].name) self.assertEqual(Timestamp("2013-02-01 00:00:00+0000", tz="UTC"), tick_data.iloc[3].name) self.assertEqual(0, tick_data.iloc[0]["symbol"]) self.assertEqual("1", tick_data.iloc[0]["bid_size"]) self.assertEqual("1", tick_data.iloc[0]["ask_size"]) self.assertEqual("1", tick_data.iloc[1]["bid_size"]) self.assertEqual("1", tick_data.iloc[1]["ask_size"]) self.assertEqual("1", tick_data.iloc[2]["bid_size"]) self.assertEqual("1", tick_data.iloc[2]["ask_size"]) self.assertEqual("1", tick_data.iloc[3]["bid_size"]) self.assertEqual("1", tick_data.iloc[3]["ask_size"]) def test_build_ticks_with_tick_data(self): # Arrange tick_data = TestDataProvider.audusd_ticks() self.tick_builder = QuoteTickDataWrangler( instrument=TestInstrumentProvider.default_fx_ccy( TestStubs.symbol_audusd_fxcm()), data_quotes=tick_data, data_bars_bid=None, data_bars_ask=None, ) # Act self.tick_builder.pre_process(0) ticks = self.tick_builder.build_ticks() # Assert self.assertEqual(100000, len(ticks)) self.assertEqual(Price("0.67067"), ticks[0].bid) self.assertEqual(Price("0.67070"), ticks[0].ask) self.assertEqual(Quantity("1"), ticks[0].bid_size) self.assertEqual(Quantity("1"), ticks[0].ask_size) self.assertEqual( Timestamp("2020-01-30 15:28:09.820000+0000", tz="UTC"), ticks[0].timestamp) def test_build_ticks_with_bar_data(self): # Arrange bid_data = TestDataProvider.usdjpy_1min_bid() ask_data = TestDataProvider.usdjpy_1min_ask() self.tick_builder = QuoteTickDataWrangler( instrument=TestInstrumentProvider.default_fx_ccy( TestStubs.symbol_usdjpy_fxcm()), data_quotes=None, data_bars_bid={BarAggregation.MINUTE: bid_data}, data_bars_ask={BarAggregation.MINUTE: ask_data}, ) # Act self.tick_builder.pre_process(0) ticks = self.tick_builder.build_ticks() # Assert self.assertEqual(115044, len(ticks)) self.assertEqual(Price("91.715"), ticks[0].bid) self.assertEqual(Price("91.717"), ticks[0].ask) self.assertEqual(Quantity("1"), ticks[0].bid_size) self.assertEqual(Quantity("1"), ticks[0].ask_size) self.assertEqual( Timestamp("2013-01-31 23:59:59.700000+0000", tz="UTC"), ticks[0].timestamp)
class QuoteTickDataWranglerTests(unittest.TestCase): def setUp(self): # Fixture Setup self.clock = TestClock() def test_tick_data(self): # Arrange # Act ticks = TestDataProvider.usdjpy_ticks() # Assert self.assertEqual(1000, len(ticks)) def test_pre_process_with_tick_data(self): # Arrange tick_data = TestDataProvider.usdjpy_ticks() self.tick_builder = QuoteTickDataWrangler( instrument=TestInstrumentProvider.default_fx_ccy("USD/JPY"), data_quotes=tick_data, data_bars_bid=None, data_bars_ask=None, ) # Act self.tick_builder.pre_process(0, 42) ticks = self.tick_builder.processed_data # Assert self.assertEqual(BarAggregation.TICK, self.tick_builder.resolution) self.assertEqual(1000, len(ticks)) self.assertEqual(Timestamp("2013-01-01 22:02:35.907000", tz="UTC"), ticks.iloc[1].name) def test_pre_process_with_bar_data(self): # Arrange bid_data = TestDataProvider.usdjpy_1min_bid() ask_data = TestDataProvider.usdjpy_1min_ask() self.tick_builder = QuoteTickDataWrangler( instrument=TestInstrumentProvider.default_fx_ccy("USD/JPY"), data_quotes=None, data_bars_bid={BarAggregation.MINUTE: bid_data}, data_bars_ask={BarAggregation.MINUTE: ask_data}, ) # Act self.tick_builder.pre_process(0, 42) tick_data = self.tick_builder.processed_data # Assert self.assertEqual(BarAggregation.MINUTE, self.tick_builder.resolution) self.assertEqual(115044, len(tick_data)) self.assertEqual( Timestamp("2013-01-31 23:59:59.700000+0000", tz="UTC"), tick_data.iloc[0].name, ) self.assertEqual( Timestamp("2013-01-31 23:59:59.800000+0000", tz="UTC"), tick_data.iloc[1].name, ) self.assertEqual( Timestamp("2013-01-31 23:59:59.900000+0000", tz="UTC"), tick_data.iloc[2].name, ) self.assertEqual(Timestamp("2013-02-01 00:00:00+0000", tz="UTC"), tick_data.iloc[3].name) self.assertEqual(0, tick_data.iloc[0]["instrument_id"]) self.assertEqual("1000000", tick_data.iloc[0]["bid_size"]) self.assertEqual("1000000", tick_data.iloc[0]["ask_size"]) self.assertEqual("1000000", tick_data.iloc[1]["bid_size"]) self.assertEqual("1000000", tick_data.iloc[1]["ask_size"]) self.assertEqual("1000000", tick_data.iloc[2]["bid_size"]) self.assertEqual("1000000", tick_data.iloc[2]["ask_size"]) self.assertEqual("1000000", tick_data.iloc[3]["bid_size"]) self.assertEqual("1000000", tick_data.iloc[3]["ask_size"]) def test_build_ticks_with_tick_data(self): # Arrange tick_data = TestDataProvider.audusd_ticks() self.tick_builder = QuoteTickDataWrangler( instrument=TestInstrumentProvider.default_fx_ccy("AUD/USD"), data_quotes=tick_data, data_bars_bid=None, data_bars_ask=None, ) # Act self.tick_builder.pre_process(0) ticks = self.tick_builder.build_ticks() # Assert self.assertEqual(100000, len(ticks)) self.assertEqual(Price.from_str("0.67067"), ticks[0].bid) self.assertEqual(Price.from_str("0.67070"), ticks[0].ask) self.assertEqual(Quantity.from_str("1000000"), ticks[0].bid_size) self.assertEqual(Quantity.from_str("1000000"), ticks[0].ask_size) self.assertEqual(1580398089820000000, ticks[0].ts_recv_ns) self.assertEqual(1580504394500999936, ticks[99999].ts_recv_ns) def test_build_ticks_with_bar_data(self): # Arrange bid_data = TestDataProvider.usdjpy_1min_bid() ask_data = TestDataProvider.usdjpy_1min_ask() self.tick_builder = QuoteTickDataWrangler( instrument=TestInstrumentProvider.default_fx_ccy("USD/JPY"), data_quotes=None, data_bars_bid={BarAggregation.MINUTE: bid_data}, data_bars_ask={BarAggregation.MINUTE: ask_data}, ) # Act self.tick_builder.pre_process(0) ticks = self.tick_builder.build_ticks() # Assert self.assertEqual(115044, len(ticks)) self.assertEqual(Price.from_str("91.715"), ticks[0].bid) self.assertEqual(Price.from_str("91.717"), ticks[0].ask) self.assertEqual(Quantity.from_str("1000000"), ticks[0].bid_size) self.assertEqual(Quantity.from_str("1000000"), ticks[0].ask_size) self.assertEqual(1359676799700000000, ticks[0].ts_recv_ns)