Skip to content

"Vibe-Trading: Your Personal Trading Agent"

feat(loaders): convert additive A-share qfq to multiplicative when the offsets validate

Yufeng He25e8e4bc4c82
diff --git a/agent/backtest/loaders/additive_conversion.py b/agent/backtest/loaders/additive_conversion.py
new file mode 100644
index 000000000..85d3797b4
--- /dev/null
+++ b/agent/backtest/loaders/additive_conversion.py
@@ -0,0 +1,129 @@
+"""Convert additive qfq series to the multiplicative (ratio) convention.
+
+A-share forward-adjusted prices from Tencent, Eastmoney and AKShare are
+dividend-ADDITIVE: between corporate actions ``qfq - raw`` is a constant
+offset, and each down-step at an ex-date is the cash dividend per share
+(HKUDS/Vibe-Trading#1541). A return computed on those levels is not a total
+return (the owner measured 600519.SH 2019 buy-and-hold at +184.07% additive
+against +100.43% multiplicative), and old levels can even go negative.
+
+The offsets are enough to recover the multiplicative series: at each step the
+factor is ``(prev_raw_close - dividend) / prev_raw_close``, and the cumulative
+product scales raw bars onto the 前复权 ratio basis anchored at the last bar,
+the same convention ``cn_adjust.apply_qfq`` uses for Tushare factors. Any
+inconsistency (misaligned calendars, offsets that are not plateaus, a
+non-positive or oversized dividend) returns ``None`` so the caller keeps the
+additive series, stamp and warning instead of shipping a bad conversion.
+"""
+
+from __future__ import annotations
+
+import pandas as pd
+
+# Offsets come from exact float subtraction, but allow cents-level slack when
+# grouping them into plateaus so a binary-representation wobble cannot split
+# one corporate-action segment into two.
+_OFFSET_TOL = 1e-6
+
+# Adjacent plateaus closer than this are the same segment: one bar's rounding
+# noise must not mint a fake dividend (a 2016 first bar wobbles 0.02).
+_PLATEAU_MERGE_TOL = 0.05
+
+
+def _plateau_spans(offset: pd.Series) -> list[tuple[int, int, float]] | None:
+ """Group the offset series into [start, end) spans of one constant value.
+
+ Returns None when the offsets vary bar to bar (not plateaus), which means
+ the series is not dividend-additive and there is nothing to convert.
+ """
+ spans: list[tuple[int, int, float]] = []
+ start = 0
+ current = float(offset.iloc[0])
+ for i in range(1, len(offset)):
+ value = float(offset.iloc[i])
+ if abs(value - current) > _OFFSET_TOL:
+ spans.append((start, i, current))
+ start = i
+ current = value
+ spans.append((start, len(offset), current))
+ # Fold sub-noise steps back into their segment: a boundary whose offset
+ # moves by less than a few cents is a rounding artifact, not an action.
+ merged: list[tuple[int, int, float]] = []
+ for span in spans:
+ if merged and abs(span[2] - merged[-1][2]) < _PLATEAU_MERGE_TOL:
+ merged[-1] = (merged[-1][0], span[1], merged[-1][2])
+ else:
+ merged.append(span)
+ return merged
+
+
+def convert_additive_to_multiplicative(
+ raw: pd.DataFrame, additive: pd.DataFrame
+) -> pd.DataFrame | None:
+ """Convert an additive qfq frame to the multiplicative convention.
+
+ Args:
+ raw: Unadjusted OHLCV bars (``close`` required), ascending date index.
+ additive: The same window's additive-adjusted bars.
+
+ Returns:
+ The converted frame (OHLC scaled by the cumulative ratio, ``volume``
+ divided by it, other columns untouched), or ``None`` when the inputs
+ cannot support a trustworthy conversion.
+ """
+ if raw is None or additive is None or raw.empty or additive.empty:
+ return None
+ if "close" not in raw.columns or "close" not in additive.columns:
+ return None
+
+ joined = raw.join(additive, how="inner", lsuffix="_raw", rsuffix="_adj")
+ # Every raw bar must be covered by the additive series; a partial window
+ # would silently convert only part of the history.
+ if len(joined) != len(raw) or len(joined) != len(additive):
+ return None
+
+ offset = joined["close_adj"] - joined["close_raw"]
+ spans = _plateau_spans(offset)
+ if not spans:
+ return None
+ # A genuine additive series steps a handful of times over hundreds of
+ # bars (the owner measured five offsets over 500 bars). One-bar plateaus
+ # dominating the window are not dividends; they are the signature of a
+ # ratio-adjusted series, where qfq - raw drifts with the price level, and
+ # converting that would double-adjust.
+ single_bar_spans = sum(1 for start, end, _ in spans if end - start == 1)
+ if len(spans) > 1 and single_bar_spans * 2 > len(spans):
+ return None
+
+ # Factor per boundary between plateau k and k+1. The offset steps toward
+ # zero at each ex-date, and the step size is the cash dividend per share.
+ factors: list[tuple[int, float]] = [] # (first bar of plateau k+1, factor)
+ for k in range(len(spans) - 1):
+ _, end_k, off_k = spans[k]
+ _, _, off_next = spans[k + 1]
+ dividend = off_next - off_k # offset rises toward 0 on the ex-date
+ prev_raw_close = float(joined["close_raw"].iloc[end_k - 1])
+ if prev_raw_close <= 0:
+ return None
+ if dividend <= 0 or dividend >= prev_raw_close:
+ # A non-positive step is not a dividend, and a dividend at or
+ # above the price is a data anomaly, not an adjustment.
+ return None
+ factors.append((end_k, (prev_raw_close - dividend) / prev_raw_close))
+
+ # Cumulative ratio per bar, anchored at the last bar (前复权): bars after
+ # the last corporate action keep ratio 1, and each step multiplies
+ # everything before it by that action's factor.
+ ratio = pd.Series(1.0, index=joined.index)
+ for first_bar_after, factor in factors:
+ ratio.iloc[:first_bar_after] *= factor
+ if (ratio <= 0).any():
+ return None
+
+ out = raw.copy()
+ for col in ("open", "high", "low", "close"):
+ if col in out.columns:
+ out[col] = out[col] * ratio
+ if "volume" in out.columns:
+ out["volume"] = out["volume"] / ratio
+ return out
diff --git a/agent/backtest/loaders/akshare_loader.py b/agent/backtest/loaders/akshare_loader.py
index 7f40ee66b..df244e61d 100644
--- a/agent/backtest/loaders/akshare_loader.py
+++ b/agent/backtest/loaders/akshare_loader.py
@@ -14,6 +14,7 @@ import pandas as pd
from backtest.engines._market_hooks import _detect_market, _is_china_futures
from backtest.loaders._symbol_utils import _is_etf_listed
+from backtest.loaders.additive_conversion import convert_additive_to_multiplicative
from backtest.loaders.base import cached_loader_fetch, validate_date_range
from backtest.loaders.registry import register
@@ -144,6 +145,34 @@ class DataLoader:
fields=None,
fetch=lambda code=code: self._fetch_one(code, start_date, end_date, interval),
)
+ if df is not None and not df.empty and _is_a_share(code) and not _is_etf_listed(code):
+ # #1541: adjust="qfq" is dividend-additive and cannot serve
+ # returns; convert to the multiplicative convention when
+ # the adjust="" companion supports it.
+ raw_df = None
+ try:
+ raw_df = cached_loader_fetch(
+ source=self.name,
+ symbol=code,
+ timeframe=interval,
+ start_date=start_date,
+ end_date=end_date,
+ fields=["raw"],
+ fetch=lambda code=code: self._fetch_a_share_raw(code, start_date, end_date, interval),
+ )
+ except Exception as exc: # noqa: BLE001 - degrade to additive
+ logger.warning(
+ "akshare raw companion fetch failed for %s, serving additive: %s",
+ code, exc,
+ )
+ converted = convert_additive_to_multiplicative(raw_df, df)
+ if converted is not None:
+ converted.attrs = {
+ **getattr(df, "attrs", {}),
+ "adjustment": "split_dividend",
+ }
+ result[code] = converted
+ continue
if df is not None and not df.empty:
result[code] = df
except Exception as exc:
@@ -210,6 +239,30 @@ class DataLoader:
return None
return self._normalize(df, date_col="日期")
+ def _fetch_a_share_raw(
+ self, code: str, start_date: str, end_date: str, interval: str,
+ ) -> Optional[pd.DataFrame]:
+ """Fetch the raw (unadjusted) companion of ``_fetch_a_share`` (#1541)."""
+ import akshare as ak
+
+ symbol = code.split(".")[0]
+ period = _INTERVAL_MAP_DAILY.get(interval)
+ if period is None:
+ raise ValueError(
+ f"Unsupported interval {interval!r}; akshare a-share supports "
+ f"{sorted(_INTERVAL_MAP_DAILY)}"
+ )
+ df = ak.stock_zh_a_hist(
+ symbol=symbol,
+ period=period,
+ start_date=start_date.replace("-", ""),
+ end_date=end_date.replace("-", ""),
+ adjust="",
+ )
+ if df is None or df.empty:
+ return None
+ return self._normalize(df, date_col="日期")
+
def _fetch_us(self, ak, code: str, start_date: str, end_date: str) -> Optional[pd.DataFrame]:
"""Fetch US stock via stock_us_hist."""
symbol = code.replace(".US", "")
diff --git a/agent/backtest/loaders/eastmoney_loader.py b/agent/backtest/loaders/eastmoney_loader.py
index 12887545f..e090227a7 100644
--- a/agent/backtest/loaders/eastmoney_loader.py
+++ b/agent/backtest/loaders/eastmoney_loader.py
@@ -21,6 +21,7 @@ from typing import Dict, List, Optional
import pandas as pd
from backtest.loaders import eastmoney_client
+from backtest.loaders.additive_conversion import convert_additive_to_multiplicative
from backtest.loaders.base import cached_loader_fetch, validate_date_range
from backtest.loaders.registry import register
@@ -109,6 +110,36 @@ class DataLoader:
code, start_date, end_date, interval
),
)
+ if df is not None and not df.empty and _is_a_share(code):
+ # #1541: fqt=1 is dividend-additive and cannot serve
+ # returns; convert to the multiplicative convention when
+ # the fqt=0 companion supports it.
+ raw_df = None
+ try:
+ raw_df = cached_loader_fetch(
+ source=self.name,
+ symbol=code,
+ timeframe=interval,
+ start_date=start_date,
+ end_date=end_date,
+ fields=["raw"],
+ fetch=lambda code=code: self._fetch_one(
+ code, start_date, end_date, interval, fqt=0
+ ),
+ )
+ except Exception as exc: # noqa: BLE001 - degrade to additive
+ logger.warning(
+ "eastmoney fqt=0 companion fetch failed for %s, serving additive: %s",
+ code, exc,
+ )
+ converted = convert_additive_to_multiplicative(raw_df, df)
+ if converted is not None:
+ converted.attrs = {
+ **getattr(df, "attrs", {}),
+ "adjustment": "split_dividend",
+ }
+ result[code] = converted
+ continue
if df is not None and not df.empty:
result[code] = df
except Exception as exc: # noqa: BLE001 - one bad symbol must not abort
@@ -117,6 +148,7 @@ class DataLoader:
def _fetch_one(
self, code: str, start_date: str, end_date: str, interval: str,
+ fqt: int = 1,
) -> Optional[pd.DataFrame]:
"""Resolve one symbol and build its OHLCV frame, or ``None`` on a miss.
@@ -142,7 +174,7 @@ class DataLoader:
rows = eastmoney_client.fetch_kline(
secid,
klt=klt,
- fqt=1,
+ fqt=fqt,
beg=_to_compact_date(start_date),
end=_to_compact_date(end_date),
)
@@ -180,3 +212,7 @@ class DataLoader:
if df.empty:
return None
return df
+
+
+def _is_a_share(code: str) -> bool:
+ return code.upper().endswith((".SZ", ".SH", ".BJ"))
diff --git a/agent/backtest/loaders/tencent_loader.py b/agent/backtest/loaders/tencent_loader.py
index 7901a32e3..37a242971 100644
--- a/agent/backtest/loaders/tencent_loader.py
+++ b/agent/backtest/loaders/tencent_loader.py
@@ -20,6 +20,7 @@ import urllib.request
import certifi
import pandas as pd
+from backtest.loaders.additive_conversion import convert_additive_to_multiplicative
from backtest.loaders.base import cached_loader_fetch, validate_date_range
from backtest.loaders.registry import register
@@ -102,6 +103,36 @@ class DataLoader:
fields=None,
fetch=lambda code=code: self._fetch_one(code, start_date, end_date),
)
+ if df is not None and not df.empty and _is_a_share(code):
+ # #1541: the additive qfq cannot serve returns; convert to
+ # the multiplicative convention when the raw companion
+ # supports it, keeping the additive series otherwise.
+ raw_df = None
+ try:
+ raw_df = cached_loader_fetch(
+ source=self.name,
+ symbol=code,
+ timeframe=interval,
+ start_date=start_date,
+ end_date=end_date,
+ fields=["raw"],
+ fetch=lambda code=code: self._fetch_one(
+ code, start_date, end_date, forward_adjusted=False
+ ),
+ )
+ except Exception as exc: # noqa: BLE001 - degrade to additive
+ logger.warning(
+ "tencent raw companion fetch failed for %s, serving additive: %s",
+ code, exc,
+ )
+ converted = convert_additive_to_multiplicative(raw_df, df)
+ if converted is not None:
+ converted.attrs = {
+ **getattr(df, "attrs", {}),
+ "adjustment": "split_dividend",
+ }
+ result[code] = converted
+ continue
if df is not None and not df.empty:
result[code] = df
except Exception as exc:
@@ -110,6 +141,7 @@ class DataLoader:
def _request_page(
self, code: str, start_date: str, end_date: str,
+ forward_adjusted: bool = True,
) -> Optional[pd.DataFrame]:
"""Fetch up to `_PAGE_SIZE` bars in [start_date, end_date]."""
if not _is_a_share(code) and not _is_hk_equity(code):
@@ -131,7 +163,8 @@ class DataLoader:
url = (
f"{_BASE_URL}?param={tencent_code},day,"
- f"{start_date},{end_date},{_PAGE_SIZE},qfq"
+ f"{start_date},{end_date},{_PAGE_SIZE}"
+ f"{',qfq' if forward_adjusted else ''}"
)
req = urllib.request.Request(url, headers={
@@ -158,7 +191,10 @@ class DataLoader:
return None
# Try "day" first, then "qfqday" (forward-adjusted)
- klines = stock_data[stock_key].get("qfqday") or stock_data[stock_key].get("day")
+ if forward_adjusted:
+ klines = stock_data[stock_key].get("qfqday") or stock_data[stock_key].get("day")
+ else:
+ klines = stock_data[stock_key].get("day")
if not klines:
return None
@@ -188,6 +224,7 @@ class DataLoader:
def _fetch_one(
self, code: str, start_date: str, end_date: str,
+ forward_adjusted: bool = True,
) -> Optional[pd.DataFrame]:
"""Paginate [start_date, end_date] in `_PAGE_SIZE`-bar windows.
@@ -222,7 +259,9 @@ class DataLoader:
last_error: Optional[Exception] = None
for attempt in range(_PAGE_RETRIES):
try:
- page = self._request_page(code, start_date, cursor_end)
+ page = self._request_page(
+ code, start_date, cursor_end, forward_adjusted
+ )
last_error = None
break
except Exception as exc: # noqa: BLE001 - transient network jitter
@@ -247,7 +286,9 @@ class DataLoader:
# empty and terminates the walk.
time.sleep(_PAGE_BACKOFF)
try:
- page = self._request_page(code, start_date, cursor_end)
+ page = self._request_page(
+ code, start_date, cursor_end, forward_adjusted
+ )
except Exception as exc: # noqa: BLE001 - transient network jitter
raise ValueError(
f"incomplete tencent history: {code} re-request at "
diff --git a/agent/src/market_data.py b/agent/src/market_data.py
index a60c21eae..6cdd479c0 100644
--- a/agent/src/market_data.py
+++ b/agent/src/market_data.py
@@ -382,6 +382,12 @@ def fetch_market_data(
currency_conversion = frame_attrs.get("currency_conversion")
if not isinstance(currency_conversion, str) or not currency_conversion:
currency_conversion = "none"
+ # A loader that converted its series reports the caliber it actually
+ # served on the frame; the static (source, market) table is the
+ # fallback for unconverted data (#1541).
+ adjustment = frame_attrs.get("adjustment")
+ if not isinstance(adjustment, str) or not adjustment:
+ adjustment = price_caliber(used_source or src, market, symbol)
entry: dict[str, Any] = {
"source": used_source or src,
"requested_source": source,
@@ -389,7 +395,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, symbol),
+ "adjustment": adjustment,
}
quote_currency = frame_attrs.get("quote_currency")
if isinstance(quote_currency, str) and quote_currency:
diff --git a/agent/tests/test_additive_conversion.py b/agent/tests/test_additive_conversion.py
new file mode 100644
index 000000000..857b90772
--- /dev/null
+++ b/agent/tests/test_additive_conversion.py
@@ -0,0 +1,237 @@
+"""Tests for the #1541 additive -> multiplicative caliber conversion.
+
+A-share qfq from Tencent/Eastmoney/AKShare is dividend-additive: returns
+computed on it are not total returns. The loaders now convert to the
+multiplicative convention when the raw companion supports it, and stamp the
+frame ``split_dividend`` so provenance stops warning.
+"""
+
+from __future__ import annotations
+
+import pandas as pd
+import pytest
+
+from backtest.loaders.additive_conversion import convert_additive_to_multiplicative
+
+
+def _frame(closes, volumes=None, start="2024-01-01"):
+ n = len(closes)
+ index = pd.date_range(start, periods=n, freq="B", name="trade_date")
+ volumes = volumes if volumes is not None else [1000.0] * n
+ return pd.DataFrame(
+ {
+ "open": closes,
+ "high": closes,
+ "low": closes,
+ "close": closes,
+ "volume": volumes,
+ },
+ index=index,
+ )
+
+
+def _two_action_series():
+ """Raw closes with two 2-per-share dividends at bar 3 and bar 6.
+
+ Trend rises 1 per bar; each ex-date drops the price by the dividend (2),
+ and the additive qfq subtracts each dividend from every earlier bar, so
+ its offsets are -4 / -2 / 0 in three plateaus.
+ """
+ raw = [100.0, 101.0, 102.0, 99.0, 100.0, 101.0, 98.0, 99.0]
+ additive = [96.0, 97.0, 98.0, 97.0, 98.0, 99.0, 98.0, 99.0]
+ return _frame(raw), _frame(additive)
+
+
+class TestConvertAdditiveToMultiplicative:
+ def test_two_dividends_convert_to_multiplicative(self):
+ raw, additive = _two_action_series()
+ converted = convert_additive_to_multiplicative(raw, additive)
+ assert converted is not None
+
+ # Factors: (102-2)/102 and (101-2)/101; anchored at the last bar.
+ f1 = 100.0 / 102.0
+ f2 = 99.0 / 101.0
+ expected = [
+ 100.0 * f1 * f2, 101.0 * f1 * f2, 102.0 * f1 * f2,
+ 99.0 * f2, 100.0 * f2, 101.0 * f2,
+ 98.0, 99.0,
+ ]
+ assert converted["close"].tolist() == pytest.approx(expected, rel=1e-9)
+
+ # The converted close-to-close return across each ex-date matches the
+ # (prev - dividend)/prev convention the reference series use, while
+ # the additive series distorts it.
+ r_before = raw["close"].iloc[2]
+ r_after = raw["close"].iloc[3]
+ c_before = converted["close"].iloc[2]
+ c_after = converted["close"].iloc[3]
+ conventional_return = r_after / (r_before - 2.0) - 1
+ assert c_after / c_before - 1 == pytest.approx(conventional_return, rel=1e-9)
+ assert additive["close"].iloc[3] / additive["close"].iloc[2] - 1 != pytest.approx(conventional_return, abs=1e-6)
+
+ def test_volume_scales_inversely_and_columns_untouched(self):
+ raw, additive = _two_action_series()
+ converted = convert_additive_to_multiplicative(raw, additive)
+ assert converted is not None
+ f1 = 100.0 / 102.0
+ f2 = 99.0 / 101.0
+ assert converted["volume"].iloc[-1] == 1000.0
+ assert converted["volume"].iloc[0] == pytest.approx(1000.0 / (f1 * f2), rel=1e-9)
+
+ def test_no_actions_is_identity_on_raw(self):
+ raw = _frame([10.0, 11.0, 12.0])
+ additive = _frame([10.0, 11.0, 12.0])
+ converted = convert_additive_to_multiplicative(raw, additive)
+ assert converted is not None
+ assert converted["close"].tolist() == raw["close"].tolist()
+
+ def test_misaligned_calendars_fail_closed(self):
+ raw, additive = _two_action_series()
+ converted = convert_additive_to_multiplicative(raw.iloc[:-1], additive)
+ assert converted is None
+
+ def test_non_plateau_offsets_fail_closed(self):
+ raw = _frame([100.0, 100.0, 100.0, 100.0])
+ additive = _frame([95.0, 95.5, 96.0, 96.5]) # drifts bar to bar
+ assert convert_additive_to_multiplicative(raw, additive) is None
+
+ def test_dividend_at_or_above_price_fails_closed(self):
+ raw = _frame([2.0, 2.0, 1.0, 1.0])
+ additive = _frame([0.5, 0.5, 1.0, 1.0]) # implies a 1.5 dividend on a 2.0 close? actually -0.5->? keep shape
+ # offsets: -1.5, -1.5, 0, 0 -> dividend 1.5 < 2.0, valid. Force invalid:
+ raw2 = _frame([1.0, 1.0, 0.5, 0.5])
+ additive2 = _frame([-0.6, -0.6, 0.5, 0.5]) # dividend 1.1 >= price 1.0
+ assert convert_additive_to_multiplicative(raw2, additive2) is None
+
+ def test_empty_or_missing_inputs_fail_closed(self):
+ raw, additive = _two_action_series()
+ assert convert_additive_to_multiplicative(None, additive) is None
+ assert convert_additive_to_multiplicative(raw, None) is None
+ assert convert_additive_to_multiplicative(raw.iloc[:0], additive) is None
+
+
+class TestLoaderWiring:
+ """Each stamped-additive loader converts and tags the frame when the raw
+ companion is consistent, and serves the additive frame untouched when it
+ is not."""
+
+ def _patch_fetch_pair(self, monkeypatch, loader_mod, raw, additive):
+ monkeypatch.setattr(
+ loader_mod, "cached_loader_fetch",
+ lambda source, symbol, timeframe, start_date, end_date, fields, fetch: (
+ raw if fields == ["raw"] else additive
+ ),
+ )
+
+ def test_tencent_converts_and_stamps(self, monkeypatch):
+ from backtest.loaders import tencent_loader
+
+ raw, additive = _two_action_series()
+ self._patch_fetch_pair(monkeypatch, tencent_loader, raw, additive)
+ out = tencent_loader.DataLoader().fetch(["600519.SH"], "2024-01-01", "2024-01-10")
+ df = out["600519.SH"]
+ assert df.attrs["adjustment"] == "split_dividend"
+ assert df["close"].iloc[-1] == pytest.approx(99.0, abs=1e-9)
+
+ def test_tencent_keeps_additive_when_raw_missing(self, monkeypatch):
+ from backtest.loaders import tencent_loader
+
+ _, additive = _two_action_series()
+ self._patch_fetch_pair(monkeypatch, tencent_loader, None, additive)
+ out = tencent_loader.DataLoader().fetch(["600519.SH"], "2024-01-01", "2024-01-10")
+ df = out["600519.SH"]
+ assert df["close"].tolist() == additive["close"].tolist()
+
+ def test_eastmoney_converts_and_stamps(self, monkeypatch):
+ from backtest.loaders import eastmoney_loader
+
+ raw, additive = _two_action_series()
+ self._patch_fetch_pair(monkeypatch, eastmoney_loader, raw, additive)
+ out = eastmoney_loader.DataLoader().fetch(["600519.SH"], "2024-01-01", "2024-01-10")
+ df = out["600519.SH"]
+ assert df.attrs["adjustment"] == "split_dividend"
+
+ def test_akshare_converts_and_stamps(self, monkeypatch):
+ from backtest.loaders import akshare_loader
+
+ raw, additive = _two_action_series()
+ self._patch_fetch_pair(monkeypatch, akshare_loader, raw, additive)
+ out = akshare_loader.DataLoader().fetch(["600519.SH"], "2024-01-01", "2024-01-10")
+ df = out["600519.SH"]
+ assert df.attrs["adjustment"] == "split_dividend"
+
+ def test_akshare_skips_etf_codes(self, monkeypatch):
+ from backtest.loaders import akshare_loader
+
+ _, additive = _two_action_series()
+ self._patch_fetch_pair(monkeypatch, akshare_loader, None, additive)
+ out = akshare_loader.DataLoader().fetch(["510300.SH"], "2024-01-01", "2024-01-10")
+ # ETF path is not converted; the additive frame passes through.
+ assert out["510300.SH"]["close"].tolist() == additive["close"].tolist()
+
+
+class TestProvenancePrefersFrameStamp:
+ def test_frame_adjustment_wins_over_static_table(self, monkeypatch):
+ from backtest.loaders import registry
+ from src.market_data import fetch_market_data
+
+ _, additive = _two_action_series()
+
+ class Serving:
+ name = "tencent"
+ markets = {"a_share"}
+ volume_units = {}
+
+ def is_available(self):
+ return True
+
+ def fetch(self, codes, start, end, interval="1D"):
+ df = additive.copy()
+ df.attrs = {"adjustment": "split_dividend"}
+ return {code: df for code in codes}
+
+ monkeypatch.setattr(registry, "_ensure_registered", lambda: None)
+ monkeypatch.setattr(registry, "LOADER_REGISTRY", {"tencent": Serving})
+ monkeypatch.setattr(registry, "FALLBACK_CHAINS", {"a_share": ["tencent"]})
+ out = fetch_market_data(
+ codes=["600519.SH"],
+ start_date="2024-01-01",
+ end_date="2024-01-10",
+ source="tencent",
+ include_provenance=True,
+ )
+ # The static table says tencent a_share is additive; the frame stamp
+ # from the converted loader must win.
+ assert out["_provenance"]["600519.SH"]["adjustment"] == "split_dividend"
+
+
+class TestRawFetchFailureDegrades:
+ def test_tencent_raw_failure_serves_additive(self, monkeypatch):
+ from backtest.loaders import tencent_loader
+
+ _, additive = _two_action_series()
+
+ def flaky(source, symbol, timeframe, start_date, end_date, fields, fetch):
+ if fields == ["raw"]:
+ raise ConnectionError("throttled")
+ return additive
+
+ monkeypatch.setattr(tencent_loader, "cached_loader_fetch", flaky)
+ out = tencent_loader.DataLoader().fetch(["600519.SH"], "2024-01-01", "2024-01-10")
+ df = out["600519.SH"]
+ assert df["close"].tolist() == additive["close"].tolist()
+
+ def test_akshare_raw_failure_serves_additive(self, monkeypatch):
+ from backtest.loaders import akshare_loader
+
+ _, additive = _two_action_series()
+
+ def flaky(source, symbol, timeframe, start_date, end_date, fields, fetch):
+ if fields == ["raw"]:
+ raise ConnectionError("throttled")
+ return additive
+
+ monkeypatch.setattr(akshare_loader, "cached_loader_fetch", flaky)
+ out = akshare_loader.DataLoader().fetch(["600519.SH"], "2024-01-01", "2024-01-10")
+ df = out["600519.SH"]
+ assert df["close"].tolist() == additive["close"].tolist()
diff --git a/agent/tests/test_eastmoney_loader.py b/agent/tests/test_eastmoney_loader.py
index 5c61efcc6..fafd9db0f 100644
--- a/agent/tests/test_eastmoney_loader.py
+++ b/agent/tests/test_eastmoney_loader.py
@@ -84,8 +84,11 @@ class TestFetchWithMockedClient:
["600519.SH"], "2024-01-01", "2024-01-31", interval="1D"
)
- resolve.assert_called_once_with("600519.SH")
- # 1D -> klt 101, compact dates passed through.
+ assert resolve.call_count == 2
+ resolve.assert_any_call("600519.SH")
+ # 1D -> klt 101, compact dates passed through. The A-share fetch now
+ # also pulls the fqt=0 companion for the #1541 conversion.
+ assert fetch_kline.call_count == 2
_, kwargs = fetch_kline.call_args
assert kwargs["klt"] == eastmoney_client.KLT_BY_INTERVAL["1D"]
assert kwargs["beg"] == "20240101"
@@ -180,7 +183,7 @@ class TestFetchEndToEndHttpMocked:
) as http:
out = loader.fetch(["600519.SH"], "2024-01-01", "2024-01-31")
- http.assert_called_once()
+ assert http.call_count == 2
_, kwargs = http.call_args
assert kwargs["params"]["secid"] == "1.600519"
assert kwargs["host_key"] == "eastmoney"
diff --git a/agent/tests/test_tencent_loader.py b/agent/tests/test_tencent_loader.py
index 4e34e73d3..3231a14ca 100644
--- a/agent/tests/test_tencent_loader.py
+++ b/agent/tests/test_tencent_loader.py
@@ -149,7 +149,7 @@ def _last500_api(history: pd.DataFrame, requested_ends: list[str] | None = None)
opposite first-500 assumption, which is why tail truncation passed CI.
"""
- def fake_page(code, start, end): # noqa: ANN001
+ def fake_page(code, start, end, forward_adjusted=True): # noqa: ANN001
if requested_ends is not None:
requested_ends.append(end)
window = history.loc[
@@ -207,7 +207,7 @@ def test_overlapping_pages_are_deduplicated(monkeypatch) -> None:
history = _history("2020-01-01", tencent_loader._PAGE_SIZE)
calls = {"n": 0}
- def fake_page(code, start, end): # noqa: ANN001
+ def fake_page(code, start, end, forward_adjusted=True): # noqa: ANN001
calls["n"] += 1
if calls["n"] == 1:
return history.iloc[-tencent_loader._PAGE_SIZE:]
@@ -240,7 +240,7 @@ def test_a_failed_page_raises_instead_of_returning_partial_history(
calls = {"n": 0}
sleeps: list[float] = []
- def fake_page(code, start, end): # noqa: ANN001
+ def fake_page(code, start, end, forward_adjusted=True): # noqa: ANN001
calls["n"] += 1
if calls["n"] == 1:
return history.iloc[-tencent_loader._PAGE_SIZE:]
@@ -304,7 +304,7 @@ def test_midwalk_empty_page_is_rerequested_before_truncating(monkeypatch) -> Non
sleeps: list[float] = []
monkeypatch.setattr(tencent_loader.time, "sleep", sleeps.append)
- def fake_page(code, start, end): # noqa: ANN001
+ def fake_page(code, start, end, forward_adjusted=True): # noqa: ANN001
calls["n"] += 1
if calls["n"] == 1:
return history.iloc[-tencent_loader._PAGE_SIZE:]
@@ -329,7 +329,7 @@ def test_midwalk_empty_page_that_persists_ends_the_walk(monkeypatch) -> None:
sleeps: list[float] = []
monkeypatch.setattr(tencent_loader.time, "sleep", sleeps.append)
- def fake_page(code, start, end): # noqa: ANN001
+ def fake_page(code, start, end, forward_adjusted=True): # noqa: ANN001
calls["n"] += 1
if calls["n"] == 1:
return history.iloc[-tencent_loader._PAGE_SIZE:]