Skip to main content

wickra_core/indicators/
hammer.rs

1//! Hammer candlestick pattern.
2
3use crate::ohlcv::Candle;
4use crate::traits::Indicator;
5
6/// Hammer — a single-bar bullish reversal candidate.
7///
8/// A Hammer has a small real body sitting near the top of the bar, a long
9/// lower shadow at least twice the body, and a short or absent upper shadow.
10/// It is traditionally read as a rejection of lower prices.
11///
12/// ```text
13/// body         = |close − open|
14/// upper_shadow = high − max(open, close)
15/// lower_shadow = min(open, close) − low
16/// hammer       = 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/// # Signed ±1 encoding
26///
27/// A Hammer is bullish by definition, so under the uniform candlestick sign
28/// convention (`+1.0` bullish, `−1.0` bearish, `0.0` none) it emits `+1.0`
29/// when the shape matches and `0.0` otherwise — it never emits `−1.0`. The
30/// same geometry read at the top of an uptrend is the bearish `HangingMan`,
31/// which carries the opposite sign.
32///
33/// # Example
34///
35/// ```
36/// use wickra_core::{Candle, Hammer, Indicator};
37///
38/// let mut indicator = Hammer::new();
39/// // Open 10, close 10.5, low 5, high 10.6: long lower shadow, tiny upper.
40/// let candle = Candle::new(10.0, 10.6, 5.0, 10.5, 1.0, 0).unwrap();
41/// assert_eq!(indicator.update(candle), Some(1.0));
42/// ```
43#[derive(Debug, Clone, Default)]
44pub struct Hammer {
45    has_emitted: bool,
46}
47
48impl Hammer {
49    /// Construct a new Hammer detector.
50    pub const fn new() -> Self {
51        Self { has_emitted: false }
52    }
53}
54
55impl Indicator for Hammer {
56    type Input = Candle;
57    type Output = f64;
58
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 lower >= 2.0 * body && upper <= 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    fn warmup_period(&self) -> usize {
83        1
84    }
85
86    fn is_ready(&self) -> bool {
87        self.has_emitted
88    }
89
90    fn name(&self) -> &'static str {
91        "Hammer"
92    }
93}
94
95#[cfg(test)]
96mod tests {
97    use super::*;
98    use crate::traits::BatchExt;
99
100    fn c(open: f64, high: f64, low: f64, close: f64, ts: i64) -> Candle {
101        Candle::new(open, high, low, close, 1.0, ts).unwrap()
102    }
103
104    #[test]
105    fn accessors_and_metadata() {
106        let h = Hammer::new();
107        assert_eq!(h.name(), "Hammer");
108        assert_eq!(h.warmup_period(), 1);
109        assert!(!h.is_ready());
110    }
111
112    #[test]
113    fn clean_hammer_is_one() {
114        let mut h = Hammer::new();
115        // body 0.5 (10 -> 10.5), lower shadow 5.0, upper shadow 0.1.
116        assert_eq!(h.update(c(10.0, 10.6, 5.0, 10.5, 0)), Some(1.0));
117    }
118
119    #[test]
120    fn marubozu_is_not_hammer() {
121        let mut h = Hammer::new();
122        assert_eq!(h.update(c(10.0, 12.0, 10.0, 12.0, 0)), Some(0.0));
123    }
124
125    #[test]
126    fn shooting_star_shape_is_not_hammer() {
127        // Long upper, short lower -> not a hammer.
128        let mut h = Hammer::new();
129        assert_eq!(h.update(c(10.5, 15.0, 10.0, 10.0, 0)), Some(0.0));
130    }
131
132    #[test]
133    fn doji_is_not_hammer() {
134        let mut h = Hammer::new();
135        assert_eq!(h.update(c(10.0, 11.0, 9.0, 10.0, 0)), Some(0.0));
136    }
137
138    #[test]
139    fn zero_range_yields_zero() {
140        let mut h = Hammer::new();
141        assert_eq!(h.update(c(10.0, 10.0, 10.0, 10.0, 0)), Some(0.0));
142    }
143
144    #[test]
145    fn batch_equals_streaming() {
146        let candles: Vec<Candle> = (0..40)
147            .map(|i| {
148                let base = 100.0 + i as f64;
149                c(base, base + 2.0, base - 4.0, base + 0.5, i)
150            })
151            .collect();
152        let mut a = Hammer::new();
153        let mut b = Hammer::new();
154        assert_eq!(
155            a.batch(&candles),
156            candles.iter().map(|x| b.update(*x)).collect::<Vec<_>>()
157        );
158    }
159
160    #[test]
161    fn reset_clears_state() {
162        let mut h = Hammer::new();
163        h.update(c(10.0, 10.6, 5.0, 10.5, 0));
164        assert!(h.is_ready());
165        h.reset();
166        assert!(!h.is_ready());
167    }
168}