Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix: close the two gaps found reviewing #1368 and #1369

Haozhe Wu759dfab924f4
diff --git a/agent/backtest/engines/global_futures.py b/agent/backtest/engines/global_futures.py
index feedc1c4d..4f6da650b 100644
--- a/agent/backtest/engines/global_futures.py
+++ b/agent/backtest/engines/global_futures.py
@@ -94,12 +94,33 @@ _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).
+# Every listed product, longest first: _extract_product resolves a symbol
+# against this table BEFORE falling back to shape regexes, because the shapes
+# alone are ambiguous. Three ways they fail: a digit inside the letters (M2K,
+# M2KZ4) that no letters-only group can span, a product whose own last letter
+# doubles as a month code (MYM, FESX, FDAX -> "MY" + M + 2503), and a product
+# that is a prefix of another (SI vs SIL). Longest-match-first settles all
+# three; a product NOT in this table still falls through to the regexes.
_KNOWN_PRODUCTS = frozenset(_MULTIPLIER)
+_PRODUCTS_LONGEST_FIRST = tuple(sorted(_KNOWN_PRODUCTS, key=len, reverse=True))
+
+#: Contract suffix left after stripping a listed product: a month code plus a
+#: 1-4 digit year (Z4, F25, M2025), or a bare YYMM (2503).
+_CONTRACT_SUFFIX_RE = re.compile(r"^(?:[FGHJKMNQUVXZ]\d{1,4}|\d{4})$")
+
+
+def _listed_product_prefix(code: str) -> str | None:
+ """Longest listed product that ``code`` starts with, if the rest is a
+ contract suffix.
+
+ Returns:
+ The product code, or ``None`` when no listed product explains
+ ``code`` (the caller then falls back to the shape regexes).
+ """
+ for product in _PRODUCTS_LONGEST_FIRST:
+ if code.startswith(product) and _CONTRACT_SUFFIX_RE.match(code[len(product):]):
+ return product
+ return None
def _extract_product(symbol: str) -> str:
@@ -115,13 +136,12 @@ 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.
+ Shape alone is ambiguous, so a listed product always wins first:
+ MYM2503 parses just as well as "MY" + June + year 2503 as it does as
+ MYM + YYMM 2503, and M2K / M2KZ4 cannot be split by a letters-only
+ group at all. ``_listed_product_prefix`` resolves both, longest match
+ first (so SILZ4 is micro silver, not SI). Only a product missing from
+ the multiplier table falls through to the shape regexes below.
Args:
symbol: Futures symbol string.
@@ -134,13 +154,13 @@ def _extract_product(symbol: str) -> str:
if code in _KNOWN_PRODUCTS:
return code
+ listed = _listed_product_prefix(code)
+ if listed is not None:
+ return listed
+
# 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/src/strategy_discovery/evidence_harness.py b/agent/src/strategy_discovery/evidence_harness.py
index add6de265..5f8bf1067 100644
--- a/agent/src/strategy_discovery/evidence_harness.py
+++ b/agent/src/strategy_discovery/evidence_harness.py
@@ -330,14 +330,25 @@ def _month_spans(bar_indices: Sequence[int], dates: Sequence[date]) -> tuple[str
return tuple(spans)
-def _bar_returns(bar_indices: Sequence[int], series: Sequence[float]) -> list[float]:
- """Per-bar simple returns for the given bars (previous bar may be any regime)."""
+def _bar_returns(
+ bar_indices: Sequence[int], series: Sequence[float | None]
+) -> list[float]:
+ """Per-bar simple returns for the given bars (previous bar may be any regime).
+
+ The anchor bar (``index - 1``) is deliberately outside ``bar_indices``,
+ so a caller that verified its OWN bars are present has verified nothing
+ about the anchor: the benchmark series carries ``None`` per bar wherever
+ the column was absent or unparseable. A missing endpoint yields no
+ return for that bar, the same way a non-finite one already does.
+ """
returns: list[float] = []
for index in bar_indices:
if index <= 0:
continue
previous = series[index - 1]
current = series[index]
+ if previous is None or current is None:
+ continue
if previous <= 0 or not math.isfinite(previous) or not math.isfinite(current):
continue
returns.append(current / previous - 1.0)
@@ -509,7 +520,7 @@ def compute_evidence_for_run(
benchmark_compounded: float | None = None
if all(benchmarks[i] is not None for i in bar_indices):
- benchmark_returns = _bar_returns(bar_indices, benchmarks) # type: ignore[arg-type]
+ benchmark_returns = _bar_returns(bar_indices, benchmarks)
benchmark_compounded = _compounded(benchmark_returns)
excess = (
compounded_return - benchmark_compounded
diff --git a/agent/tests/test_global_futures_engine.py b/agent/tests/test_global_futures_engine.py
index 91a78b62e..ea986c4e4 100644
--- a/agent/tests/test_global_futures_engine.py
+++ b/agent/tests/test_global_futures_engine.py
@@ -75,6 +75,17 @@ class TestExtractProduct:
("FESX2503", "FESX"),
("FESXZ4", "FESX"),
("FDAXH25", "FDAX"),
+ # The dated form of an embedded-digit product: the bare M2K
+ # check cannot see it, and no letters-only group spans "M2K".
+ ("M2KZ4", "M2K"),
+ ("M2K2503", "M2K"),
+ # A listed product that is a prefix of another listed product:
+ # longest match wins, so micro silver stays micro silver.
+ ("SILZ4", "SIL"),
+ ("SIZ4", "SI"),
+ # A product absent from the multiplier table still falls
+ # through to the shape regexes rather than being swallowed.
+ ("6NZ4", "6N"),
],
)
def test_extract(self, symbol: str, expected: str) -> None:
@@ -214,6 +225,8 @@ class TestContractMultiplier:
("M2K", 5),
("MYM2503", 0.5),
("FESXZ4", 10),
+ ("M2KZ4", 5),
+ ("SILZ4", 1000),
],
)
def test_multipliers(self, symbol: str, expected: float) -> None:
diff --git a/agent/tests/test_strategy_discovery_harness.py b/agent/tests/test_strategy_discovery_harness.py
index fa58b1257..4c9e585a7 100644
--- a/agent/tests/test_strategy_discovery_harness.py
+++ b/agent/tests/test_strategy_discovery_harness.py
@@ -148,6 +148,36 @@ def _engine_equity_frame_with_regime_exposure(
return eq_df
+def _engine_equity_frame_with_benchmark_gap(gap_index: int) -> pd.DataFrame:
+ """Same frame as _engine_equity_frame, with ONE blank benchmark cell.
+
+ ``gap_index`` is the bar immediately BEFORE a regime's first bar, so
+ every bar the regime owns still has a benchmark while the return
+ anchor for its first bar does not — the exact shape a per-regime
+ availability check cannot see.
+ """
+ eq_df = _engine_equity_frame()
+ eq_df.iloc[gap_index, eq_df.columns.get_loc("benchmark_equity")] = float("nan")
+ return eq_df
+
+
+def _write_run_fixture_with_benchmark_gap(base_dir: Path, gap_index: int) -> Path:
+ """_write_run_fixture with a single unparseable benchmark cell."""
+ run_dir = base_dir / "benchmark_gap_run"
+ artifacts = run_dir / "artifacts"
+ artifacts.mkdir(parents=True, exist_ok=True)
+
+ _engine_equity_frame_with_benchmark_gap(gap_index).to_csv(artifacts / "equity.csv")
+
+ round_trips = [
+ (trade_date, "TEST.SH", 50.0, "signal" if i < 8 else "end")
+ for i, trade_date in enumerate(ALL_TRADE_DAYS)
+ ]
+ _write_trades_csv(artifacts, _engine_trade_rows(round_trips))
+ _write_run_state(run_dir, trade_count=len(ALL_TRADE_DAYS))
+ return run_dir
+
+
def _entry_row(trade_date: date, code: str) -> dict:
return {
"timestamp": trade_date.strftime("%Y-%m-%d"),
@@ -545,6 +575,33 @@ class TestComputeEvidence:
assert by_regime["bear_market"].position_size == pytest.approx(0.25)
assert by_regime["bull_market"].position_size == pytest.approx(0.75)
+ def test_benchmark_gap_on_the_anchor_bar_does_not_sink_the_run(
+ self, tmp_path
+ ) -> None:
+ # The bear regime's own bars (8..17) all carry a benchmark, but the
+ # return anchor for its first bar (7) does not. Checking only the
+ # regime's own bars therefore admits a None anchor into _bar_returns,
+ # where `previous <= 0` used to raise TypeError -- swallowed by
+ # rebuild_evidence's except, so the WHOLE run produced no evidence.
+ run_dir = _write_run_fixture_with_benchmark_gap(tmp_path, gap_index=7)
+ rows = _compute_with_fixture_windows(run_dir)
+ by_regime = {row.regime: row for row in rows}
+ assert "bear_market" in by_regime, (
+ "a single missing benchmark cell must not erase the run's evidence"
+ )
+ # The regime still gets a benchmark: the gap costs bar 8 its return,
+ # bars 9..17 are unaffected.
+ assert by_regime["bear_market"].benchmark_in_regime is not None
+
+ def test_bar_returns_skips_a_missing_anchor_instead_of_comparing_none(self) -> None:
+ # Unit-level statement of the same rule: the anchor is outside
+ # bar_indices by construction, so it can be None.
+ assert sd_harness._bar_returns([1, 2, 3], [None, 100.0, 101.0, 102.0]) == [
+ pytest.approx(101.0 / 100.0 - 1.0),
+ pytest.approx(102.0 / 101.0 - 1.0),
+ ]
+ assert sd_harness._bar_returns([1], [100.0, None]) == []
+
def test_single_position_run_carries_no_concurrency_caveat(self, tmp_path) -> None:
run_dir = _write_run_fixture(tmp_path)
rows = _compute_with_fixture_windows(run_dir)