Maximum Drawdown in Python
The largest peak-to-trough decline in portfolio value observed over a given period.
Definition
Maximum Drawdown (MDD) is the single most important risk metric in quantitative finance. It represents the maximum observed loss from a historical peak in portfolio value to the subsequent trough before a new peak is attained. It does not measure frequency of losses but rather the worst-case scenario an investor would have had to endure — psychologically and financially. MDD is expressed as a percentage and is always negative or zero.
Quantitative Formula
Where is the portfolio peak value at time , is the portfolio value at time after the peak, and is the end of the evaluation period. The formula captures the worst trough relative to the best preceding peak across the entire time series.
Why It Matters in Backtesting
A backtest that ignores MDD is dangerously incomplete. A strategy with 100% annual returns but a 90% MDD is practically undeployable — no human investor can psychologically or financially survive a 90% drawdown waiting for recovery. Most professional risk mandates cap acceptable MDD at 15–25%. Backtesting must simulate MDD under realistic slippage and transaction costs to avoid false confidence.
Python Implementation
import numpy as np
import pandas as pd
def calculate_max_drawdown(returns: pd.Series) -> dict:
"""Calculates Maximum Drawdown, peak date, and trough date."""
cumulative = (1 + returns).cumprod()
peak = cumulative.cummax()
drawdown = (cumulative - peak) / peak
max_dd = drawdown.min()
trough_date = drawdown.idxmin()
peak_date = cumulative[:trough_date].idxmax()
return {
"max_drawdown": max_dd,
"peak_date": peak_date,
"trough_date": trough_date,
"drawdown_series": drawdown
}Test this in a live environment
Stop running Jupyter notebooks locally. Paste this Maximum Drawdown code directly into Valetha's Strategy Lab and run a full historical backtest in seconds.
Open the Python Strategy Lab