Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(backtest): report global futures priced on generic defaults

cgycoreyed32b527555e
diff --git a/agent/backtest/engines/global_futures.py b/agent/backtest/engines/global_futures.py
index 2afdf3131..bc38b18e5 100644
--- a/agent/backtest/engines/global_futures.py
+++ b/agent/backtest/engines/global_futures.py
@@ -12,13 +12,19 @@ Market rules:
from __future__ import annotations
+import logging
import re
+from typing import Any, Dict, Mapping, TypeVar
import pandas as pd
from backtest.engines.china_a import _blocked_by_limit
from backtest.engines.futures_base import FuturesBaseEngine
+logger = logging.getLogger(__name__)
+
+_T = TypeVar("_T")
+
# ── Contract multiplier (USD per point / per unit) ──
@@ -47,6 +53,11 @@ _MULTIPLIER: dict[str, float] = {
"FESX": 10, "FDAX": 25, "FGBL": 1000,
}
+#: Multiplier used when a product is absent from ``_MULTIPLIER``. It is ES's
+#: own value, so an unlisted contract is priced as though it were an E-mini
+#: future; the run records every such fallback under ``pricing_assumptions``.
+_DEFAULT_MULTIPLIER = 50
+
# ── Margin per contract (approximate USD, initial margin) ──
# Reference table — future use for margin-call checks. Not yet consumed.
@@ -179,12 +190,22 @@ class GlobalFuturesEngine(FuturesBaseEngine):
Config keys:
- slippage: default 0.0003
- commission_per_contract: override, default varies by product
+
+ A product absent from the multiplier or commission table is not an error:
+ it is silently priced on a generic constant (50, ES's own multiplier, and
+ $2.50). The run reports every such fallback under ``pricing_assumptions``
+ so a default cannot pass for data (#1393's failure class).
"""
def __init__(self, config: dict):
# Leverage: most futures have 5-15% margin → 7-20x leverage.
# Price is unknown at init, so use a reasonable fixed default.
# User can override via config["leverage"].
+ # (product, field) -> the generic constant this run was priced on. A
+ # product missing from a table does not fail, it silently takes a
+ # default, so the run has to say which numbers were assumed rather
+ # than looked up. Recording only: this recorder changes no value.
+ self._pricing_defaults: dict[tuple[str, str], Any] = {}
leverage = config.get("leverage", 10.0)
config = {**config, "leverage": leverage}
super().__init__(config)
@@ -193,6 +214,57 @@ class GlobalFuturesEngine(FuturesBaseEngine):
self.slippage_rate: float = config.get("slippage", 0.0003)
self._comm_override = config.get("commission_per_contract")
+ def _priced(
+ self,
+ table: Mapping[str, _T],
+ product: str,
+ default: _T,
+ field: str,
+ ) -> _T:
+ """Look a product up, recording the miss when it falls back.
+
+ A product absent from one of the tables is priced on a generic
+ constant that looks exactly like data in the output -- for example an
+ unlisted contract is sized on ES's multiplier of 50. This returns the
+ same value the plain ``table.get(product, default)`` returned before
+ and additionally remembers the miss, so the run can state which of its
+ numbers were assumed.
+
+ Args:
+ table: One of the product tables in this module.
+ product: Product code as ``_extract_product`` spells it.
+ default: The generic constant used when the product is absent.
+ field: Name of the field, as reported in ``pricing_assumptions``.
+
+ Returns:
+ The table entry, or ``default`` when the product is not listed.
+ """
+ if product in table:
+ return table[product]
+ key = (product, field)
+ if key not in self._pricing_defaults:
+ self._pricing_defaults[key] = default
+ logger.warning(
+ "Global futures product %r has no %s entry; pricing it on the "
+ "generic default %r. The backtest reports this under "
+ "pricing_assumptions.",
+ product,
+ field,
+ default,
+ )
+ return default
+
+ def _engine_diagnostics(self) -> Dict[str, Any]:
+ """Report every table lookup this run priced on a generic default."""
+ if not self._pricing_defaults:
+ return {}
+ return {
+ "pricing_assumptions": [
+ {"product": product, "field": field, "value": value}
+ for (product, field), value in sorted(self._pricing_defaults.items())
+ ]
+ }
+
def can_execute(self, symbol: str, direction: int, bar: pd.Series) -> bool:
"""Global futures: T+0, both directions, limit checks for equity index.
@@ -251,7 +323,9 @@ class GlobalFuturesEngine(FuturesBaseEngine):
Commission in USD.
"""
product = _extract_product(symbol)
- rate = _COMMISSION_PER_CONTRACT.get(product, _DEFAULT_COMMISSION)
+ rate = self._priced(
+ _COMMISSION_PER_CONTRACT, product, _DEFAULT_COMMISSION, "commission"
+ )
return size * rate
def apply_slippage(self, price: float, direction: int) -> float:
@@ -261,7 +335,11 @@ class GlobalFuturesEngine(FuturesBaseEngine):
def get_contract_multiplier(self, symbol: str) -> float:
"""Product-specific contract multiplier."""
product = _extract_product(symbol)
- return float(_MULTIPLIER.get(product, 50))
+ return float(
+ self._priced(
+ _MULTIPLIER, product, _DEFAULT_MULTIPLIER, "contract_multiplier"
+ )
+ )
# ── Helpers ──
diff --git a/agent/tests/test_global_futures_pricing_assumptions.py b/agent/tests/test_global_futures_pricing_assumptions.py
new file mode 100644
index 000000000..888151ea3
--- /dev/null
+++ b/agent/tests/test_global_futures_pricing_assumptions.py
@@ -0,0 +1,174 @@
+"""A global-futures run must say which numbers it assumed (#1393's twin).
+
+``global_futures.py`` keeps two product tables of different sizes. A product
+listed in one and absent from the other does not fail the run -- it silently
+takes a generic constant that is indistinguishable from a real table entry in
+the output: multiplier 50 (ES's own multiplier) and commission ``2.50``.
+
+A product absent from both tables takes both defaults. Its multiplier sets
+the contract count: the engine sizes to a target notional, so the error is
+neutralised in notional and margin, while the number of contracts -- and with
+it the per-contract commission and lot rounding -- is wrong by the ratio of
+the contract's real multiplier to 50. A product present in the multiplier
+table but absent from the commission table is half real: the contract count
+is right and the fee is assumed. 22 of the 43 listed products have no
+commission entry at all.
+
+The direction agreed on #1393 is to make the default observable rather than to
+fill the tables, because filling them leaves the *next* added product silently
+defaulting. Numbers are unchanged here; the run states which of them were
+assumed. ``_PRICE_LIMIT`` is deliberately not reported: a product absent from
+it is not "priced" on a fallback, it is simply not subject to a limit modelled
+for equity index contracts.
+"""
+
+from __future__ import annotations
+
+import logging
+
+import pytest
+
+from backtest.engines.global_futures import (
+ _COMMISSION_PER_CONTRACT,
+ _DEFAULT_COMMISSION,
+ _DEFAULT_MULTIPLIER,
+ _MULTIPLIER,
+ GlobalFuturesEngine,
+)
+
+
+def _engine(*codes: str, **config) -> GlobalFuturesEngine:
+ return GlobalFuturesEngine(
+ {"codes": list(codes), "initial_capital": 1_000_000, **config}
+ )
+
+
+def _assumptions(engine: GlobalFuturesEngine) -> dict[tuple[str, str], object]:
+ reported = engine._engine_diagnostics().get("pricing_assumptions", [])
+ return {(row["product"], row["field"]): row["value"] for row in reported}
+
+
+class TestPricingAssumptionsAreReported:
+ def test_an_unlisted_product_reports_both_defaults(self) -> None:
+ engine = _engine("ZZZF25")
+ engine.get_contract_multiplier("ZZZF25")
+ engine.calc_commission_for_symbol("ZZZF25", 1, 400.0, True)
+
+ assumed = _assumptions(engine)
+ assert assumed[("ZZZ", "contract_multiplier")] == float(_DEFAULT_MULTIPLIER)
+ assert assumed[("ZZZ", "commission")] == _DEFAULT_COMMISSION
+
+ def test_a_listed_product_missing_from_the_commission_table_is_reported(self) -> None:
+ """KC is the half-real contract: multiplier looked up, commission assumed."""
+ assert "KC" in _MULTIPLIER
+ assert "KC" not in _COMMISSION_PER_CONTRACT
+
+ engine = _engine("KCH25")
+ engine.get_contract_multiplier("KCH25")
+ engine.calc_commission_for_symbol("KCH25", 1, 380.0, True)
+
+ assumed = _assumptions(engine)
+ assert ("KC", "contract_multiplier") not in assumed
+ assert assumed[("KC", "commission")] == _DEFAULT_COMMISSION
+
+ def test_the_value_the_run_used_is_unchanged(self) -> None:
+ """Observability only: every number stays exactly what it was."""
+ engine = _engine("ZZZF25")
+ assert engine.get_contract_multiplier("ZZZF25") == float(_DEFAULT_MULTIPLIER)
+ assert engine.get_contract_multiplier("ESZ4") == float(_MULTIPLIER["ES"])
+ assert engine.calc_commission_for_symbol("ZZZF25", 2, 400.0, True) == 5.0
+ assert engine.calc_commission_for_symbol("ESZ4", 2, 6400.0, True) == 4.5
+
+ def test_a_real_entry_equal_to_the_default_is_not_reported(self) -> None:
+ """ES's listed multiplier is 50 -- the default's own value."""
+ assert _MULTIPLIER["ES"] == _DEFAULT_MULTIPLIER
+ engine = _engine("ESZ4")
+ assert engine.get_contract_multiplier("ESZ4") == float(_DEFAULT_MULTIPLIER)
+ engine.calc_commission_for_symbol("ESZ4", 1, 6400.0, True)
+
+ assert _assumptions(engine) == {}
+
+ def test_a_fully_covered_run_declares_nothing(self) -> None:
+ engine = _engine("ESZ4")
+ engine.get_contract_multiplier("ESZ4")
+ engine.calc_commission_for_symbol("ESZ4", 1, 6400.0, True)
+
+ assert engine._engine_diagnostics() == {}
+
+ def test_a_commission_override_is_not_a_table_default(self) -> None:
+ """A configured override never consults the table, so nothing is assumed."""
+ engine = _engine("ZZZF25", commission_per_contract=0.99)
+ assert engine.calc_commission(2.0, 400.0, 1, True) == 1.98
+
+ assert _assumptions(engine) == {}
+
+ def test_the_warning_fires_once_per_field_not_once_per_bar(
+ self, caplog: pytest.LogCaptureFixture
+ ) -> None:
+ engine = _engine("ZZZF25")
+ with caplog.at_level(
+ logging.WARNING, logger="backtest.engines.global_futures"
+ ):
+ for _ in range(50):
+ engine.get_contract_multiplier("ZZZF25")
+
+ multiplier_warnings = [
+ record
+ for record in caplog.records
+ if "contract_multiplier" in record.getMessage()
+ ]
+ assert len(multiplier_warnings) == 1
+ assert "ZZZ" in multiplier_warnings[0].getMessage()
+
+
+class TestDiagnosticsSeam:
+ def test_base_engine_declares_nothing_by_default(self) -> None:
+ from backtest.engines.china_a import ChinaAEngine
+
+ assert ChinaAEngine({"initial_capital": 1_000_000})._engine_diagnostics() == {}
+
+ def test_the_report_reaches_the_run_metrics_not_just_the_hook(
+ self, tmp_path
+ ) -> None:
+ """A hook nobody merges into the result is the failure this guards."""
+ import numpy as np
+ import pandas as pd
+
+ index = pd.date_range("2026-01-01", periods=60, freq="D")
+ prices = np.linspace(400.0, 440.0, 60)
+ frame = pd.DataFrame(
+ {
+ "open": prices,
+ "high": prices * 1.01,
+ "low": prices * 0.99,
+ "close": prices,
+ "volume": 1e5,
+ "pre_settle": np.r_[prices[0], prices[:-1]],
+ },
+ index=index,
+ )
+
+ class _Loader:
+ def fetch(self, *args, **kwargs):
+ return {"ZZZF25": frame}
+
+ class _Signals:
+ def generate(self, data_map):
+ return {"ZZZF25": pd.Series(1.0, index=index)}
+
+ config = {
+ "codes": ["ZZZF25"],
+ "initial_capital": 1_000_000,
+ "start_date": "2026-01-01",
+ "end_date": "2026-03-01",
+ }
+ result = GlobalFuturesEngine(config).run_backtest(
+ config, _Loader(), _Signals(), tmp_path
+ )
+
+ metrics = result.get("metrics", result)
+ reported = {
+ (row["product"], row["field"]) for row in metrics["pricing_assumptions"]
+ }
+ assert ("ZZZ", "contract_multiplier") in reported
+ assert ("ZZZ", "commission") in reported