def compute_cdar(pnl: array_like, var: array_like) -> np.ndarray:
"""Compute the Drawdown of a portfolio
Parameters
----------
pnl : array_like
Profit and Loss values.
Returns
-------
np.ndarray
Drawdowns
"""
# Compute the drawdowns
running_max = np.maximum.accumulate(pnl)
drawdowns = running_max - pnl
drawdown_values = np.zeros_like(var)
for i, item in enumerate(var):
drawdown_values[i] = np.nanmean(drawdowns[drawdowns > item])
drawdown_values = np.nan_to_num(drawdown_values)
return drawdown_values
The above implementation of compute_cdar expects pnl argument to the cumulative pnl, while the the pnl passed in is daily pnl.
The above implementation of compute_cdar expects pnl argument to the cumulative pnl, while the the pnl passed in is daily pnl.
Reference: https://scikit-portfolio.github.io/scikit-portfolio/efficient_cdar/