pub struct OuHalfLife { /* private fields */ }Expand description
Half-life of mean reversion of the spread a − b, from an Ornstein–Uhlenbeck
fit.
Each update takes one (a, b) price pair and forms the spread
sₜ = aₜ − bₜ. Over the trailing window of period spreads the indicator
fits the discrete Ornstein–Uhlenbeck (mean-reverting AR(1)) model by
ordinary least squares of the change on the level:
Δsₜ = λ · sₜ₋₁ + c + εₜ
half_life = −ln(2) / λ (only when λ < 0)λ is the speed of mean reversion: a more negative λ pulls the spread back
to its mean faster. The half-life is the number of bars for a deviation
to decay by half — the single most useful number for sizing a pairs trade’s
holding period and look-back. When the spread is not mean-reverting
(λ ≥ 0, a random walk or a trend) or the regression is degenerate (a flat
spread), the indicator returns 0, meaning “no finite half-life”.
Each update is O(period): the OLS slope is recomputed from the window’s
running geometry. Output is in bars and is always ≥ 0.
§Example
use wickra_core::{Indicator, OuHalfLife};
let mut hl = OuHalfLife::new(40).unwrap();
let mut last = None;
for t in 0..120 {
let b = 100.0 + f64::from(t);
// `a` hugs `b` with a fast mean-reverting wobble ⇒ short half-life.
let a = b + 2.0 * (f64::from(t) * 0.9).sin();
last = hl.update((a, b));
}
let half_life = last.unwrap();
assert!(half_life > 0.0 && half_life < 40.0);Implementations§
Source§impl OuHalfLife
impl OuHalfLife
Trait Implementations§
Source§impl Clone for OuHalfLife
impl Clone for OuHalfLife
Source§fn clone(&self) -> OuHalfLife
fn clone(&self) -> OuHalfLife
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 OuHalfLife
impl Debug for OuHalfLife
Source§impl Indicator for OuHalfLife
impl Indicator for OuHalfLife
Source§type Input = (f64, f64)
type Input = (f64, f64)
f64 for a price, or Candle / Tick).Source§fn update(&mut self, input: (f64, f64)) -> Option<f64>
fn update(&mut self, input: (f64, 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 OuHalfLife
impl RefUnwindSafe for OuHalfLife
impl Send for OuHalfLife
impl Sync for OuHalfLife
impl Unpin for OuHalfLife
impl UnsafeUnpin for OuHalfLife
impl UnwindSafe for OuHalfLife
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