Skip to main content

wickra_core/indicators/
hikkake.rs

1//! Hikkake candlestick pattern.
2
3use crate::ohlcv::Candle;
4use crate::traits::Indicator;
5
6/// Hikkake — a 3-bar trap. An inside bar (bar2 fully contained by bar1) sets up a
7/// breakout that immediately fails on bar3, trapping breakout traders and pointing
8/// the opposite way.
9///
10/// ```text
11/// inside bar : bar2.high < bar1.high  &&  bar2.low > bar1.low
12/// bullish (+1.0): bar3 makes a LOWER high AND LOWER low than bar2
13///                 (a false downside break -> expect a move up)
14/// bearish (−1.0): bar3 makes a HIGHER high AND HIGHER low than bar2
15///                 (a false upside break -> expect a move down)
16/// ```
17///
18/// Output is `+1.0` (bullish setup), `−1.0` (bearish setup), or `0.0` otherwise.
19/// The detector fires when the three-bar setup completes on bar3; it does not
20/// separately flag the optional later confirmation bar. The first two bars always
21/// return `0.0` because the window is not yet filled. Pattern-shape check only —
22/// no trend filter is applied; combine with a trend indicator for actionable
23/// signals.
24///
25/// # Signed ±1 encoding
26///
27/// This detector emits the uniform candlestick sign convention shared across the
28/// pattern family — `+1.0` bullish, `−1.0` bearish, `0.0` no pattern — so it
29/// drops straight into a machine-learning feature matrix where the bullish and
30/// bearish setups occupy a single dimension.
31///
32/// # Example
33///
34/// ```
35/// use wickra_core::{Candle, Hikkake, Indicator};
36///
37/// let mut indicator = Hikkake::new();
38/// indicator.update(Candle::new(10.0, 15.0, 5.0, 12.0, 1.0, 0).unwrap());
39/// indicator.update(Candle::new(11.0, 13.0, 8.0, 12.0, 1.0, 1).unwrap());
40/// let out = indicator
41///     .update(Candle::new(9.0, 12.0, 6.0, 7.0, 1.0, 2).unwrap());
42/// assert_eq!(out, Some(1.0));
43/// ```
44#[derive(Debug, Clone, Default)]
45pub struct Hikkake {
46    prev: Option<Candle>,
47    prev_prev: Option<Candle>,
48    has_emitted: bool,
49}
50
51impl Hikkake {
52    /// Construct a new Hikkake detector.
53    pub const fn new() -> Self {
54        Self {
55            prev: None,
56            prev_prev: None,
57            has_emitted: false,
58        }
59    }
60}
61
62impl Indicator for Hikkake {
63    type Input = Candle;
64    type Output = f64;
65
66    fn update(&mut self, candle: Candle) -> Option<f64> {
67        self.has_emitted = true;
68        let bar1 = self.prev_prev;
69        let bar2 = self.prev;
70        self.prev_prev = self.prev;
71        self.prev = Some(candle);
72        let (Some(bar1), Some(bar2)) = (bar1, bar2) else {
73            return Some(0.0);
74        };
75        // bar2 must be an inside bar of bar1.
76        if !(bar2.high < bar1.high && bar2.low > bar1.low) {
77            return Some(0.0);
78        }
79        // Bullish: bar3 breaks below the inside bar (lower high and lower low).
80        if candle.high < bar2.high && candle.low < bar2.low {
81            return Some(1.0);
82        }
83        // Bearish: bar3 breaks above the inside bar (higher high and higher low).
84        if candle.high > bar2.high && candle.low > bar2.low {
85            return Some(-1.0);
86        }
87        Some(0.0)
88    }
89
90    fn reset(&mut self) {
91        self.prev = None;
92        self.prev_prev = None;
93        self.has_emitted = false;
94    }
95
96    fn warmup_period(&self) -> usize {
97        3
98    }
99
100    fn is_ready(&self) -> bool {
101        self.has_emitted
102    }
103
104    fn name(&self) -> &'static str {
105        "Hikkake"
106    }
107}
108
109#[cfg(test)]
110mod tests {
111    use super::*;
112    use crate::traits::BatchExt;
113
114    fn c(open: f64, high: f64, low: f64, close: f64, ts: i64) -> Candle {
115        Candle::new(open, high, low, close, 1.0, ts).unwrap()
116    }
117
118    #[test]
119    fn accessors_and_metadata() {
120        let t = Hikkake::new();
121        assert_eq!(t.name(), "Hikkake");
122        assert_eq!(t.warmup_period(), 3);
123        assert!(!t.is_ready());
124    }
125
126    #[test]
127    fn bullish_hikkake_is_plus_one() {
128        let mut t = Hikkake::new();
129        assert_eq!(t.update(c(10.0, 15.0, 5.0, 12.0, 0)), Some(0.0));
130        assert_eq!(t.update(c(11.0, 13.0, 8.0, 12.0, 1)), Some(0.0));
131        assert_eq!(t.update(c(9.0, 12.0, 6.0, 7.0, 2)), Some(1.0));
132    }
133
134    #[test]
135    fn bearish_hikkake_is_minus_one() {
136        let mut t = Hikkake::new();
137        assert_eq!(t.update(c(10.0, 15.0, 5.0, 12.0, 0)), Some(0.0));
138        assert_eq!(t.update(c(11.0, 13.0, 8.0, 12.0, 1)), Some(0.0));
139        assert_eq!(t.update(c(12.0, 14.0, 9.0, 13.0, 2)), Some(-1.0));
140    }
141
142    #[test]
143    fn not_inside_bar_yields_zero() {
144        let mut t = Hikkake::new();
145        t.update(c(10.0, 15.0, 5.0, 12.0, 0));
146        // bar2 is not contained by bar1 (higher high).
147        t.update(c(11.0, 16.0, 8.0, 12.0, 1));
148        assert_eq!(t.update(c(9.0, 12.0, 6.0, 7.0, 2)), Some(0.0));
149    }
150
151    #[test]
152    fn outside_bar3_yields_zero() {
153        let mut t = Hikkake::new();
154        t.update(c(10.0, 15.0, 5.0, 12.0, 0));
155        t.update(c(11.0, 13.0, 8.0, 12.0, 1));
156        // bar3 engulfs bar2 (higher high and lower low) -> neither direction.
157        assert_eq!(t.update(c(11.0, 14.0, 7.0, 9.0, 2)), Some(0.0));
158    }
159
160    #[test]
161    fn first_two_bars_return_zero() {
162        let mut t = Hikkake::new();
163        assert_eq!(t.update(c(10.0, 15.0, 5.0, 12.0, 0)), Some(0.0));
164        assert_eq!(t.update(c(11.0, 13.0, 8.0, 12.0, 1)), Some(0.0));
165    }
166
167    #[test]
168    fn batch_equals_streaming() {
169        let candles: Vec<Candle> = (0..40)
170            .map(|i| {
171                let base = 100.0 + i as f64;
172                match i % 3 {
173                    0 => c(base, base + 6.0, base - 6.0, base, i),
174                    1 => c(base, base + 2.0, base - 2.0, base, i),
175                    _ => c(base, base + 1.0, base - 5.0, base - 4.0, i),
176                }
177            })
178            .collect();
179        let mut a = Hikkake::new();
180        let mut b = Hikkake::new();
181        assert_eq!(
182            a.batch(&candles),
183            candles.iter().map(|x| b.update(*x)).collect::<Vec<_>>()
184        );
185    }
186
187    #[test]
188    fn reset_clears_state() {
189        let mut t = Hikkake::new();
190        t.update(c(10.0, 15.0, 5.0, 12.0, 0));
191        t.update(c(11.0, 13.0, 8.0, 12.0, 1));
192        t.update(c(9.0, 12.0, 6.0, 7.0, 2));
193        assert!(t.is_ready());
194        t.reset();
195        assert!(!t.is_ready());
196        assert_eq!(t.update(c(10.0, 15.0, 5.0, 12.0, 0)), Some(0.0));
197    }
198}