Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(data): resolve Yahoo futures (=F) and forex (=X) suffix tickers

Haozhe Wu8b4dcaf31368
diff --git a/agent/backtest/loaders/yahoo_loader.py b/agent/backtest/loaders/yahoo_loader.py
index 88a8ebeee..8119ebaba 100644
--- a/agent/backtest/loaders/yahoo_loader.py
+++ b/agent/backtest/loaders/yahoo_loader.py
@@ -39,8 +39,14 @@ _INTERVAL_MAP = {
def _is_supported(code: str) -> bool:
- """Return whether *code* is a symbol this loader handles (US/HK/India)."""
- return code.strip().upper().endswith((".US", ".HK", ".NS", ".BO"))
+ """Return whether *code* is a symbol this loader handles.
+
+ Covers US/HK/India equities plus Yahoo's own futures (``GC=F``) and forex
+ (``EURUSD=X``) suffix conventions, which the public chart endpoint serves
+ verbatim (the code is used as-is in the request URL, no conversion) (#718).
+ """
+ upper = code.strip().upper()
+ return upper.endswith((".US", ".HK", ".NS", ".BO", "=F", "=X"))
def _to_yahoo_interval(interval: str) -> str:
diff --git a/agent/src/market_data.py b/agent/src/market_data.py
index 8899fa6ed..01153ffd0 100644
--- a/agent/src/market_data.py
+++ b/agent/src/market_data.py
@@ -26,6 +26,12 @@ _SOURCE_PATTERNS = [
# India: NSE (RELIANCE.NS) / BSE (500325.BO). Tickers may carry '&' and '-'
# (e.g. M&M.NS, BAJAJ-AUTO.NS). Served by Yahoo's public chart endpoint.
(re.compile(r"^[A-Z0-9&.\-]+\.(NS|BO)$", re.I), "yahoo"),
+ # Yahoo futures (GC=F, CL=F) and forex (EURUSD=X) suffix conventions —
+ # served verbatim by Yahoo's public chart endpoint (#718). Without these,
+ # such symbols fell through to the ``tushare`` default and were routed to
+ # China-market loaders that cannot resolve them.
+ (re.compile(r"^[A-Z0-9]+=F$", re.I), "yahoo"),
+ (re.compile(r"^[A-Z]+=X$", re.I), "yahoo"),
(re.compile(r"^[A-Z]+-USDT$", re.I), "okx"),
(re.compile(r"^[A-Z]+/USDT$", re.I), "ccxt"),
]
diff --git a/agent/tests/test_market_data.py b/agent/tests/test_market_data.py
index d3cfc5541..d1e12bc67 100644
--- a/agent/tests/test_market_data.py
+++ b/agent/tests/test_market_data.py
@@ -47,6 +47,12 @@ from src.market_data import (
("BTC-USDT", "okx"),
("ETH/USDT", "ccxt"),
("local:my_file", "local"),
+ # Yahoo futures / forex suffix conventions (#718) — must not fall to the
+ # ``tushare`` default (which routed them to China-market loaders).
+ ("GC=F", "yahoo"), # gold future
+ ("CL=F", "yahoo"), # crude future
+ ("EURUSD=X", "yahoo"), # forex pair
+ ("JPY=X", "yahoo"),
("something_weird", "tushare"), # documented fallback
],
)
@@ -54,6 +60,49 @@ def test_detect_source(code: str, expected: str) -> None:
assert detect_source(code) == expected
+def test_yahoo_loader_accepts_futures_and_forex_suffixes() -> None:
+ """The yahoo direct loader must accept =F/=X, not just equity suffixes (#718)."""
+ from backtest.loaders.yahoo_loader import _is_supported
+
+ assert _is_supported("GC=F") is True
+ assert _is_supported("EURUSD=X") is True
+ assert _is_supported("AAPL.US") is True # unchanged
+ assert _is_supported("600519.SH") is False # A-share still not yahoo
+
+
+def test_fetch_market_data_auto_routes_yahoo_suffix_symbols() -> None:
+ """auto mode groups GC=F/EURUSD=X under yahoo, not the tushare/China chain (#718)."""
+ seen_sources: list[str] = []
+
+ class _StubLoader:
+ def fetch(self, codes, start, end, *, interval="1D"): # noqa: ANN001
+ index = pd.DatetimeIndex(pd.to_datetime(["2024-01-02"]))
+ return {
+ code: pd.DataFrame(
+ {"open": [1.0], "high": [1.0], "low": [1.0], "close": [1.0], "volume": [0.0]},
+ index=index,
+ )
+ for code in codes
+ }
+
+ def _resolver(source: str):
+ seen_sources.append(source)
+ return _StubLoader
+
+ out = fetch_market_data(
+ codes=["GC=F", "EURUSD=X"],
+ start_date="2024-01-01",
+ end_date="2024-01-03",
+ source="auto",
+ loader_resolver=_resolver,
+ )
+
+ assert "_unresolved" not in out
+ assert "GC=F" in out and "EURUSD=X" in out
+ # First source tried must be yahoo (not tushare/akshare from the China chain).
+ assert seen_sources and seen_sources[0] == "yahoo"
+
+
# --------------------------------------------------------------------------
# cap_rows
# --------------------------------------------------------------------------