Skip to main content

wickra_core/indicators/
shooting_star.rs

1//! Shooting Star candlestick pattern.
2
3use crate::ohlcv::Candle;
4use crate::traits::Indicator;
5
6/// Shooting Star — a single-bar bearish reversal candidate.
7///
8/// A Shooting Star has the same geometry as an Inverted Hammer (small body
9/// near the bottom, long upper shadow ≥ 2× body, short lower shadow) but is
10/// read bearishly because it appears at the top of an uptrend.
11///
12/// ```text
13/// body         = |close − open|
14/// upper_shadow = high − max(open, close)
15/// lower_shadow = min(open, close) − low
16/// star         = upper_shadow >= 2 * body
17///               && lower_shadow <= body
18///               && body > 0
19/// ```
20///
21/// Output is `−1.0` when the shape matches, `0.0` otherwise. Pattern-shape
22/// check only — no trend filter is applied; combine with a trend indicator
23/// for actionable signals.
24///
25/// # Signed ±1 encoding
26///
27/// A Shooting Star is bearish by definition, so under the uniform candlestick
28/// sign convention (`+1.0` bullish, `−1.0` bearish, `0.0` none) it emits
29/// `−1.0` when the shape matches and `0.0` otherwise — it never emits `+1.0`.
30/// The same geometry read at the bottom of a downtrend is the bullish
31/// `InvertedHammer`, which carries the opposite sign.
32///
33/// # Example
34///
35/// ```
36/// use wickra_core::{Candle, Indicator, ShootingStar};
37///
38/// let mut indicator = ShootingStar::new();
39/// let candle = Candle::new(10.0, 15.0, 9.9, 10.5, 1.0, 0).unwrap();
40/// assert_eq!(indicator.update(candle), Some(-1.0));
41/// ```
42#[derive(Debug, Clone, Default)]
43pub struct ShootingStar {
44    has_emitted: bool,
45}
46
47impl ShootingStar {
48    /// Construct a new Shooting Star detector.
49    pub const fn new() -> Self {
50        Self { has_emitted: false }
51    }
52}
53
54impl Indicator for ShootingStar {
55    type Input = Candle;
56    type Output = f64;
57
58    #[inline]
59    fn update(&mut self, candle: Candle) -> Option<f64> {
60        self.has_emitted = true;
61        let range = candle.high - candle.low;
62        if range <= 0.0 {
63            return Some(0.0);
64        }
65        let body = (candle.close - candle.open).abs();
66        if body <= 0.0 {
67            return Some(0.0);
68        }
69        let upper = candle.high - candle.open.max(candle.close);
70        let lower = candle.open.min(candle.close) - candle.low;
71        Some(if upper >= 2.0 * body && lower <= body {
72            -1.0
73        } else {
74            0.0
75        })
76    }
77
78    fn reset(&mut self) {
79        self.has_emitted = false;
80    }
81
82    #[inline]
83    fn warmup_period(&self) -> usize {
84        1
85    }
86
87    #[inline]
88    fn is_ready(&self) -> bool {
89        self.has_emitted
90    }
91
92    #[inline]
93    fn name(&self) -> &'static str {
94        "ShootingStar"
95    }
96}
97
98#[cfg(test)]
99mod tests {
100    use super::*;
101    use crate::traits::BatchExt;
102
103    fn c(open: f64, high: f64, low: f64, close: f64, ts: i64) -> Candle {
104        Candle::new(open, high, low, close, 1.0, ts).unwrap()
105    }
106
107    #[test]
108    fn accessors_and_metadata() {
109        let s = ShootingStar::new();
110        assert_eq!(s.name(), "ShootingStar");
111        assert_eq!(s.warmup_period(), 1);
112        assert!(!s.is_ready());
113    }
114
115    #[test]
116    fn clean_shooting_star_is_minus_one() {
117        let mut s = ShootingStar::new();
118        assert_eq!(s.update(c(10.0, 15.0, 9.9, 10.5, 0)), Some(-1.0));
119    }
120
121    #[test]
122    fn hammer_shape_is_not_shooting_star() {
123        let mut s = ShootingStar::new();
124        assert_eq!(s.update(c(10.0, 10.6, 5.0, 10.5, 0)), Some(0.0));
125    }
126
127    #[test]
128    fn doji_is_not_shooting_star() {
129        let mut s = ShootingStar::new();
130        assert_eq!(s.update(c(10.0, 11.0, 9.0, 10.0, 0)), Some(0.0));
131    }
132
133    #[test]
134    fn zero_range_yields_zero() {
135        let mut s = ShootingStar::new();
136        assert_eq!(s.update(c(10.0, 10.0, 10.0, 10.0, 0)), Some(0.0));
137    }
138
139    #[test]
140    fn batch_equals_streaming() {
141        let candles: Vec<Candle> = (0..40)
142            .map(|i| {
143                let base = 100.0 + i as f64;
144                c(base, base + 4.0, base - 0.1, base + 0.5, i)
145            })
146            .collect();
147        let mut a = ShootingStar::new();
148        let mut b = ShootingStar::new();
149        assert_eq!(
150            a.batch(&candles),
151            candles.iter().map(|x| b.update(*x)).collect::<Vec<_>>()
152        );
153    }
154
155    #[test]
156    fn reset_clears_state() {
157        let mut s = ShootingStar::new();
158        s.update(c(10.0, 15.0, 9.9, 10.5, 0));
159        assert!(s.is_ready());
160        s.reset();
161        assert!(!s.is_ready());
162    }
163}