fix(backtest): max drawdown now correctly measures peak-to-trough percentage

Previous formula was: max(absolute_drawdown) / initial_capital, which
exceeds 100% when the portfolio grows then drops (e.g. from 600k to 300k
on a 100k initial = 300% drawdown, which is nonsensical).

Fixed to use drawdown_pct (drawdown / peak) which is always in [0, 1].
This correctly measures the maximum percentage drop from the highest
equity peak, matching the standard financial definition.

Also added regression test: test_max_drawdown_never_exceeds_100_pct.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
GitHub
2026-06-09 19:00:11 +08:00
co-authored by Claude Opus 4.8
parent 5550702620
commit 46298e68d7
3 changed files with 37 additions and 6 deletions
+33 -2
View File
@@ -31,11 +31,13 @@ def _make_equity_curve(n: int = 252, total_return: float = 0.1) -> pd.DataFrame:
# 计算回撤
peak = np.maximum.accumulate(total)
drawdown = peak - total
drawdown_pct = np.divide(drawdown, peak, out=np.zeros_like(drawdown), where=(peak != 0))
return pd.DataFrame({
"datetime": np.arange(n),
"total": total,
"drawdown": drawdown,
"drawdown_pct": drawdown_pct,
})
@@ -65,6 +67,35 @@ def test_total_return() -> None:
assert abs(metrics["total_return"] - 0.1) < 0.01
def test_max_drawdown_never_exceeds_100_pct() -> None:
"""测试最大回撤永远不超过 100%(从峰值的跌幅)。"""
# 模拟先涨 5 倍再腰斩的资金曲线
total = np.concatenate([
np.linspace(100000, 600000, 126), # 涨到 60 万
np.linspace(600000, 300000, 126), # 跌到 30 万
])
peak = np.maximum.accumulate(total)
drawdown = peak - total
drawdown_pct = np.divide(drawdown, peak, out=np.zeros_like(drawdown), where=(peak != 0))
equity = pd.DataFrame({
"datetime": np.arange(252),
"total": total,
"drawdown": drawdown,
"drawdown_pct": drawdown_pct,
})
trades = _make_trades()
analyzer = PerformanceAnalyzer(equity, trades)
metrics = analyzer.compute()
# 最大回撤 = 从峰值跌 50%(30万 / 60万),不应超过 1.0
assert 0.0 <= metrics["max_drawdown"] <= 1.0, (
f"max_drawdown = {metrics['max_drawdown']:.2%}, should be in [0, 100%]"
)
assert abs(metrics["max_drawdown"] - 0.5) < 0.01
def test_max_drawdown_zero_when_monotonic() -> None:
"""测试单调递增时最大回撤接近 0。"""
equity = _make_equity_curve(n=252, total_return=0.1)
@@ -163,7 +194,7 @@ def test_all_keys_present() -> None:
def test_empty_equity_curve() -> None:
"""测试空资金曲线返回全零指标。"""
equity = pd.DataFrame({"total": [], "drawdown": []})
equity = pd.DataFrame({"total": [], "drawdown": [], "drawdown_pct": []})
trades = _make_trades()
analyzer = PerformanceAnalyzer(equity, trades)
@@ -175,7 +206,7 @@ def test_empty_equity_curve() -> None:
def test_single_point_equity_curve() -> None:
"""测试只有一个点的资金曲线返回全零指标。"""
equity = pd.DataFrame({"total": [100000], "drawdown": [0]})
equity = pd.DataFrame({"total": [100000], "drawdown": [0], "drawdown_pct": [0.0]})
trades = _make_trades()
analyzer = PerformanceAnalyzer(equity, trades)