drawdowns¶
All functions here take a price series (or any cumulative-value series) - not a returns series. Use Portfolio.price_index() or portpy.metrics.returns.prices_from_returns if you only have returns.
drawdowns ¶
Drawdown analysis: depth, duration, recovery, and tail risk of peak-to-trough declines.
All functions here take a price series (or any cumulative-value series, e.g. a synthetic index built from (1 + returns).cumprod()) - not a returns series.
drawdown_series ¶
Percentage decline from the running peak at every point in time.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prices
|
Series
|
Price (or cumulative-value) series, e.g. (1 + returns).cumprod(). |
required |
Returns:
| Type | Description |
|---|---|
Series
|
A Series of the same index as |
Source code in src/portpy/metrics/drawdowns.py
max_drawdown ¶
The single deepest peak-to-trough decline.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prices
|
Series
|
Price (or cumulative-value) series. |
required |
as_result
|
bool
|
If True, return a MetricResult instead of a plain float. |
False
|
Returns:
| Type | Description |
|---|---|
float | MetricResult
|
A negative number (or 0.0 if the series never declined), or a |
float | MetricResult
|
MetricResult wrapping it. |
Source code in src/portpy/metrics/drawdowns.py
drawdown_duration ¶
At every point in time, how many periods have passed since the last new high.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prices
|
Series
|
Price (or cumulative-value) series. |
required |
Returns:
| Type | Description |
|---|---|
Series
|
A Series of period counts since the last peak, same index as |
Source code in src/portpy/metrics/drawdowns.py
time_to_recovery ¶
Periods it took to recover from the single worst drawdown.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prices
|
Series
|
Price (or cumulative-value) series. |
required |
as_result
|
bool
|
If True, return a MetricResult instead of a plain float. |
False
|
Returns:
| Type | Description |
|---|---|
float | MetricResult | None
|
The number of periods to recovery, or None if the worst drawdown |
float | MetricResult | None
|
hasn't recovered by the end of the series. |
Source code in src/portpy/metrics/drawdowns.py
top_n_drawdowns ¶
The n deepest drawdown episodes, sorted worst-first.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prices
|
Series
|
Price (or cumulative-value) series. |
required |
n
|
int
|
Maximum number of episodes to return. |
5
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
A DataFrame with columns start, trough, end, depth, |
DataFrame
|
duration_to_trough, duration_to_recovery, recovered (see |
DataFrame
|
|
Source code in src/portpy/metrics/drawdowns.py
average_drawdown ¶
Mean depth across all distinct drawdown episodes (not a continuous time-average - see pain_index for that).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prices
|
Series
|
Price (or cumulative-value) series. |
required |
as_result
|
bool
|
If True, return a MetricResult instead of a plain float. |
False
|
Returns:
| Type | Description |
|---|---|
float | MetricResult
|
The average episode depth (0.0 if there were no episodes). |
Source code in src/portpy/metrics/drawdowns.py
drawdown_at_risk ¶
drawdown_at_risk(
prices: Series,
confidence: float = DEFAULT_CONFIDENCE_LEVEL,
as_result: bool = False,
) -> float | MetricResult
Value-at-Risk applied to the drawdown series (DaR): the drawdown level breached only (1 - confidence) of the time. See conditional_drawdown_at_risk for the mean-of-tail analogue (CDaR).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prices
|
Series
|
Price (or cumulative-value) series. |
required |
confidence
|
float
|
Confidence level in (0, 1), e.g. 0.95 for 95%. |
DEFAULT_CONFIDENCE_LEVEL
|
as_result
|
bool
|
If True, return a MetricResult instead of a plain float. |
False
|
Returns:
| Type | Description |
|---|---|
float | MetricResult
|
The drawdown percentile at |