"Vibe-Trading: Your Personal Trading Agent"
Python ai-agentalgorithmic-tradingbacktestingfintechllmmcpmulti-agentpythonquantitative-financetrading
fix(backtest): fill options signals on the next bar, not the signal date (#1293)
cgycorey677ebed8f47b
diff --git a/agent/backtest/engines/options_portfolio.py b/agent/backtest/engines/options_portfolio.pyindex 4389f91b2..66aaad657 100644--- a/agent/backtest/engines/options_portfolio.py+++ b/agent/backtest/engines/options_portfolio.py@@ -213,6 +213,7 @@ def run_options_backtest(exercise_style = options_cfg.get("exercise_style", "european") # v2: "european" or "american"iv_skew = options_cfg.get("iv_skew", 0.0) # v2: smile skew param (0 = flat)iv_curvature = options_cfg.get("iv_curvature", 0.0) # v2: smile curvature+ same_day_fill = options_cfg.get("same_day_fill", False)# Load underlying datadata_map = loader.fetch(codes, start_date, end_date)@@ -253,9 +254,21 @@ def run_options_backtest(greeks_records: List[Dict[str, Any]] = []equity_records: List[Dict[str, Any]] = []- for current_date in dates:+ for idx, current_date in enumerate(dates):ts = pd.Timestamp(current_date)date_str = str(ts.date()) if hasattr(ts, "date") else str(ts)+ # Signals are dated the bar they were computed on and priced/filled on+ # the next bar -- the framework convention the equity engines follow+ # (#1293). A signal dated before the first evaluation bar is never+ # executed, matching how warm-up-dated signals behaved before. Set+ # options_config.same_day_fill to price a signal on its own date.+ if same_day_fill:+ signal_date = date_str+ elif idx > 0:+ prev = pd.Timestamp(dates[idx - 1])+ signal_date = str(prev.date()) if hasattr(prev, "date") else str(prev)+ else:+ signal_date = None# 1. Get underlying price and IV for the current dayspot_prices: Dict[str, float] = {}@@ -332,8 +345,8 @@ def run_options_backtest(})positions.remove(pos)- # 3. Execute today's signals- day_signals = signal_by_date.get(date_str, [])+ # 3. Execute the prior bar's signals at today's prices+ day_signals = signal_by_date.get(signal_date, []) if signal_date else []for sig in day_signals:action = sig.get("action", "")legs = sig.get("legs", [])diff --git a/agent/tests/test_options_next_bar_fill.py b/agent/tests/test_options_next_bar_fill.pynew file mode 100644index 000000000..edf41e8b6--- /dev/null+++ b/agent/tests/test_options_next_bar_fill.py@@ -0,0 +1,123 @@+"""Regression: options signals must fill on the next bar, not the signal date.++The options engine priced and filled a signal dated T with T's own close and+IV, so a signal computed on T's close embeded the information it was computed+from -- a full day of underlying move times delta on every options backtest.+The equity engines in the same framework execute the next bar; this pins the+options engine to the same convention. ``same_day_fill: True`` restores the+legacy same-date fill for strategies that want it.+"""++from __future__ import annotations++from pathlib import Path++import pandas as pd+import pytest++from backtest.engines.options_portfolio import bs_price, run_options_backtest++_DATES = pd.bdate_range("2025-01-01", periods=4)+_CLOSES = [100.0, 110.0, 90.0, 120.0]+_BARS = pd.DataFrame(+ {+ "open": _CLOSES,+ "high": [c + 1.0 for c in _CLOSES],+ "low": [c - 1.0 for c in _CLOSES],+ "close": _CLOSES,+ "volume": [1000, 1000, 1000, 1000],+ },+ index=_DATES,+)++_EXPIRY = "2025-02-21"+_STRIKE = 100.0+_QTY = 10+++class _FlatLoader:+ name = "yfinance"++ def fetch(self, codes, start_date, end_date): # noqa: ANN001+ return {"SPY": _BARS.copy()}+++def _signal(date: str):+ class _Engine:+ def generate(self, data_map): # noqa: ANN001+ return [+ {+ "date": date,+ "action": "open",+ "underlying": "SPY",+ "legs": [{"type": "call", "strike": _STRIKE, "expiry": _EXPIRY, "qty": _QTY}],+ }+ ]++ return _Engine()+++def _fill_price(day: pd.Timestamp, iv: float) -> float:+ t = max((pd.Timestamp(_EXPIRY) - day).days / 365.0, 0.001)+ return bs_price(_BARS.at[day, "close"], _STRIKE, t, 0.0, iv, "call")+++def _run(tmp_path: Path, *, date: str, same_day: bool = False):+ run_options_backtest(+ {+ "codes": ["SPY"],+ "start_date": "2025-01-01",+ "end_date": "2025-01-07",+ "source": "yfinance",+ "engine": "options",+ "initial_cash": 100_000.0,+ "commission": 0.0,+ "options_config": {+ "risk_free_rate": 0.0,+ "contract_multiplier": 1.0,+ **({"same_day_fill": True} if same_day else {}),+ },+ },+ _FlatLoader(),+ _signal(date),+ tmp_path,+ )+ return pd.read_csv(tmp_path / "artifacts" / "trades.csv")+++def test_signal_dated_t_fills_on_the_next_bar(tmp_path: Path) -> None:+ trades = _run(tmp_path, date="2025-01-02")++ assert len(trades) == 1+ assert trades.iloc[0]["timestamp"] == "2025-01-03"+ assert trades.iloc[0]["price"] == pytest.approx(+ _fill_price(pd.Timestamp("2025-01-03"), 0.3), abs=1e-4+ )+++def test_fill_price_uses_next_bar_spot_not_signal_date_spot(tmp_path: Path) -> None:+ """Pricing at 90 (2025-01-03) with an expiry 49 days out differs from 110."""+ trades = _run(tmp_path, date="2025-01-02")++ price_on_02 = bs_price(110.0, _STRIKE, max((pd.Timestamp(_EXPIRY) - pd.Timestamp("2025-01-02")).days / 365.0, 0.001), 0.0, 0.3, "call")+ assert trades.iloc[0]["price"] != pytest.approx(price_on_02)+++def test_same_day_fill_flag_preserves_legacy_behavior(tmp_path: Path) -> None:+ trades = _run(tmp_path, date="2025-01-02", same_day=True)++ assert len(trades) == 1+ assert trades.iloc[0]["timestamp"] == "2025-01-02"+ assert trades.iloc[0]["price"] == pytest.approx(+ _fill_price(pd.Timestamp("2025-01-02"), 0.3), abs=1e-4+ )+++def test_signal_dated_before_first_bar_never_executes(tmp_path: Path) -> None:+ trades = _run(tmp_path, date="2024-12-31")+ assert len(trades) == 0+++def test_signal_on_last_bar_has_no_next_bar_to_fill(tmp_path: Path) -> None:+ trades = _run(tmp_path, date="2025-01-06")+ assert len(trades) == 0diff --git a/agent/tests/test_options_smile_consistency.py b/agent/tests/test_options_smile_consistency.pyindex 2d7c8a898..6c05516d0 100644--- a/agent/tests/test_options_smile_consistency.py+++ b/agent/tests/test_options_smile_consistency.py@@ -165,10 +165,14 @@ class TestGreeksUseTheLegVol:smile_vol = leg_iv(100.0, 110.0, _BASE_IV, -0.15, 0.05)expiry = pd.Timestamp("2025-02-21")- time_to_expiry = max((expiry - _DATES[0]).days / 365.0, 0.001)+ # The signal dated 2025-01-01 fills on the next bar at the same 100.0+ # spot, so the first greeks row is zero and the position's greeks+ # appear on the fill bar (2025-01-02).+ fill_day = _DATES[1]+ time_to_expiry = max((expiry - fill_day).days / 365.0, 0.001)expected = bs_greeks(100.0, 110.0, time_to_expiry, 0.0, smile_vol, "call")- assert float(greeks.iloc[0]["delta"]) == pytest.approx(+ assert float(greeks.iloc[1]["delta"]) == pytest.approx(expected["delta"] * 10, rel=1e-6)