Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(backtest): re-read the run interval in CompositeEngine.run_backtest

Haozhe Wua0ab2f112d53
diff --git a/agent/backtest/engines/composite.py b/agent/backtest/engines/composite.py
index cc91330a6..a368419f5 100644
--- a/agent/backtest/engines/composite.py
+++ b/agent/backtest/engines/composite.py
@@ -153,6 +153,9 @@ class CompositeEngine(BaseEngine):
ValueError: If the codes span more than one settlement currency.
"""
_reject_mixed_currency(config.get("codes") or list(self._symbol_market))
+ # The run config, not the construction config, is authoritative for the
+ # bar span — same convention as CryptoEngine.run_backtest.
+ self._run_interval = str(config.get("interval", "1D"))
return super().run_backtest(config, *args, **kwargs)
def _rule_for(self, symbol: str) -> BaseEngine:
diff --git a/agent/tests/test_composite_currency_guard.py b/agent/tests/test_composite_currency_guard.py
index 42edc0bce..29a2a903b 100644
--- a/agent/tests/test_composite_currency_guard.py
+++ b/agent/tests/test_composite_currency_guard.py
@@ -124,3 +124,23 @@ class TestRunBacktestFailsClosed:
codes = ["IF2406.CFFEX", "ESZ4"]
engine = CompositeEngine({"initial_cash": 1_000_000, "codes": codes}, codes)
assert engine._rule_for("IF2406.CFFEX") is not engine._rule_for("ESZ4")
+
+
+class TestCompositeRunInterval:
+ """The run config, not the construction config, sets the funding bar span.
+
+ ``CryptoEngine`` re-reads ``interval`` in ``run_backtest`` (crypto.py:105);
+ the composite must follow the same convention or a run launched with a
+ different interval than the one it was constructed with settles funding at
+ the stale span (#1290).
+ """
+
+ def test_run_backtest_rereads_the_interval(self) -> None:
+ codes = ["BTC-USDT"]
+ engine = CompositeEngine({"initial_cash": 100_000, "interval": "1D"}, codes)
+ assert engine._run_interval == "1D"
+ try:
+ engine.run_backtest({"codes": codes, "interval": "4H"}, None, None, None)
+ except Exception:
+ pass # the pipeline needs a real loader; only the re-read matters here
+ assert engine._run_interval == "4H"