"Vibe-Trading: Your Personal Trading Agent"
Python ai-agentalgorithmic-tradingbacktestingfintechllmmcpmulti-agentpythonquantitative-financetrading
fix(caliber): warn when a run serves an additive series, not only when it mixes
cgycoreye2bb00e1aa43
diff --git a/agent/backtest/loaders/registry.py b/agent/backtest/loaders/registry.pyindex 6de32b8fd..5997b99b2 100644--- a/agent/backtest/loaders/registry.py+++ b/agent/backtest/loaders/registry.py@@ -299,6 +299,13 @@ _COMPARABLE_CALIBERS = frozenset({"raw", "split", "split_dividend", "split_dividend_additive"})+#: Calibers whose daily moves cannot be read as returns at all. Cash dividends+#: enter the price *level* as an offset rather than scaling the series, so a+#: window return is not a total return and an old level can even go negative.+#: Unlike a mixed basket, this is a defect of the series itself: it needs a+#: warning even when every symbol in the run came from the one source.+_ADDITIVE_CALIBERS = frozenset({"split_dividend_additive"})+def price_caliber(source: str, market: str | None = None) -> str:"""Return the adjustment caliber of ``source``'s served prices.@@ -341,6 +348,37 @@ def mixed_caliber_warning(stamps: dict[str, tuple[str, str]]) -> str | None:)+def additive_caliber_warning(stamps: dict[str, tuple[str, str]]) -> str | None:+ """Build the served-additive-caliber warning for a run, or None.++ ``stamps`` maps each served symbol to the (source, caliber) pair that+ served it. Unlike :func:`mixed_caliber_warning` this fires on a *single*+ additive source, because the problem is the series rather than the basket:+ a run that mixes nothing is still computing returns off levels that have+ dividends added back as a flat offset. That is the common A-share case,+ since ``tencent`` heads the chain (#1493).+ """+ additive = {+ symbol: source+ for symbol, (source, caliber) in stamps.items()+ if caliber in _ADDITIVE_CALIBERS+ }+ if not additive:+ return None+ shown = ", ".join(+ f"{symbol} ({source})" for symbol, source in sorted(additive.items())[:4]+ )+ if len(additive) > 4:+ shown += f", +{len(additive) - 4} more"+ return (+ "additive price adjustment in this run: " + shown + ". "+ "Cash dividends are added back as a flat offset rather than "+ "reinvested, so period returns and volatilities are not total "+ "returns and long-horizon backtests are distorted (old prices can "+ "go negative). Prefer a multiplicative source for return math."+ )++# ---------------------------------------------------------------------------# Source-order overrides: per-market env-configurable chain priority# ---------------------------------------------------------------------------diff --git a/agent/backtest/runner.py b/agent/backtest/runner.pyindex 4071fac15..89924de59 100644--- a/agent/backtest/runner.py+++ b/agent/backtest/runner.py@@ -25,6 +25,7 @@ from backtest.loaders.registry import (FALLBACK_CHAINS,LOADER_REGISTRY,VALID_SOURCES,+ additive_caliber_warning,get_loader_cls_with_fallback,is_no_network_fallback_source,mixed_caliber_warning,@@ -1857,7 +1858,20 @@ def fetch_data_map(config: dict) -> DataFetchResult:caliber_stamps = {code: stamp for code, stamp in caliber_stamps.items() if code in data_map}- caliber_warning = mixed_caliber_warning(caliber_stamps)+ # Both warnings can apply at once (a tencent+baostock basket mixes calibers+ # *and* serves an additive one), and each says something the other does not,+ # so they are reported together rather than one shadowing the other.+ caliber_warning = (+ "\n".join(+ warning+ for warning in (+ mixed_caliber_warning(caliber_stamps),+ additive_caliber_warning(caliber_stamps),+ )+ if warning+ )+ or None+ )if caliber_warning:logger.warning("%s", caliber_warning)return DataFetchResult(diff --git a/agent/tests/test_price_caliber.py b/agent/tests/test_price_caliber.pyindex c14b35059..731c8ac4e 100644--- a/agent/tests/test_price_caliber.py+++ b/agent/tests/test_price_caliber.py@@ -13,6 +13,7 @@ import pytestfrom backtest import runnerfrom backtest.loaders.registry import (FALLBACK_CHAINS,+ additive_caliber_warning,mixed_caliber_warning,price_caliber,)@@ -116,6 +117,39 @@ def test_tencent_only_basket_stays_silent() -> None:)+def test_additive_warning_fires_on_a_single_source() -> None:+ """The blind spot the caliber alone could not cover: a run with no mix at+ all still computes its returns off additive levels, and with tencent+ heading the A-share chain that is the ordinary case, not an edge one."""+ msg = additive_caliber_warning({"600519.SH": ("tencent", "split_dividend_additive")})+ assert msg is not None+ assert "600519.SH" in msg and "tencent" in msg+ assert "not total returns" in msg+++def test_additive_warning_stays_silent_on_multiplicative_and_raw() -> None:+ """Tencent's HK path rides in the same basket and must not trip it: that+ series is stamped raw by the market override, not additive."""+ assert (+ additive_caliber_warning(+ {+ "600519.SH": ("baostock", "split_dividend"),+ "AAPL.US": ("yahoo", "split_dividend"),+ "0700.HK": ("tencent", "raw"),+ "BTC-USDT": ("okx", "na"),+ }+ )+ is None+ )+++def test_additive_warning_truncates_a_long_served_list() -> None:+ stamps = {f"{600000 + i}.SH": ("tencent", "split_dividend_additive") for i in range(6)}+ msg = additive_caliber_warning(stamps)+ assert msg is not None+ assert "+2 more" in msg++def test_non_equity_markets_stamp_na() -> None:assert price_caliber("binance", "crypto") == "na"# The market wins over the per-source table: yfinance serving BTC has@@ -299,6 +333,64 @@ def test_fetch_data_map_silent_on_single_caliber(assert "mixed price calibers" not in caplog.text+class _TencentStub:+ name = "tencent"++ def fetch(self, codes, start, end, fields=None, interval="1D"): # noqa: ANN001, ANN201+ return {"600519.SH": _df()}+++def test_fetch_data_map_warns_on_additive_caliber(+ monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture+) -> None:+ """A single-source tencent run mixes nothing, so the mixed-caliber warning+ is right to stay silent — but the series it served is additive, and the+ run must still say so."""+ monkeypatch.setattr(runner, "resolve_loader", lambda market: _TencentStub())+ monkeypatch.setattr(runner, "LOADER_REGISTRY", {})++ result = runner.fetch_data_map(+ {+ "source": "auto",+ "codes": ["600519.SH"],+ "start_date": "2024-01-01",+ "end_date": "2024-01-03",+ "interval": "1D",+ }+ )++ assert result.caliber_warning is not None+ assert "additive price adjustment" in result.caliber_warning+ assert "600519.SH" in result.caliber_warning+ assert "not total returns" in result.caliber_warning+ assert "mixed price calibers" not in result.caliber_warning+ assert "additive price adjustment" in caplog.text+++def test_fetch_data_map_reports_both_warnings_when_they_apply(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ """tencent (additive) serving 600519.SH beside sina (raw) serving TSLA.US is+ both a mixed basket and an additive series; one warning must not shadow the+ other, since each says something the other does not."""+ monkeypatch.setattr(runner, "resolve_loader", lambda market: _TencentStub())+ monkeypatch.setattr(runner, "LOADER_REGISTRY", {"sina": _SinaStub})++ result = runner.fetch_data_map(+ {+ "source": "auto",+ "codes": ["600519.SH", "TSLA.US"],+ "start_date": "2024-01-01",+ "end_date": "2024-01-03",+ "interval": "1D",+ }+ )++ assert result.caliber_warning is not None+ assert "mixed price calibers" in result.caliber_warning+ assert "additive price adjustment" in result.caliber_warning++# --------------------------------------------------------------------------# The table is a claim about the loader. #1320 changed the FMP and Tiingo# loaders to serve adjusted OHLC and left this table saying "raw", which is