pub struct MaxDrawdown { /* private fields */ }Expand description
Rolling Maximum Drawdown — the deepest peak-to-trough decline within the trailing window.
The input is treated as an equity-curve sample (or any non-negative value
series). For each bar the indicator computes the largest fractional decline
from any prior peak inside the trailing period-bar window:
drawdown_t = (equity_t − peak_t) / peak_t (a negative number)
MaxDrawdown = min(drawdown_t over window) (most-negative value)peak_t is the running maximum from the oldest value in the window up to
t, and only a positive peak counts.
Output is the magnitude of the worst drawdown as a non-negative fraction
(0.20 = 20 % drop from peak). A monotonically rising equity curve has a
max drawdown of 0. Setting period greater than or equal to the number of
bars you will ever feed makes the metric effectively cumulative — the
indicator never forgets the global peak.
Each update is amortised O(1). Within the window the running peaks split
the values into segments, each a peak followed by the values 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 value 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. Because the subtraction and the division both round
monotonically, the result is bit for bit the one a rescan of the window
gives.
§Example
use wickra_core::{Indicator, MaxDrawdown};
let mut mdd = MaxDrawdown::new(10).unwrap();
// Equity peaks at 110 then drops to 88 — a 20% drawdown.
for v in [100.0, 110.0, 100.0, 95.0, 88.0, 90.0, 92.0, 95.0, 100.0, 105.0] {
mdd.update(v);
}
assert!((mdd.update(106.0).unwrap() - 0.20).abs() < 1e-9);Implementations§
Source§impl MaxDrawdown
impl MaxDrawdown
Trait Implementations§
Source§impl Clone for MaxDrawdown
impl Clone for MaxDrawdown
Source§impl Debug for MaxDrawdown
impl Debug for MaxDrawdown
Source§impl Indicator for MaxDrawdown
impl Indicator for MaxDrawdown
Source§fn update(&mut self, input: f64) -> Option<f64>
fn update(&mut self, input: f64) -> Option<f64>
None if there is no value for this input. Read moreSource§fn reset(&mut self)
fn reset(&mut self)
Source§fn warmup_period(&self) -> usize
fn warmup_period(&self) -> usize
None output can be produced.Source§fn is_ready(&self) -> bool
fn is_ready(&self) -> bool
Source§fn name(&self) -> &'static str
fn name(&self) -> &'static str
Source§fn batch_nan_into(&mut self, inputs: &[Self::Input], out: &mut [f64])
fn batch_nan_into(&mut self, inputs: &[Self::Input], out: &mut [f64])
Source§fn batch_fast_into(&mut self, inputs: &[Self::Input], out: &mut [f64])
fn batch_fast_into(&mut self, inputs: &[Self::Input], out: &mut [f64])
batch_nan_into, but
an indicator with a vectorised kernel may reassociate its arithmetic to
run it in SIMD lanes. Each value then agrees with the exact batch to within
the tolerance the indicator documents (a few units in the last place), not
bit for bit; warmup positions, NaN placement and the output length are
identical. The kernels are deterministic: the same input produces the same
bits on every platform, with or without SIMD hardware. Read moreAuto Trait Implementations§
impl Freeze for MaxDrawdown
impl RefUnwindSafe for MaxDrawdown
impl Send for MaxDrawdown
impl Sync for MaxDrawdown
impl Unpin for MaxDrawdown
impl UnsafeUnpin for MaxDrawdown
impl UnwindSafe for MaxDrawdown
Blanket Implementations§
Source§impl<T> BatchExt for Twhere
T: Indicator,
impl<T> BatchExt for Twhere
T: Indicator,
Source§fn batch(&mut self, inputs: &[Self::Input]) -> Vec<Option<Self::Output>>
fn batch(&mut self, inputs: &[Self::Input]) -> Vec<Option<Self::Output>>
None during warmup) per input.Source§impl<T> BatchNanExt for T
impl<T> BatchNanExt for T
Source§fn batch_nan(&mut self, inputs: &[f64]) -> Vec<f64>
fn batch_nan(&mut self, inputs: &[f64]) -> Vec<f64>
f64 per input, warmup positions filled with NaN, bit-for-bit equal
to replaying update.Source§fn batch_fast(&mut self, inputs: &[f64]) -> Vec<f64>
fn batch_fast(&mut self, inputs: &[f64]) -> Vec<f64>
Indicator::batch_fast_into) into a fresh
vector: within the indicator’s documented tolerance of
batch_nan, deterministic across platforms.Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more