Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(backtest): classify Tushare-spelled China futures as China futures

Yufeng He8f648fb0353a
diff --git a/agent/backtest/engines/_market_hooks.py b/agent/backtest/engines/_market_hooks.py
index 8c831975c..7086489c5 100644
--- a/agent/backtest/engines/_market_hooks.py
+++ b/agent/backtest/engines/_market_hooks.py
@@ -55,7 +55,8 @@ _MARKET_PATTERNS = [
# they still reach the forex whitelist below.
(re.compile(r"^[A-Z]{2,}(?:USDT|USDC|BUSD)$", re.I), "crypto"),
# China futures: product+delivery.exchange (e.g. IF2406.CFFEX, rb2410.SHFE)
- (re.compile(r"^[A-Za-z]{1,2}\d{3,4}\.(ZCE|DCE|SHFE|INE|CFFEX|GFEX)$", re.I), "futures"),
+ # Tushare suffix spellings (SHF/CZC/CFX/GFE) classify here too.
+ (re.compile(r"^[A-Za-z]{1,2}\d{3,4}\.(ZCE|DCE|SHFE|INE|CFFEX|GFEX|SHF|CZC|CFX|GFE)$", re.I), "futures"),
# Global futures: product+month-code (e.g. ESZ4, CLF25, GCM2025)
(re.compile(r"^[A-Z]{2,4}[FGHJKMNQUVXZ]\d{1,2}$", re.I), "futures"),
# Global futures: product+YYMM (e.g. CL2412, ES2503)
@@ -102,6 +103,10 @@ _MARKET_PATTERNS = [
_CHINA_EXCHANGES = {"CFFEX", "SHFE", "DCE", "ZCE", "INE", "GFEX"}
+# Tushare spells the same exchanges differently (ts_code='CU1811.SHF');
+# normalize to the canonical suffix before any set membership test (#1394).
+_EXCHANGE_ALIASES = {"SHF": "SHFE", "CZC": "ZCE", "CFX": "CFFEX", "GFE": "GFEX"}
+
# Supported settlement-currency contract per market. A composite backtest holds
# one shared capital pool, so a code set spanning two of these would add CNY to
# USD to KRW as if they were the same unit. The suffix alone cannot prove an
@@ -222,7 +227,7 @@ def _is_china_futures(code: str) -> bool:
# else = False. Without this guard the product-code heuristic below
# would misclassify global futures whose product letters happen to
# collide with a CN product (e.g. ``M2412.CBOT`` — US soybean meal).
- return parts[1] in _CHINA_EXCHANGES
+ return _EXCHANGE_ALIASES.get(parts[1], parts[1]) in _CHINA_EXCHANGES
# Bare code (no exchange suffix): fall back to product-code heuristic.
m = re.match(r"([A-Za-z]+)\d+", parts[0])
if m:
diff --git a/agent/tests/test_market_detection.py b/agent/tests/test_market_detection.py
index 8b390cb4d..1fd2a263e 100644
--- a/agent/tests/test_market_detection.py
+++ b/agent/tests/test_market_detection.py
@@ -12,6 +12,7 @@ from __future__ import annotations
import pytest
from backtest.engines._market_hooks import _is_china_futures, code_currency
+from backtest.engines._market_hooks import _is_china_futures
from backtest.runner import (
_detect_market,
_detect_source,
@@ -123,6 +124,23 @@ class TestDetectMarket:
assert _detect_market("123456") == "a_share"
assert _detect_market("@#$") == "a_share"
+ def test_tushare_suffix_china_futures_classify_as_china_futures(self) -> None:
+ # #1394: Tushare spells the exchanges SHF/CZC/CFX/GFE where the
+ # canonical set is SHFE/ZCE/CFFEX/GFEX; without the alias these
+ # contracts fell through to the a_share default or the global engine
+ # (wrong multiplier, wrong currency, wrong band rules).
+ for code in ("CU1811.SHF", "CF501.CZC", "IF2406.CFX", "SC2409.GFE"):
+ assert _detect_market(code) == "futures", code
+ assert code_currency(code) == "CNY", code
+ # Canonical spellings keep working.
+ assert _detect_market("CU2406.SHFE") == "futures"
+ assert code_currency("rb2410.DCE") == "CNY"
+ # A non-China exchange must still not classify as China futures.
+ assert not _is_china_futures("M2412.CBOT")
+ assert not _is_china_futures("CL2412.NYMEX")
+ # And a global futures code with a recognized venue keeps its currency.
+ assert code_currency("ES.CME") == "USD"
+
def test_concatenated_crypto_pairs_route_to_crypto(self) -> None:
# Separator-less spot pairs (the Binance spelling) used to fall
# through every pattern and pick up a_share rules, T+1 and no