The augment_drawdown function calculates the drawdown metrics for a financial time series using either pandas or polars engine, and returns the augmented DataFrame with peak value, drawdown, and drawdown percentage columns.
The input data can be either a pandas DataFrame or a pandas DataFrameGroupBy object containing the time series data for drawdown calculation.
required
date_column
str
The name of the column containing dates or timestamps.
required
close_column
str
The column containing the values (e.g., price) to calculate drawdowns from.
required
reduce_memory
bool
If True, reduces memory usage of the DataFrame before calculation. Default is False.
False
engine
str
The computation engine to use: βpandasβ or βpolarsβ. Default is βpandasβ.
'pandas'
Returns
Name
Type
Description
pd.DataFrame
A pandas DataFrame augmented with three columns: - {close_column}_peak: Running maximum value up to each point - {close_column}_drawdown: Absolute difference from peak to current value - {close_column}_drawdown_pct: Percentage decline from peak to current value
Notes
Drawdown is a measure of peak-to-trough decline in a time series, typically used to assess the risk of a financial instrument:
Peak Value: The highest value observed up to each point in time
Drawdown: The absolute difference between the peak and current value
Drawdown Percentage: The percentage decline from the peak value
Examples
import pandas as pdimport pytimetk as tkdf = tk.load_dataset('stocks_daily', parse_dates=['date'])# Single stock drawdowndd_df = ( df.query("symbol == 'AAPL'") .augment_drawdown( date_column='date', close_column='close', ))dd_df.head()