"Vibe-Trading: Your Personal Trading Agent"
Python ai-agentalgorithmic-tradingbacktestingfintechllmmcpmulti-agentpythonquantitative-financetrading
fix(loaders): Tencent's A-share caliber is dividend-additive, and its HK series is raw
cgycorey6b1f85edeaf7
diff --git a/agent/backtest/loaders/registry.py b/agent/backtest/loaders/registry.pyindex 366dbf9c8..84a99ec61 100644--- a/agent/backtest/loaders/registry.py+++ b/agent/backtest/loaders/registry.py@@ -254,7 +254,12 @@ PRICE_CALIBER_BY_SOURCE: dict[str, str] = {"yahoo": "split_dividend", # quote series split-adjusted at origin, scaled to adjclose"yfinance": "split_dividend", # auto_adjust=True"eastmoney": "split_dividend", # fqt=1 (forward-adjusted) on every kline call- "tencent": "split_dividend", # fqkline qfq+ # Tencent's qfq subtracts cash dividends from the price level instead of+ # scaling by a ratio: between corporate actions qfq = raw + c, and c steps by+ # the dividend at each ex-date (#1493, measured on 600519.SH where five+ # constant offsets cover 500 bars). The level is therefore not on the same+ # scale as the multiplicative sources above, so it gets its own caliber.+ "tencent": "split_dividend_additive", # fqkline qfq, additive in dividends"akshare": "split_dividend", # adjust="qfq", including the stock_us_hist path"baostock": "split_dividend", # adjustflag="2""tushare": "split_dividend", # adj_factor applied via cn_adjust (A-share/fund)@@ -272,6 +277,11 @@ PRICE_CALIBER_BY_SOURCE: dict[str, str] = {PRICE_CALIBER_BY_SOURCE_MARKET: dict[tuple[str, str], str] = {# Tushare publishes no HK adjustment-factor series, so its HK path is raw.("tushare", "hk_equity"): "raw",+ # Tencent's HK series is unadjusted despite the qfq request parameter: its+ # qfq and hfq replies are identical to its own day series on 00939.HK and+ # 00700.HK, over a window where eastmoney's adjusted HK series differs from+ # raw, so the actions exist and this source does not apply them (#1493).+ ("tencent", "hk_equity"): "raw",}#: Markets with no corporate-action adjustment concept. Their sources stamp@@ -280,14 +290,20 @@ _NA_CALIBER_MARKETS = frozenset({"crypto", "forex", "futures", "macro"})#: Calibers that participate in mixed-caliber comparison. "unknown" and "na"#: never do: the first is unmeasured, the second has nothing to adjust for.-_COMPARABLE_CALIBERS = frozenset({"raw", "split", "split_dividend"})+#: "split_dividend_additive" does participate — it is the whole point of the+#: caliber: an additive level and a multiplicative one are two different scales,+#: so a basket holding both must be warned about (#1493).+_COMPARABLE_CALIBERS = frozenset(+ {"raw", "split", "split_dividend", "split_dividend_additive"}+)def price_caliber(source: str, market: str | None = None) -> str:"""Return the adjustment caliber of ``source``'s served prices.- One of "raw", "split", "split_dividend", "na" (a market without- corporate actions), or "unknown" (an unmeasured source).+ 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"diff --git a/agent/mcp_server.py b/agent/mcp_server.pyindex eaee3d302..6f81d0be3 100644--- a/agent/mcp_server.py+++ b/agent/mcp_server.py@@ -1825,8 +1825,11 @@ def get_market_data(Price caliber: which source served a symbol decides what its prices mean(some adjust for splits and dividends, others serve raw quotes). Eachsymbol's ``_provenance.adjustment`` states the caliber ("raw" / "split" /- "split_dividend" / "na" / "unknown") — read it before comparing price- levels across symbols.+ "split_dividend" / "split_dividend_additive" / "na" / "unknown") — read it+ before comparing price levels across symbols. Note that+ ``split_dividend_additive`` (Tencent's A-share qfq) adjusts dividends by+ shifting the price level rather than scaling it, so it is not on the same+ scale as ``split_dividend`` even though both account for dividends."""registry = _get_registry()return registry.execute(diff --git a/agent/tests/test_price_caliber.py b/agent/tests/test_price_caliber.pyindex 090ab5d18..d0dfc407d 100644--- a/agent/tests/test_price_caliber.py+++ b/agent/tests/test_price_caliber.py@@ -38,7 +38,7 @@ def _df() -> pd.DataFrame:("yahoo", "split_dividend"),("yfinance", "split_dividend"),("eastmoney", "split_dividend"),- ("tencent", "split_dividend"),+ ("tencent", "split_dividend_additive"),("akshare", "split_dividend"),("baostock", "split_dividend"),("tushare", "split_dividend"),@@ -65,6 +65,55 @@ def test_tushare_hk_override_is_raw() -> None:assert price_caliber("tushare", "a_share") == "split_dividend"+# --------------------------------------------------------------------------+# Tencent's adjustment is additive in dividends (#1493): between corporate+# actions its qfq equals raw plus a constant, measured on 600519.SH where the+# offset takes five values over 500 bars and steps only at dividend dates. An+# additive series is not on the same scale as a multiplicative one, so sharing+# the `split_dividend` label made the mixed-caliber warning blind to exactly the+# mix it exists to catch. Its HK series is not adjusted at all.+# --------------------------------------------------------------------------+++def test_tencent_a_share_caliber_is_additive_not_multiplicative() -> None:+ """A-share qfq subtracts cash dividends from the level, not by a ratio."""+ assert price_caliber("tencent", "a_share") == "split_dividend_additive"+++def test_tencent_hk_series_is_unadjusted() -> None:+ """Tencent's HK qfq and hfq are identical to its own day series (measured on+ 00939.HK and 00700.HK), so HK stamps raw. The actions are real — eastmoney's+ adjusted HK series differs from raw over the same window — this source just+ does not apply them."""+ assert price_caliber("tencent", "hk_equity") == "raw"+++def test_tencent_a_share_basket_warns_against_multiplicative_sources() -> None:+ """The label collision was the bug: with both sources on `split_dividend`+ the warning returned None for a basket that mixes them."""+ stamps = {+ "600519.SH": ("tencent", price_caliber("tencent", "a_share")),+ "000001.SZ": ("baostock", price_caliber("baostock", "a_share")),+ }+ msg = mixed_caliber_warning(stamps)+ assert msg is not None+ assert "600519.SH" in msg and "000001.SZ" in msg+ assert "split_dividend_additive" in msg and "split_dividend" in msg+++def test_tencent_only_basket_stays_silent() -> None:+ """One additive source is not a mix, so nothing to warn about."""+ assert (+ mixed_caliber_warning(+ {+ "600519.SH": ("tencent", "split_dividend_additive"),+ "000001.SZ": ("tencent", "split_dividend_additive"),+ }+ )+ is None+ )++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@@ -80,6 +129,7 @@ def test_every_chain_source_resolves() -> None:"raw","split","split_dividend",+ "split_dividend_additive","na","unknown",}@@ -146,7 +196,21 @@ def test_provenance_stamps_adjustment_for_adjusted_source() -> None:loader_resolver=lambda src: _StubLoader,include_provenance=True,)- assert out["_provenance"]["600519.SH"]["adjustment"] == "split_dividend"+ assert out["_provenance"]["600519.SH"]["adjustment"] == "split_dividend_additive"+++def test_provenance_marks_tencent_hk_as_unadjusted() -> None:+ """HK rows served by tencent are unadjusted, and the field the caller reads+ before comparing price levels has to say so (#1493)."""+ out = fetch_market_data(+ codes=["00939.HK"],+ start_date="2024-01-01",+ end_date="2024-01-03",+ source="tencent",+ loader_resolver=lambda src: _StubLoader,+ include_provenance=True,+ )+ assert out["_provenance"]["00939.HK"]["adjustment"] == "raw"def test_provenance_stamps_adjustment_for_raw_source() -> None: