pub struct ZigZag { /* private fields */ }Expand description
ZigZag — a non-repainting percent-threshold swing detector. Tracks the most
recent extreme (high or low) and confirms a reversal once price has moved
the configured percentage away from it.
uptrend (last swing was a low):
while highs make new highs, keep updating the pivot high
once close (or low) drops by ≥ threshold·high → confirm pivot high
downtrend (last swing was a high):
while lows make new lows, keep updating the pivot low
once close (or high) rises by ≥ threshold·low → confirm pivot lowThe indicator emits Some(swing) only on the bar where a reversal is
confirmed, returning the price and direction of the just-completed
extreme. Bars between confirmations return None. The first bar bootstraps
the state — it determines an initial reference price but does not emit.
The threshold is a fractional change (0.05 ≈ 5%); it must be strictly
positive and below 1.0.
§Example
use wickra_core::{Candle, Indicator, ZigZag};
let mut zz = ZigZag::new(0.10).unwrap();
for (i, p) in [100.0, 105.0, 115.0, 100.0, 90.0, 100.0].iter().enumerate() {
let c = Candle::new(*p, *p + 0.5, *p - 0.5, *p, 1.0, i as i64).unwrap();
let _ = zz.update(c);
}Implementations§
Trait Implementations§
Source§impl Indicator for ZigZag
impl Indicator for ZigZag
Source§type Input = Candle
type Input = Candle
Type of one input data point (typically
f64 for a price, or Candle / Tick).Source§type Output = ZigZagOutput
type Output = ZigZagOutput
Type of one output value.
Source§fn update(&mut self, candle: Candle) -> Option<ZigZagOutput>
fn update(&mut self, candle: Candle) -> Option<ZigZagOutput>
Feed one new data point into the indicator and return the freshly computed
output, or
None if the indicator is still warming up.Source§fn reset(&mut self)
fn reset(&mut self)
Reset all internal state, leaving the indicator equivalent to a freshly
constructed instance with the same parameters.
Source§fn warmup_period(&self) -> usize
fn warmup_period(&self) -> usize
Number of inputs required before the first non-
None output can be produced.Auto Trait Implementations§
impl Freeze for ZigZag
impl RefUnwindSafe for ZigZag
impl Send for ZigZag
impl Sync for ZigZag
impl Unpin for ZigZag
impl UnsafeUnpin for ZigZag
impl UnwindSafe for ZigZag
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>>
Run the indicator over a slice of inputs in order, returning one output (or
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
Mutably borrows from an owned value. Read more
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>
Converts
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>
Converts
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