pub struct BandpassFilter { /* private fields */ }Expand description
Ehlers’ Bandpass Filter — a two-pole resonator that passes the cyclic content
around a target period and rejects both the trend (low frequencies) and the
noise (high frequencies).
From John Ehlers’ Cycle Analytics for Traders (2013):
beta = cos(2π / period)
gamma = 1 / cos(4π · bandwidth / period)
alpha = gamma − sqrt(gamma² − 1)
BP_t = 0.5·(1 − alpha)·(price_t − price_{t−2})
+ beta·(1 + alpha)·BP_{t−1} − alpha·BP_{t−2}bandwidth (a fraction, typically 0.3) sets how wide a band of periods is
admitted: narrow bandwidth gives a sharp, ringing resonator tuned tightly to
period; wide bandwidth lets more of the spectrum through. The output is a
zero-mean oscillator — it swings symmetrically around 0, peaking when the
dominant cycle aligns with period. It is the building block for cycle-phase
and cycle-amplitude work.
The recursion needs two prior prices and two prior outputs; until then it emits
0 (Ehlers’ initial condition), so warmup_period is 1 and a value is
produced every bar. Each update is O(1).
§Example
use wickra_core::{Indicator, BandpassFilter};
let mut indicator = BandpassFilter::new(20, 0.3).unwrap();
let mut last = None;
for i in 0..80 {
last = indicator.update(100.0 + (f64::from(i) * 0.3).sin() * 5.0);
}
assert!(last.is_some());Implementations§
Source§impl BandpassFilter
impl BandpassFilter
Sourcepub fn new(period: usize, bandwidth: f64) -> Result<BandpassFilter, Error>
pub fn new(period: usize, bandwidth: f64) -> Result<BandpassFilter, Error>
Construct a bandpass filter tuned to period with the given bandwidth
fraction.
§Errors
Returns Error::PeriodZero if period == 0 and
Error::InvalidParameter if bandwidth is not finite or outside
(0, 1).
Trait Implementations§
Source§impl Clone for BandpassFilter
impl Clone for BandpassFilter
Source§fn clone(&self) -> BandpassFilter
fn clone(&self) -> BandpassFilter
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for BandpassFilter
impl Debug for BandpassFilter
Source§impl Indicator for BandpassFilter
impl Indicator for BandpassFilter
Source§fn update(&mut self, price: f64) -> Option<f64>
fn update(&mut self, price: f64) -> Option<f64>
None if the indicator is still warming up.Source§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 BandpassFilter
impl RefUnwindSafe for BandpassFilter
impl Send for BandpassFilter
impl Sync for BandpassFilter
impl Unpin for BandpassFilter
impl UnsafeUnpin for BandpassFilter
impl UnwindSafe for BandpassFilter
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§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