async def test_ftx_http_client(): loop = asyncio.get_event_loop() clock = LiveClock() client: FTXHttpClient = get_cached_ftx_http_client( loop=loop, clock=clock, logger=Logger(clock=clock), key=os.getenv("FTX_API_KEY"), secret=os.getenv("FTX_API_SECRET"), ) await client.connect() response = await client.list_markets( # market="ETH-PERP", ) print(json.dumps(response, indent=4)) provider = FTXInstrumentProvider( client=client, logger=Logger(clock=clock), ) await provider.load_all_async() # for instrument in provider.get_all().values(): # print(instrument) await client.disconnect()
async def test_binance_spot_market_http_client(): loop = asyncio.get_event_loop() clock = LiveClock() client = get_cached_binance_http_client( loop=loop, clock=clock, logger=Logger(clock=clock), key=os.getenv("BINANCE_API_KEY"), secret=os.getenv("BINANCE_API_SECRET"), ) await client.connect() market = BinanceSpotMarketHttpAPI(client=client) response = await market.exchange_info(symbols=["BTCUSDT", "ETHUSDT"]) print(json.dumps(response, indent=4)) provider = BinanceInstrumentProvider( client=client, logger=Logger(clock=clock), ) await provider.load_all_async() print(provider.count) await client.disconnect()
def setUp(self): # Fixture Setup clock = TestClock() logger = Logger(clock) self.order_factory = OrderFactory( trader_id=TraderId("TESTER", "000"), strategy_id=StrategyId("S", "001"), clock=TestClock(), ) state = AccountState( account_id=AccountId("BINANCE", "1513111"), balances=[Money("10.00000000", BTC)], balances_free=[Money("0.00000000", BTC)], balances_locked=[Money("0.00000000", BTC)], info={}, event_id=uuid4(), timestamp_ns=0, ) self.data_cache = DataCache(logger) self.account = Account(state) self.portfolio = Portfolio(clock, logger) self.portfolio.register_account(self.account) self.portfolio.register_cache(self.data_cache) self.data_cache.add_instrument(AUDUSD_SIM) self.data_cache.add_instrument(GBPUSD_SIM) self.data_cache.add_instrument(BTCUSDT_BINANCE) self.data_cache.add_instrument(BTCUSD_BITMEX) self.data_cache.add_instrument(ETHUSD_BITMEX)
def setUp(self): # Fixture Setup clock = TestClock() logger = Logger(clock) trader_id = TraderId("TESTER-000") self.order_factory = OrderFactory( trader_id=trader_id, strategy_id=StrategyId("S-001"), clock=TestClock(), ) cache_db = BypassCacheDatabase( trader_id=trader_id, logger=logger, ) cache = Cache( database=cache_db, logger=logger, ) self.portfolio = Portfolio( cache=cache, clock=clock, logger=logger, ) self.exec_engine = ExecutionEngine( portfolio=self.portfolio, cache=cache, clock=clock, logger=logger, )
def setUp(self): # Fixture Setup self.clock = TestClock() self.uuid_factory = UUIDFactory() self.logger = Logger(self.clock) self.cache = TestStubs.cache() self.portfolio = Portfolio( cache=self.cache, clock=self.clock, logger=self.logger, ) self.data_engine = DataEngine( portfolio=self.portfolio, cache=self.cache, clock=self.clock, logger=self.logger, ) self.venue = Venue("SIM") self.client = DataClient( client_id=ClientId("TEST_PROVIDER"), engine=self.data_engine, clock=self.clock, logger=self.logger, )
def setUp(self): # Fixture Setup self.clock = LiveClock() self.uuid_factory = UUIDFactory() self.logger = Logger(self.clock, level_stdout=LogLevel.DEBUG) self.portfolio = Portfolio( clock=self.clock, logger=self.logger, ) # Fresh isolated loop testing pattern self.loop = asyncio.new_event_loop() asyncio.set_event_loop(self.loop) self.engine = LiveDataEngine( loop=self.loop, portfolio=self.portfolio, clock=self.clock, logger=self.logger, ) self.client = LiveDataClient( name="BLOOMBERG", engine=self.engine, clock=self.clock, logger=self.logger, )
def test_handle_trade_tick_when_volume_below_threshold_updates(self): # Arrange bar_store = ObjectStorer() handler = bar_store.store instrument_id = TestStubs.audusd_id() bar_spec = BarSpecification(10000, BarAggregation.VOLUME, PriceType.LAST) bar_type = BarType(instrument_id, bar_spec) aggregator = VolumeBarAggregator(bar_type, handler, Logger(TestClock())) tick1 = TradeTick( instrument_id=AUDUSD_SIM.id, price=Price("1.00001"), size=Quantity(1), aggressor_side=AggressorSide.BUY, match_id=TradeMatchId("123456"), timestamp_ns=0, ) # Act aggregator.handle_trade_tick(tick1) # Assert self.assertEqual(0, len(bar_store.get_store()))
def setup(self): # Fixture Setup self.loop = asyncio.get_event_loop() self.loop.set_debug(True) self.clock = LiveClock() self.uuid_factory = UUIDFactory() self.logger = Logger(self.clock) self.trader_id = TestStubs.trader_id() self.msgbus = MessageBus( trader_id=self.trader_id, clock=self.clock, logger=self.logger, ) self.cache = TestStubs.cache() self.portfolio = Portfolio( msgbus=self.msgbus, cache=self.cache, clock=self.clock, logger=self.logger, ) self.engine = LiveDataEngine( loop=self.loop, msgbus=self.msgbus, cache=self.cache, clock=self.clock, logger=self.logger, )
async def test_binance_websocket_client(): loop = asyncio.get_event_loop() clock = LiveClock() client = get_cached_binance_http_client( loop=loop, clock=clock, logger=Logger(clock=clock), key=os.getenv("BINANCE_API_KEY"), secret=os.getenv("BINANCE_API_SECRET"), ) await client.connect() user = BinanceUserDataHttpAPI(client=client) response = await user.create_listen_key_spot() key = response["listenKey"] ws = BinanceUserDataWebSocket( loop=loop, clock=clock, logger=LiveLogger(loop=loop, clock=clock), handler=print, ) ws.subscribe(key=key) await ws.connect(start=True) await asyncio.sleep(4) await ws.close() await client.disconnect()
def test_handle_quote_tick_when_value_beyond_threshold_sends_bar_to_handler( self): # Arrange bar_store = ObjectStorer() handler = bar_store.store instrument_id = TestStubs.audusd_id() bar_spec = BarSpecification(100000, BarAggregation.VALUE, PriceType.BID) bar_type = BarType(instrument_id, bar_spec) aggregator = ValueBarAggregator(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(20000), ask_size=Quantity.from_int(20000), 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(60000), ask_size=Quantity.from_int(20000), 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(30500), ask_size=Quantity.from_int(20000), 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.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_str("99999"), bar_store.get_store()[0].volume) self.assertEqual(Decimal("10501.400"), aggregator.get_cumulative_value())
def setup(self): # Fixture Setup self.clock = TestClock() self.uuid_factory = UUIDFactory() self.logger = Logger(self.clock) self.cache = TestStubs.cache() self.portfolio = Portfolio( cache=self.cache, clock=self.clock, logger=self.logger, ) self.data_engine = DataEngine( portfolio=self.portfolio, cache=self.cache, clock=self.clock, logger=self.logger, ) self.data_engine.process(USDJPY_SIM) self.client = BacktestMarketDataClient( client_id=ClientId("SIM"), engine=self.data_engine, clock=TestClock(), logger=self.logger, )
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, 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) # Assert last_bar = bar_store.get_store()[-1] 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_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 instrument = ETHUSDT_BINANCE bar_spec = BarSpecification(1000, BarAggregation.VOLUME, PriceType.LAST) bar_type = BarType(instrument.id, bar_spec) aggregator = VolumeBarAggregator( 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()) == 26 assert last_bar.open == Price.from_str("425.17") assert last_bar.high == Price.from_str("425.24") assert last_bar.low == Price.from_str("424.69") assert last_bar.close == Price.from_str("425.14") assert last_bar.volume == Quantity.from_int(1000)
def setUp(self): # Fixture Setup self.clock = TestClock() self.uuid_factory = UUIDFactory() self.logger = Logger(self.clock) self.portfolio = Portfolio( clock=self.clock, logger=self.logger, ) self.data_engine = DataEngine( portfolio=self.portfolio, clock=self.clock, logger=self.logger, ) self.venue = Venue("SIM") self.client = MarketDataClient( client_id=ClientId(self.venue.value), engine=self.data_engine, clock=self.clock, logger=self.logger, )
def test_handle_quote_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.BID) bar_type = BarType(instrument_id, bar_spec) aggregator = ValueBarAggregator( AUDUSD_SIM, 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(3000), ask_size=Quantity.from_int(2000), ts_event_ns=0, ts_recv_ns=0, ) # Act aggregator.handle_quote_tick(tick1) # Assert self.assertEqual(0, len(bar_store.get_store())) self.assertEqual(Decimal("3000.03000"), aggregator.get_cumulative_value())
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)
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.VOLUME, PriceType.LAST) bar_type = BarType(instrument.id, bar_spec) aggregator = VolumeBarAggregator( instrument, bar_type, handler, Logger(TestClock()), ) wrangler = TradeTickDataWrangler( instrument=instrument, data=TestDataProvider.ethusdt_trades(), ) wrangler.pre_process(instrument_indexer=0) ticks = wrangler.build_ticks() # Act for tick in ticks: aggregator.handle_trade_tick(tick) # Assert last_bar = bar_store.get_store()[-1] self.assertEqual(187, len(bar_store.get_store())) self.assertEqual(Price.from_str("426.44"), last_bar.open) self.assertEqual(Price.from_str("426.84"), last_bar.high) self.assertEqual(Price.from_str("426.00"), last_bar.low) self.assertEqual(Price.from_str("426.82"), last_bar.close) self.assertEqual(Quantity.from_int(1000), last_bar.volume)
def test_handle_quote_tick_when_volume_below_threshold_updates(self): # Arrange bar_store = ObjectStorer() handler = bar_store.store instrument = AUDUSD_SIM bar_spec = BarSpecification(10000, BarAggregation.VOLUME, PriceType.BID) bar_type = BarType(instrument.id, bar_spec) aggregator = VolumeBarAggregator( 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(3000), ask_size=Quantity.from_int(2000), 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_handle_trade_tick_when_volume_below_threshold_updates(self): # Arrange bar_store = ObjectStorer() handler = bar_store.store instrument = AUDUSD_SIM bar_spec = BarSpecification(10000, BarAggregation.VOLUME, PriceType.LAST) bar_type = BarType(instrument.id, bar_spec) aggregator = VolumeBarAggregator( 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, 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_update_timed_with_test_clock_sends_single_bar_to_handler(self): # Arrange clock = TestClock() bar_store = ObjectStorer() handler = bar_store.store instrument_id = TestStubs.audusd_id() bar_spec = BarSpecification(1, BarAggregation.MINUTE, PriceType.MID) bar_type = BarType(instrument_id, bar_spec) aggregator = TimeBarAggregator( AUDUSD_SIM, bar_type, handler, True, TestClock(), Logger(clock), ) 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), ts_event_ns=0, ts_recv_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), ts_event_ns=0, ts_recv_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), ts_event_ns=2 * 60 * 1_000_000_000, # 2 minutes in nanoseconds ts_recv_ns=2 * 60 * 1_000_000_000, # 2 minutes in nanoseconds ) # 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 Price.from_str("1.000025") == bar_store.get_store()[0].open assert Price.from_str("1.000035") == bar_store.get_store()[0].high assert Price.from_str("1.000025") == bar_store.get_store()[0].low assert Price.from_str("1.000035") == bar_store.get_store()[0].close assert Quantity.from_int(2) == bar_store.get_store()[0].volume assert 60_000_000_000 == bar_store.get_store()[0].ts_recv_ns
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() instrument = TestInstrumentProvider.default_fx_ccy("USD/JPY") self.wrangler = QuoteTickDataWrangler( instrument=instrument, 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 instrument_id = TestStubs.usdjpy_id() bar_spec = BarSpecification(3, BarAggregation.TICK, PriceType.MID) bar_type = BarType(instrument_id, bar_spec) clock = TestClock() logger = Logger(clock) ticks = self.wrangler.build_ticks() builder = BulkTickBarBuilder(instrument, bar_type, logger, handler) # Act builder.receive(ticks) # Assert self.assertEqual(333, len(bar_store.get_store()[0]))
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()), ) # Setup data wrangler = QuoteTickDataWrangler(instrument) provider = TestDataProvider() ticks = wrangler.process( data=provider.read_csv_ticks("truefx-audusd-ticks.csv")[:10000], default_volume=1, ) # 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.670635") assert last_bar.high == Price.from_str("0.670705") assert last_bar.low == Price.from_str("0.670370") assert last_bar.close == Price.from_str("0.670655") assert last_bar.volume == Quantity.from_int(1000)
def setUp(self): # Fixture Setup self.clock = LiveClock() self.uuid_factory = UUIDFactory() self.logger = Logger(self.clock, level_stdout=LogLevel.DEBUG) self.cache = TestStubs.cache() self.portfolio = Portfolio( cache=self.cache, clock=self.clock, logger=self.logger, ) # Fresh isolated loop testing pattern self.loop = asyncio.new_event_loop() asyncio.set_event_loop(self.loop) self.engine = LiveDataEngine( loop=self.loop, portfolio=self.portfolio, cache=self.cache, clock=self.clock, logger=self.logger, ) self.client = LiveMarketDataClient( client_id=ClientId(BINANCE.value), engine=self.engine, clock=self.clock, logger=self.logger, )
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, trade_id="123456", ts_event=0, ts_init=0, ) # Act aggregator.handle_trade_tick(tick1) # Assert assert len(bar_store.get_store()) == 0
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=0, ts_init=0, ) # Act aggregator.handle_quote_tick(tick1) # Assert assert len(bar_store.get_store()) == 0
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, Logger(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] self.assertEqual(67, len(bar_store.get_store())) # self.assertEqual(Price("0.66921"), last_bar.open) # TODO: WIP - Check subtle differences # 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)
def setup(self): # Fixture Setup self.clock = TestClock() self.uuid_factory = UUIDFactory() self.logger = Logger( clock=self.clock, level_stdout=LogLevel.DEBUG, ) self.trader_id = TestStubs.trader_id() self.account_id = TestStubs.account_id() self.venue = Venue("SIM") self.msgbus = MessageBus( trader_id=self.trader_id, clock=self.clock, logger=self.logger, ) self.cache = TestStubs.cache() self.portfolio = Portfolio( msgbus=self.msgbus, cache=self.cache, clock=self.clock, logger=self.logger, ) self.exec_engine = ExecutionEngine( msgbus=self.msgbus, cache=self.cache, clock=self.clock, logger=self.logger, ) self.risk_engine = RiskEngine( portfolio=self.portfolio, msgbus=self.msgbus, cache=self.cache, clock=self.clock, logger=self.logger, ) self.exec_client = MockExecutionClient( client_id=ClientId(self.venue.value), venue_type=VenueType.ECN, account_id=self.account_id, account_type=AccountType.MARGIN, base_currency=USD, msgbus=self.msgbus, cache=self.cache, clock=self.clock, logger=self.logger, ) self.portfolio.update_account(TestStubs.event_margin_account_state()) self.exec_engine.register_client(self.exec_client) # Prepare data self.cache.add_instrument(AUDUSD_SIM)
def test_handle_trade_tick_when_volume_at_threshold_sends_bar_to_handler( self): # Arrange bar_store = ObjectStorer() handler = bar_store.store instrument_id = TestStubs.audusd_id() bar_spec = BarSpecification(10000, BarAggregation.VOLUME, PriceType.LAST) bar_type = BarType(instrument_id, bar_spec) aggregator = VolumeBarAggregator(bar_type, handler, Logger(TestClock())) tick1 = TradeTick( instrument_id=AUDUSD_SIM.id, price=Price.from_str("1.00001"), size=Quantity.from_int(3000), 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(4000), 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(3000), 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(10000), bar_store.get_store()[0].volume)
def test_handle_trade_tick_when_volume_at_threshold_sends_bar_to_handler( self): # Arrange bar_store = ObjectStorer() handler = bar_store.store instrument = AUDUSD_SIM bar_spec = BarSpecification(10000, BarAggregation.VOLUME, PriceType.LAST) bar_type = BarType(instrument.id, bar_spec) aggregator = VolumeBarAggregator( instrument, bar_type, handler, Logger(TestClock()), ) tick1 = TradeTick( instrument_id=instrument.id, price=Price.from_str("1.00001"), size=Quantity.from_int(3000), 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(4000), 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(3000), 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(10000)