"Vibe-Trading: Your Personal Trading Agent"
Python ai-agentalgorithmic-tradingbacktestingfintechllmmcpmulti-agentpythonquantitative-financetrading
fix(akshare): probe both forex symbol-map layouts after akshare 1.20 move
Jackzigen8e728a4b2121
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex bcf1ea3d3..94dde1571 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -7,6 +7,11 @@ This project adheres to [Semantic Versioning](https://semver.org/).### Fixed+- **Forex symbol detection** no longer depends on AKShare's module layout:+ `akshare.forex` was deleted in 1.20.0 and the symbol map moved to+ `akshare.fx.cons_forex`, so the single hard-coded import turned every FX pair+ into an A-share lookup. Both layouts are now probed, and a wholly missing map+ degrades to "not a forex pair" instead of silently matching nothing.- **Global futures routing and pricing**: digit-bearing contracts such as`M2K=F` and `6EZ2026.CME`, including `local:` inputs, reach the globalfutures engine with their full product code. OJ and ZR use quote-unitdiff --git a/agent/backtest/loaders/akshare_loader.py b/agent/backtest/loaders/akshare_loader.pyindex 19867be40..17158ec58 100644--- a/agent/backtest/loaders/akshare_loader.py+++ b/agent/backtest/loaders/akshare_loader.py@@ -6,6 +6,7 @@ data aggregator covering Chinese and global markets. No API token required.from __future__ import annotations+import importlibimport loggingimport refrom typing import Dict, List, Optional@@ -66,12 +67,20 @@ def _sina_contract(code: str) -> str:_CN_FUTURES_MAIN_RE = re.compile(r"^[A-Z]{1,2}0$")--+#: AKShare moved the forex symbol map between releases: ``akshare.forex.cons``+#: existed up to 1.19.x and ``akshare.fx.cons_forex`` carries it from 1.20.0 on,+#: where the whole ``akshare.forex`` package was deleted. requirements only pin+#: ``akshare>=1.12.0``, so both layouts have to be probed — importing a single+#: hard-coded path silently degrades forex detection to "never a forex pair",+#: which routes FX symbols to the A-share endpoint.+_FOREX_SYMBOL_MAP_MODULES = (+ "akshare.fx.cons_forex", # akshare >= 1.20.0+ "akshare.forex.cons", # akshare <= 1.19.x+)def _is_forex(code: str) -> bool:- """Detect forex pairs by matching against AKShare's symbol_market_map.+ """Detect forex pairs by matching against AKShare's symbol map.Issue #54 — forex symbols (EURUSD, GBPUSD, etc.) have no exchange suffixand previously fell through to the A-share endpoint.@@ -79,11 +88,15 @@ def _is_forex(code: str) -> bool:# Accept the canonical slash form (EUR/USD) too, so the forex fallback# chain (mt5 → akshare) actually engages for project-style codes.upper = code.upper().removesuffix(".FX").replace("/", "")- try:- from akshare.forex.cons import symbol_market_map- except Exception:- return False- return upper in symbol_market_map+ for module_name in _FOREX_SYMBOL_MAP_MODULES:+ try:+ module = importlib.import_module(module_name)+ symbol_market_map = module.symbol_market_map+ except (ImportError, AttributeError): # layout moved again, or module unavailable+ continue+ if upper in symbol_market_map:+ return True+ return False@registerdiff --git a/agent/tests/test_akshare_loader.py b/agent/tests/test_akshare_loader.pyindex ea628659d..50c72ded2 100644--- a/agent/tests/test_akshare_loader.py+++ b/agent/tests/test_akshare_loader.py@@ -8,6 +8,7 @@ network — real-data smoke is in tests/_smoke_akshare_real.py if/when needed.from __future__ import annotations+import importlibimport sysfrom types import SimpleNamespacefrom unittest.mock import MagicMock@@ -15,6 +16,7 @@ from unittest.mock import MagicMockimport pandas as pdimport pytest+from backtest.loaders import akshare_loaderfrom backtest.loaders.akshare_loader import (DataLoader,_is_a_share,@@ -25,6 +27,10 @@ from backtest.loaders.akshare_loader import ()from tests.loader_contract import assert_loader_contract+#: Captured once, before any test monkeypatches importlib, so the layout tests+#: can delegate unknown module names to the real loader.+_REAL_IMPORT = importlib.import_module+# ---------------------------------------------------------------------------# Predicate tests@@ -76,6 +82,35 @@ class TestIsForex:assert not _is_forex("ZZZZZZ")+class TestForexSymbolMapLocation:+ """AKShare 1.20.0 deleted ``akshare.forex`` and moved the symbol map to+ ``akshare.fx.cons_forex``. Detection must survive either layout, otherwise+ every FX pair silently routes to the A-share endpoint (CI red on 1.20.x)."""++ @staticmethod+ def _patch_blocked(monkeypatch: pytest.MonkeyPatch, *blocked: str) -> None:+ def fake_import(name: str, *args: object, **kwargs: object) -> object:+ if name in blocked:+ raise ModuleNotFoundError(f"No module named {name!r}")+ return _REAL_IMPORT(name, *args, **kwargs)++ monkeypatch.setattr(akshare_loader.importlib, "import_module", fake_import)++ def test_newer_layout_alone_still_detects_pairs(self, monkeypatch) -> None:+ pytest.importorskip("akshare.fx.cons_forex", reason="akshare >= 1.20 only")+ self._patch_blocked(monkeypatch, "akshare.forex.cons")+ assert _is_forex("EURUSD")++ def test_legacy_layout_alone_still_detects_pairs(self, monkeypatch) -> None:+ pytest.importorskip("akshare.forex.cons", reason="akshare <= 1.19 only")+ self._patch_blocked(monkeypatch, "akshare.fx.cons_forex")+ assert _is_forex("EURUSD")++ def test_missing_symbol_map_degrades_to_false(self, monkeypatch) -> None:+ self._patch_blocked(monkeypatch, "akshare.fx.cons_forex", "akshare.forex.cons")+ assert not _is_forex("EURUSD")++# ---------------------------------------------------------------------------# Routing tests — verify _fetch_one dispatches to the right endpoint without# actually hitting AKShare.