Contents Lesson 15 of 19

9 min read · professional

Measure drawdown properly

By the end of this lesson you will have time under water and return over drawdown beside the maximum drawdown you already print, all three under both curves, and the longest underwater stretch of your strategy shaded on the chart with its dates.

Total return is one number about the end. Drawdown is about the middle, which is where people actually quit.

The definition, and the loop

Maximum drawdown is the worst peak-to-trough decline in the equity curve. One pass, running maximum:

let peak = -Infinity, worst = 0;
for (const v of equity) {
  peak = Math.max(peak, v);
  worst = Math.min(worst, v / peak - 1);
}

Two mistakes that produce a smaller, friendlier number: comparing to the final value rather than the running peak, and computing on monthly samples of a daily curve — a monthly series never sees the intramonth low, so it reports a shallower hole than the one you would have lived in.

The number that makes it real

SPY, adjusted close, measured live over 2007–2014:

Peak 2007-10-09, 110.863
Trough 2009-03-09, 49.6787
Maximum drawdown −55.2%
Recovery 2012-08-16
Time under water 1,224 trading days — 4.9 years

Those are total-return figures, and the convention is half the answer. On raw close the same episode is −56.5%, from 156.48 down to 68.11, and the peak was not regained until 14 March 2013 — seven months later than the row above. The gap is dividends. A price chart shows you the second pair; a dividend-adjusted series shows you the first, and a report that does not say which is quoting a number nobody can reproduce.

Look at the last row rather than the fourth. A −55% drawdown that recovers in a month and one that takes nearly five years are identical on a return chart and completely different to live through. Almost nobody holds a rule for 1,224 trading days while it is below where it started.

Print three, not one

  • Maximum drawdown — the depth.
  • Time under water — longest run below a prior peak, in trading days. The one people omit and the one that decides whether a strategy is survivable.
  • Return divided by max drawdown — a crude but honest units-of-pain figure. It is not a Sharpe ratio and should not be called one.

Deliberately not here: Sharpe. It needs a risk-free rate, an annualisation convention and a distributional assumption you have not examined, and quoting it early gives a number more authority than the work behind it. The performance-measurement course in Portfolio Management does it properly.

Compare the middles, not just the ends

Put your strategy's three numbers beside buy-and-hold's. A rule that returns slightly less with half the drawdown is a genuinely better tool for a human, and the total-return column alone will never tell you that.

The finance behind it

Both figures are treated properly in Portfolio Management, including the one this course prints and most reports omit: What does a drawdown actually measure? and How long were you under water?. The Sharpe ratio this course deliberately skips is done there too: How do you compute a Sharpe ratio without fooling yourself?

Try it now

Add time under water to your report, then find the longest underwater stretch in your own strategy and mark those dates on the chart. Ask yourself honestly whether you would have kept running the rule through that stretch. That answer is a real result, and it is not in any statistic.

The prompt

Work one short equity series out by hand first, and keep your answers: a curve of 1, 1.2, 0.9, 1.0, 1.3 is 25% deep and two bars under water, and its return over drawdown is 1.2 (a 30% return over a 25% hole). Mock mode on, plan mode on, and paste your series and answers under this:

Plan first. In src/lib/backtest/metrics.ts keep the maximum drawdown
against the running peak that is already there, and add
timeUnderWater(curve, dates): the longest run
of bars below a prior peak, with its first and last date, where a bar
back at the peak counts as recovered; and returnOverDrawdown: total
return divided by the size of the maximum drawdown. Print all three
under both curves, and shade the strategy's longest underwater stretch
on the chart. Tests first in tests/metrics.test.ts, on the series and
answers below, worked out by hand. No request. Run npm test, then show
me the diff.

A good answer changes metrics.ts, EquityChart.tsx and the page, adds the test with your numbers in it, and makes no request.

Check it yourself

  1. The expected values in the test are the ones you worked out, not ones the agent computed with the code it was testing.
  2. A bar exactly back at the peak ends the stretch, and a test says so.
  3. In mock mode three numbers sit under each curve, and the strategy's longest stretch is shaded with its dates printed.
  4. If your history reaches back before 2007, buy and hold shows the 2007–2009 hole as its maximum drawdown, near −55%, and a longest stretch under water well past a thousand trading days. On a shorter history it shows only what your window held, which is the subject of the lesson after next.

Compare with ours

Our time under water
// src/lib/backtest/metrics.ts
export function timeUnderWater(curve: number[], dates: string[]) {
  let peak = -Infinity, run = 0, longest = 0, end = 0;
  for (let i = 0; i < curve.length; i++) {
    if (curve[i] >= peak) { peak = curve[i]; run = 0; }   // back at the peak: recovered
    else if (++run > longest) { longest = run; end = i; }
  }
  return longest === 0 ? null : { days: longest, from: dates[end - longest + 1], to: dates[end] };
}

The full state at the end of this unit is on the starter's branch ref/backtest-strategies/u4.

Checkpoint

npm test && npm run typecheck
git status   # only the files this step touched, no .env*
git add -A
git commit -m "Drawdown against the running peak, time under water, and return over drawdown"

Then answer this lesson's questions.