Guide

Maximum Drawdown: How to Measure Risk in a Backtest

Net profit tells you what a strategy made. Max drawdown tells you whether you could have stayed in it long enough to find out. Here is exactly how it is measured, and where TradingView's default number understates it.

11 min read

Every backtest eventually gets reduced to one question in the trader's head: could I have actually held this? Net profit does not answer that. Win rate does not answer that. The number that comes closest is maximum drawdown, the largest peak-to-trough decline the equity curve experienced anywhere in the test period. A strategy that tripled an account but fell 70% at some point along the way is a strategy almost nobody would have stayed in, no matter how good the final number looks in the TradingView Strategy Tester summary.

This guide covers exactly how max drawdown is calculated, why it is a path-dependent measure and not just a difference between a high and a low, the specific way TradingView's default reporting understates it, and how to build your own live drawdown tracker directly into a Pine Script strategy.

What max drawdown actually measures

Max drawdown is computed by walking an equity curve from left to right, remembering the highest value seen so far, and measuring how far below that running peak every later point falls. The deepest of those gaps, expressed as a percentage of the peak, is the maximum drawdown for the whole series.

The order of events matters, which is the detail people miss most often. Max drawdown is not the difference between the highest and lowest values in a dataset. If the lowest point happened before the highest point was ever reached, no drawdown occurred at that low, because there was no prior peak to fall from yet. Shuffle the same set of daily returns into a different order and you get a different max drawdown from the identical underlying numbers. This is why two strategies with the same net profit and the same win rate can have wildly different drawdown profiles: the sequence in which wins and losses arrived is doing the work.

If you want to run this calculation on your own equity curve or account balance history without touching code, the max drawdown calculator takes a pasted series of balances and returns the peak, the trough, how many data points the decline spanned, and the gain required to recover.

Depth is not the whole picture

Two numbers usually matter as much as the percentage depth of a drawdown, and both are easy to overlook because the Strategy Tester headline figure does not surface them directly.

The first is duration. A 25% drawdown that recovers within three weeks is a completely different experience from a 25% drawdown that grinds on for fourteen months with no new equity high. The percentage is identical. The psychological and practical cost is not. TradingView does not report drawdown duration as a headline number, but you can read it off the equity curve chart in the Strategy Tester by finding the distance between the peak and the point where the curve finally exceeds it again.

The second is the asymmetry of recovery. Losses and the gains needed to undo them do not scale one to one, because a loss shrinks the base you have left to compound from. A 20% drawdown needs a 25% gain to get back to even. A 50% drawdown needs a 100% gain. A 80% drawdown needs a 400% gain. This asymmetry accelerates quickly, which is exactly why deep drawdowns are so dangerous even when the strategy behind them eventually recovers on paper. The drawdown recovery calculator makes this curve concrete for any starting loss percentage.

The closed-trade blind spot

Here is the detail that catches most people who trust the Strategy Tester's headline drawdown figure without reading the fine print. By default, TradingView calculates equity, and therefore drawdown, only at the moment a trade closes. The value of an open position while it is still live is not included in the running equity curve unless you change the calculation settings.

BasisWhat it measuresWhat TradingView shows by default
Closed-trade equityBalance is updated only when a position closesYes, unless you enable calc_on_every_tick
Intrabar equityBalance is updated on every tick while a position is openOnly with calc_on_every_tick or bar magnifier enabled

This means a trade that dipped 35% underwater intrabar before eventually closing at a small profit contributes nothing to the reported drawdown. The Tester only sees the entry and the final exit. If your strategy holds positions for any meaningful length of time, or trades a volatile instrument, the true worst-case equity exposure during the test period can be considerably deeper than the closed-trade number the summary panel shows you.

Enabling calc_on_every_tick in your strategy() declaration, or turning on bar magnifier data where available, brings intrabar equity into the calculation and typically pushes the reported drawdown wider, sometimes substantially. It also changes fill timing and simulation speed, so it is not something to flip on casually for every script. What it buys you is an honest answer to how bad the ride actually was, rather than how bad it looked at the specific moments trades happened to close.

Building a live drawdown tracker in Pine Script

The Strategy Tester will compute max drawdown for you automatically, but understanding the mechanism is worth doing once, both to sanity check the Tester's number and because plotting drawdown live on the chart makes it visible during development rather than buried in a summary tab you check only at the end.

Generating a Pine Script strategy with live risk metrics from a plain-English description
pine
//@version=6
strategy("Drawdown tracker", overlay = true)

// ... entry and exit rules go here ...

// TradingView's Strategy Tester reports max drawdown for you, but tracking it
// yourself is a one-time pattern worth knowing: 'var' keeps the peak alive
// across bars instead of resetting it every time.
var float peakEquity = na
peakEquity := na(peakEquity) ? strategy.equity : math.max(peakEquity, strategy.equity)

drawdownPct = (peakEquity - strategy.equity) / peakEquity * 100

var float worstDrawdown = 0.0
worstDrawdown := math.max(worstDrawdown, drawdownPct)

plot(drawdownPct,   "Current drawdown %", color = color.new(color.red, 0))
plot(worstDrawdown, "Max drawdown %",     color = color.new(color.gray, 0))

The mechanism is the same one the drawdown calculator uses: track the highest equity value seen so far, then measure the gap between that running peak and the current value on every bar. The detail that trips people up is the var keyword on peakEquity and worstDrawdown. Without it, both variables would reinitialize to their starting value on every single bar, which means the running peak would never actually accumulate and the drawdown would always read as roughly zero. var tells Pine Script to initialize the variable exactly once, on the first bar, and keep whatever value it holds across every bar after that.

Plotting both the current drawdown and the running worst drawdown gives you two different signals during development. The current drawdown line shows you, at any point in the backtest, how far underwater the equity curve is right now. The worst drawdown line only moves when a new record low is set, so it acts as a running high-water mark for how bad things have gotten so far. Watching both together while stepping through a backtest makes it much easier to spot exactly which trade or sequence of trades caused the deepest damage, rather than only learning the final number after the fact.

Reading max drawdown honestly

A few habits separate a useful drawdown reading from a misleading one. Treat the reported maximum as the worst thing that happened to occur in the specific window you tested, not as a ceiling on what is possible. A longer test, a different market regime, or simple bad luck can always produce a deeper decline than anything in your sample. Do not compare drawdown figures across strategies tested on different date ranges or different instruments; the comparison is only meaningful when the conditions are held constant.

It is also worth deciding, before you look at the results, what maximum drawdown you are actually willing to trade through. A strategy with an excellent profit factor and a 65% historical drawdown is not automatically a bad strategy, but it requires an amount of conviction and capital allocation that most traders do not have and should not pretend to have. Setting that threshold in advance, rather than rationalizing whatever number the backtest produced, is what keeps a good-looking equity curve from becoming a real account you cannot bring yourself to stay in. The guide on why backtest results differ from live trading covers the other gaps, beyond drawdown, between what the Tester reports and what live trading actually delivers.

From measuring risk to building the strategy that produces it

Every drawdown figure in this guide only exists once a trading idea has been turned into code that runs in the Strategy Tester. That step, going from a rule in your head to a working strategy() script, is where most people either stall or end up with a script that does not actually implement what they intended. Describing the entry, exit, and position sizing rules in plain English to PineScripter produces Pine Script v6 that compiles on the first paste, so the next thing you see is a real equity curve and a real drawdown number rather than a syntax error. For the full path from indicator logic to a backtestable strategy, see the guide on converting an indicator to a strategy.


Disclaimer: PineScripter is a coding tool for Pine Script development. It does not provide financial advice and does not guarantee trading profits. Always backtest strategies thoroughly and understand the risks before live trading.