Python

accumulate: equity curve and max drawdown

Running totals with itertools.accumulate: a running sum for the equity curve, a running max for the peaks, and drawdown from their difference.

Prerequisites

Python 3.9+

Python
from itertools import accumulate

pnl_journalier = [120.5, -80.0, 45.2, -210.0, 95.0]

# Equity curve : cumul des P&L à partir du capital initial
equity = list(accumulate(pnl_journalier, initial=10_000))

# Plus haut historique (running max) pour calculer le drawdown
peaks = list(accumulate(equity, max))
drawdowns = [e - p for e, p in zip(equity, peaks)]

max_dd = min(drawdowns)
print(f"Max drawdown : {max_dd:.2f}")

Result

>>> equity
[10000, 10120.5, 10040.5, 10085.7, 9875.7, 9970.7]
>>> [round(d, 2) for d in drawdowns]
[0, 0.0, -80.0, -34.8, -244.8, -149.8]

Max drawdown : -244.80
itertoolsaccumulateFinanceDrawdown

Related snippets

Back to the Data Lab