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-EXTERNAL",
            "open": "1.00001",
            "high": "1.00004",
            "low": "1.00002",
            "close": "1.00003",
            "volume": "100000",
            "ts_event": 0,
            "ts_init": 0,
        }
    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 = TestDataStubs.bar_5decimal()

        # Act
        result = Bar.from_dict(Bar.to_dict(bar))

        # Assert
        assert result == bar
    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_reset(self):
        # Arrange
        bar_type = TestStubs.bartype_audusd_1min_bid()
        strategy = MockStrategy(bar_type)
        strategy.register(
            trader_id=self.trader_id,
            portfolio=self.portfolio,
            msgbus=self.msgbus,
            cache=self.cache,
            clock=self.clock,
            logger=self.logger,
        )

        bar = Bar(
            bar_type,
            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,
        )

        strategy.handle_bar(bar)

        # Act
        strategy.reset()

        # Assert
        assert "on_reset" in strategy.calls
        assert strategy.is_initialized
        assert strategy.ema1.count == 0
        assert strategy.ema2.count == 0
示例#6
0
 def bar_3decimal() -> Bar:
     return Bar(
         bar_type=TestDataStubs.bartype_usdjpy_1min_bid(),
         open=Price.from_str("90.002"),
         high=Price.from_str("90.004"),
         low=Price.from_str("90.001"),
         close=Price.from_str("90.003"),
         volume=Quantity.from_int(1_000_000),
         ts_event=0,
         ts_init=0,
     )
    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_check_when_low_above_close_raises_value_error(self):
     # Arrange, Act, Assert
     with pytest.raises(ValueError):
         Bar(
             AUDUSD_1_MIN_BID,
             Price.from_str("1.00000"),
             Price.from_str("1.00005"),
             Price.from_str("1.00000"),
             Price.from_str("0.99999"),  # Close below low
             Quantity.from_int(100000),
             0,
             0,
             True,
         )
 def test_check_when_high_below_close_raises_value_error(self):
     # Arrange, Act, Assert
     with pytest.raises(ValueError):
         Bar(
             AUDUSD_1_MIN_BID,
             Price.from_str("1.00000"),
             Price.from_str("1.00000"),  # High below close
             Price.from_str("1.00000"),
             Price.from_str("1.00005"),
             Quantity.from_int(100000),
             0,
             0,
             True,
         )
示例#10
0
    def test_handle_bar(self):
        # Arrange
        bar = 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
        self.swings.handle_bar(bar)

        # Assert
        assert self.swings.has_inputs
    def test_hash_str_repr(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, Assert
        assert isinstance(hash(bar), int)
        assert (
            str(bar) == "AUD/USD.SIM-1-MINUTE-BID-EXTERNAL,1.00001,1.00004,1.00002,1.00003,100000,0"
        )
        assert (
            repr(bar)
            == "Bar(AUD/USD.SIM-1-MINUTE-BID-EXTERNAL,1.00001,1.00004,1.00002,1.00003,100000,0)"
        )
示例#12
0
def parse_bars_http(
    instrument: Instrument,
    bar_type: BarType,
    data: List[Dict[str, Any]],
    ts_event_delta: int,
    ts_init: int,
) -> List[Bar]:
    bars: List[Bar] = []
    for row in data:
        bar: Bar = Bar(
            bar_type=bar_type,
            open=Price(row["open"], instrument.price_precision),
            high=Price(row["high"], instrument.price_precision),
            low=Price(row["low"], instrument.price_precision),
            close=Price(row["close"], instrument.price_precision),
            volume=Quantity(row["volume"], instrument.size_precision),
            check=True,
            ts_event=secs_to_nanos(row["time"]) + ts_event_delta,
            ts_init=ts_init,
        )
        bars.append(bar)

    return bars
 def test_fully_qualified_name(self):
     # Arrange, Act, Assert
     assert Bar.fully_qualified_name() == "nautilus_trader.model.data.bar.Bar"
示例#14
0
def deserialize(data: Dict) -> Bar:
    ignore = ("instrument_id", )
    bar = Bar.from_dict({k: v for k, v in data.items() if k not in ignore})
    return bar
示例#15
0
def serialize(bar: Bar):
    data = bar.to_dict(bar)
    data["instrument_id"] = bar.type.instrument_id.value
    return data