pub struct Wma { /* private fields */ }Expand description
Weighted Moving Average with linear weights 1, 2, ..., period.
Output is sum(weight_i * price_i) / sum(weights). Maintained incrementally in
O(1) by keeping the rolling sum of values and the rolling weighted sum.
Both running sums are recomputed exactly from the live window every
16 · period steady-state updates, the same bound the SMA puts on drift.
Without it the weighted sum — updated as W − S + period · x, a difference of
quantities period times larger than the result — lost about 6e-10 of
relative accuracy over 500 000 updates of a price series.
§Example
use wickra_core::{Indicator, Wma};
let mut indicator = Wma::new(3).unwrap();
let mut last = None;
for i in 0..80 {
last = indicator.update(100.0 + f64::from(i));
}
assert!(last.is_some());Implementations§
Trait Implementations§
Source§impl Indicator for Wma
impl Indicator for Wma
Source§fn batch_nan_into(&mut self, inputs: &[f64], out: &mut [f64])
fn batch_nan_into(&mut self, inputs: &[f64], out: &mut [f64])
The exact batch with the window sums, cursor and reseed count in
locals: update’s operations in update’s order – the same bits,
the same reseed cadence – held in registers rather than written back
through self for every input, which made the replay three times slower
than streaming at long periods. The warmup still goes through update.
Source§fn batch_fast_into(&mut self, inputs: &[f64], out: &mut [f64])
fn batch_fast_into(&mut self, inputs: &[f64], out: &mut [f64])
SIMD kernel: rolling sums and the weighted-numerator recurrence
N = N − S + period · x as prefix scans, re-anchored on exact window
values every 16 · period inputs, scaled by 1 / Σ weights. Agrees with
the exact batch to within a few units in the last place; warmup NaNs
and length are identical. Afterwards the window is rebuilt exactly from
the last period inputs.
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.Auto Trait Implementations§
impl Freeze for Wma
impl RefUnwindSafe for Wma
impl Send for Wma
impl Sync for Wma
impl Unpin for Wma
impl UnsafeUnpin for Wma
impl UnwindSafe for Wma
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