Skip to main content

wickra_core/indicators/
rickshaw_man.rs

1//! Rickshaw Man candlestick pattern.
2
3use crate::ohlcv::Candle;
4use crate::traits::Indicator;
5
6/// Rickshaw Man — a single-bar indecision signal. A long-legged doji whose tiny
7/// body sits near the *middle* of a wide range, the most balanced form of
8/// indecision: neither side controlled the close and the midpoint pins it.
9///
10/// ```text
11/// range = high − low
12/// doji        = |close − open| <= 0.1 * range
13/// long upper  = high − max(open, close) >= 0.3 * range
14/// long lower  = min(open, close) − low  >= 0.3 * range
15/// centred body = body midpoint within the central 40–60 % of the range
16/// ```
17///
18/// Output is `+1.0` when the rickshaw man prints and `0.0` otherwise. This is a
19/// non-directional indecision flag — it never emits `−1.0`. A rickshaw man is a
20/// special case of a long-legged doji (the body additionally sits at the centre),
21/// so both detectors may flag the same bar. Body and shadow thresholds follow the
22/// geometric house style (fixed fractions of the bar range) rather than TA-Lib's
23/// rolling averages. Pattern-shape check only — no trend filter is applied;
24/// combine with a trend indicator for actionable signals.
25///
26/// # Signed ±1 encoding
27///
28/// This detector emits the uniform candlestick sign convention shared across the
29/// pattern family — `+1.0` detected, `0.0` no pattern — so it drops straight into
30/// a machine-learning feature matrix as a single dimension.
31///
32/// # Example
33///
34/// ```
35/// use wickra_core::{Candle, Indicator, RickshawMan};
36///
37/// let mut indicator = RickshawMan::new();
38/// // Tiny body centred in a wide range, long shadows both sides.
39/// let candle = Candle::new(10.0, 12.0, 8.0, 10.0, 1.0, 0).unwrap();
40/// assert_eq!(indicator.update(candle), Some(1.0));
41/// ```
42#[derive(Debug, Clone, Default)]
43pub struct RickshawMan {
44    has_emitted: bool,
45}
46
47impl RickshawMan {
48    /// Construct a new Rickshaw Man detector.
49    pub const fn new() -> Self {
50        Self { has_emitted: false }
51    }
52}
53
54impl Indicator for RickshawMan {
55    type Input = Candle;
56    type Output = f64;
57
58    fn update(&mut self, candle: Candle) -> Option<f64> {
59        self.has_emitted = true;
60        let range = candle.high - candle.low;
61        if range <= 0.0 {
62            return Some(0.0);
63        }
64        if (candle.close - candle.open).abs() > 0.1 * range {
65            return Some(0.0);
66        }
67        let upper = candle.high - candle.open.max(candle.close);
68        let lower = candle.open.min(candle.close) - candle.low;
69        let body_mid = 0.5 * (candle.open + candle.close);
70        let pos = (body_mid - candle.low) / range;
71        if upper >= 0.3 * range && lower >= 0.3 * range && (0.4..=0.6).contains(&pos) {
72            return Some(1.0);
73        }
74        Some(0.0)
75    }
76
77    fn reset(&mut self) {
78        self.has_emitted = false;
79    }
80
81    fn warmup_period(&self) -> usize {
82        1
83    }
84
85    fn is_ready(&self) -> bool {
86        self.has_emitted
87    }
88
89    fn name(&self) -> &'static str {
90        "RickshawMan"
91    }
92}
93
94#[cfg(test)]
95mod tests {
96    use super::*;
97    use crate::traits::BatchExt;
98
99    fn c(open: f64, high: f64, low: f64, close: f64, ts: i64) -> Candle {
100        Candle::new(open, high, low, close, 1.0, ts).unwrap()
101    }
102
103    #[test]
104    fn accessors_and_metadata() {
105        let t = RickshawMan::new();
106        assert_eq!(t.name(), "RickshawMan");
107        assert_eq!(t.warmup_period(), 1);
108        assert!(!t.is_ready());
109    }
110
111    #[test]
112    fn rickshaw_is_plus_one() {
113        let mut t = RickshawMan::new();
114        assert_eq!(t.update(c(10.0, 12.0, 8.0, 10.0, 0)), Some(1.0));
115    }
116
117    #[test]
118    fn off_centre_body_yields_zero() {
119        let mut t = RickshawMan::new();
120        // Long-legged but the body sits near the top, not the middle.
121        assert_eq!(t.update(c(11.4, 12.0, 8.0, 11.45, 0)), Some(0.0));
122    }
123
124    #[test]
125    fn one_sided_shadow_yields_zero() {
126        let mut t = RickshawMan::new();
127        // Dragonfly shape: no upper shadow -> not a rickshaw man.
128        assert_eq!(t.update(c(10.0, 10.05, 6.0, 10.0, 0)), Some(0.0));
129    }
130
131    #[test]
132    fn non_doji_yields_zero() {
133        let mut t = RickshawMan::new();
134        assert_eq!(t.update(c(9.0, 12.0, 8.0, 11.0, 0)), Some(0.0));
135    }
136
137    #[test]
138    fn zero_range_yields_zero() {
139        let mut t = RickshawMan::new();
140        assert_eq!(t.update(c(10.0, 10.0, 10.0, 10.0, 0)), Some(0.0));
141    }
142
143    #[test]
144    fn batch_equals_streaming() {
145        let candles: Vec<Candle> = (0..40)
146            .map(|i| {
147                let base = 100.0 + i as f64;
148                c(base, base + 2.0, base - 2.0, base + 0.05, i)
149            })
150            .collect();
151        let mut a = RickshawMan::new();
152        let mut b = RickshawMan::new();
153        assert_eq!(
154            a.batch(&candles),
155            candles.iter().map(|x| b.update(*x)).collect::<Vec<_>>()
156        );
157    }
158
159    #[test]
160    fn reset_clears_state() {
161        let mut t = RickshawMan::new();
162        t.update(c(10.0, 12.0, 8.0, 10.0, 0));
163        assert!(t.is_ready());
164        t.reset();
165        assert!(!t.is_ready());
166    }
167}