Skip to main content

wickra_core/indicators/
high_wave.rs

1//! High-Wave candlestick pattern.
2
3use crate::ohlcv::Candle;
4use crate::traits::Indicator;
5
6/// High-Wave — a single-bar extreme-indecision signal. A small body with very
7/// long shadows on *both* sides: price swung far up and far down yet finished
8/// near the open, a sign that trend conviction has evaporated.
9///
10/// ```text
11/// range = high − low
12/// long upper = high − max(open, close) >= 0.4 * range
13/// long lower = min(open, close) − low  >= 0.4 * range
14/// ```
15///
16/// The two long-shadow conditions force the body below `0.2 * range`, so no
17/// separate body test is needed. Output is `+1.0` when the high-wave prints and
18/// `0.0` otherwise — a non-directional indecision flag, it never emits `−1.0`.
19/// Shadow thresholds follow the geometric house style rather than TA-Lib's
20/// rolling averages. Pattern-shape check only — no trend filter is applied;
21/// combine with a trend indicator for actionable signals.
22///
23/// # Signed ±1 encoding
24///
25/// This detector emits the uniform candlestick sign convention shared across the
26/// pattern family — `+1.0` detected, `0.0` no pattern — so it drops straight into
27/// a machine-learning feature matrix as a single dimension.
28///
29/// # Example
30///
31/// ```
32/// use wickra_core::{Candle, HighWave, Indicator};
33///
34/// let mut indicator = HighWave::new();
35/// // Small body, long shadows both sides.
36/// let candle = Candle::new(10.0, 12.0, 8.0, 10.3, 1.0, 0).unwrap();
37/// assert_eq!(indicator.update(candle), Some(1.0));
38/// ```
39#[derive(Debug, Clone, Default)]
40pub struct HighWave {
41    has_emitted: bool,
42}
43
44impl HighWave {
45    /// Construct a new High-Wave detector.
46    pub const fn new() -> Self {
47        Self { has_emitted: false }
48    }
49}
50
51impl Indicator for HighWave {
52    type Input = Candle;
53    type Output = f64;
54
55    #[inline]
56    fn update(&mut self, candle: Candle) -> Option<f64> {
57        self.has_emitted = true;
58        let range = candle.high - candle.low;
59        if range <= 0.0 {
60            return Some(0.0);
61        }
62        let upper = candle.high - candle.open.max(candle.close);
63        let lower = candle.open.min(candle.close) - candle.low;
64        if upper >= 0.4 * range && lower >= 0.4 * range {
65            return Some(1.0);
66        }
67        Some(0.0)
68    }
69
70    fn reset(&mut self) {
71        self.has_emitted = false;
72    }
73
74    #[inline]
75    fn warmup_period(&self) -> usize {
76        1
77    }
78
79    #[inline]
80    fn is_ready(&self) -> bool {
81        self.has_emitted
82    }
83
84    #[inline]
85    fn name(&self) -> &'static str {
86        "HighWave"
87    }
88}
89
90#[cfg(test)]
91mod tests {
92    use super::*;
93    use crate::traits::BatchExt;
94
95    fn c(open: f64, high: f64, low: f64, close: f64, ts: i64) -> Candle {
96        Candle::new(open, high, low, close, 1.0, ts).unwrap()
97    }
98
99    #[test]
100    fn accessors_and_metadata() {
101        let t = HighWave::new();
102        assert_eq!(t.name(), "HighWave");
103        assert_eq!(t.warmup_period(), 1);
104        assert!(!t.is_ready());
105    }
106
107    #[test]
108    fn high_wave_is_plus_one() {
109        let mut t = HighWave::new();
110        assert_eq!(t.update(c(10.0, 12.0, 8.0, 10.3, 0)), Some(1.0));
111    }
112
113    #[test]
114    fn short_upper_shadow_yields_zero() {
115        let mut t = HighWave::new();
116        // Long lower shadow but short upper -> not a high-wave.
117        assert_eq!(t.update(c(11.5, 12.0, 8.0, 11.7, 0)), Some(0.0));
118    }
119
120    #[test]
121    fn short_lower_shadow_yields_zero() {
122        let mut t = HighWave::new();
123        // Long upper shadow but short lower -> not a high-wave.
124        assert_eq!(t.update(c(8.3, 12.0, 8.0, 8.5, 0)), Some(0.0));
125    }
126
127    #[test]
128    fn big_body_yields_zero() {
129        let mut t = HighWave::new();
130        // A large body cannot leave both shadows long.
131        assert_eq!(t.update(c(8.5, 12.0, 8.0, 11.5, 0)), Some(0.0));
132    }
133
134    #[test]
135    fn zero_range_yields_zero() {
136        let mut t = HighWave::new();
137        assert_eq!(t.update(c(10.0, 10.0, 10.0, 10.0, 0)), Some(0.0));
138    }
139
140    #[test]
141    fn batch_equals_streaming() {
142        let candles: Vec<Candle> = (0..40)
143            .map(|i| {
144                let base = 100.0 + i as f64;
145                c(base, base + 3.0, base - 3.0, base + 0.2, i)
146            })
147            .collect();
148        let mut a = HighWave::new();
149        let mut b = HighWave::new();
150        assert_eq!(
151            a.batch(&candles),
152            candles.iter().map(|x| b.update(*x)).collect::<Vec<_>>()
153        );
154    }
155
156    #[test]
157    fn reset_clears_state() {
158        let mut t = HighWave::new();
159        t.update(c(10.0, 12.0, 8.0, 10.3, 0));
160        assert!(t.is_ready());
161        t.reset();
162        assert!(!t.is_ready());
163    }
164}