Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(options): honor partial-close quantity instead of flattening the lot

Haozhe Wuaaa1061b783f
diff --git a/agent/backtest/engines/options_portfolio.py b/agent/backtest/engines/options_portfolio.py
index c6346232d..4ef4a01c1 100644
--- a/agent/backtest/engines/options_portfolio.py
+++ b/agent/backtest/engines/options_portfolio.py
@@ -433,12 +433,26 @@ def run_options_backtest(
})
elif action == "close":
- # Close: find matching position
+ # Close: find matching position, honoring a partial-close qty.
matched = _find_matching_position(
positions, underlying, leg_type, strike, expiry)
if matched:
- pnl = (opt_price - matched.entry_price) * matched.qty * contract_multiplier
- abs_close = opt_price * abs(matched.qty) * contract_multiplier
+ # An explicit leg ``qty`` closes only that many contracts
+ # (clamped to the open size); a close leg with no ``qty``
+ # closes the whole lot (legacy behavior). Cash/PnL and the
+ # remaining position all scale to the amount actually closed
+ # so a partial close no longer flattens the lot (#577).
+ requested = leg.get("qty")
+ full_mag = abs(matched.qty)
+ close_mag = full_mag if requested is None else min(abs(requested), full_mag)
+ if close_mag <= 0:
+ continue
+ sign = 1 if matched.qty > 0 else -1
+ closed_qty = sign * close_mag
+ remaining_qty = matched.qty - closed_qty
+
+ pnl = (opt_price - matched.entry_price) * closed_qty * contract_multiplier
+ abs_close = opt_price * close_mag * contract_multiplier
if matched.qty > 0:
# Long close: sell to recover
cash += abs_close * (1 - commission)
@@ -454,11 +468,24 @@ def run_options_backtest(
"expiry": expiry,
"side": "close",
"price": round(opt_price, 4),
- "qty": matched.qty,
+ "qty": closed_qty,
"pnl": round(pnl, 4),
"entry_date": matched.entry_date,
})
- positions.remove(matched)
+ if abs(remaining_qty) < 1e-9:
+ positions.remove(matched)
+ else:
+ # Reduce the open lot to its remainder instead of
+ # removing it (new object; positions stay immutable).
+ positions[positions.index(matched)] = OptionPosition(
+ option_type=matched.option_type,
+ strike=matched.strike,
+ expiry=matched.expiry,
+ qty=remaining_qty,
+ entry_price=matched.entry_price,
+ entry_date=matched.entry_date,
+ underlying_code=matched.underlying_code,
+ )
# 4. Compute portfolio mark-to-market value and Greeks
portfolio_value = cash
diff --git a/agent/tests/test_options_partial_close.py b/agent/tests/test_options_partial_close.py
new file mode 100644
index 000000000..cebc5c241
--- /dev/null
+++ b/agent/tests/test_options_partial_close.py
@@ -0,0 +1,119 @@
+"""Regression (#577): options close honors a partial-close quantity.
+
+The options engine's ``close`` branch used to compute cash/PnL from the full
+matched lot and remove the whole position, ignoring the leg's requested ``qty``.
+A partial close therefore flattened the entire lot. These tests pin the new
+behavior: an explicit ``qty`` closes only that many contracts and leaves the
+remainder open; a close leg with no ``qty`` still closes the whole lot.
+"""
+
+from __future__ import annotations
+
+from pathlib import Path
+
+import pandas as pd
+
+from backtest.engines.options_portfolio import run_options_backtest
+
+_DATES = pd.bdate_range("2025-01-01", periods=4)
+_BARS = pd.DataFrame(
+ {
+ "open": [100.0, 101.0, 102.0, 103.0],
+ "high": [101.0, 102.0, 103.0, 104.0],
+ "low": [99.0, 100.0, 101.0, 102.0],
+ "close": [100.5, 101.5, 102.5, 103.5],
+ "volume": [1000, 1100, 1200, 1300],
+ },
+ index=_DATES,
+)
+
+
+class _FakeLoader:
+ name = "yfinance"
+
+ def fetch(self, codes, start_date, end_date): # noqa: ANN001
+ return {"SPY": _BARS.copy()}
+
+
+def _signal_engine(close_qty):
+ """Build a signal engine that opens 10 contracts then closes ``close_qty``.
+
+ ``close_qty=None`` omits the leg qty entirely (legacy full-close path).
+ """
+ close_leg = {"type": "call", "strike": 101.0, "expiry": "2025-03-21"}
+ if close_qty is not None:
+ close_leg["qty"] = close_qty
+
+ class _Engine:
+ def generate(self, data_map): # noqa: ANN001
+ return [
+ {
+ "date": "2025-01-01",
+ "action": "open",
+ "underlying": "SPY",
+ "legs": [
+ {"type": "call", "strike": 101.0, "expiry": "2025-03-21", "qty": 10}
+ ],
+ },
+ {
+ "date": "2025-01-03",
+ "action": "close",
+ "underlying": "SPY",
+ "legs": [close_leg],
+ },
+ ]
+
+ return _Engine()
+
+
+def _run(tmp_path: Path, close_qty):
+ run_options_backtest(
+ {
+ "codes": ["SPY"],
+ "start_date": "2025-01-01",
+ "end_date": "2025-01-06",
+ "source": "yfinance",
+ "engine": "options",
+ "initial_cash": 100_000,
+ },
+ _FakeLoader(),
+ _signal_engine(close_qty),
+ tmp_path,
+ )
+ artifacts = tmp_path / "artifacts"
+ trades = pd.read_csv(artifacts / "trades.csv")
+ greeks = pd.read_csv(artifacts / "greeks.csv")
+ return trades, greeks
+
+
+def test_partial_close_closes_only_requested_qty(tmp_path: Path) -> None:
+ trades, greeks = _run(tmp_path, close_qty=4)
+
+ closes = trades[trades["side"] == "close"]
+ assert len(closes) == 1
+ # Only 4 of the 10 contracts closed — not the whole lot.
+ assert closes.iloc[0]["qty"] == 4
+
+ # 6 contracts remain open, so the final-day total delta stays non-zero.
+ final_delta = float(greeks.iloc[-1]["delta"])
+ assert abs(final_delta) > 1e-6
+
+
+def test_full_close_without_qty_flattens_lot(tmp_path: Path) -> None:
+ trades, greeks = _run(tmp_path, close_qty=None)
+
+ closes = trades[trades["side"] == "close"]
+ assert len(closes) == 1
+ # Legacy behavior preserved: the whole 10-lot closes.
+ assert closes.iloc[0]["qty"] == 10
+ assert abs(float(greeks.iloc[-1]["delta"])) < 1e-9
+
+
+def test_close_qty_exceeding_open_clamps_to_lot(tmp_path: Path) -> None:
+ trades, greeks = _run(tmp_path, close_qty=25)
+
+ closes = trades[trades["side"] == "close"]
+ assert len(closes) == 1
+ # Requested 25 but only 10 open — clamp, don't over-close.
+ assert closes.iloc[0]["qty"] == 10
+ assert abs(float(greeks.iloc[-1]["delta"])) < 1e-9