Skip to main content

wickra_core/indicators/
tristar.rs

1#![allow(clippy::doc_markdown)]
2
3//! Tristar — a three-doji reversal pattern.
4//!
5//! A Tristar is three consecutive Doji candles where the middle one gaps away
6//! from its neighbours, forming a star. A bearish Tristar (top) has the middle
7//! doji sitting above the other two; a bullish Tristar (bottom) has it below.
8//!
9//! - **Bullish** (`+1.0`): three dojis, the middle doji's body centre below both
10//!   neighbours' body centres.
11//! - **Bearish** (`-1.0`): three dojis, the middle above both neighbours.
12//! - Otherwise the output is `0.0`.
13//!
14//! A doji is a candle whose body is `<= 0.1 * range`. The three-bar lookback means
15//! the first value lands on the third candle.
16
17use crate::ohlcv::Candle;
18use crate::traits::Indicator;
19
20/// Body-centre of a candle.
21fn body_mid(candle: Candle) -> f64 {
22    f64::midpoint(candle.open, candle.close)
23}
24
25/// Whether a candle is a doji (body small relative to range).
26fn is_doji(candle: Candle) -> bool {
27    let body = (candle.close - candle.open).abs();
28    let range = candle.high - candle.low;
29    range > 0.0 && body <= 0.1 * range
30}
31
32/// Tristar — three-doji star reversal detector.
33/// # Example
34///
35/// ```
36/// use wickra_core::{Tristar, Candle, Indicator};
37///
38/// let mut indicator = Tristar::new();
39/// // `None` during warmup, then `Some(_)` once enough bars are seen.
40/// let mut out = None;
41/// for i in 0..40i64 {
42///     let p = 100.0 + (i as f64 * 0.4).sin() * 5.0;
43///     let candle = Candle::new(p, p + 1.5, p - 1.5, p + 0.3, 1_000.0, i).unwrap();
44///     out = indicator.update(candle);
45/// }
46/// let _ = out;
47/// ```
48#[derive(Debug, Clone, Default)]
49pub struct Tristar {
50    c1: Option<Candle>,
51    c2: Option<Candle>,
52    last_value: Option<f64>,
53}
54
55impl Tristar {
56    /// Construct a new `Tristar`.
57    #[must_use]
58    pub fn new() -> Self {
59        Self::default()
60    }
61
62    /// Latest emitted signal if available.
63    pub const fn value(&self) -> Option<f64> {
64        self.last_value
65    }
66}
67
68impl Indicator for Tristar {
69    type Input = Candle;
70    type Output = f64;
71
72    fn update(&mut self, candle: Candle) -> Option<f64> {
73        let (Some(first), Some(middle)) = (self.c1, self.c2) else {
74            self.c1 = self.c2;
75            self.c2 = Some(candle);
76            self.last_value = Some(0.0);
77            return Some(0.0);
78        };
79        let v = if is_doji(first) && is_doji(middle) && is_doji(candle) {
80            let mid = body_mid(middle);
81            let n1 = body_mid(first);
82            let n3 = body_mid(candle);
83            if mid > n1 && mid > n3 {
84                -1.0
85            } else if mid < n1 && mid < n3 {
86                1.0
87            } else {
88                0.0
89            }
90        } else {
91            0.0
92        };
93        self.c1 = self.c2;
94        self.c2 = Some(candle);
95        self.last_value = Some(v);
96        Some(v)
97    }
98
99    fn reset(&mut self) {
100        self.c1 = None;
101        self.c2 = None;
102        self.last_value = None;
103    }
104
105    fn warmup_period(&self) -> usize {
106        3
107    }
108
109    fn is_ready(&self) -> bool {
110        self.last_value.is_some()
111    }
112
113    fn name(&self) -> &'static str {
114        "Tristar"
115    }
116}
117
118#[cfg(test)]
119mod tests {
120    use super::*;
121    use crate::traits::BatchExt;
122
123    /// A doji centred at `mid` (tiny body, symmetric shadows).
124    fn doji(mid: f64) -> Candle {
125        Candle::new_unchecked(mid, mid + 1.0, mid - 1.0, mid + 0.02, 0.0, 0)
126    }
127
128    /// A non-doji (big body).
129    fn solid(open: f64, close: f64) -> Candle {
130        Candle::new_unchecked(
131            open,
132            open.max(close) + 0.1,
133            open.min(close) - 0.1,
134            close,
135            0.0,
136            0,
137        )
138    }
139
140    #[test]
141    fn accessors_and_metadata() {
142        let t = Tristar::new();
143        assert_eq!(t.warmup_period(), 3);
144        assert_eq!(t.name(), "Tristar");
145        assert!(!t.is_ready());
146        assert_eq!(t.value(), None);
147    }
148
149    #[test]
150    fn first_two_bars_seed_without_signal() {
151        let mut t = Tristar::new();
152        assert_eq!(t.update(doji(100.0)), Some(0.0));
153        assert_eq!(t.update(doji(100.0)), Some(0.0));
154        assert!(t.update(doji(100.0)).is_some());
155    }
156
157    #[test]
158    fn bearish_tristar_top() {
159        // middle doji centred above the two neighbours -> top -> -1.
160        let mut t = Tristar::new();
161        t.update(doji(100.0));
162        t.update(doji(105.0)); // middle, highest
163        assert_eq!(t.update(doji(100.0)), Some(-1.0));
164    }
165
166    #[test]
167    fn bullish_tristar_bottom() {
168        let mut t = Tristar::new();
169        t.update(doji(100.0));
170        t.update(doji(95.0)); // middle, lowest
171        assert_eq!(t.update(doji(100.0)), Some(1.0));
172    }
173
174    #[test]
175    fn non_doji_is_zero() {
176        let mut t = Tristar::new();
177        t.update(doji(100.0));
178        t.update(solid(100.0, 110.0)); // not a doji
179        assert_eq!(t.update(doji(100.0)), Some(0.0));
180    }
181
182    #[test]
183    fn reset_clears_state() {
184        let mut t = Tristar::new();
185        t.update(doji(100.0));
186        t.update(doji(105.0));
187        t.update(doji(100.0));
188        assert!(t.is_ready());
189        t.reset();
190        assert!(!t.is_ready());
191        assert_eq!(t.update(doji(100.0)), Some(0.0));
192    }
193
194    #[test]
195    fn batch_equals_streaming() {
196        let candles: Vec<Candle> = (0..40)
197            .map(|i| doji(100.0 + (f64::from(i) * 0.4).sin() * 5.0))
198            .collect();
199        let batch = Tristar::new().batch(&candles);
200        let mut b = Tristar::new();
201        let streamed: Vec<_> = candles.iter().map(|x| b.update(*x)).collect();
202        assert_eq!(batch, streamed);
203    }
204}