Compute Maximum Drawdown#

The compute_max_dd function calculates the maximum drawdown of a given pandas DataFrame of prices. Maximum drawdown is a measure of the largest decrease in the value of an investment, indicating the highest amount of loss experienced in a given time period. This is a useful metric for assessing the risk associated with an investment.

To use the compute_max_dd function, you need to provide a pandas DataFrame containing price data with a datetime index. The function works with time series data.

The function takes the input DataFrame and returns the maximum drawdown for each column in the input DataFrame.

compute_max_dd() function#

Function#

def compute_max_dd(dataframe):
    """
    Compute the maximum drawdown of a given DataFrame of prices.

    Parameters
    ----------
    dataframe : pandas.DataFrame
        DataFrame containing price data. Index should be of datetime type.

    Returns
    -------
    pandas.Series
        A pandas Series containing the maximum drawdown for each column in the input DataFrame.
    """
    return compute_drawdowns(dataframe).min()

Description#

The compute_max_dd function computes the maximum drawdown of a given pandas DataFrame of prices.

Parameters#

  • dataframe (pandas.DataFrame): DataFrame containing price data. The index should be of datetime type.

Returns#

  • pandas.Series: A pandas Series containing the maximum drawdown for each column in the input DataFrame.

Example#

import ds4finance as dsf
import yfinance as yf
import pandas as pd

def download_data(ticker, start, end):
    data = yf.download(ticker, start=start, end=end, progress=False)
    adj_close = data['Adj Close']
    return adj_close

ticker = 'SPY'
start_date = '2015-12-31'
end_date = '2022-12-31'

# Load your price data into a pandas DataFrame
spy = pd.DataFrame(download_data(ticker, start_date, end_date))
spy.columns = ['spy']

# Calculate the maximum drawdown
max_drawdown = dsf.compute_max_dd(spy)
max_drawdown
spy   -33.717271
dtype: float64