pub struct MacdIndicator { /* private fields */ }Expand description
MACD = EMA(fast) − EMA(slow), with a signal EMA on top.
Standard parameters are fast = 12, slow = 26, signal = 9. The signal EMA
is seeded from the first signal raw MACD values, so the first full
MacdOutput is emitted after slow + signal − 1 inputs (assuming the
slow EMA seeded by then).
§Example
use wickra_core::{Indicator, MacdIndicator};
let mut indicator = MacdIndicator::new(3, 6, 3).unwrap();
let mut last = None;
for i in 0..80 {
last = indicator.update(100.0 + f64::from(i));
}
assert!(last.is_some());Implementations§
Source§impl MacdIndicator
impl MacdIndicator
Sourcepub fn new(fast: usize, slow: usize, signal: usize) -> Result<Self>
pub fn new(fast: usize, slow: usize, signal: usize) -> Result<Self>
Construct a MACD with the given periods.
§Errors
Returns Error::PeriodZero if any period is zero, and
Error::InvalidPeriod if fast >= slow.
Sourcepub fn classic() -> Self
pub fn classic() -> Self
Default (12, 26, 9) configuration, matching every classical chart package.
Sourcepub const fn periods(&self) -> (usize, usize, usize)
pub const fn periods(&self) -> (usize, usize, usize)
Configured periods as (fast, slow, signal).
Sourcepub const fn value(&self) -> Option<MacdOutput>
pub const fn value(&self) -> Option<MacdOutput>
Most recent fully-computed output if available.
Sourcepub fn batch_macd(&mut self, inputs: &[f64]) -> Vec<f64>
pub fn batch_macd(&mut self, inputs: &[f64]) -> Vec<f64>
Vectorized flat batch for bindings: n * 3 values laid out as
[macd, signal, histogram] per input row, warmup rows all NaN.
Allocates the result and fills it through
batch_macd_into. Separate from the trait
batch, which stays a bit-identical update
replay.
Sourcepub fn batch_macd_fast_into(&mut self, inputs: &[f64], out: &mut [f64])
pub fn batch_macd_fast_into(&mut self, inputs: &[f64], out: &mut [f64])
Opt-in fast variant of batch_macd_into: after
the exact warmup (those rows are identical to the exact batch), the
fast, slow and signal EMAs run as SIMD linear-recurrence scans over
blocks. Every value agrees with the exact batch to within a few units in
the last place; warmup rows and length are identical, and the result is
the same on every platform. Only a fresh indicator over finite values
within 1e100, long enough for a full output, takes the kernel;
anything else is the exact batch. Afterwards the three EMAs continue
streaming from the kernel’s last values.
§Panics
Panics if out.len() != inputs.len() * 3.
Sourcepub fn batch_macd_fast(&mut self, inputs: &[f64]) -> Vec<f64>
pub fn batch_macd_fast(&mut self, inputs: &[f64]) -> Vec<f64>
batch_macd_fast_into into a fresh
vector.
Sourcepub fn batch_macd_into(&mut self, inputs: &[f64], out: &mut [f64])
pub fn batch_macd_into(&mut self, inputs: &[f64], out: &mut [f64])
batch_macd into a caller-owned buffer of
inputs.len() * 3 values, overwriting every cell.
For a fresh slice long enough for a full output, whose values are all
finite with magnitude at most 1e300, it runs the fast EMA, slow EMA
and signal EMA as three recurrences in one pass: the warmup phases run
on their own so the steady-state loop has no per-row branch, and the
three independent chains overlap in the pipeline. The seeds are the same
running means Ema keeps (summed from -0.0 in input order) and the
recurrences the same mul_add, so every value is bit-for-bit equal to
replaying update. The magnitude bound keeps every EMA, sum and MACD
difference finite, which is what lets the signal EMA take every MACD
value (streaming skips a non-finite one). Anything else (not fresh, a
value out of range, or too short to emit) replays update.
§Panics
Panics if out.len() != inputs.len() * 3.
Trait Implementations§
Source§impl Clone for MacdIndicator
impl Clone for MacdIndicator
Source§impl Debug for MacdIndicator
impl Debug for MacdIndicator
Source§impl Indicator for MacdIndicator
impl Indicator for MacdIndicator
Source§type Output = MacdOutput
type Output = MacdOutput
Source§fn update(&mut self, input: f64) -> Option<MacdOutput>
fn update(&mut self, input: f64) -> Option<MacdOutput>
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 MacdIndicator
impl RefUnwindSafe for MacdIndicator
impl Send for MacdIndicator
impl Sync for MacdIndicator
impl Unpin for MacdIndicator
impl UnsafeUnpin for MacdIndicator
impl UnwindSafe for MacdIndicator
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> 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