Pandasdrill

Level 5: Analysis Exercise 79 of 102

Drawdown from the running high

A pandas exercise from Pandasdrill. Solve it with a Python editor in your browser running real pandas, and it is checked the moment you submit.

The task

For each day in `prices`, return how far the `close` is below the highest close seen so far, as a fraction: the close divided by the running maximum, minus 1. A day that sets a new high gives 0, and a close of 90 after a high of 100 gives -0.1. Return a Series aligned with the original rows.

What this exercise teaches

cummax is the running maximum, the same idea as cumsum for totals. Dividing by it compares every close with the peak before it, which is how drawdown is measured. Dividing by max() would compare with the overall peak, including highs that had not happened yet.

A hint if you get stuck

cummax() gives the highest value up to and including each row. Divide the column by it and subtract 1.

Try it now, free, no account needed

Your first 8 exercises on Pandasdrill are free. Open it on the practice page, solve it, and get instant feedback with a hint. A free account adds the worked solution; Pro unlocks every exercise, timed mode and the certificate.

Open this exercise

More exercises in Analysis