Skip to main content

wickra_core/indicators/
t3.rs

1//! Tillson T3 Moving Average.
2
3use crate::error::{Error, Result};
4use crate::traits::Indicator;
5
6use super::Ema;
7
8/// Tillson's T3 — a six-fold cascaded EMA recombined with a *volume factor* `v`.
9///
10/// T3 is the generalised DEMA applied three times. Tim Tillson's expansion of
11/// that triple application over six chained EMAs (`e1 … e6`, each of the same
12/// `period`) gives the closed form used here:
13///
14/// ```text
15/// c1 = −v³
16/// c2 = 3v² + 3v³
17/// c3 = −6v² − 3v − 3v³
18/// c4 = 1 + 3v + v³ + 3v²
19/// T3 = c1·e6 + c2·e5 + c3·e4 + c4·e3
20/// ```
21///
22/// The volume factor `v ∈ [0, 1]` controls the lag/smoothness trade-off:
23/// `v = 0` collapses T3 to the plain triple-cascaded EMA `e3`, while the
24/// conventional `v = 0.7` adds a hump that sharpens the response to turns.
25/// The coefficients always sum to `1`, so a constant series maps to itself.
26///
27/// The first output lands after `6·period − 5` inputs — the index at which the
28/// sixth cascaded EMA seeds.
29///
30/// # Example
31///
32/// ```
33/// use wickra_core::{Indicator, T3};
34///
35/// let mut indicator = T3::new(5, 0.7).unwrap();
36/// let mut last = None;
37/// for i in 0..120 {
38///     last = indicator.update(100.0 + f64::from(i));
39/// }
40/// assert!(last.is_some());
41/// ```
42#[derive(Debug, Clone)]
43pub struct T3 {
44    period: usize,
45    v: f64,
46    c1: f64,
47    c2: f64,
48    c3: f64,
49    c4: f64,
50    e1: Ema,
51    e2: Ema,
52    e3: Ema,
53    e4: Ema,
54    e5: Ema,
55    e6: Ema,
56    current: Option<f64>,
57}
58
59impl T3 {
60    /// Construct a new T3 with the given `period` and volume factor `v`.
61    ///
62    /// # Errors
63    ///
64    /// Returns [`Error::PeriodZero`] if `period == 0`, or
65    /// [`Error::InvalidPeriod`] if `v` is non-finite or outside `[0.0, 1.0]`.
66    pub fn new(period: usize, v: f64) -> Result<Self> {
67        if period == 0 {
68            return Err(Error::PeriodZero);
69        }
70        if !v.is_finite() || !(0.0..=1.0).contains(&v) {
71            return Err(Error::InvalidPeriod {
72                message: "T3 volume factor must be a finite value in [0.0, 1.0]",
73            });
74        }
75        let v2 = v * v;
76        let v3 = v2 * v;
77        Ok(Self {
78            period,
79            v,
80            c1: -v3,
81            c2: 3.0 * v2 + 3.0 * v3,
82            c3: -6.0 * v2 - 3.0 * v - 3.0 * v3,
83            c4: 1.0 + 3.0 * v + v3 + 3.0 * v2,
84            e1: Ema::new(period)?,
85            e2: Ema::new(period)?,
86            e3: Ema::new(period)?,
87            e4: Ema::new(period)?,
88            e5: Ema::new(period)?,
89            e6: Ema::new(period)?,
90            current: None,
91        })
92    }
93
94    /// Configured period.
95    pub const fn period(&self) -> usize {
96        self.period
97    }
98
99    /// Configured volume factor `v`.
100    pub const fn volume_factor(&self) -> f64 {
101        self.v
102    }
103
104    /// Current value if available.
105    pub const fn value(&self) -> Option<f64> {
106        self.current
107    }
108}
109
110impl Indicator for T3 {
111    type Input = f64;
112    type Output = f64;
113
114    fn update(&mut self, input: f64) -> Option<f64> {
115        if !input.is_finite() {
116            // Non-finite input is ignored; the cascade is not advanced.
117            return self.current;
118        }
119        let e1 = self.e1.update(input)?;
120        let e2 = self.e2.update(e1)?;
121        let e3 = self.e3.update(e2)?;
122        let e4 = self.e4.update(e3)?;
123        let e5 = self.e5.update(e4)?;
124        let e6 = self.e6.update(e5)?;
125        let out = self.c1 * e6 + self.c2 * e5 + self.c3 * e4 + self.c4 * e3;
126        self.current = Some(out);
127        Some(out)
128    }
129
130    fn reset(&mut self) {
131        self.e1.reset();
132        self.e2.reset();
133        self.e3.reset();
134        self.e4.reset();
135        self.e5.reset();
136        self.e6.reset();
137        self.current = None;
138    }
139
140    fn warmup_period(&self) -> usize {
141        6 * self.period - 5
142    }
143
144    fn is_ready(&self) -> bool {
145        self.current.is_some()
146    }
147
148    fn name(&self) -> &'static str {
149        "T3"
150    }
151}
152
153#[cfg(test)]
154mod tests {
155    use super::*;
156    use crate::traits::BatchExt;
157    use approx::assert_relative_eq;
158
159    #[test]
160    fn new_rejects_zero_period() {
161        assert!(matches!(T3::new(0, 0.7), Err(Error::PeriodZero)));
162    }
163
164    /// Cover the const accessors `period` / `volume_factor` / `value` and
165    /// the Indicator-impl `name` (lines 95-107, 148-150). Existing tests
166    /// query `warmup_period` (covered by `first_emission_at_warmup_period`)
167    /// but never inspect period, v, value, or name.
168    #[test]
169    fn accessors_and_metadata() {
170        let mut t3 = T3::new(5, 0.7).unwrap();
171        assert_eq!(t3.period(), 5);
172        assert_relative_eq!(t3.volume_factor(), 0.7, epsilon = 1e-12);
173        assert_eq!(t3.name(), "T3");
174        assert_eq!(t3.value(), None);
175        for _ in 0..t3.warmup_period() {
176            t3.update(50.0);
177        }
178        assert!(t3.value().is_some());
179    }
180
181    #[test]
182    fn new_rejects_out_of_range_volume_factor() {
183        assert!(matches!(T3::new(5, -0.1), Err(Error::InvalidPeriod { .. })));
184        assert!(matches!(T3::new(5, 1.5), Err(Error::InvalidPeriod { .. })));
185        assert!(matches!(
186            T3::new(5, f64::NAN),
187            Err(Error::InvalidPeriod { .. })
188        ));
189        assert!(T3::new(5, 0.0).is_ok());
190        assert!(T3::new(5, 1.0).is_ok());
191    }
192
193    #[test]
194    fn coefficients_sum_to_one() {
195        // c1 + c2 + c3 + c4 == 1 for any v, so a constant series is preserved.
196        for &v in &[0.0, 0.3, 0.7, 1.0] {
197            let t3 = T3::new(5, v).unwrap();
198            assert_relative_eq!(t3.c1 + t3.c2 + t3.c3 + t3.c4, 1.0, epsilon = 1e-12);
199        }
200    }
201
202    #[test]
203    fn first_emission_at_warmup_period() {
204        let mut t3 = T3::new(4, 0.7).unwrap();
205        assert_eq!(t3.warmup_period(), 6 * 4 - 5);
206        let out = t3.batch(&(1..=60).map(f64::from).collect::<Vec<_>>());
207        for v in out.iter().take(t3.warmup_period() - 1) {
208            assert!(v.is_none());
209        }
210        assert!(out[t3.warmup_period() - 1].is_some());
211    }
212
213    #[test]
214    fn constant_series_yields_the_constant() {
215        let mut t3 = T3::new(6, 0.7).unwrap();
216        let out = t3.batch(&[50.0; 80]);
217        let last = out.iter().rev().flatten().next().unwrap();
218        assert_relative_eq!(*last, 50.0, epsilon = 1e-9);
219    }
220
221    #[test]
222    fn zero_volume_factor_collapses_to_triple_cascaded_ema() {
223        // With v = 0 the coefficients are c1=c2=c3=0, c4=1, so T3 == e3,
224        // the third stage of the EMA cascade.
225        let prices: Vec<f64> = (1..=80)
226            .map(|i| 100.0 + (f64::from(i) * 0.2).sin() * 9.0)
227            .collect();
228        let mut t3 = T3::new(5, 0.0).unwrap();
229        let got = t3.batch(&prices);
230
231        let mut e1 = Ema::new(5).unwrap();
232        let mut e2 = Ema::new(5).unwrap();
233        let mut e3 = Ema::new(5).unwrap();
234        let want: Vec<Option<f64>> = prices
235            .iter()
236            .map(|p| {
237                e1.update(*p)
238                    .and_then(|a| e2.update(a))
239                    .and_then(|b| e3.update(b))
240            })
241            .collect();
242
243        for i in (t3.warmup_period() - 1)..prices.len() {
244            assert_relative_eq!(got[i].unwrap(), want[i].unwrap(), epsilon = 1e-9);
245        }
246    }
247
248    #[test]
249    fn ignores_non_finite_input() {
250        let mut t3 = T3::new(4, 0.7).unwrap();
251        let out = t3.batch(&(1..=60).map(f64::from).collect::<Vec<_>>());
252        let last = *out.last().unwrap();
253        assert!(last.is_some());
254        assert_eq!(t3.update(f64::NAN), last);
255        assert_eq!(t3.update(f64::INFINITY), last);
256    }
257
258    #[test]
259    fn reset_clears_state() {
260        let mut t3 = T3::new(4, 0.7).unwrap();
261        t3.batch(&(1..=60).map(f64::from).collect::<Vec<_>>());
262        assert!(t3.is_ready());
263        t3.reset();
264        assert!(!t3.is_ready());
265        assert_eq!(t3.update(1.0), None);
266    }
267
268    #[test]
269    fn batch_equals_streaming() {
270        let prices: Vec<f64> = (1..=120)
271            .map(|i| 100.0 + (f64::from(i) * 0.25).sin() * 7.0)
272            .collect();
273        let batch = T3::new(7, 0.7).unwrap().batch(&prices);
274        let mut b = T3::new(7, 0.7).unwrap();
275        let streamed: Vec<_> = prices.iter().map(|p| b.update(*p)).collect();
276        assert_eq!(batch, streamed);
277    }
278}