Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(backtest): resolve embedded-digit and month-code-collision product codes

Lanre Shittu52cdb634c9af
diff --git a/agent/backtest/engines/global_futures.py b/agent/backtest/engines/global_futures.py
index d4c545bbd..feedc1c4d 100644
--- a/agent/backtest/engines/global_futures.py
+++ b/agent/backtest/engines/global_futures.py
@@ -94,6 +94,13 @@ _DEFAULT_COMMISSION = 2.50
_MONTH_CODES = set("FGHJKMNQUVXZ")
+# Every listed product, for two disambiguation checks _extract_product needs:
+# a bare symbol that already IS a full product name (M2K has a digit in the
+# middle, so the letters-only regexes below can't recognize it on their own),
+# and a product whose own last letter happens to double as a month code
+# (MYM, FESX, FDAX all end in one).
+_KNOWN_PRODUCTS = frozenset(_MULTIPLIER)
+
def _extract_product(symbol: str) -> str:
"""Extract product code from futures symbol.
@@ -108,17 +115,32 @@ def _extract_product(symbol: str) -> str:
product group also accepts a single leading digit followed by one
letter (e.g. 6EZ4 -> 6E), on top of the plain 2-4 letter form.
+ A product whose own last letter is itself a valid month code (MYM,
+ FESX, FDAX) reads as ambiguous in YYMM form: MYM2503 parses just as
+ well as "MY" + June + year 2503 as it does as MYM + YYMM 2503. When
+ that happens, the longer, listed product wins over the coincidental
+ month-code reading. A bare symbol with a digit in the middle of its
+ letters (M2K) cannot be split by the regexes below at all, so it is
+ checked directly against the listed products first.
+
Args:
symbol: Futures symbol string.
Returns:
- Product code (e.g. 'ES', 'CL', 'GC', '6E').
+ Product code (e.g. 'ES', 'CL', 'GC', '6E', 'M2K').
"""
code = symbol.split(".")[0].upper()
+ if code in _KNOWN_PRODUCTS:
+ return code
+
# Pattern 1: product + month-code + year (ESZ4, CLF25, GCM2025, 6EZ4)
m = re.match(r"(\d[A-Z]|[A-Z]{2,4})([FGHJKMNQUVXZ])(\d{1,4})$", code)
if m:
+ extended = m.group(1) + m.group(2)
+ rest = code[len(extended) :]
+ if extended in _KNOWN_PRODUCTS and rest.isdigit() and len(rest) == 4:
+ return extended
return m.group(1)
# Pattern 2: product + YYMM (NQ2503, CL2412, 6EH25)
diff --git a/agent/tests/test_global_futures_engine.py b/agent/tests/test_global_futures_engine.py
index d914687f9..91a78b62e 100644
--- a/agent/tests/test_global_futures_engine.py
+++ b/agent/tests/test_global_futures_engine.py
@@ -69,6 +69,12 @@ class TestExtractProduct:
("6JH25", "6J"),
("6B", "6B"),
("6A.CME", "6A"),
+ ("M2K", "M2K"),
+ ("MYM2503", "MYM"),
+ ("MYMZ4", "MYM"),
+ ("FESX2503", "FESX"),
+ ("FESXZ4", "FESX"),
+ ("FDAXH25", "FDAX"),
],
)
def test_extract(self, symbol: str, expected: str) -> None:
@@ -205,6 +211,9 @@ class TestContractMultiplier:
("6EZ4", 125000),
("6JH25", 12500000),
("6B", 62500),
+ ("M2K", 5),
+ ("MYM2503", 0.5),
+ ("FESXZ4", 10),
],
)
def test_multipliers(self, symbol: str, expected: float) -> None: