Guides

Plot an interactive drawdown (underwater) curve in Python

Drawdown is the distance from the running equity peak. Here's the 8-line Plotly version, and a one-line tearsheet.

Why it happens

Drawdown at time t is equity / running_max(equity) - 1. Use compounded equity, not summed returns, and start the running max at 1.0 so an early loss counts as a drawdown.

Fix it with plain pandas

import numpy as np
import plotly.graph_objects as go

equity = (1 + returns.fillna(0)).cumprod()
drawdown = equity / np.maximum.accumulate(np.maximum(equity, 1.0)) - 1

fig = go.Figure(go.Scatter(x=drawdown.index, y=drawdown, fill="tozeroy", line_color="orange"))
fig.update_layout(template="plotly_dark", yaxis_tickformat=".0%", title="Underwater curve")
fig.show()

Or with Deptho

The tearsheet adds rolling Sharpe/Sortino, alpha decay and, if you pass positions, funding and fee drag. Save it as one HTML file to send to an allocator.

import deptho as dp

dp.tearsheet(returns, title="My strategy").show()     # or .save("report.html")

Related