Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(backtest): classify dated global futures that carry their venue suffix

Haozhe Wu349bf202b808
diff --git a/agent/backtest/engines/_market_hooks.py b/agent/backtest/engines/_market_hooks.py
index 7086489c5..78e57653e 100644
--- a/agent/backtest/engines/_market_hooks.py
+++ b/agent/backtest/engines/_market_hooks.py
@@ -63,6 +63,19 @@ _MARKET_PATTERNS = [
(re.compile(r"^[A-Z]{2,4}\d{4}$", re.I), "futures"),
# Global futures: bare product code with exchange (e.g. ES.CME)
(re.compile(r"^[A-Z]{2,4}\.(CME|CBOT|NYMEX|COMEX|ICE|EUREX)$", re.I), "futures"),
+ # Global futures: dated contract carrying its venue (ESZ4.CME, CL2412.NYMEX,
+ # GCM2025.COMEX). The bare dated forms above matched, and the continuous
+ # form with a venue matched, but the combination fell through every pattern
+ # to the a_share default below — a USD contract then priced in CNY under
+ # T+1 with no shorting. Same class as #1394 on the global side. The product
+ # width opens to {1,4} here (not on the bare forms) because a recognized
+ # futures venue already proves the class: CBOT lists single-letter grains
+ # (C, S, W, O), which ``^[A-Z]{2,4}\d{4}$`` cannot express without also
+ # claiming bare codes it has no venue to justify.
+ (re.compile(
+ r"^[A-Z]{1,4}(?:[FGHJKMNQUVXZ]\d{1,2}|\d{4})\.(CME|CBOT|NYMEX|COMEX|ICE|EUREX)$",
+ re.I,
+ ), "futures"),
# Forex pairs: XXX/YYY or XXXXXX.FX
(re.compile(r"^[A-Z]{3}/[A-Z]{3}$"), "forex"),
(re.compile(r"^[A-Z]{6}\.FX$"), "forex"),
diff --git a/agent/tests/test_market_detection.py b/agent/tests/test_market_detection.py
index 1fd2a263e..8b7636cff 100644
--- a/agent/tests/test_market_detection.py
+++ b/agent/tests/test_market_detection.py
@@ -12,7 +12,6 @@ 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,
@@ -141,6 +140,26 @@ class TestDetectMarket:
# And a global futures code with a recognized venue keeps its currency.
assert code_currency("ES.CME") == "USD"
+ def test_dated_global_futures_with_a_venue_suffix_classify_as_futures(self) -> None:
+ # The sibling of #1394 on the global side: the bare dated forms
+ # (CL2412, ESZ4) classified, and the continuous form with a venue
+ # (ES.CME) classified, but the combination fell through to the a_share
+ # default -- so a USD contract was settled in CNY under A-share T+1
+ # rules with no shorting.
+ for code in ("CL2412.NYMEX", "ES2503.CME", "ESZ4.CME", "GCM2025.COMEX"):
+ assert _detect_market(code) == "futures", code
+ assert not _is_china_futures(code), code
+ assert code_currency(code) == "USD", code
+ # The venue decides the currency, not the default.
+ assert code_currency("FDAX2412.EUREX") == "EUR"
+ # A recognized venue is what admits a single-letter product (CBOT
+ # grains); the bare form stays out because nothing proves its class.
+ assert _detect_market("C2412.CBOT") == "futures"
+ assert _detect_market("C2412") == "a_share"
+ # Equities that share the shape must not be captured.
+ assert _detect_market("AAPL.US") == "us_equity"
+ assert _detect_market("TD.TO") == "ca_equity"
+
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