"Vibe-Trading: Your Personal Trading Agent"
Python ai-agentalgorithmic-tradingbacktestingfintechllmmcpmulti-agentpythonquantitative-financetrading
fix(options): seed the max-drawdown peak with initial cash
Yufeng He51b369fa4544
diff --git a/agent/backtest/engines/options_portfolio.py b/agent/backtest/engines/options_portfolio.pyindex a82e533e3..f5500d33e 100644--- a/agent/backtest/engines/options_portfolio.py+++ b/agent/backtest/engines/options_portfolio.py@@ -776,6 +776,12 @@ def _calc_options_metrics(max_dd: float | None = Noneif path_is_finite:peak = equity_vals.cummax()+ # The account holds initial_cash before the first recorded bar, so+ # that cash is the real high-water mark; without the seed a first-bar+ # loss (commission alone causes one) never shows up in the drawdown.+ # Same convention as backtest.metrics.calc_metrics.+ if valid_initial_cash:+ peak = peak.clip(lower=float(initial_cash))if bool((peak > 0).all()):dd = (equity_vals - peak) / peakmax_dd = float(dd.min())diff --git a/agent/tests/test_options_portfolio_correctness.py b/agent/tests/test_options_portfolio_correctness.pyindex 117f73d35..3659be1d5 100644--- a/agent/tests/test_options_portfolio_correctness.py+++ b/agent/tests/test_options_portfolio_correctness.py@@ -262,3 +262,36 @@ def test_sortino_undefined_cases_remain_json_safe(assert metrics["sortino"] is Noneassert any("Sortino" in warning for warning in metrics["warnings"])json.dumps(metrics, allow_nan=False)+++def test_max_drawdown_counts_the_drop_from_initial_cash() -> None:+ # The first recorded bar already reflects the open's commission, so the+ # account's real high-water mark (initial cash) is not in the series.+ equity = pd.Series([95.0, 90.0, 99.0])+ metrics = _calc_options_metrics(equity, 100.0, [], bars_per_year=252)++ assert metrics["max_drawdown"] == pytest.approx(-0.10)+ json.dumps(metrics, allow_nan=False)+++def test_max_drawdown_seeds_initial_cash_on_a_single_bar() -> None:+ metrics = _calc_options_metrics(pd.Series([90.0]), 100.0, [], bars_per_year=252)++ assert metrics["max_drawdown"] == pytest.approx(-0.10)+ json.dumps(metrics, allow_nan=False)+++def test_max_drawdown_stays_zero_on_an_all_up_path() -> None:+ metrics = _calc_options_metrics(+ pd.Series([100.0, 105.0, 110.0]), 100.0, [], bars_per_year=252+ )++ assert metrics["max_drawdown"] == 0.0+ json.dumps(metrics, allow_nan=False)+++def test_max_drawdown_falls_back_to_observed_peak_without_valid_initial_cash() -> None:+ metrics = _calc_options_metrics(pd.Series([95.0, 90.0]), 0.0, [], bars_per_year=252)++ assert metrics["max_drawdown"] == pytest.approx(round((90.0 - 95.0) / 95.0, 6))+ json.dumps(metrics, allow_nan=False)