Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(backtest): fail closed on local benchmark instead of yfinance fallback

xor_xe100a832a4287
diff --git a/agent/backtest/benchmark.py b/agent/backtest/benchmark.py
index 798d7db2e..9591c8d7b 100644
--- a/agent/backtest/benchmark.py
+++ b/agent/backtest/benchmark.py
@@ -55,7 +55,8 @@ def resolve_benchmark(
explicit: Override ticker (e.g. "SPY" passed via config).
loader: Loader of the configured data source. When given, the
benchmark is fetched through it first, falling back to
- yfinance if it yields no data.
+ yfinance if it yields no data — except ``local``,
+ which fails closed to keep offline runs offline.
Returns:
BenchmarkResult with return series and total return, or None if no
@@ -66,7 +67,11 @@ def resolve_benchmark(
return None
try:
- bench_df = _fetch_benchmark(ticker, start_date, end_date, interval, loader=loader)
+ bench_df = _fetch_benchmark(
+ ticker, start_date, end_date, interval,
+ loader=loader,
+ allow_fallback=source != "local",
+ )
except Exception:
return None
@@ -139,12 +144,14 @@ def _fetch_benchmark(
end_date: str,
interval: str,
loader: Optional[Any] = None,
+ allow_fallback: bool = True,
) -> pd.DataFrame:
"""Fetch benchmark OHLCV data.
- Tries the configured source's loader first (when given), so an explicit
- source such as ``local`` stays offline. Falls back to yfinance
- (single symbol, no auth) when no loader is given or it yields no data.
+ Tries the configured source's loader first (when given). Falls back to
+ yfinance (single symbol, no auth) when no loader is given or it yields
+ no data — unless ``allow_fallback`` is False (offline sources fail
+ closed instead of making a network request).
"""
if loader is not None:
try:
@@ -157,6 +164,9 @@ def _fetch_benchmark(
if not df.empty:
return df
+ if not allow_fallback:
+ return pd.DataFrame()
+
result = YfinanceLoader().fetch([ticker], start_date, end_date, interval=interval)
return _extract_frame(result, ticker)
diff --git a/agent/tests/test_local_source_routing.py b/agent/tests/test_local_source_routing.py
index b0f630d2e..d500db2d1 100644
--- a/agent/tests/test_local_source_routing.py
+++ b/agent/tests/test_local_source_routing.py
@@ -54,6 +54,11 @@ class _EmptyLoader:
return {}
+class _RaisingLoader:
+ def fetch(self, *args: object, **kwargs: object) -> Dict[str, pd.DataFrame]:
+ raise RuntimeError("boom")
+
+
class TestBenchmarkLoaderForwarding:
def test_explicit_source_loader_is_used_instead_of_yfinance(
self, monkeypatch: pytest.MonkeyPatch,
@@ -78,15 +83,38 @@ class TestBenchmarkLoaderForwarding:
assert loader.fetched == ["AAPL.US"]
assert result.total_ret == pytest.approx(0.1)
- def test_falls_back_to_yfinance_when_source_loader_has_no_data(
+ @pytest.mark.parametrize("loader", [_EmptyLoader(), _RaisingLoader(), None])
+ def test_local_source_fails_closed_without_yfinance(
+ self, monkeypatch: pytest.MonkeyPatch, loader: object,
+ ) -> None:
+ """source=local must never construct a yfinance loader, even when the
+ local loader yields no benchmark data or raises."""
+
+ def _no_network() -> None:
+ raise AssertionError("yfinance loader must not be created")
+
+ monkeypatch.setattr("backtest.benchmark.YfinanceLoader", _no_network)
+
+ result = resolve_benchmark(
+ strategy_codes=["AAPL.US"],
+ source="local",
+ start_date="2023-01-03",
+ end_date="2023-01-04",
+ explicit="SPY",
+ loader=loader,
+ )
+
+ assert result is None
+
+ def test_non_local_source_falls_back_to_yfinance_when_no_data(
self, monkeypatch: pytest.MonkeyPatch,
) -> None:
fallback = _FakeLoader([100.0, 105.0])
monkeypatch.setattr("backtest.benchmark.YfinanceLoader", lambda: fallback)
result = resolve_benchmark(
- strategy_codes=["AAPL.US"],
- source="local",
+ strategy_codes=["600519.SH"],
+ source="tushare",
start_date="2023-01-03",
end_date="2023-01-04",
explicit="SPY",