Skip to main content

wickra_core/indicators/
hikkake_modified.rs

1//! Modified Hikkake candlestick pattern.
2
3use crate::ohlcv::Candle;
4use crate::traits::Indicator;
5
6/// Modified Hikkake — a close-confirmed variant of the [`Hikkake`](crate::Hikkake)
7/// trap. An inside bar is followed by a bar that breaks out *and is immediately
8/// rejected*: it pierces the inside bar's range intrabar but closes back inside,
9/// a stronger signal than the plain breakout setup.
10///
11/// ```text
12/// inside bar : bar2.high < bar1.high  &&  bar2.low > bar1.low
13/// bullish (+1.0): bar3 makes a lower high AND lower low than bar2,
14///                 yet closes back above the inside-bar low   (close3 > bar2.low)
15/// bearish (−1.0): bar3 makes a higher high AND higher low than bar2,
16///                 yet closes back below the inside-bar high  (close3 < bar2.high)
17/// ```
18///
19/// Output is `+1.0` (bullish), `−1.0` (bearish), or `0.0` otherwise. The extra
20/// close-recovery condition is what distinguishes it from the plain Hikkake, which
21/// fires on the high/low break alone. The first two bars always return `0.0`
22/// because the three-bar window is not yet filled. Pattern-shape check only — no
23/// trend filter is applied; combine with a trend indicator for actionable 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 as a single dimension.
30///
31/// # Example
32///
33/// ```
34/// use wickra_core::{Candle, HikkakeModified, Indicator};
35///
36/// let mut indicator = HikkakeModified::new();
37/// indicator.update(Candle::new(10.0, 15.0, 5.0, 12.0, 1.0, 0).unwrap());
38/// indicator.update(Candle::new(11.0, 13.0, 8.0, 12.0, 1.0, 1).unwrap());
39/// let out = indicator
40///     .update(Candle::new(9.0, 12.0, 6.0, 9.0, 1.0, 2).unwrap());
41/// assert_eq!(out, Some(1.0));
42/// ```
43#[derive(Debug, Clone, Default)]
44pub struct HikkakeModified {
45    prev: Option<Candle>,
46    prev_prev: Option<Candle>,
47    has_emitted: bool,
48}
49
50impl HikkakeModified {
51    /// Construct a new Modified Hikkake detector.
52    pub const fn new() -> Self {
53        Self {
54            prev: None,
55            prev_prev: None,
56            has_emitted: false,
57        }
58    }
59}
60
61impl Indicator for HikkakeModified {
62    type Input = Candle;
63    type Output = f64;
64
65    fn update(&mut self, candle: Candle) -> Option<f64> {
66        self.has_emitted = true;
67        let bar1 = self.prev_prev;
68        let bar2 = self.prev;
69        self.prev_prev = self.prev;
70        self.prev = Some(candle);
71        let (Some(bar1), Some(bar2)) = (bar1, bar2) else {
72            return Some(0.0);
73        };
74        if !(bar2.high < bar1.high && bar2.low > bar1.low) {
75            return Some(0.0);
76        }
77        // Bullish: false downside break that closes back above the inside-bar low.
78        if candle.high < bar2.high && candle.low < bar2.low && candle.close > bar2.low {
79            return Some(1.0);
80        }
81        // Bearish: false upside break that closes back below the inside-bar high.
82        if candle.high > bar2.high && candle.low > bar2.low && candle.close < bar2.high {
83            return Some(-1.0);
84        }
85        Some(0.0)
86    }
87
88    fn reset(&mut self) {
89        self.prev = None;
90        self.prev_prev = None;
91        self.has_emitted = false;
92    }
93
94    fn warmup_period(&self) -> usize {
95        3
96    }
97
98    fn is_ready(&self) -> bool {
99        self.has_emitted
100    }
101
102    fn name(&self) -> &'static str {
103        "HikkakeModified"
104    }
105}
106
107#[cfg(test)]
108mod tests {
109    use super::*;
110    use crate::traits::BatchExt;
111
112    fn c(open: f64, high: f64, low: f64, close: f64, ts: i64) -> Candle {
113        Candle::new(open, high, low, close, 1.0, ts).unwrap()
114    }
115
116    #[test]
117    fn accessors_and_metadata() {
118        let t = HikkakeModified::new();
119        assert_eq!(t.name(), "HikkakeModified");
120        assert_eq!(t.warmup_period(), 3);
121        assert!(!t.is_ready());
122    }
123
124    #[test]
125    fn bullish_modified_hikkake_is_plus_one() {
126        let mut t = HikkakeModified::new();
127        assert_eq!(t.update(c(10.0, 15.0, 5.0, 12.0, 0)), Some(0.0));
128        assert_eq!(t.update(c(11.0, 13.0, 8.0, 12.0, 1)), Some(0.0));
129        assert_eq!(t.update(c(9.0, 12.0, 6.0, 9.0, 2)), Some(1.0));
130    }
131
132    #[test]
133    fn bearish_modified_hikkake_is_minus_one() {
134        let mut t = HikkakeModified::new();
135        assert_eq!(t.update(c(10.0, 15.0, 5.0, 12.0, 0)), Some(0.0));
136        assert_eq!(t.update(c(11.0, 13.0, 8.0, 12.0, 1)), Some(0.0));
137        assert_eq!(t.update(c(13.0, 14.0, 9.0, 10.0, 2)), Some(-1.0));
138    }
139
140    #[test]
141    fn break_without_close_recovery_yields_zero() {
142        let mut t = HikkakeModified::new();
143        t.update(c(10.0, 15.0, 5.0, 12.0, 0));
144        t.update(c(11.0, 13.0, 8.0, 12.0, 1));
145        // Lower high and lower low, but closes below the inside-bar low -> plain
146        // Hikkake break, not the close-confirmed modified version.
147        assert_eq!(t.update(c(9.0, 12.0, 6.0, 7.0, 2)), Some(0.0));
148    }
149
150    #[test]
151    fn not_inside_bar_yields_zero() {
152        let mut t = HikkakeModified::new();
153        t.update(c(10.0, 15.0, 5.0, 12.0, 0));
154        t.update(c(11.0, 16.0, 8.0, 12.0, 1));
155        assert_eq!(t.update(c(9.0, 12.0, 6.0, 9.0, 2)), Some(0.0));
156    }
157
158    #[test]
159    fn first_two_bars_return_zero() {
160        let mut t = HikkakeModified::new();
161        assert_eq!(t.update(c(10.0, 15.0, 5.0, 12.0, 0)), Some(0.0));
162        assert_eq!(t.update(c(11.0, 13.0, 8.0, 12.0, 1)), Some(0.0));
163    }
164
165    #[test]
166    fn batch_equals_streaming() {
167        let candles: Vec<Candle> = (0..40)
168            .map(|i| {
169                let base = 100.0 + i as f64;
170                match i % 3 {
171                    0 => c(base, base + 6.0, base - 6.0, base, i),
172                    1 => c(base, base + 2.0, base - 2.0, base, i),
173                    _ => c(base, base + 1.0, base - 5.0, base, i),
174                }
175            })
176            .collect();
177        let mut a = HikkakeModified::new();
178        let mut b = HikkakeModified::new();
179        assert_eq!(
180            a.batch(&candles),
181            candles.iter().map(|x| b.update(*x)).collect::<Vec<_>>()
182        );
183    }
184
185    #[test]
186    fn reset_clears_state() {
187        let mut t = HikkakeModified::new();
188        t.update(c(10.0, 15.0, 5.0, 12.0, 0));
189        t.update(c(11.0, 13.0, 8.0, 12.0, 1));
190        t.update(c(9.0, 12.0, 6.0, 9.0, 2));
191        assert!(t.is_ready());
192        t.reset();
193        assert!(!t.is_ready());
194        assert_eq!(t.update(c(10.0, 15.0, 5.0, 12.0, 0)), Some(0.0));
195    }
196}