nautechsystems / nautilus_trader

A high-performance algorithmic trading platform and event-driven backtester
https://nautilustrader.io
GNU Lesser General Public License v3.0
2.14k stars 481 forks source link

BacktestNode won't clear data in streaming mode #1564

Closed dpmabo closed 7 months ago

dpmabo commented 7 months ago

Bug Report

BacktestNode won't clear data in streaming mode, add the line 'engine.clear_data()' to fix.

def _run_streaming(
        self,
        run_config_id: str,
        engine: BacktestEngine,
        data_configs: list[BacktestDataConfig],
        batch_size_bytes: int,
    ) -> None:
        # Create session for entire stream
        session = DataBackendSession(chunk_size=batch_size_bytes)

        # Add query for all data configs
        for config in data_configs:
            catalog = self.load_catalog(config)
            if config.data_type == Bar:
                # TODO: Temporary hack - improve bars config and decide implementation with `filter_expr`
                assert config.instrument_id, "No `instrument_id` for Bar data config"
                assert config.bar_spec, "No `bar_spec` for Bar data config"
                bar_type = config.instrument_id + "-" + config.bar_spec + "-EXTERNAL"
            else:
                bar_type = None
            session = catalog.backend_session(
                data_cls=config.data_type,
                instrument_ids=(
                    [config.instrument_id] if config.instrument_id and not bar_type else []
                ),
                bar_types=[bar_type] if bar_type else [],
                start=config.start_time,
                end=config.end_time,
                session=session,
            )

        # Stream data
        for chunk in session.to_query_result():
            engine.add_data(
                data=capsule_to_list(chunk),
                validate=False,  # Cannot validate mixed type stream
                sort=True,  # Temporarily sorting  # Already sorted from kmerge
            )
            engine.run(
                run_config_id=run_config_id,
                streaming=True,
            )
            # FIXME: SHOULD CLEAR data consumed
            engine.clear_data()

        engine.end()
        engine.dispose()

Expected Behavior

backTestNode should clear data in every backtest round

Actual Behavior

miss call to clear_data()

Steps to Reproduce the Problem

Run any backtest through BacktestNode in streaming mode

Specifications

cjdsellers commented 7 months ago

Hi @dpmabo

Thanks for the detailed report and proposed fix!

cjdsellers commented 7 months ago

Now fixed from c2738e05987af383c8da665c257ad7821602fe56.