Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(data): a price index is stamped raw, not by what its source does to a stock

Haozhe Wue626ffe32a3c
diff --git a/agent/backtest/loaders/registry.py b/agent/backtest/loaders/registry.py
index 02deed07f..a28943221 100644
--- a/agent/backtest/loaders/registry.py
+++ b/agent/backtest/loaders/registry.py
@@ -10,6 +10,7 @@ of import order.
from __future__ import annotations
import logging
+import re
from threading import Lock
from typing import Any, Type
@@ -304,6 +305,19 @@ PRICE_CALIBER_BY_SOURCE_MARKET: dict[tuple[str, str], str] = {
#: "na" and stay out of mixed-caliber comparisons.
_NA_CALIBER_MARKETS = frozenset({"crypto", "forex", "futures", "macro"})
+#: A price index has no corporate actions, so every source serves the one
+#: unadjusted level, whatever it does to a stock: eastmoney fqt=1 equals fqt=0
+#: on every bar of 000300.SH, 000001.SH, 000016.SH, 000905.SH, 399001.SZ,
+#: 399006.SZ and 899050.BJ, Tencent's fqkline returns only "day" for them, and
+#: Yahoo's adjclose equals close on ^GSPC, ^NDX and ^HSI (measured 2026-09-22).
+#: The per-source table stamped them by what the source does to a stock, so an
+#: A-share index read as additive and a Yahoo index as dividend-adjusted (#1541).
+#: Indices stay comparable: a price index beside a dividend-adjusted stock is a
+#: real caliber mix. A-share index codes are exchange-specific: 000001.SH is
+#: the SSE Composite, 000001.SZ is Ping An Bank.
+_INDEX_CALIBER_MARKETS = frozenset({"index"})
+_A_SHARE_INDEX_CODE = re.compile(r"^(?:000\d{3}\.SH|399\d{3}\.SZ|899\d{3}\.BJ)$", re.I)
+
#: Calibers that participate in mixed-caliber comparison. "unknown" and "na"
#: never do: the first is unmeasured, the second has nothing to adjust for.
#: "split_dividend_additive" does participate — it is the whole point of the
@@ -321,15 +335,26 @@ _COMPARABLE_CALIBERS = frozenset(
_ADDITIVE_CALIBERS = frozenset({"split_dividend_additive"})
-def price_caliber(source: str, market: str | None = None) -> str:
+def price_caliber(source: str, market: str | None = None, symbol: str | None = None) -> str:
"""Return the adjustment caliber of ``source``'s served prices.
- One of "raw", "split", "split_dividend", "split_dividend_additive" (dividend
- adjustment applied to the level rather than by a ratio), "na" (a market
- without corporate actions), or "unknown" (an unmeasured source).
+ Args:
+ source: Loader that served the prices.
+ market: Market the symbol belongs to.
+ symbol: The served symbol, needed to tell an A-share index from a stock.
+
+ Returns:
+ One of "raw", "split", "split_dividend", "split_dividend_additive"
+ (dividend adjustment applied to the level rather than by a ratio),
+ "na" (a market without corporate actions), or "unknown" (an
+ unmeasured source).
"""
if market in _NA_CALIBER_MARKETS:
return "na"
+ if market in _INDEX_CALIBER_MARKETS or (
+ market == "a_share" and symbol is not None and _A_SHARE_INDEX_CODE.match(symbol)
+ ):
+ return "raw"
return PRICE_CALIBER_BY_SOURCE_MARKET.get(
(source, market), PRICE_CALIBER_BY_SOURCE.get(source, "unknown")
)
diff --git a/agent/backtest/runner.py b/agent/backtest/runner.py
index 89924de59..a62d14b3a 100644
--- a/agent/backtest/runner.py
+++ b/agent/backtest/runner.py
@@ -1630,7 +1630,7 @@ def _fetch_auto(codes: List[str], config: dict, interval: str = "1D") -> dict:
local_name = str(getattr(local_loader, "name", "local") or "local")
served_by.add(local_name)
for code in local_result:
- caliber_stamps[code] = (local_name, price_caliber(local_name, _detect_market(code)))
+ caliber_stamps[code] = (local_name, price_caliber(local_name, _detect_market(code), code))
merged.update(local_result)
market_groups = _group_codes_by_market([code for code in codes if code not in set(local_codes)])
@@ -1660,7 +1660,7 @@ def _fetch_auto(codes: List[str], config: dict, interval: str = "1D") -> dict:
if market_result:
served_by.add(src_name)
for code in market_result:
- caliber_stamps[code] = (src_name, price_caliber(src_name, market))
+ caliber_stamps[code] = (src_name, price_caliber(src_name, market, code))
missing = [code for code in market_codes if code not in market_result]
# Retry only missing symbols so a partial primary response does not
@@ -1684,7 +1684,7 @@ def _fetch_auto(codes: List[str], config: dict, interval: str = "1D") -> dict:
fb_served_by = str(getattr(fb_loader, "name", fb_name) or fb_name)
served_by.add(fb_served_by)
for code in mapped:
- caliber_stamps[code] = (fb_served_by, price_caliber(fb_served_by, market))
+ caliber_stamps[code] = (fb_served_by, price_caliber(fb_served_by, market, code))
logger.info(
"Runtime fallback: %s -> %s for %s", src_name, fb_name, market
)
@@ -1780,7 +1780,7 @@ def fetch_data_map(config: dict) -> DataFetchResult:
for code in data_map:
caliber_stamps[code] = (
served_by,
- price_caliber(served_by, _detect_market(code)),
+ price_caliber(served_by, _detect_market(code), code),
)
used_sources = [served_by] if data_map else []
missing = [code for code in codes if code not in data_map]
@@ -1839,7 +1839,7 @@ def fetch_data_map(config: dict) -> DataFetchResult:
for code in mapped:
caliber_stamps[code] = (
fb_served_by,
- price_caliber(fb_served_by, _detect_market(code)),
+ price_caliber(fb_served_by, _detect_market(code), code),
)
if not used_sources:
source = fb_served_by
diff --git a/agent/src/market_data.py b/agent/src/market_data.py
index 0687129c2..3aa1cd9b6 100644
--- a/agent/src/market_data.py
+++ b/agent/src/market_data.py
@@ -380,7 +380,7 @@ def fetch_market_data(
"fallback_used": bool(used_source and used_source != src),
"currency_conversion": currency_conversion,
"volume_unit": volume_units.get(market),
- "adjustment": price_caliber(used_source or src, market),
+ "adjustment": price_caliber(used_source or src, market, symbol),
}
quote_currency = frame_attrs.get("quote_currency")
if isinstance(quote_currency, str) and quote_currency:
@@ -441,7 +441,7 @@ def fetch_market_data(
"fallback_used": True,
"currency_conversion": "none",
"volume_unit": base.get("volume_unit"),
- "adjustment": base.get("adjustment", price_caliber(src, market)),
+ "adjustment": base.get("adjustment", price_caliber(src, market, code)),
"venue_fallback": True,
"resolved_symbol": sibling,
}
diff --git a/agent/tests/test_price_caliber.py b/agent/tests/test_price_caliber.py
index 1f38665fd..6ffaeee00 100644
--- a/agent/tests/test_price_caliber.py
+++ b/agent/tests/test_price_caliber.py
@@ -179,6 +179,34 @@ def test_non_equity_markets_stamp_na() -> None:
assert price_caliber("mt5", "forex") == "na"
[email protected]("source", ["tencent", "eastmoney", "akshare", "tushare", "baostock"])
[email protected]("code", ["000300.SH", "000001.SH", "399001.SZ", "399006.SZ", "899050.BJ"])
+def test_an_a_share_index_is_raw_on_every_source(source: str, code: str) -> None:
+ """An index has no corporate actions: eastmoney's fqt=1 equals its fqt=0 on
+ every bar and Tencent serves only "day", so the additive stamp a stock gets
+ from these sources is wrong for it (#1541)."""
+ assert price_caliber(source, "a_share", code) == "raw"
+
+
[email protected]("code", ["000001.SZ", "600519.SH", "510300.SH", "399001.SH", "000300.SZ", "830799.BJ"])
+def test_an_a_share_stock_or_fund_keeps_its_source_caliber(code: str) -> None:
+ """The index codes are exchange-specific: 000001.SZ is Ping An Bank, not
+ the SSE Composite, and 510300.SH is an ETF that pays distributions."""
+ assert price_caliber("tencent", "a_share", code) == "split_dividend_additive"
+ assert price_caliber("tushare", "a_share", code) == "split_dividend"
+
+
+def test_a_yahoo_index_is_raw() -> None:
+ """Yahoo's adjclose equals close on ^GSPC / ^NDX / ^HSI: nothing was adjusted."""
+ assert price_caliber("yahoo", "index", "^GSPC") == "raw"
+ assert price_caliber("yfinance", "index") == "raw"
+ assert price_caliber("yahoo", "us_equity", "AAPL.US") == "split_dividend"
+
+
+def test_without_a_symbol_the_market_default_applies() -> None:
+ assert price_caliber("tencent", "a_share") == "split_dividend_additive"
+
+
def test_every_chain_source_resolves() -> None:
for market, chain in FALLBACK_CHAINS.items():
for source in chain:
@@ -270,6 +298,19 @@ def test_provenance_marks_tencent_hk_as_unadjusted() -> None:
assert out["_provenance"]["00939.HK"]["adjustment"] == "raw"
+def test_provenance_marks_an_a_share_index_as_unadjusted() -> None:
+ out = fetch_market_data(
+ codes=["000300.SH", "600519.SH"],
+ start_date="2024-01-01",
+ end_date="2024-01-03",
+ source="tencent",
+ loader_resolver=lambda src: _StubLoader,
+ include_provenance=True,
+ )
+ assert out["_provenance"]["000300.SH"]["adjustment"] == "raw"
+ assert out["_provenance"]["600519.SH"]["adjustment"] == "split_dividend_additive"
+
+
def test_provenance_stamps_adjustment_for_raw_source() -> None:
out = fetch_market_data(
codes=["AAPL.US"],
@@ -388,6 +429,30 @@ def test_fetch_data_map_warns_on_additive_caliber(
assert "additive price adjustment" in caplog.text
+def test_fetch_data_map_does_not_call_an_index_additive(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ """A tencent run over an index and a stock warns about the stock only."""
+
+ class _TencentBoth:
+ name = "tencent"
+
+ def fetch(self, codes, start, end, fields=None, interval="1D"): # noqa: ANN001, ANN201
+ return {code: _df() for code in codes}
+
+ monkeypatch.setattr(runner, "resolve_loader", lambda market: _TencentBoth())
+ monkeypatch.setattr(runner, "LOADER_REGISTRY", {})
+ config = {"source": "auto", "start_date": "2024-01-01", "end_date": "2024-01-03", "interval": "1D"}
+
+ index_only = runner.fetch_data_map({**config, "codes": ["000300.SH"]})
+ assert index_only.caliber_warning is None
+
+ both = runner.fetch_data_map({**config, "codes": ["000300.SH", "600519.SH"]})
+ assert both.caliber_warning is not None
+ assert "additive price adjustment in this run: 600519.SH (tencent)." in both.caliber_warning
+ assert "mixed price calibers" in both.caliber_warning
+
+
def test_fetch_data_map_reports_both_warnings_when_they_apply(
monkeypatch: pytest.MonkeyPatch,
) -> None: