Skip to main content

wickra_core/indicators/
spinning_top.rs

1//! Spinning Top candlestick pattern.
2
3use crate::error::{Error, Result};
4use crate::ohlcv::Candle;
5use crate::traits::Indicator;
6
7/// Spinning Top — a single-bar indecision candle with a small body and two
8/// long shadows.
9///
10/// ```text
11/// body         = |close − open|
12/// upper_shadow = high − max(open, close)
13/// lower_shadow = min(open, close) − low
14/// range        = high − low
15/// spinning     = body <= body_threshold * range
16///               && upper_shadow >= 2 * body
17///               && lower_shadow >= 2 * body
18///               && body > 0
19/// ```
20///
21/// While direction is ambiguous by intent, the output is direction-signed so
22/// downstream filters can distinguish a green spinning top (`+1.0`) from a red
23/// one (`−1.0`). A clean Doji (body == 0) is *not* a Spinning Top.
24///
25/// `body_threshold` defaults to `0.3` and must lie in `(0, 1]`.
26///
27/// # Example
28///
29/// ```
30/// use wickra_core::{Candle, Indicator, SpinningTop};
31///
32/// let mut indicator = SpinningTop::new();
33/// // Body 0.5, both shadows 3.0 -> spinning.
34/// let candle = Candle::new(10.0, 13.5, 7.0, 10.5, 1.0, 0).unwrap();
35/// assert_eq!(indicator.update(candle), Some(1.0));
36/// ```
37#[derive(Debug, Clone)]
38pub struct SpinningTop {
39    body_threshold: f64,
40    has_emitted: bool,
41}
42
43impl Default for SpinningTop {
44    fn default() -> Self {
45        Self::new()
46    }
47}
48
49impl SpinningTop {
50    /// Construct a Spinning Top detector with the default body threshold.
51    pub const fn new() -> Self {
52        Self {
53            body_threshold: 0.3,
54            has_emitted: false,
55        }
56    }
57
58    /// Construct a Spinning Top detector with a custom body / range threshold.
59    pub fn with_threshold(body_threshold: f64) -> Result<Self> {
60        if !(body_threshold > 0.0 && body_threshold <= 1.0) {
61            return Err(Error::InvalidPeriod {
62                message: "spinning top body threshold must lie in (0, 1]",
63            });
64        }
65        Ok(Self {
66            body_threshold,
67            has_emitted: false,
68        })
69    }
70
71    /// Configured body / range threshold.
72    pub fn body_threshold(&self) -> f64 {
73        self.body_threshold
74    }
75}
76
77impl Indicator for SpinningTop {
78    type Input = Candle;
79    type Output = f64;
80
81    fn update(&mut self, candle: Candle) -> Option<f64> {
82        self.has_emitted = true;
83        let range = candle.high - candle.low;
84        if range <= 0.0 {
85            return Some(0.0);
86        }
87        let body_signed = candle.close - candle.open;
88        let body = body_signed.abs();
89        if body <= 0.0 {
90            return Some(0.0);
91        }
92        if body > self.body_threshold * range {
93            return Some(0.0);
94        }
95        let upper = candle.high - candle.open.max(candle.close);
96        let lower = candle.open.min(candle.close) - candle.low;
97        if upper >= 2.0 * body && lower >= 2.0 * body {
98            Some(if body_signed > 0.0 { 1.0 } else { -1.0 })
99        } else {
100            Some(0.0)
101        }
102    }
103
104    fn reset(&mut self) {
105        self.has_emitted = false;
106    }
107
108    fn warmup_period(&self) -> usize {
109        1
110    }
111
112    fn is_ready(&self) -> bool {
113        self.has_emitted
114    }
115
116    fn name(&self) -> &'static str {
117        "SpinningTop"
118    }
119}
120
121#[cfg(test)]
122mod tests {
123    use super::*;
124    use crate::traits::BatchExt;
125
126    fn c(open: f64, high: f64, low: f64, close: f64, ts: i64) -> Candle {
127        Candle::new(open, high, low, close, 1.0, ts).unwrap()
128    }
129
130    #[test]
131    fn rejects_invalid_threshold() {
132        assert!(SpinningTop::with_threshold(0.0).is_err());
133        assert!(SpinningTop::with_threshold(1.5).is_err());
134    }
135
136    #[test]
137    fn accepts_valid_threshold() {
138        let s = SpinningTop::with_threshold(0.25).unwrap();
139        assert!((s.body_threshold() - 0.25).abs() < 1e-12);
140    }
141
142    #[test]
143    fn accessors_and_metadata() {
144        let s = SpinningTop::default();
145        assert_eq!(s.name(), "SpinningTop");
146        assert_eq!(s.warmup_period(), 1);
147        assert!(!s.is_ready());
148        assert!((s.body_threshold() - 0.3).abs() < 1e-12);
149    }
150
151    #[test]
152    fn green_spinning_top_is_plus_one() {
153        let mut s = SpinningTop::new();
154        // body 0.5 (10 -> 10.5), upper 3.0, lower 3.0, range 6.5 -> 0.5/6.5 < 0.3.
155        assert_eq!(s.update(c(10.0, 13.5, 7.0, 10.5, 0)), Some(1.0));
156    }
157
158    #[test]
159    fn red_spinning_top_is_minus_one() {
160        let mut s = SpinningTop::new();
161        assert_eq!(s.update(c(10.5, 13.5, 7.0, 10.0, 0)), Some(-1.0));
162    }
163
164    #[test]
165    fn marubozu_is_not_spinning() {
166        let mut s = SpinningTop::new();
167        assert_eq!(s.update(c(10.0, 12.0, 10.0, 12.0, 0)), Some(0.0));
168    }
169
170    #[test]
171    fn doji_is_not_spinning() {
172        // body == 0 fails the body > 0 guard.
173        let mut s = SpinningTop::new();
174        assert_eq!(s.update(c(10.0, 11.0, 9.0, 10.0, 0)), Some(0.0));
175    }
176
177    #[test]
178    fn hammer_shape_is_not_spinning_top() {
179        // Lower shadow is long but upper is tiny -> only one long shadow.
180        let mut s = SpinningTop::new();
181        assert_eq!(s.update(c(10.0, 10.6, 5.0, 10.5, 0)), Some(0.0));
182    }
183
184    #[test]
185    fn zero_range_yields_zero() {
186        let mut s = SpinningTop::new();
187        assert_eq!(s.update(c(10.0, 10.0, 10.0, 10.0, 0)), Some(0.0));
188    }
189
190    #[test]
191    fn batch_equals_streaming() {
192        let candles: Vec<Candle> = (0..40)
193            .map(|i| {
194                let base = 100.0 + i as f64;
195                c(base, base + 3.0, base - 3.0, base + 0.5, i)
196            })
197            .collect();
198        let mut a = SpinningTop::new();
199        let mut b = SpinningTop::new();
200        assert_eq!(
201            a.batch(&candles),
202            candles.iter().map(|x| b.update(*x)).collect::<Vec<_>>()
203        );
204    }
205
206    #[test]
207    fn reset_clears_state() {
208        let mut s = SpinningTop::new();
209        s.update(c(10.0, 13.5, 7.0, 10.5, 0));
210        assert!(s.is_ready());
211        s.reset();
212        assert!(!s.is_ready());
213    }
214}