pub struct TrendStrengthIndex { /* private fields */ }Expand description
Trend Strength Index: fits an ordinary-least-squares line to the last
period prices against their bar index and reports the coefficient of
determination r^2, signed by the slope of the fit.
regress y = close on x = 0..period-1
r^2 = (n·Σxy − Σx·Σy)^2 / [ (n·Σx² − (Σx)²)(n·Σy² − (Σy)²) ]
TSI = sign(slope) · r^2 (slope sign = sign of n·Σxy − Σx·Σy)r^2 in [0, 1] measures how well a straight line explains the price over
the window — how trendy the segment is, regardless of direction. Carrying
the slope sign turns it into a directional reading in [-1, 1]: values near
+1 are a strong, clean uptrend; near -1 a strong downtrend; near 0 a
flat or noisy market with no linear structure. A window of constant prices
(zero variance in y) has no defined trend and returns 0.
§Example
use wickra_core::{Indicator, TrendStrengthIndex};
let mut indicator = TrendStrengthIndex::new(20).unwrap();
let mut last = None;
for i in 0..40 {
last = indicator.update(100.0 + f64::from(i));
}
// A clean ramp is a perfect uptrend -> r^2 = 1.
assert!((last.unwrap() - 1.0).abs() < 1e-9);Implementations§
Source§impl TrendStrengthIndex
impl TrendStrengthIndex
Sourcepub fn new(period: usize) -> Result<Self>
pub fn new(period: usize) -> Result<Self>
Construct a Trend Strength Index over the given window.
§Errors
Returns Error::PeriodZero if period == 0, or Error::InvalidPeriod
if period == 1 (a regression needs at least two points).
Trait Implementations§
Source§impl Clone for TrendStrengthIndex
impl Clone for TrendStrengthIndex
Source§fn clone(&self) -> TrendStrengthIndex
fn clone(&self) -> TrendStrengthIndex
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 TrendStrengthIndex
impl Debug for TrendStrengthIndex
Source§impl Indicator for TrendStrengthIndex
impl Indicator for TrendStrengthIndex
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 TrendStrengthIndex
impl RefUnwindSafe for TrendStrengthIndex
impl Send for TrendStrengthIndex
impl Sync for TrendStrengthIndex
impl Unpin for TrendStrengthIndex
impl UnsafeUnpin for TrendStrengthIndex
impl UnwindSafe for TrendStrengthIndex
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