Sling Academy
Home/Python/Visualizing Drawdowns and Underwater Curves with quantstats

Visualizing Drawdowns and Underwater Curves with quantstats

Last updated: December 22, 2024

In portfolio analysis, understanding the performance downside is as crucial as strategy returns or growth. Drawdowns and underwater curves are critical metrics for assessing an investment’s health and risk management. By visualizing these concepts, one gets a more nuanced perspective on financial performance. quantstats, a powerful library in Python, makes this task exceedingly user-friendly. This article will guide you on how to visualize drawdowns and underwater curves using quantstats.

What Are Drawdowns and Underwater Curves?

Before diving into the practical aspect, it is essential to understand these financial terminologies. A drawdown represents the decline in the value of an investment, usually described as a percentage from the peak to the trough. It signifies how much an investment is down from its peak before it recovers to set a new peak.

An underwater curve is a visual representation that portrays the drawdown over time, showing how long and deep the drawdowns are, and thus, how difficult it would be for the investment to recover its losses.

Setting Up Your Environment

Firstly, ensure you have quantstats installed into your Python environment. If you haven't done so already, you can install it with the following command:

pip install quantstats

quantstats depends on a few other libraries such as numpy and pandas. These will usually be handled with the installation of quantstats, but you may need to manually install them as well:

pip install numpy pandas

Visualizing Drawdowns

Let's look at a simple example of visualizing drawdowns with quantstats. Suppose you have a Pandas DataFrame or Series of returns, for the sake of simplicity, let's simulate some data:

import quantstats as qs
import pandas as pd
import numpy as np

# simulate daily returns
np.random.seed(123)
simulated_returns = pd.Series(np.random.normal(0, 0.01, 1000), name='Asset Returns')

# plot drawdown
qs.plots.drawdown(simulated_returns)

In this example, we simulate daily returns and visualize the drawdown of an investment portfolio. With just a few lines of code, quantstats provides a neat visualization that highlights peak points and corresponding drawdowns.

Understanding Underwater Plots

The underwater plot is essentially the time-based reporting of drawdowns. It measures the depth of the drawdown over periods. It offers an intuitive way to assess how long and deep each drawdown phase lasted:

# plot underwater
qs.plots.underwater(simulated_returns)

This line of code will generate a plot that clearly shows the data over time with respect to its drawdowns. The y-axis indicates the percentage drawdown, and the x-axis represents time, enabling investors to understand recovery spans visually.

Real-World Example

For a practical example, you would generally use historical actual data:

import yfinance as yf

# Download historical data for a ticker
stock_data = yf.download('AAPL', start='2020-01-01', end='2023-01-01')

daily_returns = stock_data['Adj Close'].pct_change().dropna()

qs.plots.drawdown(daily_returns)
qs.plots.underwater(daily_returns)

In this real-world example, we leverage yfinance to fetch historical stock prices for Apple Inc. (AAPL) and compute daily returns. The quantstats functions qs.plots.drawdown() and qs.plots.underwater() are used to visualize those financial metrics, unlocking insights into potential risk exposures and recovery lengths.

Conclusion

Visualizing drawdowns and underwater curves provides an appealing and comprehensible representation of financial performance, especially when assessing risks. The ease and simplicity quantstats offers through its intuitive functions for evaluating complex risk-related metrics make it an invaluable tool for analysts and data-driven investors.

Next Article: Analyzing Risk-Adjusted Returns with quantstats Metrics

Previous Article: Integrating quantstats with Backtrader or Zipline for Analysis

Series: Algorithmic trading with Python

Python

You May Also Like

  • Introduction to yfinance: Fetching Historical Stock Data in Python
  • Monitoring Volatility and Daily Averages Using cryptocompare
  • Advanced DOM Interactions: XPath and CSS Selectors in Playwright (Python)
  • Automating Strategy Updates and Version Control in freqtrade
  • Setting Up a freqtrade Dashboard for Real-Time Monitoring
  • Deploying freqtrade on a Cloud Server or Docker Environment
  • Optimizing Strategy Parameters with freqtrade’s Hyperopt
  • Risk Management: Setting Stop Loss, Trailing Stops, and ROI in freqtrade
  • Integrating freqtrade with TA-Lib and pandas-ta Indicators
  • Handling Multiple Pairs and Portfolios with freqtrade
  • Using freqtrade’s Backtesting and Hyperopt Modules
  • Developing Custom Trading Strategies for freqtrade
  • Debugging Common freqtrade Errors: Exchange Connectivity and More
  • Configuring freqtrade Bot Settings and Strategy Parameters
  • Installing freqtrade for Automated Crypto Trading in Python
  • Scaling cryptofeed for High-Frequency Trading Environments
  • Building a Real-Time Market Dashboard Using cryptofeed in Python
  • Customizing cryptofeed Callbacks for Advanced Market Insights
  • Integrating cryptofeed into Automated Trading Bots