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) partial_bar = Bar( bar_type=bar_type, open=Price.from_str("1.00001"), high=Price.from_str("1.00010"), low=Price.from_str("1.00000"), close=Price.from_str("1.00002"), volume=Quantity.from_str("1"), ts_event=1_000_000_000, ts_init=1_000_000_000, ) # Act builder.set_partial(partial_bar) bar = builder.build_now() # Assert assert bar.open == Price.from_str("1.00001") assert bar.high == Price.from_str("1.00010") assert bar.low == Price.from_str("1.00000") assert bar.close == Price.from_str("1.00002") assert bar.volume == Quantity.from_str("1") assert bar.ts_init == 1_000_000_000 assert builder.ts_last == 1_000_000_000
def test_build_when_received_updates_returns_expected_bar(self): # Arrange bar_type = TestStubs.bartype_btcusdt_binance_100tick_last() builder = BarBuilder(BTCUSDT_BINANCE, bar_type, use_previous_close=True) builder.update(Price.from_str("1.00001"), Quantity.from_str("1.0"), 0) builder.update(Price.from_str("1.00002"), Quantity.from_str("1.5"), 0) builder.update( Price.from_str("1.00000"), Quantity.from_str("1.5"), 1_000_000_000, ) # Act bar = builder.build_now() # Also resets builder # Assert self.assertEqual(Price.from_str("1.00001"), bar.open) self.assertEqual(Price.from_str("1.00002"), bar.high) self.assertEqual(Price.from_str("1.00000"), bar.low) self.assertEqual(Price.from_str("1.00000"), bar.close) self.assertEqual(Quantity.from_str("4.0"), bar.volume) self.assertEqual(1_000_000_000, bar.ts_recv_ns) self.assertEqual(1_000_000_000, builder.last_timestamp_ns) self.assertEqual(0, builder.count)
def test_build_when_received_updates_returns_expected_bar(self): # Arrange bar_type = TestStubs.bartype_btcusdt_binance_100tick_last() builder = BarBuilder(BTCUSDT_BINANCE, bar_type) builder.update(Price.from_str("1.00001"), Quantity.from_str("1.0"), 0) builder.update(Price.from_str("1.00002"), Quantity.from_str("1.5"), 0) builder.update( Price.from_str("1.00000"), Quantity.from_str("1.5"), 1_000_000_000, ) # Act bar = builder.build_now() # Also resets builder # Assert assert bar.open == Price.from_str("1.00001") assert bar.high == Price.from_str("1.00002") assert bar.low == Price.from_str("1.00000") assert bar.close == Price.from_str("1.00000") assert bar.volume == Quantity.from_str("4.0") assert bar.ts_init == 1_000_000_000 assert builder.ts_last == 1_000_000_000 assert builder.count == 0
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_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_build_when_no_updates_raises_exception(self): # Arrange bar_type = TestStubs.bartype_audusd_1min_bid() builder = BarBuilder(AUDUSD_SIM, bar_type) # Act, Assert with pytest.raises(TypeError): builder.build()
def test_single_update_results_in_expected_properties(self): # Arrange bar_spec = TestStubs.bar_spec_1min_mid() builder = BarBuilder(bar_spec, use_previous_close=True) # Act builder.update(Price("1.00000"), Quantity("1"), UNIX_EPOCH) # Assert self.assertTrue(builder.initialized) self.assertEqual(UNIX_EPOCH, builder.last_timestamp) self.assertEqual(1, builder.count)
def test_single_update_results_in_expected_properties(self): # Arrange bar_type = TestStubs.bartype_btcusdt_binance_100tick_last() builder = BarBuilder(BTCUSDT_BINANCE, bar_type) # Act builder.update(Price.from_str("1.00000"), Quantity.from_str("1"), 0) # Assert assert builder.initialized assert builder.ts_last == 0 assert builder.count == 1
def test_single_update_results_in_expected_properties(self): # Arrange bar_type = TestStubs.bartype_btcusdt_binance_100tick_last() builder = BarBuilder(bar_type, use_previous_close=True) # Act builder.update(Price("1.00000"), Quantity("1"), 0) # Assert self.assertTrue(builder.initialized) self.assertEqual(0, builder.last_timestamp_ns) self.assertEqual(1, builder.count)
def test_single_update_when_timestamp_less_than_last_update_ignores(self): # Arrange bar_type = TestStubs.bartype_btcusdt_binance_100tick_last() builder = BarBuilder(bar_type, use_previous_close=True) builder.update(Price("1.00000"), Quantity("1"), 0) # Act builder.update(Price("1.00001"), Quantity("1"), -1_000_000_000) # Assert self.assertTrue(builder.initialized) self.assertEqual(0, builder.last_timestamp_ns) self.assertEqual(1, builder.count)
def test_single_update_when_timestamp_less_than_last_update_ignores(self): # Arrange bar_type = TestStubs.bartype_btcusdt_binance_100tick_last() builder = BarBuilder(BTCUSDT_BINANCE, bar_type) builder.update(Price.from_str("1.00000"), Quantity.from_str("1"), 1_000) # Act builder.update(Price.from_str("1.00001"), Quantity.from_str("1"), 500) # Assert assert builder.initialized assert builder.ts_last == 1_000 assert builder.count == 1
def test_build_when_no_updates_raises_exception(self): # Arrange bar_type = TestStubs.bartype_audusd_1min_bid() builder = BarBuilder(AUDUSD_SIM, bar_type, use_previous_close=False) # Act # Assert self.assertRaises(TypeError, builder.build)
def test_build_when_no_updates_raises_exception(self): # Arrange bar_spec = TestStubs.bar_spec_1min_mid() builder = BarBuilder(bar_spec, use_previous_close=False) # Act # Assert self.assertRaises(TypeError, builder.build)
def test_str_repr(self): # Arrange bar_spec = TestStubs.bar_spec_1min_mid() builder = BarBuilder(bar_spec, use_previous_close=False) # Act # Assert self.assertEqual("BarBuilder(bar_spec=1-MINUTE-MID,None,None,None,None,0)", str(builder)) self.assertEqual("BarBuilder(bar_spec=1-MINUTE-MID,None,None,None,None,0)", repr(builder))
def test_instantiate(self): # Arrange bar_type = TestStubs.bartype_btcusdt_binance_100tick_last() builder = BarBuilder(BTCUSDT_BINANCE, bar_type) # Act, Assert assert not builder.initialized assert builder.ts_last == 0 assert builder.count == 0
def test_instantiate(self): # Arrange bar_type = TestStubs.bartype_btcusdt_binance_100tick_last() builder = BarBuilder(bar_type, use_previous_close=False) # Act # Assert self.assertFalse(builder.use_previous_close) self.assertFalse(builder.initialized) self.assertEqual(0, builder.last_timestamp_ns) self.assertEqual(0, builder.count)
def test_set_partial_when_already_set_does_not_update(self): # Arrange bar_type = TestStubs.bartype_btcusdt_binance_100tick_last() builder = BarBuilder(BTCUSDT_BINANCE, bar_type) partial_bar1 = Bar( bar_type=bar_type, open=Price.from_str("1.00001"), high=Price.from_str("1.00010"), low=Price.from_str("1.00000"), close=Price.from_str("1.00002"), volume=Quantity.from_str("1"), ts_event=1_000_000_000, ts_init=1_000_000_000, ) partial_bar2 = Bar( bar_type=bar_type, open=Price.from_str("2.00001"), high=Price.from_str("2.00010"), low=Price.from_str("2.00000"), close=Price.from_str("2.00002"), volume=Quantity.from_str("2"), ts_event=1_000_000_000, ts_init=3_000_000_000, ) # Act builder.set_partial(partial_bar1) builder.set_partial(partial_bar2) bar = builder.build(4_000_000_000) # Assert assert bar.open == Price.from_str("1.00001") assert bar.high == Price.from_str("1.00010") assert bar.low == Price.from_str("1.00000") assert bar.close == Price.from_str("1.00002") assert bar.volume == Quantity.from_str("1") assert bar.ts_init == 4_000_000_000 assert builder.ts_last == 1_000_000_000
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_instantiate(self): # Arrange bar_spec = TestStubs.bar_spec_1min_mid() builder = BarBuilder(bar_spec, use_previous_close=False) # Act # Assert self.assertEqual(bar_spec, builder.bar_spec) self.assertFalse(builder.use_previous_close) self.assertFalse(builder.initialized) self.assertIsNone(builder.last_timestamp) self.assertEqual(0, builder.count)
def test_multiple_updates_correctly_increments_count(self): # Arrange bar_spec = TestStubs.bar_spec_1min_mid() builder = BarBuilder(bar_spec, use_previous_close=True) # Act builder.update(Price("1.00000"), Quantity("1"), UNIX_EPOCH) builder.update(Price("1.00000"), Quantity("1"), UNIX_EPOCH) builder.update(Price("1.00000"), Quantity("1"), UNIX_EPOCH) builder.update(Price("1.00000"), Quantity("1"), UNIX_EPOCH) builder.update(Price("1.00000"), Quantity("1"), UNIX_EPOCH) # Assert self.assertEqual(5, builder.count)
def test_multiple_updates_correctly_increments_count(self): # Arrange bar_type = TestStubs.bartype_btcusdt_binance_100tick_last() builder = BarBuilder(bar_type, use_previous_close=True) # Act builder.update(Price("1.00000"), Quantity("1"), 0) builder.update(Price("1.00000"), Quantity("1"), 0) builder.update(Price("1.00000"), Quantity("1"), 0) builder.update(Price("1.00000"), Quantity("1"), 0) builder.update(Price("1.00000"), Quantity("1"), 0) # Assert self.assertEqual(5, builder.count)
def test_update(self): # Arrange bar_spec = TestStubs.bar_spec_1min_mid() builder = BarBuilder(bar_spec, use_previous_close=True) tick1 = QuoteTick(symbol=AUDUSD_FXCM, bid=Price("1.00001"), ask=Price("1.00004"), bid_size=Quantity(1), ask_size=Quantity(1), timestamp=UNIX_EPOCH) tick2 = QuoteTick(symbol=AUDUSD_FXCM, bid=Price("1.00002"), ask=Price("1.00005"), bid_size=Quantity(1), ask_size=Quantity(1), timestamp=UNIX_EPOCH) tick3 = QuoteTick(symbol=AUDUSD_FXCM, bid=Price("1.00000"), ask=Price("1.00003"), bid_size=Quantity(1), ask_size=Quantity(1), timestamp=UNIX_EPOCH) # Act builder.handle_quote_tick(tick1) builder.handle_quote_tick(tick2) builder.handle_quote_tick(tick3) # Assert self.assertEqual(bar_spec, builder.bar_spec) self.assertEqual(3, builder.count) self.assertEqual(UNIX_EPOCH, builder.last_update)
def test_build_with_previous_close(self): # Arrange bar_spec = TestStubs.bar_spec_1min_mid() builder = BarBuilder(bar_spec, use_previous_close=True) tick1 = QuoteTick(symbol=AUDUSD_FXCM, bid=Price("1.00001"), ask=Price("1.00004"), bid_size=Quantity(1), ask_size=Quantity(1), timestamp=UNIX_EPOCH) tick2 = QuoteTick(symbol=AUDUSD_FXCM, bid=Price("1.00002"), ask=Price("1.00005"), bid_size=Quantity(1), ask_size=Quantity(1), timestamp=UNIX_EPOCH) tick3 = QuoteTick(symbol=AUDUSD_FXCM, bid=Price("1.00000"), ask=Price("1.00003"), bid_size=Quantity(1), ask_size=Quantity(1), timestamp=UNIX_EPOCH) builder.handle_quote_tick(tick1) builder.handle_quote_tick(tick2) builder.handle_quote_tick(tick3) builder.build() # Act bar = builder.build() # Also resets builder # Assert self.assertEqual(Price("1.000015"), bar.open) self.assertEqual(Price("1.000015"), bar.high) self.assertEqual(Price("1.000015"), bar.low) self.assertEqual(Price("1.000015"), bar.close) self.assertEqual(Quantity(), bar.volume) self.assertEqual(UNIX_EPOCH, bar.timestamp) self.assertEqual(UNIX_EPOCH, builder.last_update) self.assertEqual(0, builder.count)
def test_str_repr(self): # Arrange bar_type = TestStubs.bartype_btcusdt_binance_100tick_last() builder = BarBuilder(BTCUSDT_BINANCE, bar_type) # Act, Assert assert ( str(builder) == "BarBuilder(BTC/USDT.BINANCE-100-TICK-LAST-EXTERNAL,None,None,None,None,0)" ) assert ( repr(builder) == "BarBuilder(BTC/USDT.BINANCE-100-TICK-LAST-EXTERNAL,None,None,None,None,0)" )
def test_multiple_updates_correctly_increments_count(self): # Arrange bar_type = TestStubs.bartype_btcusdt_binance_100tick_last() builder = BarBuilder(BTCUSDT_BINANCE, bar_type) # Act builder.update(Price.from_str("1.00000"), Quantity.from_int(1), 1_000) builder.update(Price.from_str("1.00000"), Quantity.from_int(1), 1_000) builder.update(Price.from_str("1.00000"), Quantity.from_int(1), 1_000) builder.update(Price.from_str("1.00000"), Quantity.from_int(1), 1_000) builder.update(Price.from_str("1.00000"), Quantity.from_int(1), 1_000) # Assert assert builder.count == 5
def test_str_repr(self): # Arrange bar_type = TestStubs.bartype_btcusdt_binance_100tick_last() builder = BarBuilder(bar_type, use_previous_close=False) # Act # Assert self.assertEqual( "BarBuilder(BTC/USDT.BINANCE-100-TICK-LAST,None,None,None,None,0)", str(builder), ) self.assertEqual( "BarBuilder(BTC/USDT.BINANCE-100-TICK-LAST,None,None,None,None,0)", repr(builder), )
def test_single_update_when_timestamp_less_than_last_update_ignores(self): # Arrange bar_spec = TestStubs.bar_spec_1min_mid() builder = BarBuilder(bar_spec, use_previous_close=True) builder.update(Price("1.00000"), Quantity("1"), UNIX_EPOCH) # Act builder.update(Price("1.00001"), Quantity("1"), UNIX_EPOCH - timedelta(seconds=1)) # Assert self.assertTrue(builder.initialized) self.assertEqual(UNIX_EPOCH, builder.last_timestamp) self.assertEqual(1, builder.count)
def test_build_when_received_updates_returns_expected_bar(self): # Arrange bar_spec = TestStubs.bar_spec_1min_bid() builder = BarBuilder(bar_spec, use_previous_close=True) builder.update(Price("1.00001"), Quantity("1.0"), UNIX_EPOCH) builder.update(Price("1.00002"), Quantity("1.5"), UNIX_EPOCH) builder.update(Price("1.00000"), Quantity("1.5"), UNIX_EPOCH + timedelta(seconds=1)) # Act bar = builder.build() # Also resets builder # Assert self.assertEqual(Price("1.00001"), bar.open) self.assertEqual(Price("1.00002"), bar.high) self.assertEqual(Price("1.00000"), bar.low) self.assertEqual(Price("1.00000"), bar.close) self.assertEqual(Quantity("4.0"), bar.volume) self.assertEqual(UNIX_EPOCH + timedelta(seconds=1), bar.timestamp) self.assertEqual(UNIX_EPOCH + timedelta(seconds=1), builder.last_timestamp) self.assertEqual(0, builder.count)
def test_build_with_previous_close(self): # Arrange bar_spec = TestStubs.bar_spec_1min_mid() builder = BarBuilder(bar_spec, use_previous_close=True) builder.update(Price("1.00001"), Quantity("1.0"), UNIX_EPOCH) builder.build() # This close should become the next open builder.update(Price("1.00000"), Quantity("1.0"), UNIX_EPOCH) builder.update(Price("1.00003"), Quantity("1.0"), UNIX_EPOCH) builder.update(Price("1.00002"), Quantity("1.0"), UNIX_EPOCH) bar2 = builder.build() # Assert self.assertEqual(Price("1.00001"), bar2.open) self.assertEqual(Price("1.00003"), bar2.high) self.assertEqual(Price("1.00000"), bar2.low) self.assertEqual(Price("1.00002"), bar2.close) self.assertEqual(Quantity("3.0"), bar2.volume)
def test_build_with_previous_close(self): # Arrange bar_type = TestStubs.bartype_btcusdt_binance_100tick_last() builder = BarBuilder(BTCUSDT_BINANCE, bar_type, use_previous_close=True) builder.update(Price.from_str("1.00001"), Quantity.from_str("1.0"), 0) builder.build_now() # This close should become the next open builder.update(Price.from_str("1.00000"), Quantity.from_str("1.0"), 0) builder.update(Price.from_str("1.00003"), Quantity.from_str("1.0"), 0) builder.update(Price.from_str("1.00002"), Quantity.from_str("1.0"), 0) bar2 = builder.build_now() # Assert self.assertEqual(Price.from_str("1.00001"), bar2.open) self.assertEqual(Price.from_str("1.00003"), bar2.high) self.assertEqual(Price.from_str("1.00000"), bar2.low) self.assertEqual(Price.from_str("1.00002"), bar2.close) self.assertEqual(Quantity.from_str("3.0"), bar2.volume)