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
Ejemplo n.º 2
0
    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)
Ejemplo n.º 3
0
    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(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_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_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)
Ejemplo n.º 7
0
    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_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
Ejemplo n.º 9
0
    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)
Ejemplo n.º 10
0
    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)
Ejemplo n.º 12
0
    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)

        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)