Compute Drawdowns Table#
The compute_drawdowns_table
function is designed to help financial analysts and investors analyze the drawdowns of various financial instruments such as stocks, bonds, and other securities. It calculates the top drawdowns and their respective durations for the given price data.
To use the compute_drawdowns_table
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 an optional number
parameter. If you provide an integer value for number, the function will display the top number
of drawdowns in the resulting table. If you don’t provide the number
parameter, the function will use the default value of 5.
After passing the DataFrame to the compute_drawdowns_table
function, it will return a DataFrame containing the top drawdowns table.
Description#
The compute_drawdowns_table function computes the top drawdowns and their respective durations for a given pandas DataFrame of prices.
Parameters#
prices (pandas.DataFrame): DataFrame containing price data. The index should be of datetime type. number (int, optional): The number of top drawdowns to display in the resulting table. Default is 5.
Returns#
pandas.DataFrame: A DataFrame containing the top drawdowns table.
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-01-01'
end_date = '2020-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 drawdowns table for the given price data
drawdowns_table = dsf.compute_drawdowns_table(spy)
drawdowns_table
Begin | End | Depth | Length | |
---|---|---|---|---|
1 | 2020-02-19 | 2020-08-10 | -33.72% | 0 Years 6 Months 9 Days |
2 | 2018-09-20 | 2019-04-12 | -19.35% | 0 Years 7 Months 8 Days |
3 | 2015-07-20 | 2016-04-18 | -13.02% | 0 Years 9 Months 2 Days |
4 | 2018-01-26 | 2018-08-06 | -10.1% | 0 Years 7 Months 20 Days |
5 | 2020-09-02 | 2020-11-11 | -9.44% | 0 Years 2 Months 9 Days |
compute_drawdowns_table() function#
```python
Function to compute the drawdowns table for a given DataFrame of prices.
Parameters
----------
prices : pandas.DataFrame
DataFrame containing price data. Index should be of datetime type.
number : int, optional
Number of top drawdowns to display in the resulting table. Default is 5.
Returns
-------
pandas.DataFrame
A DataFrame containing the top drawdowns table.
```