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_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_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) 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_build_with_previous_close(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.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 assert bar2.open == Price.from_str("1.00001") assert bar2.high == Price.from_str("1.00003") assert bar2.low == Price.from_str("1.00000") assert bar2.close == Price.from_str("1.00002") assert bar2.volume == Quantity.from_str("3.0")
def test_build_with_previous_close(self): # Arrange bar_type = TestStubs.bartype_btcusdt_binance_100tick_last() builder = BarBuilder(bar_type, use_previous_close=True) builder.update(Price("1.00001"), Quantity("1.0"), 0) builder.build_now() # This close should become the next open builder.update(Price("1.00000"), Quantity("1.0"), 0) builder.update(Price("1.00003"), Quantity("1.0"), 0) builder.update(Price("1.00002"), Quantity("1.0"), 0) bar2 = builder.build_now() # 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)