def test_to_dict(self): # Arrange bar = Bar( AUDUSD_1_MIN_BID, Price.from_str("1.00001"), Price.from_str("1.00004"), Price.from_str("1.00002"), Price.from_str("1.00003"), Quantity.from_int(100000), 0, 0, ) # Act values = Bar.to_dict(bar) # Assert assert values == { "type": "Bar", "bar_type": "AUD/USD.SIM-1-MINUTE-BID", "open": "1.00001", "high": "1.00004", "low": "1.00002", "close": "1.00003", "volume": "100000", "ts_event_ns": 0, "ts_recv_ns": 0, }
def test_bar_with_two_bars_returns_expected_bar(self): # Arrange bar_type = TestStubs.bartype_gbpusd_1sec_mid() bar1 = Bar( Price("1.00001"), Price("1.00004"), Price("1.00002"), Price("1.00003"), Quantity(100000), UNIX_EPOCH, ) bar2 = Bar( Price("1.00002"), Price("1.00003"), Price("1.00004"), Price("1.00005"), Quantity(200000), UNIX_EPOCH, ) self.cache.add_bar(bar_type, bar1) self.cache.add_bar(bar_type, bar2) # Act result = self.cache.bar(bar_type, index=0) # Assert self.assertEqual(2, self.cache.bar_count(bar_type)) self.assertEqual(bar2, result)
def test_equality(self): # Arrange bar1 = Bar( AUDUSD_1_MIN_BID, Price.from_str("1.00001"), Price.from_str("1.00004"), Price.from_str("1.00002"), Price.from_str("1.00003"), Quantity.from_int(100000), 0, 0, ) bar2 = Bar( AUDUSD_1_MIN_BID, Price.from_str("1.00000"), Price.from_str("1.00004"), Price.from_str("1.00002"), Price.from_str("1.00003"), Quantity.from_int(100000), 0, 0, ) # Act # Assert assert bar1 == bar1 assert bar1 != bar2
def test_from_dict_returns_expected_bar(self): # Arrange bar = TestStubs.bar_5decimal() # Act result = Bar.from_dict(Bar.to_dict(bar)) # Assert assert result == bar
def test_from_serializable_string_given_malformed_string_raises_value_error(self): # Arrange bar = Bar( AUDUSD_1_MIN_BID, Price("1.00001"), Price("1.00004"), Price("1.00002"), Price("1.00003"), Quantity(100000), 0, ) # Act # Assert with pytest.raises(ValueError): bar.from_serializable_str(AUDUSD_1_MIN_BID, "NOT_BAR_VALUES!")
def test_process_bar_when_subscriber_then_sends_to_registered_handler(self): # Arrange self.data_engine.register_client(self.binance_client) self.binance_client.connect() bar_spec = BarSpecification(1000, BarAggregation.TICK, PriceType.MID) bar_type = BarType(ETHUSDT_BINANCE.id, bar_spec, internal_aggregation=True) handler = ObjectStorer() subscribe = Subscribe( provider=BINANCE.value, data_type=DataType(Bar, metadata={"BarType": bar_type}), handler=handler.store_2, command_id=self.uuid_factory.generate(), command_timestamp=self.clock.utc_now(), ) self.data_engine.execute(subscribe) bar = Bar( Price("1051.00000"), Price("1055.00000"), Price("1050.00000"), Price("1052.00000"), Quantity(100), UNIX_EPOCH, ) data = BarData(bar_type, bar) # Act self.data_engine.process(data) # Assert self.assertEqual([(bar_type, bar)], handler.get_store())
def test_reset_strategy(self): # Arrange bar_type = TestStubs.bartype_audusd_1min_bid() strategy = TestStrategy1(bar_type) strategy.register_trader( TraderId("TESTER", "000"), clock=self.clock, uuid_factory=self.uuid_factory, logger=self.logger, ) bar_type = TestStubs.bartype_gbpusd_1sec_mid() bar = Bar( Price("1.00001"), Price("1.00004"), Price("1.00002"), Price("1.00003"), Quantity(100000), datetime(1970, 1, 1, 00, 00, 0, 0, pytz.utc), ) strategy.handle_bar(bar_type, bar) # Act strategy.reset() # Assert self.assertEqual(ComponentState.INITIALIZED, strategy.state()) self.assertEqual(0, strategy.ema1.count) self.assertEqual(0, strategy.ema2.count) self.assertTrue("custom reset logic" in strategy.object_storer.get_store())
def test_update_indicators(self): # Arrange bar_type = TestStubs.bartype_gbpusd_1sec_mid() strategy = TestStrategy1(bar_type) strategy.register_trader( TraderId("TESTER", "000"), clock=self.clock, uuid_factory=self.uuid_factory, logger=self.logger, ) bar = Bar( Price("1.00001"), Price("1.00004"), Price("1.00002"), Price("1.00003"), Quantity(100000), datetime(1970, 1, 1, 00, 00, 0, 0, pytz.utc), ) # Act strategy.handle_bar(bar_type, bar) # Assert self.assertEqual(1, strategy.ema1.count) self.assertEqual(1, strategy.ema2.count)
def test_set_partial_updates_bar_to_expected_properties(self): # Arrange bar_type = TestStubs.bartype_btcusdt_binance_100tick_last() builder = BarBuilder(BTCUSDT_BINANCE, bar_type, use_previous_close=True) partial_bar = Bar( bar_type=bar_type, open_price=Price.from_str("1.00001"), high_price=Price.from_str("1.00010"), low_price=Price.from_str("1.00000"), close_price=Price.from_str("1.00002"), volume=Quantity.from_str("1"), ts_event_ns=1_000_000_000, ts_recv_ns=1_000_000_000, ) # Act builder.set_partial(partial_bar) bar = builder.build_now() # Assert self.assertEqual(Price.from_str("1.00001"), bar.open) self.assertEqual(Price.from_str("1.00010"), bar.high) self.assertEqual(Price.from_str("1.00000"), bar.low) self.assertEqual(Price.from_str("1.00002"), bar.close) self.assertEqual(Quantity.from_str("1"), bar.volume) self.assertEqual(1_000_000_000, bar.ts_recv_ns) self.assertEqual(1_000_000_000, builder.last_timestamp_ns)
def test_process_bar_when_subscriber_then_sends_to_registered_handler(self): # Arrange self.data_engine.register_client(self.binance_client) self.binance_client.connect() bar_spec = BarSpecification(1000, BarAggregation.TICK, PriceType.MID) bar_type = BarType(ETHUSDT_BINANCE.id, bar_spec, internal_aggregation=True) handler = ObjectStorer() subscribe = Subscribe( client_id=ClientId(BINANCE.value), data_type=DataType(Bar, metadata={"BarType": bar_type}), handler=handler.store, command_id=self.uuid_factory.generate(), timestamp_ns=self.clock.timestamp_ns(), ) self.data_engine.execute(subscribe) bar = Bar( bar_type, Price.from_str("1051.00000"), Price.from_str("1055.00000"), Price.from_str("1050.00000"), Price.from_str("1052.00000"), Quantity.from_int(100), 0, 0, ) # Act self.data_engine.process(bar) # Assert self.assertEqual([bar], handler.get_store())
def test_to_serializable_string(self): # Arrange bar = Bar( Price("1.00001"), Price("1.00004"), Price("1.00002"), Price("1.00003"), Quantity(100000), UNIX_EPOCH, ) # Act serializable = bar.to_serializable_str() # Assert assert "1.00001,1.00004,1.00002,1.00003,100000,0" == serializable
def test_from_serializable_string_given_malformed_string_raises_value_error( self): # Arrange bar = Bar( Price("1.00001"), Price("1.00004"), Price("1.00002"), Price("1.00003"), Quantity(100000), UNIX_EPOCH, ) # Act # Assert with pytest.raises(ValueError): bar.from_serializable_str("NOT_A_BAR")
def test_set_partial_updates_bar_to_expected_properties(self): # Arrange bar_spec = TestStubs.bar_spec_1min_mid() builder = BarBuilder(bar_spec, use_previous_close=True) partial_bar = Bar( open_price=Price("1.00001"), high_price=Price("1.00010"), low_price=Price("1.00000"), close_price=Price("1.00002"), volume=Quantity("1"), timestamp=UNIX_EPOCH + timedelta(seconds=1), ) # Act builder.set_partial(partial_bar) bar = builder.build() # Assert self.assertEqual(Price("1.00001"), bar.open) self.assertEqual(Price("1.00010"), bar.high) self.assertEqual(Price("1.00000"), bar.low) self.assertEqual(Price("1.00002"), bar.close) self.assertEqual(Quantity("1"), bar.volume) self.assertEqual(UNIX_EPOCH + timedelta(seconds=1), bar.timestamp) self.assertEqual(UNIX_EPOCH + timedelta(seconds=1), builder.last_timestamp)
def test_to_serializable_string(self): # Arrange bar = Bar( AUDUSD_1_MIN_BID, Price("1.00001"), Price("1.00004"), Price("1.00002"), Price("1.00003"), Quantity(100000), 0, ) # Act serializable = bar.to_serializable_str() # Assert assert serializable == "1.00001,1.00004,1.00002,1.00003,100000,0"
def bar_3decimal() -> Bar: return Bar( Price("90.002"), Price("90.004"), Price("90.001"), Price("90.003"), Quantity(100000), UNIX_EPOCH, )
def test_from_serializable_string_given_valid_string_returns_expected_bar(self): # Arrange bar = TestStubs.bar_5decimal() # Act result = Bar.from_serializable_str(bar.type, bar.to_serializable_str()) # Assert assert result == bar
def test_parse_bar_from_string(self): # Arrange bar = TestStubs.bar_5decimal() # Act result = Bar.from_serializable_string(bar.to_serializable_string()) # Assert self.assertEqual(bar, result)
def build_bar_with_checking(): Bar( Price("1.00001"), Price("1.00004"), Price("1.00002"), Price("1.00003"), Quantity("100000"), UNIX_EPOCH, check=True, )
def build_bar_no_checking(): Bar( Price("1.00001"), Price("1.00004"), Price("1.00002"), Price("1.00003"), Quantity("100000"), UNIX_EPOCH, check=False, )
def bar_3decimal() -> Bar: return Bar( TestStubs.bartype_usdjpy_1min_bid(), Price("90.002"), Price("90.004"), Price("90.001"), Price("90.003"), Quantity(100000), 0, )
def bar_5decimal() -> Bar: return Bar( TestStubs.bartype_audusd_1min_bid(), Price("1.00002"), Price("1.00004"), Price("1.00001"), Price("1.00003"), Quantity(100000), 0, )
def build_bar_with_checking(): Bar( AUDUSD_1MIN_BID, Price("1.00001"), Price("1.00004"), Price("1.00002"), Price("1.00003"), Quantity("100000"), 0, check=True, )
def bar_5decimal() -> Bar: return Bar( bar_type=TestStubs.bartype_audusd_1min_bid(), open_price=Price.from_str("1.00002"), high_price=Price.from_str("1.00004"), low_price=Price.from_str("1.00001"), close_price=Price.from_str("1.00003"), volume=Quantity.from_int(1_000_000), timestamp_origin_ns=0, timestamp_ns=0, )
def bar_3decimal() -> Bar: return Bar( bar_type=TestStubs.bartype_usdjpy_1min_bid(), open_price=Price.from_str("90.002"), high_price=Price.from_str("90.004"), low_price=Price.from_str("90.001"), close_price=Price.from_str("90.003"), volume=Quantity.from_int(1_000_000), ts_event_ns=0, ts_recv_ns=0, )
def test_set_partial_when_already_set_does_not_update(self): # Arrange bar_type = TestStubs.bartype_btcusdt_binance_100tick_last() builder = BarBuilder(bar_type, use_previous_close=True) partial_bar1 = Bar( bar_type=bar_type, open_price=Price.from_str("1.00001"), high_price=Price.from_str("1.00010"), low_price=Price.from_str("1.00000"), close_price=Price.from_str("1.00002"), volume=Quantity.from_str("1"), timestamp_origin_ns=1_000_000_000, timestamp_ns=1_000_000_000, ) partial_bar2 = Bar( bar_type=bar_type, open_price=Price.from_str("2.00001"), high_price=Price.from_str("2.00010"), low_price=Price.from_str("2.00000"), close_price=Price.from_str("2.00002"), volume=Quantity.from_str("2"), timestamp_origin_ns=1_000_000_000, timestamp_ns=3_000_000_000, ) # Act builder.set_partial(partial_bar1) builder.set_partial(partial_bar2) bar = builder.build(4_000_000_000) # Assert self.assertEqual(Price.from_str("1.00001"), bar.open) self.assertEqual(Price.from_str("1.00010"), bar.high) self.assertEqual(Price.from_str("1.00000"), bar.low) self.assertEqual(Price.from_str("1.00002"), bar.close) self.assertEqual(Quantity.from_str("1"), bar.volume) self.assertEqual(4_000_000_000, bar.timestamp_ns) self.assertEqual(1_000_000_000, builder.last_timestamp_ns)
def test_check_when_high_below_close_raises_value_error(self): # Arrange # Act # Assert with pytest.raises(ValueError): Bar( Price("1.00000"), Price("1.00000"), # High below close Price("1.00000"), Price("1.00005"), Quantity(100000), UNIX_EPOCH, True, )
def test_check_when_low_above_close_raises_value_error(self): # Arrange # Act # Assert with pytest.raises(ValueError): Bar( Price("1.00000"), Price("1.00005"), Price("1.00000"), Price("0.99999"), # Close below low Quantity(100000), UNIX_EPOCH, True, )
def test_equality(self): # Arrange bar1 = Bar( Price("1.00001"), Price("1.00004"), Price("1.00002"), Price("1.00003"), Quantity(100000), UNIX_EPOCH, ) bar2 = Bar( Price("1.00000"), Price("1.00004"), Price("1.00002"), Price("1.00003"), Quantity(100000), UNIX_EPOCH, ) # Act # Assert assert bar1 == bar1 assert bar1 != bar2
def test_check_when_high_below_low_raises_value_error(self): # Arrange # Act # Assert with pytest.raises(ValueError): Bar( AUDUSD_1_MIN_BID, Price("1.00001"), Price("1.00000"), # High below low Price("1.00002"), Price("1.00003"), Quantity(100000), 0, True, )
def test_equality(self): # Arrange bar1 = Bar( Price("1.00001"), Price("1.00004"), Price("1.00002"), Price("1.00003"), Quantity(100000), UNIX_EPOCH, ) bar2 = Bar( Price("1.00000"), Price("1.00004"), Price("1.00002"), Price("1.00003"), Quantity(100000), UNIX_EPOCH, ) # Act # Assert self.assertEqual(bar1, bar1) self.assertNotEqual(bar1, bar2)