pub struct SpreadBollingerBands { /* private fields */ }Expand description
Bollinger bands on the spread a − b of two series.
Each update takes one (a, b) price pair and forms the spread
sₜ = aₜ − bₜ. Over the trailing window of period spreads it builds a
classic Bollinger envelope:
middle = mean(s) σ = stddev(s)
upper = middle + num_std · σ
lower = middle − num_std · σ
%b = (s_now − lower) / (upper − lower)Applied to a spread rather than a price, the bands are a ready-made pairs
mean-reversion signal: the spread riding the upper band is stretched
rich (a short-the-spread setup), the lower band stretched cheap, and a
return to the middle is the exit. %b compresses the location into one
number for thresholding. The spread is the raw difference a − b, so feed
already-comparable legs (e.g. a hedged pair, two yields, or log prices); pair
this with crate::BetaNeutralSpread when the legs need a hedge ratio first.
A flat spread yields a zero-width band; %b is then reported as the neutral
0.5. Each update is O(1): the mean and variance come from two running
sums maintained as the window slides.
§Example
use wickra_core::{Indicator, SpreadBollingerBands};
let mut bb = SpreadBollingerBands::new(20, 2.0).unwrap();
let mut last = None;
for t in 0..60 {
let b = 100.0 + f64::from(t);
let a = b + 2.0 * (f64::from(t) * 0.5).sin();
last = bb.update((a, b));
}
let out = last.unwrap();
assert!(out.lower <= out.middle && out.middle <= out.upper);Implementations§
Source§impl SpreadBollingerBands
impl SpreadBollingerBands
Sourcepub fn new(period: usize, num_std: f64) -> Result<SpreadBollingerBands, Error>
pub fn new(period: usize, num_std: f64) -> Result<SpreadBollingerBands, Error>
Construct new spread Bollinger bands.
period is the look-back window; num_std is the band width in standard
deviations.
§Errors
Returns Error::InvalidPeriod if period < 2, or
Error::InvalidParameter if num_std is not strictly positive (and
finite).
Trait Implementations§
Source§impl Clone for SpreadBollingerBands
impl Clone for SpreadBollingerBands
Source§fn clone(&self) -> SpreadBollingerBands
fn clone(&self) -> SpreadBollingerBands
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 SpreadBollingerBands
impl Debug for SpreadBollingerBands
Source§impl Indicator for SpreadBollingerBands
impl Indicator for SpreadBollingerBands
Source§type Input = (f64, f64)
type Input = (f64, f64)
f64 for a price, or Candle / Tick).Source§type Output = SpreadBollingerBandsOutput
type Output = SpreadBollingerBandsOutput
Source§fn update(&mut self, input: (f64, f64)) -> Option<SpreadBollingerBandsOutput>
fn update(&mut self, input: (f64, f64)) -> Option<SpreadBollingerBandsOutput>
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 SpreadBollingerBands
impl RefUnwindSafe for SpreadBollingerBands
impl Send for SpreadBollingerBands
impl Sync for SpreadBollingerBands
impl Unpin for SpreadBollingerBands
impl UnsafeUnpin for SpreadBollingerBands
impl UnwindSafe for SpreadBollingerBands
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