Skip to main content

wickra_core/indicators/
tsf_oscillator.rs

1//! Time Series Forecast Oscillator (TSF Oscillator).
2
3use crate::error::{Error, Result};
4use crate::indicators::tsf::Tsf;
5use crate::traits::Indicator;
6
7/// Time Series Forecast Oscillator — the percentage gap between the close and
8/// the **one-bar-ahead** time-series forecast of the close.
9///
10/// ```text
11/// TSFOsc_t = 100 · (close_t − TSF(close, period)_t) / close_t
12/// ```
13///
14/// where [`Tsf`](crate::Tsf) projects the rolling least-squares line one bar
15/// past the window (`a + b·period`). It is the close-relative companion to
16/// [`Cfo`](crate::Cfo), which measures the same percentage gap against the
17/// regression value at the *current* bar (`a + b·(period − 1)`). Because `TSF`
18/// advances one bar further than `LinearRegression`, the two differ by exactly
19/// the slope term `100·b/close`: on a trending series `TSFOsc` reads more
20/// negative in an uptrend (the forecast has already stepped above price) and
21/// more positive in a downtrend.
22///
23/// Positive readings mean the close sits *above* its forward forecast (price
24/// has overshot the projected trend); negative readings mean it sits below.
25/// Wraps the existing `Tsf` so the warmup matches.
26///
27/// # Example
28///
29/// ```
30/// use wickra_core::{Indicator, TsfOscillator};
31///
32/// let mut indicator = TsfOscillator::new(14).unwrap();
33/// let mut last = None;
34/// for i in 0..40 {
35///     last = indicator.update(100.0 + f64::from(i));
36/// }
37/// assert!(last.is_some());
38/// ```
39#[derive(Debug, Clone)]
40pub struct TsfOscillator {
41    period: usize,
42    tsf: Tsf,
43    current: Option<f64>,
44}
45
46impl TsfOscillator {
47    /// Construct a new TSF oscillator over `period` inputs.
48    ///
49    /// # Errors
50    /// Returns [`Error::InvalidPeriod`] if `period < 2` — a regression line is
51    /// undefined for fewer than two points.
52    pub fn new(period: usize) -> Result<Self> {
53        if period < 2 {
54            return Err(Error::InvalidPeriod {
55                message: "TSF oscillator needs period >= 2",
56            });
57        }
58        Ok(Self {
59            period,
60            tsf: Tsf::new(period)?,
61            current: None,
62        })
63    }
64
65    /// Configured period.
66    pub const fn period(&self) -> usize {
67        self.period
68    }
69}
70
71impl Indicator for TsfOscillator {
72    type Input = f64;
73    type Output = f64;
74
75    fn update(&mut self, input: f64) -> Option<f64> {
76        let forecast = self.tsf.update(input)?;
77        // Hold the previous value if the close is zero — the percentage form
78        // is undefined and a return of inf would propagate badly.
79        if input == 0.0 {
80            return self.current;
81        }
82        let value = 100.0 * (input - forecast) / input;
83        self.current = Some(value);
84        Some(value)
85    }
86
87    fn reset(&mut self) {
88        self.tsf.reset();
89        self.current = None;
90    }
91
92    fn warmup_period(&self) -> usize {
93        self.period
94    }
95
96    fn is_ready(&self) -> bool {
97        self.current.is_some()
98    }
99
100    fn name(&self) -> &'static str {
101        "TsfOscillator"
102    }
103}
104
105#[cfg(test)]
106mod tests {
107    use super::*;
108    use crate::traits::BatchExt;
109    use approx::assert_relative_eq;
110
111    #[test]
112    fn rejects_short_period() {
113        assert!(matches!(
114            TsfOscillator::new(1),
115            Err(Error::InvalidPeriod { .. })
116        ));
117        assert!(matches!(
118            TsfOscillator::new(0),
119            Err(Error::InvalidPeriod { .. })
120        ));
121    }
122
123    #[test]
124    fn accessors_and_metadata() {
125        let osc = TsfOscillator::new(14).unwrap();
126        assert_eq!(osc.period(), 14);
127        assert_eq!(osc.warmup_period(), 14);
128        assert_eq!(osc.name(), "TsfOscillator");
129        assert!(!osc.is_ready());
130    }
131
132    #[test]
133    fn reference_value() {
134        // period 3 over [1, 2, 9]: fit y = 0 + 4x, one-bar-ahead TSF at x = 3
135        // is 12. With close = 9, TSFOsc = 100·(9 − 12)/9 = −33.3333…%.
136        let mut osc = TsfOscillator::new(3).unwrap();
137        let out = osc.batch(&[1.0_f64, 2.0, 9.0]);
138        assert!(out[0].is_none());
139        assert!(out[1].is_none());
140        assert_relative_eq!(out[2].unwrap(), -100.0 / 3.0, epsilon = 1e-9);
141        assert!(osc.is_ready());
142    }
143
144    #[test]
145    fn constant_series_yields_zero() {
146        // On a flat series the regression slope is 0, so the one-bar-ahead TSF
147        // equals the constant and close − forecast is exactly 0.
148        let mut osc = TsfOscillator::new(5).unwrap();
149        let out = osc.batch(&[42.0_f64; 30]);
150        for v in out.iter().skip(4).flatten() {
151            assert_relative_eq!(*v, 0.0, epsilon = 1e-12);
152        }
153    }
154
155    #[test]
156    fn linear_uptrend_reads_negative() {
157        // Unlike CFO (evaluated at the current bar), the forecast steps one bar
158        // ahead, so on a rising line the projection sits above the close and the
159        // oscillator is negative: TSFOsc = −100·slope/close.
160        let mut osc = TsfOscillator::new(5).unwrap();
161        let prices: Vec<f64> = (1..=20).map(|i| f64::from(i) * 2.0).collect();
162        let out = osc.batch(&prices);
163        for v in out.iter().skip(4).flatten() {
164            assert!(*v < 0.0, "uptrend forecast overshoots close, got {v}");
165        }
166    }
167
168    #[test]
169    fn warmup_emits_first_value_at_period() {
170        let mut osc = TsfOscillator::new(3).unwrap();
171        assert_eq!(osc.update(1.0), None);
172        assert_eq!(osc.update(2.0), None);
173        assert!(osc.update(3.0).is_some());
174    }
175
176    #[test]
177    fn batch_equals_streaming() {
178        let prices: Vec<f64> = (1..=80)
179            .map(|i| 100.0 + (f64::from(i) * 0.3).sin() * 5.0)
180            .collect();
181        let mut a = TsfOscillator::new(14).unwrap();
182        let mut b = TsfOscillator::new(14).unwrap();
183        assert_eq!(
184            a.batch(&prices),
185            prices.iter().map(|p| b.update(*p)).collect::<Vec<_>>()
186        );
187    }
188
189    #[test]
190    fn reset_clears_state() {
191        let mut osc = TsfOscillator::new(5).unwrap();
192        osc.batch(&(1..=20).map(f64::from).collect::<Vec<_>>());
193        assert!(osc.is_ready());
194        osc.reset();
195        assert!(!osc.is_ready());
196        assert_eq!(osc.update(1.0), None);
197    }
198
199    #[test]
200    fn zero_close_holds_value() {
201        let mut osc = TsfOscillator::new(3).unwrap();
202        osc.batch(&[1.0_f64, 2.0, 3.0]);
203        let before = osc.current;
204        assert_eq!(osc.update(0.0), before);
205    }
206}