Skip to main content

wickra_core/indicators/
hanging_man.rs

1//! Hanging Man candlestick pattern.
2
3use crate::ohlcv::Candle;
4use crate::traits::Indicator;
5
6/// Hanging Man — a single-bar bearish reversal candidate.
7///
8/// A Hanging Man has the same geometry as a Hammer (small body near the top,
9/// long lower shadow ≥ 2× body, short upper shadow) but is read bearishly
10/// 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/// hanging      = lower_shadow >= 2 * body
17///               && upper_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/// # Example
26///
27/// ```
28/// use wickra_core::{Candle, HangingMan, Indicator};
29///
30/// let mut indicator = HangingMan::new();
31/// let candle = Candle::new(10.0, 10.6, 5.0, 10.5, 1.0, 0).unwrap();
32/// assert_eq!(indicator.update(candle), Some(-1.0));
33/// ```
34#[derive(Debug, Clone, Default)]
35pub struct HangingMan {
36    has_emitted: bool,
37}
38
39impl HangingMan {
40    /// Construct a new Hanging Man detector.
41    pub const fn new() -> Self {
42        Self { has_emitted: false }
43    }
44}
45
46impl Indicator for HangingMan {
47    type Input = Candle;
48    type Output = f64;
49
50    fn update(&mut self, candle: Candle) -> Option<f64> {
51        self.has_emitted = true;
52        let range = candle.high - candle.low;
53        if range <= 0.0 {
54            return Some(0.0);
55        }
56        let body = (candle.close - candle.open).abs();
57        if body <= 0.0 {
58            return Some(0.0);
59        }
60        let upper = candle.high - candle.open.max(candle.close);
61        let lower = candle.open.min(candle.close) - candle.low;
62        Some(if lower >= 2.0 * body && upper <= body {
63            -1.0
64        } else {
65            0.0
66        })
67    }
68
69    fn reset(&mut self) {
70        self.has_emitted = false;
71    }
72
73    fn warmup_period(&self) -> usize {
74        1
75    }
76
77    fn is_ready(&self) -> bool {
78        self.has_emitted
79    }
80
81    fn name(&self) -> &'static str {
82        "HangingMan"
83    }
84}
85
86#[cfg(test)]
87mod tests {
88    use super::*;
89    use crate::traits::BatchExt;
90
91    fn c(open: f64, high: f64, low: f64, close: f64, ts: i64) -> Candle {
92        Candle::new(open, high, low, close, 1.0, ts).unwrap()
93    }
94
95    #[test]
96    fn accessors_and_metadata() {
97        let h = HangingMan::new();
98        assert_eq!(h.name(), "HangingMan");
99        assert_eq!(h.warmup_period(), 1);
100        assert!(!h.is_ready());
101    }
102
103    #[test]
104    fn clean_hanging_man_is_minus_one() {
105        let mut h = HangingMan::new();
106        assert_eq!(h.update(c(10.0, 10.6, 5.0, 10.5, 0)), Some(-1.0));
107    }
108
109    #[test]
110    fn marubozu_is_not_hanging_man() {
111        let mut h = HangingMan::new();
112        assert_eq!(h.update(c(10.0, 12.0, 10.0, 12.0, 0)), Some(0.0));
113    }
114
115    #[test]
116    fn doji_is_not_hanging_man() {
117        let mut h = HangingMan::new();
118        assert_eq!(h.update(c(10.0, 11.0, 9.0, 10.0, 0)), Some(0.0));
119    }
120
121    #[test]
122    fn zero_range_yields_zero() {
123        let mut h = HangingMan::new();
124        assert_eq!(h.update(c(10.0, 10.0, 10.0, 10.0, 0)), Some(0.0));
125    }
126
127    #[test]
128    fn batch_equals_streaming() {
129        let candles: Vec<Candle> = (0..40)
130            .map(|i| {
131                let base = 100.0 + i as f64;
132                c(base, base + 2.0, base - 4.0, base + 0.5, i)
133            })
134            .collect();
135        let mut a = HangingMan::new();
136        let mut b = HangingMan::new();
137        assert_eq!(
138            a.batch(&candles),
139            candles.iter().map(|x| b.update(*x)).collect::<Vec<_>>()
140        );
141    }
142
143    #[test]
144    fn reset_clears_state() {
145        let mut h = HangingMan::new();
146        h.update(c(10.0, 10.6, 5.0, 10.5, 0));
147        assert!(h.is_ready());
148        h.reset();
149        assert!(!h.is_ready());
150    }
151}