Skip to content

"Vibe-Trading: Your Personal Trading Agent"

feat(data): gildata serves HK equities and CN indices via internal-code recall

sambazhu20fc8541bbae
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 0b5f84627..0a12d9f02 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -7,6 +7,21 @@ This project adheres to [Semantic Versioning](https://semver.org/).
### Added
+- **Gildata (恒生聚源) now serves HK equities and CN indices too.** HK symbols
+ (`00700.HK`) route to `HKStockDailyQuotes`, CN index codes (`000300.SH`,
+ `399xxx.SZ` — exchange-specific, so `000001.SZ` stays Ping An Bank) route to
+ `IndexDailyQuote`; both tools take a 聚源内码, resolved through the
+ `ParamCandidateRecall` meta-tool with an exact `ref_code` match and cached
+ to `~/.vibe-trading/cache/gildata-codes.json` (one round-trip per symbol
+ per machine). HK bars are raw traded prices — stamped `raw` as a per-market
+ exception after measuring 00700.HK (the vendor's befadj/aftadj closes are
+ separate fields and ignored); HK volume already arrives in shares. The
+ loader joins the `hk_equity` chain's tail. US equities are deliberately
+ excluded: on this token `USStockDailyQuotes` history is sparse before ~2022
+ (12 rows for 2020, 134 for 2021, complete only from 2023), which would
+ silently corrupt backtests — revisit once the vendor confirms deeper US
+ entitlement.
+
- **Gildata (恒生聚源) joins the A-share fallback chain as a token-gated
source**. The new `gildata` loader talks to the vendor's raw-api MCP
endpoint (one JSON-RPC POST per call, token on the URL query string) and
diff --git a/README.md b/README.md
index 56e48fdcd..442cbf106 100644
--- a/README.md
+++ b/README.md
@@ -456,7 +456,7 @@ One `get_market_data` call, **28 market-data sources**, one of them the optional
| `eastmoney` | A / US / HK | none | OHLCV + deep fundamentals & flow tools (throttled) |
| `baostock` · `akshare` | A (+ US/HK/futures/macro/fx) | none | free fallbacks |
| `tushare` | A / HK / futures / fund / macro | token | richest A-share |
-| `gildata` | A-share | token (Settings / `GILDATA_TOKEN`) | Hundsun Juyuan (恒生聚源) commercial feed — forward-adjusted dailies, joins the A-share chain tail |
+| `gildata` | A-share / HK / CN indices | token (Settings / `GILDATA_TOKEN`) | Hundsun Juyuan (恒生聚源) commercial feed — 前复权 A-share dailies, raw HK bars; tail of the a_share & HK chains |
| `yahoo` | US / HK / Canada / UK | none | direct chart/quotes/options; TSX `.TO` / TSXV `.V`; LSE `.L` with declared-currency normalization |
| `sina` · `stooq` | US | none | K-line to 1984 · EOD CSV |
| `yfinance` | US / HK / Canada / UK | none | wrapper; TSX `.TO` / TSXV `.V`; LSE `.L` under the same GBP/GBp contract |
@@ -476,7 +476,7 @@ One `get_market_data` call, **28 market-data sources**, one of them the optional
- **A-share** → `tencent` · `mootdx` · `eastmoney` · `baostock` · `akshare` · `tushare` · `gildata` · `local`
- **US** → `yahoo` · `stooq` · `sina` · `eastmoney` · `yfinance` · `tiingo` · `fmp` · `finnhub` · `alphavantage` · `longbridge` · `akshare` · `local`
-- **HK** → `tencent` · `eastmoney` · `yahoo` · `futu` · `akshare` · `yfinance` · `tushare` · `longbridge` · `local`
+- **HK** → `tencent` · `eastmoney` · `yahoo` · `futu` · `akshare` · `yfinance` · `tushare` · `longbridge` · `gildata` · `local`
- **India (NSE/BSE)** → `yahoo` · `yfinance` · `india_broker` · `local`
- **Korea (KOSPI/KOSDAQ)** → `pykrx` · `yahoo` · `yfinance` · `local`
- **UK (LSE)** → `yahoo` · `yfinance` · `local` *(declared GBP/GBp quotes only)*
diff --git a/README_ar.md b/README_ar.md
index 15f412ac7..72e6c1482 100644
--- a/README_ar.md
+++ b/README_ar.md
@@ -419,7 +419,7 @@ vibe-trading connector install /tmp/my-broker
| `eastmoney` | A / US / HK | none | OHLCV + deep fundamentals & flow tools (throttled) |
| `baostock` · `akshare` | A (+ US/HK/futures/macro/fx) | none | free fallbacks |
| `tushare` | A / HK / futures / fund / macro | token | richest A-share |
-| `gildata` | A-share | token (Settings / `GILDATA_TOKEN`) | Hundsun Juyuan (恒生聚源) commercial feed — forward-adjusted dailies, joins the A-share chain tail |
+| `gildata` | A-share / HK / CN indices | token (Settings / `GILDATA_TOKEN`) | Hundsun Juyuan (恒生聚源) commercial feed — 前复权 A-share dailies, raw HK bars; tail of the a_share & HK chains |
| `yahoo` | US / HK / كندا / المملكة المتحدة | none | direct chart/quotes/options؛ TSX `.TO` / TSXV `.V`؛ تطبيع LSE `.L` حسب العملة المعلنة |
| `sina` · `stooq` | US | none | K-line to 1984 · EOD CSV |
| `yfinance` | US / HK / كندا / المملكة المتحدة | none | wrapper؛ TSX `.TO` / TSXV `.V`؛ يلتزم LSE `.L` بعقد GBP/GBp نفسه |
@@ -439,7 +439,7 @@ vibe-trading connector install /tmp/my-broker
- **أسهم A** → `tencent` · `mootdx` · `eastmoney` · `baostock` · `akshare` · `tushare` · `gildata` · `local`
- **أسهم US** → `yahoo` · `stooq` · `sina` · `eastmoney` · `yfinance` · `tiingo` · `fmp` · `finnhub` · `alphavantage` · `longbridge` · `akshare` · `local`
-- **أسهم HK** → `tencent` · `eastmoney` · `yahoo` · `futu` · `akshare` · `yfinance` · `tushare` · `longbridge` · `local`
+- **أسهم HK** → `tencent` · `eastmoney` · `yahoo` · `futu` · `akshare` · `yfinance` · `tushare` · `longbridge` · `gildata` · `local`
- **أسهم الهند (NSE/BSE)** → `yahoo` · `yfinance` · `india_broker` · `local`
- **كوريا (KOSPI/KOSDAQ)** → `pykrx` · `yahoo` · `yfinance` · `local`
- **المملكة المتحدة (LSE)** → `yahoo` · `yfinance` · `local` *(الأسعار المعلنة بـ GBP/GBp فقط)*
diff --git a/README_es.md b/README_es.md
index 2d7df78e7..3914aa713 100644
--- a/README_es.md
+++ b/README_es.md
@@ -418,7 +418,7 @@ Una sola llamada `get_market_data`, **28 fuentes de datos de mercado**, una de e
| `eastmoney` | A / EE. UU. / HK | ninguna | OHLCV + herramientas de fundamentales y flujo profundas (limitada) |
| `baostock` · `akshare` | A (+ EE. UU./HK/futuros/macro/fx) | ninguna | fallbacks gratuitos |
| `tushare` | A / HK / futuros / fondos / macro | token | la más completa para A-share |
-| `gildata` | acciones A | token (Configuración / `GILDATA_TOKEN`) | fuente comercial Hundsun Juyuan (恒生聚源) — diarios ajustados hacia adelante, se une al final de la cadena de acciones A |
+| `gildata` | acciones A / HK / índices CN | token (Configuración / `GILDATA_TOKEN`) | fuente comercial Hundsun Juyuan (恒生聚源) — diarios de acciones A ajustados, HK en bruto; final de las cadenas a_share y HK |
| `yahoo` | EE. UU. / HK / Canadá / Reino Unido | ninguna | gráfico/cotizaciones/opciones directos; TSX `.TO` / TSXV `.V`; LSE `.L` con normalización según la divisa declarada |
| `sina` · `stooq` | EE. UU. | ninguna | velas hasta 1984 · CSV EOD |
| `yfinance` | EE. UU. / HK / Canadá / Reino Unido | ninguna | wrapper; TSX `.TO` / TSXV `.V`; LSE `.L` bajo el mismo contrato GBP/GBp |
@@ -438,7 +438,7 @@ Una sola llamada `get_market_data`, **28 fuentes de datos de mercado**, una de e
- **A-share** → `tencent` · `mootdx` · `eastmoney` · `baostock` · `akshare` · `tushare` · `gildata` · `local`
- **EE. UU.** → `yahoo` · `stooq` · `sina` · `eastmoney` · `yfinance` · `tiingo` · `fmp` · `finnhub` · `alphavantage` · `longbridge` · `akshare` · `local`
-- **HK** → `tencent` · `eastmoney` · `yahoo` · `futu` · `akshare` · `yfinance` · `tushare` · `longbridge` · `local`
+- **HK** → `tencent` · `eastmoney` · `yahoo` · `futu` · `akshare` · `yfinance` · `tushare` · `longbridge` · `gildata` · `local`
- **India (NSE/BSE)** → `yahoo` · `yfinance` · `india_broker` · `local`
- **Corea (KOSPI/KOSDAQ)** → `pykrx` · `yahoo` · `yfinance` · `local`
- **Reino Unido (LSE)** → `yahoo` · `yfinance` · `local` *(solo cotizaciones declaradas en GBP/GBp)*
diff --git a/README_ja.md b/README_ja.md
index 93c56921d..78d3d7952 100644
--- a/README_ja.md
+++ b/README_ja.md
@@ -419,7 +419,7 @@ vibe-trading connector install /tmp/my-broker
| `eastmoney` | A / US / HK | none | OHLCV + deep fundamentals & flow tools (throttled) |
| `baostock` · `akshare` | A (+ US/HK/futures/macro/fx) | none | free fallbacks |
| `tushare` | A / HK / futures / fund / macro | token | richest A-share |
-| `gildata` | A-share | token (Settings / `GILDATA_TOKEN`) | Hundsun Juyuan (恒生聚源) commercial feed — forward-adjusted dailies, joins the A-share chain tail |
+| `gildata` | A-share / HK / CN indices | token (Settings / `GILDATA_TOKEN`) | Hundsun Juyuan (恒生聚源) commercial feed — 前复权 A-share dailies, raw HK bars; tail of the a_share & HK chains |
| `yahoo` | US / HK / カナダ / 英国 | none | direct chart/quotes/options;TSX `.TO` / TSXV `.V`;LSE `.L` は明示通貨で正規化 |
| `sina` · `stooq` | US | none | K-line to 1984 · EOD CSV |
| `yfinance` | US / HK / カナダ / 英国 | none | wrapper;TSX `.TO` / TSXV `.V`;LSE `.L` は同じ GBP/GBp 契約に従う |
@@ -439,7 +439,7 @@ vibe-trading connector install /tmp/my-broker
- **A 株** → `tencent` · `mootdx` · `eastmoney` · `baostock` · `akshare` · `tushare` · `gildata` · `local`
- **米国株** → `yahoo` · `stooq` · `sina` · `eastmoney` · `yfinance` · `tiingo` · `fmp` · `finnhub` · `alphavantage` · `longbridge` · `akshare` · `local`
-- **香港株** → `tencent` · `eastmoney` · `yahoo` · `futu` · `akshare` · `yfinance` · `tushare` · `longbridge` · `local`
+- **香港株** → `tencent` · `eastmoney` · `yahoo` · `futu` · `akshare` · `yfinance` · `tushare` · `longbridge` · `gildata` · `local`
- **インド株(NSE/BSE)** → `yahoo` · `yfinance` · `india_broker` · `local`
- **韓国(KOSPI/KOSDAQ)** → `pykrx` · `yahoo` · `yfinance` · `local`
- **英国(LSE)** → `yahoo` · `yfinance` · `local` *(GBP/GBp と明示されたクォートのみ)*
diff --git a/README_ko.md b/README_ko.md
index 669dcb6e3..2625677a9 100644
--- a/README_ko.md
+++ b/README_ko.md
@@ -419,7 +419,7 @@ vibe-trading connector install /tmp/my-broker
| `eastmoney` | A / US / HK | none | OHLCV + deep fundamentals & flow tools (throttled) |
| `baostock` · `akshare` | A (+ US/HK/futures/macro/fx) | none | free fallbacks |
| `tushare` | A / HK / futures / fund / macro | token | richest A-share |
-| `gildata` | A-share | token (Settings / `GILDATA_TOKEN`) | Hundsun Juyuan (恒生聚源) commercial feed — forward-adjusted dailies, joins the A-share chain tail |
+| `gildata` | A-share / HK / CN indices | token (Settings / `GILDATA_TOKEN`) | Hundsun Juyuan (恒生聚源) commercial feed — 前复权 A-share dailies, raw HK bars; tail of the a_share & HK chains |
| `yahoo` | US / HK / 캐나다 / 영국 | none | direct chart/quotes/options; TSX `.TO` / TSXV `.V`; LSE `.L`은 명시 통화로 정규화 |
| `sina` · `stooq` | US | none | K-line to 1984 · EOD CSV |
| `yfinance` | US / HK / 캐나다 / 영국 | none | wrapper; TSX `.TO` / TSXV `.V`; LSE `.L`은 동일한 GBP/GBp 계약 준수 |
@@ -439,7 +439,7 @@ vibe-trading connector install /tmp/my-broker
- **A주** → `tencent` · `mootdx` · `eastmoney` · `baostock` · `akshare` · `tushare` · `gildata` · `local`
- **미국** → `yahoo` · `stooq` · `sina` · `eastmoney` · `yfinance` · `tiingo` · `fmp` · `finnhub` · `alphavantage` · `longbridge` · `akshare` · `local`
-- **홍콩** → `tencent` · `eastmoney` · `yahoo` · `futu` · `akshare` · `yfinance` · `tushare` · `longbridge` · `local`
+- **홍콩** → `tencent` · `eastmoney` · `yahoo` · `futu` · `akshare` · `yfinance` · `tushare` · `longbridge` · `gildata` · `local`
- **인도 (NSE/BSE)** → `yahoo` · `yfinance` · `india_broker` · `local`
- **한국 (KOSPI/KOSDAQ)** → `pykrx` · `yahoo` · `yfinance` · `local`
- **영국 (LSE)** → `yahoo` · `yfinance` · `local` *(명시된 GBP/GBp 호가만 허용)*
diff --git a/README_zh.md b/README_zh.md
index 545d907ce..ee61d7a13 100644
--- a/README_zh.md
+++ b/README_zh.md
@@ -434,7 +434,7 @@ vibe-trading connector install /tmp/my-broker
| `eastmoney` | A / US / HK | none | OHLCV + deep fundamentals & flow tools (throttled) |
| `baostock` · `akshare` | A (+ US/HK/futures/macro/fx) | none | free fallbacks |
| `tushare` | A / HK / futures / fund / macro | token | richest A-share |
-| `gildata` | A-share | token (Settings / `GILDATA_TOKEN`) | Hundsun Juyuan (恒生聚源) commercial feed — forward-adjusted dailies, joins the A-share chain tail |
+| `gildata` | A-share / HK / CN indices | token (Settings / `GILDATA_TOKEN`) | Hundsun Juyuan (恒生聚源) commercial feed — 前复权 A-share dailies, raw HK bars; tail of the a_share & HK chains |
| `yahoo` | 美股 / 港股 / 加拿大 / 英国 | none | direct chart/quotes/options;TSX `.TO` / TSXV `.V`;LSE `.L` 按明示币种归一化 |
| `sina` · `stooq` | 美股 | none | K-line to 1984 · EOD CSV |
| `yfinance` | 美股 / 港股 / 加拿大 / 英国 | none | wrapper;TSX `.TO` / TSXV `.V`;LSE `.L` 遵守同一 GBP/GBp 合同 |
@@ -454,7 +454,7 @@ vibe-trading connector install /tmp/my-broker
- **A股** → `tencent` · `mootdx` · `eastmoney` · `baostock` · `akshare` · `tushare` · `gildata` · `local`
- **美股** → `yahoo` · `stooq` · `sina` · `eastmoney` · `yfinance` · `tiingo` · `fmp` · `finnhub` · `alphavantage` · `longbridge` · `akshare` · `local`
-- **港股** → `tencent` · `eastmoney` · `yahoo` · `futu` · `akshare` · `yfinance` · `tushare` · `longbridge` · `local`
+- **港股** → `tencent` · `eastmoney` · `yahoo` · `futu` · `akshare` · `yfinance` · `tushare` · `longbridge` · `gildata` · `local`
- **印度(NSE/BSE)** → `yahoo` · `yfinance` · `india_broker` · `local`
- **韩国(KOSPI/KOSDAQ)** → `pykrx` · `yahoo` · `yfinance` · `local`
- **英国(LSE)** → `yahoo` · `yfinance` · `local` *(仅接受明示为 GBP/GBp 的报价)*
diff --git a/agent/.env.example b/agent/.env.example
index bcf59e4c3..707d62288 100644
--- a/agent/.env.example
+++ b/agent/.env.example
@@ -211,7 +211,7 @@ TUSHARE_TOKEN=your-tushare-token
# ALPHAVANTAGE_API_KEY=xxx # optional US-equity OHLCV fallback provider (selectable get_market_data source)
# TIINGO_API_KEY=xxx # optional US-equity OHLCV fallback provider (selectable get_market_data source)
# FMP_API_KEY=xxx # optional US-equity OHLCV fallback provider (selectable get_market_data source)
-# GILDATA_TOKEN=xxx # optional Gildata (恒生聚源) A-share OHLCV fallback provider — commercial token via [email protected]; joins the a_share chain's tail when set (selectable get_market_data source)
+# GILDATA_TOKEN=xxx # optional Gildata (恒生聚源) A-share/HK/CN-index OHLCV fallback provider — commercial token via [email protected]; joins the a_share and hk_equity chains' tails when set (selectable get_market_data source)
# GILDATA_BASE_URL=xxx # optional endpoint override (defaults to the vendor's raw-api MCP endpoint)
# TICKERALL_API_KEY=xxx # optional hosted MetaTrader 5 forex/metals feed — EXPLICIT source="tickerall" only, never an automatic fallback (no local terminal; https://tickerall.com)
# TICKERALL_ACCOUNT_ID=xxx # required alongside the key: history is served per connected broker account (read-only)
diff --git a/agent/backtest/loaders/gildata_codes.py b/agent/backtest/loaders/gildata_codes.py
new file mode 100644
index 000000000..35f5d84e3
--- /dev/null
+++ b/agent/backtest/loaders/gildata_codes.py
@@ -0,0 +1,183 @@
+"""Gildata internal-code resolution: recall + exact ref_code match + disk cache.
+
+The vendor's non-A-share quote tools take a 聚源内码 (internal numeric code)
+rather than a market symbol, so every HK / CN-index request must first resolve
+its symbol through the ``ParamCandidateRecall`` meta-tool. That tool answers a
+fuzzy candidate list; this module turns it into a deterministic mapping:
+
+* the candidate whose ``ref_code`` equals the project symbol (normalized,
+ ``.SS`` → ``.SH``) is the match — a fuzzy ``caption`` is never trusted;
+* successful mappings persist to ``~/.vibe-trading/cache/gildata-codes.json``
+ so a symbol costs one recall round-trip per machine, not per fetch;
+* an unresolved symbol returns ``None`` so the loader drops it and the
+ fallback chain serves it from another source.
+
+The MCP transport is injected (``call_tool``) so this module stays free of
+loader imports and unit tests can stub the network entirely.
+"""
+
+from __future__ import annotations
+
+import json
+import logging
+import threading
+import time
+from pathlib import Path
+from typing import Any, Callable, Dict, Optional
+
+logger = logging.getLogger(__name__)
+
+# recall param -> tool wiring, keyed by the resolution kind.
+_RECALL_WIRING: Dict[str, tuple[str, str]] = {
+ # kind: (api_name, param_name the internal code belongs to)
+ "hk_equity": ("HKStockDailyQuotes", "stockObject"),
+ "cn_index": ("IndexDailyQuote", "indexObject"),
+}
+
+_CACHE_FILENAME = "gildata-codes.json"
+_CACHE_MAX_AGE_S = 90 * 24 * 3600 # stale-proof: re-resolve quarterly
+
+_cache_lock = threading.Lock()
+_cache_memo: Dict[str, Dict[str, Any]] = {}
+
+
+def _normalize_ref(code: str) -> str:
+ """Upper-case and normalize the .SS variant onto .SH for ref matching."""
+ upper = code.strip().upper()
+ if upper.endswith(".SS"):
+ upper = upper[: -len(".SS")] + ".SH"
+ return upper
+
+
+def _cache_path() -> Path:
+ return Path.home() / ".vibe-trading" / "cache" / _CACHE_FILENAME
+
+
+def _load_cache() -> Dict[str, Any]:
+ """Read the on-disk code map; any failure yields an empty mapping."""
+ try:
+ raw = _cache_path().read_text(encoding="utf-8")
+ data = json.loads(raw)
+ return data if isinstance(data, dict) else {}
+ except Exception:
+ return {}
+
+
+def _save_cache(data: Dict[str, Any]) -> None:
+ """Persist the code map best-effort; failures only cost a re-resolve."""
+ try:
+ _cache_path().parent.mkdir(parents=True, exist_ok=True)
+ _cache_path().write_text(
+ json.dumps(data, ensure_ascii=False, indent=1), encoding="utf-8"
+ )
+ except Exception as exc:
+ logger.debug("gildata code cache write failed: %s", exc)
+
+
+def _cached(vendor_code: Optional[str]) -> Dict[str, Any]:
+ return {
+ "code": vendor_code,
+ "resolved_at": time.time(),
+ }
+
+
+def resolve_vendor_code(
+ kind: str,
+ symbol: str,
+ call_tool: Callable[[str, Dict[str, Any]], list],
+) -> Optional[str]:
+ """Resolve a project symbol to its Gildata internal code for ``kind``.
+
+ Args:
+ kind: Resolution wiring key — ``"hk_equity"`` or ``"cn_index"``.
+ symbol: Project symbol, e.g. ``00700.HK`` or ``000300.SH``.
+ call_tool: MCP transport — ``call_tool("ParamCandidateRecall", args)``
+ must return the recall result's ``candidates`` list.
+
+ Returns:
+ The vendor internal code (e.g. ``"1000546"``), or ``None`` when the
+ recall answers no candidate whose ``ref_code`` matches exactly.
+ """
+ wiring = _RECALL_WIRING.get(kind)
+ if wiring is None:
+ return None
+ api_name, param_name = wiring
+ want_ref = _normalize_ref(symbol)
+
+ with _cache_lock:
+ entry = _cache_memo.get(want_ref)
+ if (
+ entry
+ and entry.get("kind") == kind
+ and time.time() - entry.get("resolved_at", 0) < _CACHE_MAX_AGE_S
+ ):
+ return entry.get("code")
+
+ # Disk cache first (cross-process), then the live recall.
+ disk = _load_cache()
+ entry = disk.get(want_ref)
+ if (
+ isinstance(entry, dict)
+ and entry.get("kind") == kind
+ and time.time() - entry.get("resolved_at", 0) < _CACHE_MAX_AGE_S
+ ):
+ code = entry.get("code")
+ with _cache_lock:
+ _cache_memo[want_ref] = {**entry, "kind": kind}
+ return code
+
+ try:
+ arguments = {
+ "recall_list": [
+ {
+ "api_name": api_name,
+ "keyword": symbol,
+ "param_name": param_name,
+ "top_k": 20,
+ }
+ ]
+ }
+ # The recall tool's result puts candidates under results[0]; the
+ # injected transport may hand back either that unwrapped entry (the
+ # loader's _call_tool does) or the raw recall body — accept both.
+ candidates = call_tool("ParamCandidateRecall", arguments)
+ if isinstance(candidates, dict):
+ if isinstance(candidates.get("candidates"), list):
+ candidates = candidates["candidates"]
+ else:
+ results = candidates.get("results") or []
+ candidates = (
+ results[0].get("candidates", []) if results else []
+ )
+ except Exception as exc:
+ logger.warning("gildata code recall failed for %s: %s", symbol, exc)
+ return None
+ if not isinstance(candidates, list):
+ return None
+
+ hit = next(
+ (c for c in candidates if isinstance(c, dict)
+ and _normalize_ref(str(c.get("ref_code", ""))) == want_ref),
+ None,
+ )
+ if hit is None or not str(hit.get("code", "")).strip():
+ logger.warning(
+ "gildata recall found no exact ref_code match for %s (got %s refs)",
+ symbol,
+ [c.get("ref_code") for c in candidates[:5]],
+ )
+ return None
+
+ vendor_code = str(hit["code"]).strip()
+ record = {"kind": kind, **_cached(vendor_code)}
+ with _cache_lock:
+ _cache_memo[want_ref] = record
+ disk[want_ref] = record
+ _save_cache(disk)
+ return vendor_code
+
+
+def reset_code_cache() -> None:
+ """Clear the in-process memo (tests; the disk cache stays untouched)."""
+ with _cache_lock:
+ _cache_memo.clear()
diff --git a/agent/backtest/loaders/gildata_loader.py b/agent/backtest/loaders/gildata_loader.py
index abbb86353..63be80843 100644
--- a/agent/backtest/loaders/gildata_loader.py
+++ b/agent/backtest/loaders/gildata_loader.py
@@ -1,4 +1,4 @@
-"""Gildata (恒生聚源) loader: token-gated A-share OHLCV via the raw-api MCP endpoint.
+"""Gildata (恒生聚源) A-share/HK/CN-index OHLCV loader via the raw-api MCP endpoint.
Gildata serves its market data over MCP streamable-HTTP: every tool call is one
JSON-RPC POST to a single endpoint whose token rides the URL query string::
@@ -15,17 +15,27 @@ a non-zero inner ``code`` (e.g. 1001 on a bad token) is an error, while an
*unresolvable symbol silently yields ``rows: []``* — the loader must treat
that as "no data" so the fallback chain keeps walking, not as a hard failure.
-This loader uses the ``StockDailyQuote`` tool (A-share daily bars):
-
-* ``stockObject`` accepts project-style ``600519.SH`` codes directly (no
- internal-code recall needed for A-shares, unlike the HK/US/index tools);
-* ``restorationStatus=1`` selects 前复权 (forward, split-AND-dividend
- adjusted) — the same caliber tushare serves via ``adj_factor``;
-* one call covers the full requested window (verified against a 6.7-year
- range) and up to 50 symbols; this loader still fetches per symbol so each
- frame flows through the per-symbol loader cache;
-* ``turnovervolume`` is reported in 万股 (10k shares) and is converted to
- plain shares here — ``volume_units`` therefore declares ``"shares"``.
+Per-market wiring (each measured against live payloads):
+
+* A-shares → ``StockDailyQuote``. ``stockObject`` accepts project-style
+ ``600519.SH`` codes directly; ``restorationStatus=1`` selects 前复权
+ (forward, split-AND-dividend adjusted) — the tushare caliber;
+ ``turnovervolume`` is 万股 and is converted to shares.
+* HK equities (``00700.HK``) → ``HKStockDailyQuotes``. OHLC is unadjusted
+ (raw traded prices; the vendor exposes ``befadjcloseprice`` /
+ ``aftadjcloseprice`` as separate fields — ignored), volume already in
+ shares. Registered as a per-market caliber exception ``raw``.
+* CN indices (``000300.SH``, ``399006.SZ`` — index-shaped codes ride the
+ a_share market) → ``IndexDailyQuote``. Levels are raw by nature;
+ ``turnovervolume`` is 万股.
+
+HK/index tools take a 聚源内码 instead of a symbol; :mod:`gildata_codes`
+resolves those through the ``ParamCandidateRecall`` meta-tool with an exact
+``ref_code`` match and a disk cache, so a symbol costs one extra round-trip
+per machine. US equities are deliberately NOT served: this token's
+``USStockDailyQuotes`` history is sparse before ~2022 (12 rows for 2020,
+134 for 2021 — complete only from 2023), which would silently corrupt
+backtests; revisit once the vendor confirms deeper US entitlement.
Auth: set ``GILDATA_TOKEN`` in the environment (apply via the vendor's sales
channel, [email protected]). ``GILDATA_BASE_URL`` can override the endpoint.
@@ -35,12 +45,14 @@ from __future__ import annotations
import json
import logging
-from typing import Any, Dict, List, Optional
+import re
+from typing import Any, Callable, Dict, List, Optional
import pandas as pd
from backtest.loaders._http import resolve_min_interval, throttled_post_json
from backtest.loaders.base import cached_loader_fetch, validate_date_range
+from backtest.loaders.gildata_codes import resolve_vendor_code
from backtest.loaders.registry import register
logger = logging.getLogger(__name__)
@@ -68,6 +80,22 @@ _OHLCV_FIELDS = ("open", "high", "low", "close", "volume")
# 万股 (10k shares) -> shares.
_VOLUME_UNIT_MULTIPLIER = 10_000.0
+# CN index codes are exchange-specific: 000xxx.SH (上证指数系) and 399xxx.SZ
+# (深证指数系). 000001.SZ is 平安银行, 000001.SH is 上证指数 — the suffix
+# disambiguates, mirroring the tushare loader's index rule.
+_CN_INDEX_RE = re.compile(r"^(000\d{3})\.SH$|^(399\d{3})\.SZ$", re.I)
+_HK_EQUITY_RE = re.compile(r"^\d{4,5}\.HK$", re.I)
+
+
+def _is_cn_index(code: str) -> bool:
+ """Detect CN index symbols served by ``IndexDailyQuote``."""
+ return bool(_CN_INDEX_RE.match(code.strip()))
+
+
+def _is_hk_equity(code: str) -> bool:
+ """Detect HK equity symbols (e.g. ``00700.HK``)."""
+ return bool(_HK_EQUITY_RE.match(code.strip()))
+
def _token() -> str:
"""Return the configured Gildata token, stripped (``""`` if unset)."""
@@ -124,16 +152,17 @@ def _gildata_symbol(code: str) -> Optional[str]:
return None
-def _call_tool(tool: str, arguments: Dict[str, Any]) -> List[Dict[str, Any]]:
- """Invoke one MCP tool and return its ``rows``.
+def _call_tool(tool: str, arguments: Dict[str, Any]) -> Any:
+ """Invoke one MCP tool and return its first result entry.
Args:
tool: Tool name, e.g. ``"StockDailyQuote"``.
arguments: Structured tool arguments.
Returns:
- The ``rows`` list of the first result entry (empty when the vendor
- reports no data for the request).
+ The first ``results`` entry dict (``{"api_name", "columns", "rows",
+ ...}``) — quote tools carry ``rows`` there, the recall meta-tool
+ carries ``candidates``. Empty dict when the vendor reports no results.
Raises:
RuntimeError: If the token is missing, or the vendor answers with a
@@ -190,24 +219,44 @@ def _call_tool(tool: str, arguments: Dict[str, Any]) -> List[Dict[str, Any]]:
f"gildata tool {tool} returned error: {str(inner)[:200]}"
)
results = inner.get("results") or []
- if not results:
- return []
- rows = results[0].get("rows")
+ return results[0] if results else {}
+
+
+def _tool_rows(tool: str, arguments: Dict[str, Any]) -> List[Dict[str, Any]]:
+ """Invoke one quote tool and return its ``rows`` (empty on no data)."""
+ entry = _call_tool(tool, arguments)
+ rows = entry.get("rows")
return rows if isinstance(rows, list) else []
-def _parse_daily_rows(rows: List[Dict[str, Any]]) -> Optional[pd.DataFrame]:
- """Convert ``StockDailyQuote`` rows into an ascending OHLCV frame.
+def _recall_candidates(
+ tool: str, arguments: Dict[str, Any]
+) -> Dict[str, Any]:
+ """Transport adapter for :func:`gildata_codes.resolve_vendor_code`."""
+ return _call_tool(tool, arguments)
+
- Field names come from the tool's own ``columns`` map (``tradingday``,
- ``openprice``/``highprice``/``lowprice``/``closeprice``,
- ``turnovervolume`` in 万股). The ``avgprice``/``prevcloseprice`` fields
- are deliberately ignored: on adjusted series they stay on a different
- adjustment basis than OHLC (measured on 600519: qfq close 1491 vs
- avgprice 1666), so mixing them in would corrupt prices.
+def _parse_daily_rows(
+ rows: List[Dict[str, Any]],
+ *,
+ volume_scale: float = _VOLUME_UNIT_MULTIPLIER,
+) -> Optional[pd.DataFrame]:
+ """Convert vendor quote rows into an ascending OHLCV frame.
+
+ Field names come from the tools' own ``columns`` maps, which differ by
+ market: A-share/index tools use ``openprice``/``highprice``/``lowprice``/
+ ``closeprice``/``turnovervolume`` (万股), the HK tool uses bare
+ ``open``/``high``/``low``/``close``/``volume`` (already shares). Both
+ styles are accepted; ``volume_scale`` converts the reading to shares
+ (1.0 for HK). The ``avgprice``/``prevcloseprice`` and HK
+ ``befadjcloseprice``/``aftadjcloseprice`` fields are deliberately
+ ignored: on adjusted series they stay on a different adjustment basis
+ than the served OHLC (measured on 600519: qfq close 1491 vs avgprice
+ 1666), so mixing them in would corrupt prices.
Args:
- rows: Raw row dicts from :func:`_call_tool`.
+ rows: Raw row dicts from :func:`_tool_rows`.
+ volume_scale: Multiplier applied to the volume reading.
Returns:
DataFrame indexed by ``trade_date`` with float ``open/high/low/close/
@@ -219,14 +268,21 @@ def _parse_daily_rows(rows: List[Dict[str, Any]]) -> Optional[pd.DataFrame]:
def _date(row: Dict[str, Any]) -> Any:
return row.get("tradingday") or row.get("enddate")
+ def _first(row: Dict[str, Any], *keys: str) -> Any:
+ for key in keys:
+ value = row.get(key)
+ if value is not None:
+ return value
+ return None
+
records = [
{
"trade_date": _date(row),
- "open": row.get("openprice"),
- "high": row.get("highprice"),
- "low": row.get("lowprice"),
- "close": row.get("closeprice"),
- "volume": row.get("turnovervolume"),
+ "open": _first(row, "openprice", "open"),
+ "high": _first(row, "highprice", "high"),
+ "low": _first(row, "lowprice", "low"),
+ "close": _first(row, "closeprice", "close"),
+ "volume": _first(row, "turnovervolume", "volume"),
}
for row in rows
if isinstance(row, dict)
@@ -241,8 +297,8 @@ def _parse_daily_rows(rows: List[Dict[str, Any]]) -> Optional[pd.DataFrame]:
# Cast to float (not just to_numeric) so the float-OHLCV contract
# holds even when the vendor sends integers or numeric strings.
df[field] = pd.to_numeric(df[field], errors="coerce").astype(float)
- # 万股 -> shares; suspended days may arrive without a volume reading.
- df["volume"] = (df["volume"] * _VOLUME_UNIT_MULTIPLIER).fillna(0.0)
+ # Scale to shares; suspended days may arrive without a volume reading.
+ df["volume"] = (df["volume"] * volume_scale).fillna(0.0)
df = df.set_index("trade_date").sort_index()
df = df[list(_OHLCV_FIELDS)].dropna(subset=["open", "high", "low", "close"])
@@ -253,13 +309,14 @@ def _parse_daily_rows(rows: List[Dict[str, Any]]) -> Optional[pd.DataFrame]:
@register
class DataLoader:
- """Gildata A-share OHLCV loader (token-gated, MCP-over-HTTP)."""
+ """Gildata A-share/HK/CN-index OHLCV loader (token-gated, MCP-over-HTTP)."""
name = "gildata"
- markets = {"a_share"}
+ markets = {"a_share", "hk_equity"}
requires_auth = True
- # turnovervolume is converted from 万股 to shares before serving.
- volume_units = {"a_share": "shares"}
+ # A-share turnovervolume is converted from 万股 to shares before serving;
+ # HK volume already arrives in shares.
+ volume_units = {"a_share": "shares", "hk_equity": "shares"}
def __init__(self) -> None:
pass
@@ -279,12 +336,14 @@ class DataLoader:
) -> Dict[str, pd.DataFrame]:
"""Fetch daily OHLCV bars from Gildata, one symbol at a time.
- Non-A-share symbols are dropped (the fallback chain serves them from
- another source), and a single failing symbol is logged and skipped so
- it never aborts the rest of the batch.
+ Symbols route by shape: ``00700.HK`` → the HK tool, CN index codes
+ (``000300.SH``/``399006.SZ``) → the index tool, anything A-share-shaped
+ → the A-share tool. Unresolvable or unsupported symbols are dropped
+ (the fallback chain serves them from another source), and a single
+ failing symbol is logged and skipped so it never aborts the batch.
Args:
- codes: Project symbols (e.g. ``["600519.SH", "000001.SZ"]``).
+ codes: Project symbols (e.g. ``["600519.SH", "00700.HK"]``).
start_date: Inclusive start date, ``YYYY-MM-DD``.
end_date: Inclusive end date, ``YYYY-MM-DD``.
interval: Bar size; only ``"1D"`` is supported (others skipped).
@@ -308,13 +367,30 @@ class DataLoader:
logger.warning("gildata fetch skipped: %s not set", _TOKEN_ENV)
return {}
+ def _a_share_fetch(symbol: str) -> Callable[[], Optional[pd.DataFrame]]:
+ return lambda: self._fetch_one(symbol, start_date, end_date)
+
+ def _hk_fetch(code: str) -> Callable[[], Optional[pd.DataFrame]]:
+ return lambda: self._fetch_one_hk(code, start_date, end_date)
+
+ def _index_fetch(code: str) -> Callable[[], Optional[pd.DataFrame]]:
+ return lambda: self._fetch_one_index(code, start_date, end_date)
+
result: Dict[str, pd.DataFrame] = {}
for code in codes:
- symbol = _gildata_symbol(code)
- if symbol is None:
- logger.debug("gildata skipping non-A-share symbol %r", code)
- continue
try:
+ if _is_hk_equity(code):
+ fetch_one = _hk_fetch(code)
+ elif _is_cn_index(code):
+ fetch_one = _index_fetch(code)
+ else:
+ symbol = _gildata_symbol(code)
+ if symbol is None:
+ logger.debug(
+ "gildata skipping unsupported symbol %r", code
+ )
+ continue
+ fetch_one = _a_share_fetch(symbol)
df = cached_loader_fetch(
source=self.name,
symbol=code,
@@ -322,9 +398,7 @@ class DataLoader:
start_date=start_date,
end_date=end_date,
fields=None,
- fetch=lambda symbol=symbol: self._fetch_one(
- symbol, start_date, end_date
- ),
+ fetch=fetch_one,
)
if df is not None and not df.empty:
result[code] = df
@@ -338,7 +412,7 @@ class DataLoader:
start_date: str,
end_date: str,
) -> Optional[pd.DataFrame]:
- """Fetch and parse one symbol's daily bars; ``None`` on no data.
+ """Fetch and parse one A-share's daily bars; ``None`` on no data.
Args:
symbol: Normalized Gildata symbol (e.g. ``600519.SH``).
@@ -353,7 +427,7 @@ class DataLoader:
RuntimeError: If the token is missing or the vendor errors.
requests.RequestException: Propagated from the HTTP layer.
"""
- rows = _call_tool(
+ rows = _tool_rows(
_DAILY_QUOTE_TOOL,
{
"stockObject": [symbol],
@@ -363,3 +437,70 @@ class DataLoader:
},
)
return _parse_daily_rows(rows)
+
+ def _fetch_one_hk(
+ self,
+ code: str,
+ start_date: str,
+ end_date: str,
+ ) -> Optional[pd.DataFrame]:
+ """Fetch one HK equity's raw daily bars via its resolved 内码.
+
+ Unresolvable symbols return ``None`` (no recall match), so the symbol
+ drops out and the fallback chain serves it from another source.
+
+ Args:
+ code: Project symbol, e.g. ``00700.HK``.
+ start_date: Inclusive start date, ``YYYY-MM-DD``.
+ end_date: Inclusive end date, ``YYYY-MM-DD``.
+
+ Returns:
+ An ascending raw OHLCV DataFrame (volume in shares), or ``None``.
+ """
+ vendor_code = resolve_vendor_code(
+ "hk_equity", code, _recall_candidates
+ )
+ if vendor_code is None:
+ return None
+ rows = _tool_rows(
+ "HKStockDailyQuotes",
+ {
+ "stockObject": [vendor_code],
+ "beginDate": start_date,
+ "endDate": end_date,
+ },
+ )
+ # HK volume already arrives in shares — no 万股 conversion.
+ return _parse_daily_rows(rows, volume_scale=1.0)
+
+ def _fetch_one_index(
+ self,
+ code: str,
+ start_date: str,
+ end_date: str,
+ ) -> Optional[pd.DataFrame]:
+ """Fetch one CN index's daily bars via its resolved 内码.
+
+ Args:
+ code: Project index symbol, e.g. ``000300.SH`` / ``399006.SZ``.
+ start_date: Inclusive start date, ``YYYY-MM-DD``.
+ end_date: Inclusive end date, ``YYYY-MM-DD``.
+
+ Returns:
+ An ascending OHLCV DataFrame (volume converted from 万股), or
+ ``None`` when unresolved or without bars.
+ """
+ vendor_code = resolve_vendor_code(
+ "cn_index", code, _recall_candidates
+ )
+ if vendor_code is None:
+ return None
+ rows = _tool_rows(
+ "IndexDailyQuote",
+ {
+ "indexObject": [vendor_code],
+ "beginDate": start_date,
+ "endDate": end_date,
+ },
+ )
+ return _parse_daily_rows(rows)
diff --git a/agent/backtest/loaders/registry.py b/agent/backtest/loaders/registry.py
index 272323f1a..dc69d1b2e 100644
--- a/agent/backtest/loaders/registry.py
+++ b/agent/backtest/loaders/registry.py
@@ -204,6 +204,9 @@ FALLBACK_CHAINS: dict[str, list[str]] = {
"yfinance",
"tushare",
"longbridge",
+ # Key-gated commercial vendor, same tail-of-chain rationale as the
+ # a_share chain (paid quota only spent when free sources cannot serve).
+ "gildata",
"local",
],
"india_equity": ["yahoo", "yfinance", "india_broker", "local"],
@@ -269,6 +272,9 @@ 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",
+ # Gildata's HKStockDailyQuotes serves raw traded prices (befadj/aftadj
+ # closes are separate fields, ignored) — measured on 00700.HK.
+ ("gildata", "hk_equity"): "raw",
}
#: Markets with no corporate-action adjustment concept. Their sources stamp
diff --git a/agent/src/skills/data-routing/SKILL.md b/agent/src/skills/data-routing/SKILL.md
index ac570448d..ee90a4212 100644
--- a/agent/src/skills/data-routing/SKILL.md
+++ b/agent/src/skills/data-routing/SKILL.md
@@ -41,7 +41,7 @@ per-source skill.
| alphavantage | US equities | Yes (`ALPHAVANTAGE_API_KEY`) | Unrestricted | data-routing |
| tiingo | US equities | Yes (`TIINGO_API_KEY`) | Unrestricted | data-routing |
| fmp | US equities | Yes (`FMP_API_KEY`) | Unrestricted | data-routing |
-| gildata | A-shares (Hundsun Juyuan commercial feed, forward-adjusted dailies) | Yes (`GILDATA_TOKEN` / Settings) | Gildata MCP API | data-routing |
+| gildata | A-shares (前复权 dailies), HK equities (raw), CN indices | Yes (`GILDATA_TOKEN` / Settings) | Gildata MCP API | data-routing |
| qveris | Global multi-asset (paid, credits) | Yes (`QVERIS_API_KEY` / Settings) | QVeris API | qveris <!-- QVERIS-INTEGRATION --> |
## Capability → Tool Routing
diff --git a/agent/tests/test_gildata_codes.py b/agent/tests/test_gildata_codes.py
new file mode 100644
index 000000000..a7be6a4b6
--- /dev/null
+++ b/agent/tests/test_gildata_codes.py
@@ -0,0 +1,127 @@
+"""Tests for gildata_codes: recall matching, cache behavior, failure paths.
+
+The MCP transport is injected, so no test touches the network.
+"""
+
+import json
+
+import pytest
+
+from backtest.loaders import gildata_codes as gc
+
+
+def _transport(refs):
+ """Build a fake transport returning candidates with the given ref codes."""
+ calls = []
+
+ def call_tool(name, arguments):
+ calls.append((name, arguments))
+ return {
+ "api_name": "ParamCandidateRecall",
+ "candidates": [
+ {"caption": f"name{i}", "code": str(1000 + i), "ref_code": ref}
+ for i, ref in enumerate(refs)
+ ],
+ }
+
+ return call_tool, calls
+
+
[email protected](autouse=True)
+def _isolated_cache(tmp_path, monkeypatch):
+ """Point the disk cache at a temp file and clear the memo per test."""
+ monkeypatch.setattr(gc, "_cache_path", lambda: tmp_path / "codes.json")
+ gc.reset_code_cache()
+ yield
+ gc.reset_code_cache()
+
+
+class TestResolve:
+ """Exact ref_code matching against the recall candidates."""
+
+ def test_exact_ref_match(self):
+ transport, calls = _transport(["09999.HK", "00700.HK"])
+ code = gc.resolve_vendor_code("hk_equity", "00700.HK", transport)
+ assert code == "1001"
+ name, arguments = calls[0]
+ assert name == "ParamCandidateRecall"
+ assert arguments["recall_list"][0]["api_name"] == "HKStockDailyQuotes"
+ assert arguments["recall_list"][0]["param_name"] == "stockObject"
+
+ def test_ss_normalized_for_index_match(self):
+ transport, _ = _transport(["000300.SH"])
+ code = gc.resolve_vendor_code("cn_index", "000300.SS", transport)
+ assert code == "1000"
+
+ def test_no_match_returns_none(self):
+ transport, _ = _transport(["09999.HK"])
+ assert gc.resolve_vendor_code("hk_equity", "00700.HK", transport) is None
+
+ def test_transport_error_returns_none(self):
+ def boom(name, arguments):
+ raise RuntimeError("network down")
+
+ assert gc.resolve_vendor_code("hk_equity", "00700.HK", boom) is None
+
+ def test_unknown_kind_returns_none(self):
+ transport, _ = _transport(["00700.HK"])
+ assert gc.resolve_vendor_code("us_equity", "AAPL", transport) is None
+
+
+class TestCache:
+ """A resolved code is recalled once, then served from the caches."""
+
+ def test_second_resolve_hits_memo(self):
+ transport, calls = _transport(["00700.HK"])
+ assert gc.resolve_vendor_code("hk_equity", "00700.HK", transport) == "1000"
+ assert gc.resolve_vendor_code("hk_equity", "00700.HK", transport) == "1000"
+ assert len(calls) == 1
+
+ def test_disk_cache_survives_memo_clear(self):
+ # Fresh resolve: code 1000. The post-clear transport would answer a
+ # DIFFERENT code (1001) if it were consulted — proving the disk hit.
+ transport, _ = _transport(["00700.HK"])
+ assert gc.resolve_vendor_code("hk_equity", "00700.HK", transport) == "1000"
+ gc.reset_code_cache()
+ fresh_transport, fresh_calls = _transport(["09999.HK", "00700.HK"])
+ code = gc.resolve_vendor_code("hk_equity", "00700.HK", fresh_transport)
+ assert code == "1000"
+ assert fresh_calls == [] # served from disk, no new recall
+
+ def test_disk_entry_with_wrong_kind_is_ignored(self):
+ # A code cached for another market must not shadow a live recall.
+ import time
+
+ path = gc._cache_path()
+ path.parent.mkdir(parents=True, exist_ok=True)
+ path.write_text(
+ json.dumps(
+ {"00700.HK": {"kind": "cn_index", "code": "42", "resolved_at": time.time()}}
+ ),
+ encoding="utf-8",
+ )
+ transport, calls = _transport(["09999.HK", "00700.HK"])
+ code = gc.resolve_vendor_code("hk_equity", "00700.HK", transport)
+ assert code == "1001"
+ assert len(calls) == 1
+
+ def test_stale_disk_entry_re_resolves(self):
+ import time
+
+ path = gc._cache_path()
+ path.parent.mkdir(parents=True, exist_ok=True)
+ path.write_text(
+ json.dumps(
+ {"00700.HK": {
+ "kind": "hk_equity", "code": "42",
+ "resolved_at": time.time() - gc._CACHE_MAX_AGE_S - 1,
+ }}
+ ),
+ encoding="utf-8",
+ )
+ transport, calls = _transport(["09999.HK", "00700.HK"])
+ assert gc.resolve_vendor_code("hk_equity", "00700.HK", transport) == "1001"
+ assert len(calls) == 1
+ # And the fresh mapping replaced the stale one on disk.
+ disk = json.loads(path.read_text(encoding="utf-8"))
+ assert disk["00700.HK"]["code"] == "1001"
diff --git a/agent/tests/test_gildata_loader.py b/agent/tests/test_gildata_loader.py
index e8642411a..c3dd3ee8d 100644
--- a/agent/tests/test_gildata_loader.py
+++ b/agent/tests/test_gildata_loader.py
@@ -53,9 +53,12 @@ class TestRegistration:
def test_metadata(self):
assert DataLoader.name == "gildata"
- assert DataLoader.markets == {"a_share"}
+ assert DataLoader.markets == {"a_share", "hk_equity"}
assert DataLoader.requires_auth is True
- assert DataLoader.volume_units == {"a_share": "shares"}
+ assert DataLoader.volume_units == {
+ "a_share": "shares",
+ "hk_equity": "shares",
+ }
class TestIsAvailable:
@@ -203,10 +206,10 @@ class TestFetch:
args = mock_post.call_args.kwargs["json_body"]["params"]["arguments"]
assert args["stockObject"] == ["600519.SH"]
- def test_non_a_share_symbols_skipped(self, monkeypatch):
+ def test_unsupported_symbols_skipped(self, monkeypatch):
monkeypatch.setenv("GILDATA_TOKEN", "secret")
with patch.object(gl, "throttled_post_json") as mock_post:
- out = DataLoader().fetch(["AAPL.US", "00700.HK"], "2024-01-01", "2024-01-31")
+ out = DataLoader().fetch(["AAPL.US", "BTC-USDT"], "2024-01-01", "2024-01-31")
assert out == {}
mock_post.assert_not_called()
@@ -271,3 +274,128 @@ class TestFetch:
DataLoader().fetch(["600519.SH"], "2024-01-01", "2024-01-31")
url = mock_post.call_args[0][0]
assert url.startswith("https://proxy.example/mcp?")
+
+
+
+def _recall_envelope(ref_code, code):
+ inner = {
+ "code": 0,
+ "results": [
+ {
+ "api_name": "ParamCandidateRecall",
+ "candidates": [
+ {"caption": "wrong", "code": "999", "ref_code": "99999.HK"},
+ {"caption": "right", "code": code, "ref_code": ref_code},
+ ],
+ }
+ ],
+ }
+ return {"jsonrpc": "2.0", "id": 1, "result": {"content": [
+ {"type": "text", "text": json.dumps(inner)}
+ ]}}
+
+
+# HK rows exactly as HKStockDailyQuotes sends them: bare OHLC keys, volume
+# already in shares, befadj/aftadj closes as separate (ignored) fields.
+_HK_ROWS = [
+ {"tradingday": "2024-05-14", "open": 371.0, "high": 380.0, "low": 369.0,
+ "close": 378.2, "volume": 20558084,
+ "befadjcloseprice": 367.4352, "aftadjcloseprice": 2103.93},
+ {"tradingday": "2024-05-13", "open": 368.6, "high": 380.0, "low": 368.0,
+ "close": 378.2, "volume": 16718452, "befadjcloseprice": 367.4352},
+]
+
+# Index rows use the openprice-style keys with turnovervolume in 万股.
+_INDEX_ROWS = [
+ {"tradingday": "2024-01-03", "openprice": 3380.0, "highprice": 3390.0,
+ "lowprice": 3360.0, "closeprice": 3380.5, "turnovervolume": 1100000.0},
+ {"tradingday": "2024-01-02", "openprice": 3426.27, "highprice": 3426.27,
+ "lowprice": 3386.35, "closeprice": 3386.35, "turnovervolume": 1161807.26},
+]
+
+
+class TestHkFetch:
+ """:HK symbols route to the HK tool with a resolved internal code."""
+
+ def test_fetch_hk_symbol(self, monkeypatch):
+ monkeypatch.setenv("GILDATA_TOKEN", "secret")
+ from backtest.loaders.gildata_codes import reset_code_cache
+ reset_code_cache()
+
+ def _side(url, **kwargs):
+ body = kwargs["json_body"]["params"]
+ if body["name"] == "ParamCandidateRecall":
+ return _recall_envelope("00700.HK", "1000546")
+ assert body["name"] == "HKStockDailyQuotes"
+ assert body["arguments"] == {
+ "stockObject": ["1000546"],
+ "beginDate": "2024-05-01",
+ "endDate": "2024-05-31",
+ }
+ return _envelope(_HK_ROWS)
+
+ with patch.object(gl, "throttled_post_json", side_effect=_side):
+ out = DataLoader().fetch(["00700.HK"], "2024-05-01", "2024-05-31")
+ assert set(out) == {"00700.HK"}
+ df = out["00700.HK"]
+ assert len(df) == 2
+ # Ascending: first row is 2024-05-13. Volume already in shares: no
+ # 万股 scaling.
+ assert df["volume"].iloc[0] == pytest.approx(16718452.0)
+ # Raw traded prices pass through untouched.
+ assert df["close"].iloc[0] == pytest.approx(378.2)
+ # befadj/aftadj closes never leak into any served column.
+ assert df["close"].max() < 400.0
+ reset_code_cache()
+
+ def test_unresolved_recall_omits_symbol(self, monkeypatch):
+ monkeypatch.setenv("GILDATA_TOKEN", "secret")
+ from backtest.loaders.gildata_codes import reset_code_cache
+ reset_code_cache()
+
+ def _side(url, **kwargs):
+ # Recall answers candidates that do not include 00700.HK.
+ return _recall_envelope("09999.HK", "424242")
+
+ with patch.object(gl, "throttled_post_json", side_effect=_side) as mock_post:
+ out = DataLoader().fetch(["00700.HK"], "2024-05-01", "2024-05-31")
+ assert out == {}
+ # Exactly one wire call: the recall — never the quote tool.
+ assert mock_post.call_count == 1
+ reset_code_cache()
+
+
+class TestIndexFetch:
+ """CN index codes route to the index tool with a resolved internal code."""
+
+ def test_fetch_cn_index(self, monkeypatch):
+ monkeypatch.setenv("GILDATA_TOKEN", "secret")
+ from backtest.loaders.gildata_codes import reset_code_cache
+ reset_code_cache()
+
+ def _side(url, **kwargs):
+ body = kwargs["json_body"]["params"]
+ if body["name"] == "ParamCandidateRecall":
+ return _recall_envelope("000300.SH", "3145")
+ assert body["name"] == "IndexDailyQuote"
+ assert body["arguments"] == {
+ "indexObject": ["3145"],
+ "beginDate": "2024-01-01",
+ "endDate": "2024-01-31",
+ }
+ return _envelope(_INDEX_ROWS)
+
+ with patch.object(gl, "throttled_post_json", side_effect=_side):
+ out = DataLoader().fetch(["000300.SH"], "2024-01-01", "2024-01-31")
+ assert set(out) == {"000300.SH"}
+ df = out["000300.SH"]
+ # Ascending: first row is 2024-01-02. Index volume is 万股 and scales
+ # to shares like the A-share tool.
+ assert df["volume"].iloc[0] == pytest.approx(1_161_807.26 * 10_000)
+ assert df["close"].iloc[0] == pytest.approx(3386.35)
+ reset_code_cache()
+
+ def test_index_detection_exchange_specific(self):
+ # 000001.SZ is Ping An Bank (an A-share), 000001.SH is the SSE index.
+ assert gl._is_cn_index("000001.SH") is True
+ assert gl._is_cn_index("000001.SZ") is False
diff --git a/agent/tests/test_price_caliber.py b/agent/tests/test_price_caliber.py
index 6c4d75dfd..6f907f417 100644
--- a/agent/tests/test_price_caliber.py
+++ b/agent/tests/test_price_caliber.py
@@ -66,6 +66,13 @@ def test_tushare_hk_override_is_raw() -> None:
assert price_caliber("tushare", "a_share") == "split_dividend"
+def test_gildata_hk_override_is_raw() -> None:
+ """HKStockDailyQuotes serves raw traded prices (befadj/aftadj closes are
+ separate fields the loader ignores) — measured on 00700.HK."""
+ assert price_caliber("gildata", "hk_equity") == "raw"
+ assert price_caliber("gildata", "a_share") == "split_dividend"
+
+
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
diff --git a/agent/tests/test_registry.py b/agent/tests/test_registry.py
index 1b5e8efb6..c2d97f0d8 100644
--- a/agent/tests/test_registry.py
+++ b/agent/tests/test_registry.py
@@ -268,6 +268,7 @@ class TestFallbackChains:
"yfinance",
"tushare",
"longbridge",
+ "gildata",
"local",
]