Get Started

Historical drawdown

The stock market tends to rise over time, but that doesn't mean that you won't have periods of drawdown.

Drawdown can be measured as the percentage loss from the highest cumulative historical point.

In Python, you can use the .accumulate() and .maximum() functions to calculate the running maximum, and the simple formula below to calculate drawdown:

$$ \text{Drawdown} = \frac{r_t}{RM} - 1$$

  • \(r_t\): Cumulative return at time t
  • \(RM\): Running maximum

The cumulative returns of USO, an ETF that tracks oil prices, is available in the variable cum_rets.

This is a part of the course

“Introduction to Portfolio Risk Management in Python”

View Course

Exercise instructions

  • Calculate the running maximum of the cumulative returns of the USO oil ETF (cum_rets) using np.maximum.accumulate().
  • Where the running maximum (running_max) drops below 1, set the running maximum equal to 1.
  • Calculate drawdown using the simple formula above with the cum_rets and running_max.
  • Review the plot.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Calculate the running maximum
running_max = ____(cum_rets)

# Ensure the value never drops below 1
running_max[____] = 1

# Calculate the percentage drawdown
drawdown = (____)/____ - 1

# Plot the results
drawdown.plot()
plt.show()

This exercise is part of the course

Introduction to Portfolio Risk Management in Python

IntermediateSkill Level
4.5+
11 reviews

Evaluate portfolio risk and returns, construct market-cap weighted equity portfolios and learn how to forecast and hedge market risk via scenario generation.

In this chapter, you will learn two different methods to estimate the probability of sustaining losses and the expected values of those losses for a given asset or portfolio of assets.

Exercise 1: Estimating tail riskExercise 2: Historical drawdown
Exercise 3: Historical value at riskExercise 4: Historical expected shortfallExercise 5: VaR extensionsExercise 6: Changing VaR and CVaR quantilesExercise 7: Parametric VaRExercise 8: Scaling risk estimatesExercise 9: Random walksExercise 10: A random walk simulationExercise 11: Monte Carlo simulationsExercise 12: Monte Carlo VaRExercise 13: Understanding risk

What is DataCamp?

Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.

Start Learning for Free