Compute Drawdowns
(based on Initial Investment)#
The compute_drawdowns_i
function takes a pandas DataFrame of historical prices as its input and calculates drawdowns based on the initial value of the time series. Drawdowns represent the decline in value from the highest point to the lowest point in a given period. The function computes the percentage change in value from the initial value for each data point in the input DataFrame and returns a DataFrame containing the drawdown percentages for each data point.
This function computes drawdowns based on the initial value of a time series, given a DataFrame of prices.
This function should be used when the client gives more importance to the initial investment than the maximum historical value of the portfolio.
Function Signature#
def compute_drawdowns_i(dataframe):
'''
Function to compute drawdowns based on
the initial value of a time series
given a DataFrame of prices
'''
if not isinstance(dataframe, pd.DataFrame) or not isinstance(dataframe.index, pd.DatetimeIndex):
raise TypeError("Input must be a pandas DataFrame with a DateTimeIndex")
drawdowns = (dataframe / dataframe.iloc[0] -1 ) * 100
drawdowns = drawdowns[drawdowns < 0].fillna(0)
return drawdowns
Example#
import pandas as pd
import yfinance as yf
import ds4finance as dsf
# Download historical data for SPY
spy = yf.download("SPY", start="2015-01-01", end="2020-12-31", progress=False)[['Adj Close']]
# Calculate drawdowns
drawdowns_df = dsf.compute_drawdowns_i(spy)
drawdowns_df
Adj Close | |
---|---|
Date | |
2015-01-02 | 0.000000 |
2015-01-05 | -1.805941 |
2015-01-06 | -2.730813 |
2015-01-07 | -1.518768 |
2015-01-08 | 0.000000 |
... | ... |
2020-12-23 | 0.000000 |
2020-12-24 | 0.000000 |
2020-12-28 | 0.000000 |
2020-12-29 | 0.000000 |
2020-12-30 | 0.000000 |
1510 rows × 1 columns