From 8f461a06e54fb4b3e6a05df76ca6c618a934e583 Mon Sep 17 00:00:00 2001 From: kingchenc Date: Tue, 27 Oct 2026 04:30:57 +0100 Subject: [PATCH] docs: MaxDrawdown warms up over period and updates in amortised O(1) The page, the overview row and the warmup table gave a warmup of 1; MaxDrawdown returns None until the window holds period values. The streaming example unwrapped update and would panic during that warmup. The cost note described a deque that never made update O(1); the indicator now keeps the window's running peaks with the low of each segment, and the page says how. The edge cases cite the unit tests that pin them, including the non-positive peak and non-finite input. --- Indicators-Overview.md | 2 +- Indicators/Indicator-MaxDrawdown.md | 55 ++++++++++++++++++++--------- Warmup-Periods.md | 2 +- 3 files changed, 41 insertions(+), 18 deletions(-) diff --git a/Indicators-Overview.md b/Indicators-Overview.md index c3f40b0..0bf2294 100644 --- a/Indicators-Overview.md +++ b/Indicators-Overview.md @@ -633,7 +633,7 @@ take `(asset, benchmark)` pairs. | `SortinoRatio` | Sharpe with downside-only deviation. | `f64` | `f64` | unbounded | `(period, mar)` | `period` | [Indicator-SortinoRatio](/Indicators/Indicator-SortinoRatio) | | `CalmarRatio` | `mean(returns) / max_drawdown(equity)` from window. | `f64` | `f64` | unbounded; `0` if no DD | `period` | `period` | [Indicator-CalmarRatio](/Indicators/Indicator-CalmarRatio) | | `OmegaRatio` | `Σ gains-above-threshold / Σ losses-below`. | `f64` | `f64` | `[0, ∞)` (`Inf` if all-positive) | `(period, threshold)` | `period` | [Indicator-OmegaRatio](/Indicators/Indicator-OmegaRatio) | -| `MaxDrawdown` | Rolling worst peak-to-trough decline. | `f64` (equity) | `f64` | `[0, 1]` | `period` | `1` | [Indicator-MaxDrawdown](/Indicators/Indicator-MaxDrawdown) | +| `MaxDrawdown` | Rolling worst peak-to-trough decline. | `f64` (equity) | `f64` | `[0, 1]` | `period` | `period` | [Indicator-MaxDrawdown](/Indicators/Indicator-MaxDrawdown) | | `AverageDrawdown` | Rolling mean drawdown depth (same as PainIndex). | `f64` (equity) | `f64` | `[0, 1]` | `period` | `period` | [Indicator-AverageDrawdown](/Indicators/Indicator-AverageDrawdown) | | `DrawdownDuration` | Bars elapsed since all-time peak. | `f64` (equity) | `u32` | `[0, ∞)` | (no parameters) | `1` | [Indicator-DrawdownDuration](/Indicators/Indicator-DrawdownDuration) | | `PainIndex` | Mean drawdown depth (Becker). | `f64` (equity) | `f64` | `[0, 1]` | `period` | `period` | [Indicator-PainIndex](/Indicators/Indicator-PainIndex) | diff --git a/Indicators/Indicator-MaxDrawdown.md b/Indicators/Indicator-MaxDrawdown.md index bceade5..03b1b83 100644 --- a/Indicators/Indicator-MaxDrawdown.md +++ b/Indicators/Indicator-MaxDrawdown.md @@ -4,7 +4,7 @@ > Operates on an equity-curve sample (or any non-negative value > series). Output is the magnitude of the worst drawdown as a > non-negative fraction (`0.20` = 20% drop from peak). Amortised -> O(1) per update via a monotonically-decreasing deque. +> O(1) per update. ## Quick reference @@ -13,25 +13,37 @@ | Family | Risk / Performance | | Input type | `f64` — one equity-curve sample per update | | Output type | `f64` | -| Output range | `[0, 1]` (fraction of peak) | +| Output range | `[0, 1]` (fraction of peak) for non-negative equity | | Default parameters | `period` required | -| Warmup period | `1` (running peak emits from first input) | +| Warmup period | `period` (first value once the window is full) | | Interpretation | Worst drawdown over the rolling window | ## Formula ``` -peak_t = running max of equity over the trailing period bars +peak_t = running max of equity from the oldest bar in the window up to t drawdown_t = (equity_t - peak_t) / peak_t (negative) MaxDrawdown = -min(drawdown_t over window) (positive magnitude) ``` -A monotonically rising equity curve has MDD = 0. Setting `period` -≥ the number of bars you will ever feed makes the metric -effectively *cumulative* — the running peak is never dropped from -the window. See +Only a positive peak counts; while the running peak is zero or +negative, a bar adds no drawdown. A monotonically rising equity +curve has MDD = 0. Setting `period` ≥ the number of bars you will +ever feed makes the metric effectively *cumulative* — the running +peak is never dropped from the window. See `crates/wickra-core/src/indicators/max_drawdown.rs`. +**Cost.** Each update is amortised O(1), whatever the `period`. +The running peaks split the window into segments, a peak followed +by the bars below it, and a segment's deepest drawdown is +`(peak - low) / peak`. The window is held as two halves: the older +one is rebuilt from the newer one every `period` updates, storing +for each bar the peak and worst drawdown from it to the end of the +half, and the newer one keeps its own running peaks with the low of +each segment. The result is bit for bit what rescanning the window +gives (unit tests `matches_rescan_bit_for_bit` and +`matches_rescan_on_long_series`). + ## Parameters | Name | Type | Default | Constraint | Description | @@ -45,18 +57,27 @@ each value the MDD over the trailing window ending at that bar. ## Warmup -`warmup_period() == 1`. Emits from the first input (MDD = 0). +`warmup_period() == period`. The first value comes with the +`period`-th input, once the window is full; before that `update` +returns `None` (Python `None`, Node `null`, `NaN` in `batch`). Unit +tests `accessors_and_metadata` and `reference_drawdown`. ## Edge cases -- **Monotonically rising input.** MDD = 0 throughout. +- **Monotonically rising input.** MDD = 0 throughout (test + `pure_uptrend_yields_zero`). +- **Flat input.** MDD = 0 (test `constant_series_yields_zero`). - **Recovery to new peak.** Once equity exceeds the prior peak, the running peak updates and subsequent drawdowns are measured from the new peak. -- **Negative equity.** Possible with leveraged products; this - indicator assumes positive equity for the fractional drawdown - to be meaningful. -- **Reset.** Clears the rolling deque. +- **Non-positive peak.** While the running peak is zero or + negative no drawdown is measured, so an all-zero window gives 0 + (test `non_positive_peak_yields_zero`). A dip below zero after a + positive peak gives a value above 1. +- **Non-finite input.** `NaN` and `±∞` return `None` and leave the + window untouched (test `ignores_non_finite_input`). +- **Reset.** Clears the window; the next value comes after another + `period` inputs (test `reset_clears_state`). ## Examples @@ -101,8 +122,10 @@ use wickra::{Indicator, MaxDrawdown}; let mut mdd = MaxDrawdown::new(252).unwrap(); let equity_stream: Vec = Vec::new(); // your equity-curve feed for equity in equity_stream { - let dd = mdd.update(equity).unwrap(); - if dd > 0.10 { /* warning: > 10% drawdown */ } + // `None` until 252 values have arrived. + if let Some(dd) = mdd.update(equity) { + if dd > 0.10 { /* warning: > 10% drawdown */ } + } } ``` diff --git a/Warmup-Periods.md b/Warmup-Periods.md index c4abeb6..085b3ad 100644 --- a/Warmup-Periods.md +++ b/Warmup-Periods.md @@ -381,7 +381,7 @@ Some risk metrics take a single `f64` (returns / equity), others take an | `SortinoRatio` | `SortinoRatio::new(20, 0.0)` | `period` | 20 | 20th | | `CalmarRatio` | `CalmarRatio::new(20)` | `period` | 20 | 20th | | `OmegaRatio` | `OmegaRatio::new(20, 0.0)` | `period` | 20 | 20th | -| `MaxDrawdown` | `MaxDrawdown::new(252)` | constant `1` | 1 | 1st | +| `MaxDrawdown` | `MaxDrawdown::new(252)` | `period` | 252 | 252nd | | `AverageDrawdown` | `AverageDrawdown::new(20)` | `period` | 20 | 20th | | `DrawdownDuration` | `DrawdownDuration::new()` | constant `1` | 1 | 1st | | `PainIndex` | `PainIndex::new(20)` | `period` | 20 | 20th |