def test_handle_data_when_running_sends_to_on_data(self): # Arrange actor = MockActor() actor.register_base( trader_id=self.trader_id, msgbus=self.msgbus, cache=self.cache, clock=self.clock, logger=self.logger, ) actor.start() data = NewsEvent( impact=NewsImpact.HIGH, name="Unemployment Rate", currency=USD, ts_event=0, ts_init=0, ) # Act actor.handle_data(data) # Assert assert actor.calls == ["on_start", "on_data"] assert actor.object_storer.get_store()[0] == data
def test_resume(self): # Arrange actor = MockActor() actor.register_base( trader_id=self.trader_id, msgbus=self.msgbus, cache=self.cache, clock=self.clock, logger=self.logger, ) actor.start() actor.stop() # Act actor.resume() # Assert assert "on_resume" in actor.calls assert actor.state == ComponentState.RUNNING
def test_publish_data_sends_to_subscriber(self): # Arrange actor = MockActor() actor.register_base( trader_id=self.trader_id, msgbus=self.msgbus, cache=self.cache, clock=self.clock, logger=self.logger, ) handler = [] self.msgbus.subscribe( topic="data*", handler=handler.append, ) # Act data = Data( ts_event=self.clock.timestamp_ns(), ts_init=self.clock.timestamp_ns(), ) actor.publish_data(data_type=DataType(Data), data=data) # Assert assert data in handler
def test_unsubscribe_order_book_deltas(self): # Arrange actor = MockActor() actor.register_base( trader_id=self.trader_id, msgbus=self.msgbus, cache=self.cache, clock=self.clock, logger=self.logger, ) actor.unsubscribe_order_book_deltas(AUDUSD_SIM.id) # Act actor.unsubscribe_order_book_deltas(AUDUSD_SIM.id) # Assert assert self.data_engine.command_count == 2
def test_dispose(self): # Arrange actor = MockActor() actor.register_base( trader_id=self.trader_id, msgbus=self.msgbus, cache=self.cache, clock=self.clock, logger=self.logger, ) actor.reset() # Act actor.dispose() # Assert assert "on_dispose" in actor.calls assert actor.state == ComponentState.DISPOSED
def test_fault(self): # Arrange actor = MockActor() actor.register_base( trader_id=self.trader_id, msgbus=self.msgbus, cache=self.cache, clock=self.clock, logger=self.logger, ) actor.start() # Act actor.fault() # Assert assert "on_fault" in actor.calls assert actor.state == ComponentState.FAULTED
def test_handle_instrument_when_running_sends_to_on_instrument(self): # Arrange actor = MockActor() actor.register_base( trader_id=self.trader_id, msgbus=self.msgbus, cache=self.cache, clock=self.clock, logger=self.logger, ) actor.start() # Act actor.handle_instrument(AUDUSD_SIM) # Assert assert actor.calls == ["on_start", "on_instrument"] assert actor.object_storer.get_store()[0] == AUDUSD_SIM
def test_subscribe_venue_status_updates(self): # Arrange actor = MockActor() actor.register_base( trader_id=self.trader_id, msgbus=self.msgbus, cache=self.cache, clock=self.clock, logger=self.logger, ) actor.subscribe_venue_status_updates(Venue("NYMEX"))
def test_unsubscribe_custom_data_with_client_id(self): # Arrange actor = MockActor() actor.register_base( trader_id=self.trader_id, msgbus=self.msgbus, cache=self.cache, clock=self.clock, logger=self.logger, ) data_type = DataType(str, {"type": "NEWS_WIRE", "topic": "Earthquake"}) actor.subscribe_data(data_type, ClientId("QUANDL")) # Act actor.unsubscribe_data(data_type, ClientId("QUANDL")) # Assert assert self.data_engine.command_count == 2 assert actor.msgbus.subscriptions() == []
def test_unsubscribe_bars(self): # Arrange actor = MockActor() actor.register_base( trader_id=self.trader_id, msgbus=self.msgbus, cache=self.cache, clock=self.clock, logger=self.logger, ) bar_type = TestStubs.bartype_audusd_1min_bid() actor.subscribe_bars(bar_type) # Act actor.unsubscribe_bars(bar_type) # Assert assert self.data_engine.subscribed_bars() == [] assert self.data_engine.command_count == 2
def test_handle_trade_tick_when_running_sends_to_on_trade_tick(self): # Arrange actor = MockActor() actor.register_base( trader_id=self.trader_id, msgbus=self.msgbus, cache=self.cache, clock=self.clock, logger=self.logger, ) actor.start() tick = TestStubs.trade_tick_5decimal(AUDUSD_SIM.id) # Act actor.handle_trade_tick(tick) # Assert assert actor.calls == ["on_start", "on_trade_tick"] assert actor.object_storer.get_store()[0] == tick
def test_handle_bar_when_running_sends_to_on_bar(self): # Arrange actor = MockActor() actor.register_base( trader_id=self.trader_id, msgbus=self.msgbus, cache=self.cache, clock=self.clock, logger=self.logger, ) actor.start() bar = TestStubs.bar_5decimal() # Act actor.handle_bar(bar) # Assert assert actor.calls == ["on_start", "on_bar"] assert actor.object_storer.get_store()[0] == bar
def test_request_trade_ticks_sends_request_to_data_engine(self): # Arrange actor = MockActor() actor.register_base( trader_id=self.trader_id, msgbus=self.msgbus, cache=self.cache, clock=self.clock, logger=self.logger, ) # Act actor.request_trade_ticks(AUDUSD_SIM.id) # Assert assert self.data_engine.request_count == 1
def test_subscribe_order_book(self): # Arrange actor = MockActor() actor.register_base( trader_id=self.trader_id, msgbus=self.msgbus, cache=self.cache, clock=self.clock, logger=self.logger, ) # Act actor.subscribe_order_book_snapshots(AUDUSD_SIM.id, book_type=BookType.L2_MBP) # Assert assert self.data_engine.command_count == 1
def test_reset(self): # Arrange actor = MockActor() actor.register_base( trader_id=self.trader_id, msgbus=self.msgbus, cache=self.cache, clock=self.clock, logger=self.logger, ) # Act actor.reset() # Assert assert "on_reset" in actor.calls assert actor.state == ComponentState.INITIALIZED
def test_unsubscribe_instruments(self): # Arrange actor = MockActor() actor.register_base( trader_id=self.trader_id, msgbus=self.msgbus, cache=self.cache, clock=self.clock, logger=self.logger, ) # Act actor.unsubscribe_instruments(Venue("SIM")) # Assert assert self.data_engine.command_count == 1 assert self.data_engine.subscribed_instruments() == []
def test_request_bars_with_invalid_params_raises_value_error( self, start, stop): # Arrange actor = MockActor() actor.register_base( trader_id=self.trader_id, msgbus=self.msgbus, cache=self.cache, clock=self.clock, logger=self.logger, ) bar_type = TestStubs.bartype_audusd_1min_bid() # Act, Assert with pytest.raises(ValueError): actor.request_bars(bar_type, start, stop)
def test_request_bars_sends_request_to_data_engine(self): # Arrange actor = MockActor() actor.register_base( trader_id=self.trader_id, msgbus=self.msgbus, cache=self.cache, clock=self.clock, logger=self.logger, ) bar_type = TestStubs.bartype_audusd_1min_bid() # Act actor.request_bars(bar_type) # Assert assert self.data_engine.request_count == 1
def test_subscribe_ticker(self): # Arrange actor = MockActor() actor.register_base( trader_id=self.trader_id, msgbus=self.msgbus, cache=self.cache, clock=self.clock, logger=self.logger, ) # Act actor.subscribe_ticker(AUDUSD_SIM.id) # Assert expected_instrument = InstrumentId(Symbol("AUD/USD"), Venue("SIM")) assert self.data_engine.subscribed_tickers() == [expected_instrument] assert self.data_engine.command_count == 1
def test_handle_quote_tick_when_not_running_does_not_send_to_on_quote_tick( self): # Arrange actor = MockActor() actor.register_base( trader_id=self.trader_id, msgbus=self.msgbus, cache=self.cache, clock=self.clock, logger=self.logger, ) ticker = TestStubs.ticker() # Act actor.handle_ticker(ticker) # Assert assert actor.calls == [] assert actor.object_storer.get_store() == []
def test_subscribe_custom_data(self): # Arrange actor = MockActor() actor.register_base( trader_id=self.trader_id, msgbus=self.msgbus, cache=self.cache, clock=self.clock, logger=self.logger, ) data_type = DataType(str, {"type": "NEWS_WIRE", "topic": "Earthquake"}) # Act actor.subscribe_data(data_type) # Assert assert self.data_engine.command_count == 0 assert actor.msgbus.subscriptions( )[0].topic == "data.str.type=NEWS_WIRE.topic=Earthquake"
def test_subscribe_instruments(self): # Arrange actor = MockActor() actor.register_base( trader_id=self.trader_id, msgbus=self.msgbus, cache=self.cache, clock=self.clock, logger=self.logger, ) # Act actor.subscribe_instruments(Venue("SIM")) # Assert assert self.data_engine.command_count == 1 assert self.data_engine.subscribed_instruments() == [ InstrumentId.from_str("AUD/USD.SIM"), InstrumentId.from_str("GBP/USD.SIM"), InstrumentId.from_str("USD/JPY.SIM"), ]
def test_request_data_sends_request_to_data_engine(self): # Arrange actor = MockActor() actor.register_base( trader_id=self.trader_id, msgbus=self.msgbus, cache=self.cache, clock=self.clock, logger=self.logger, ) data_type = DataType(str, { "type": "NEWS_WIRE", "topic": "Earthquakes" }) # Act actor.request_data(ClientId("BLOOMBERG-01"), data_type) # Assert assert self.data_engine.request_count == 1