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    #[inline]
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        if (candle.close - candle.open).abs() > 0.1 * range {
66            return Some(0.0);
67        }
68        let upper = candle.high - candle.open.max(candle.close);
69        let lower = candle.open.min(candle.close) - candle.low;
70        let body_mid = f64::midpoint(candle.open, candle.close);
71        let pos = (body_mid - candle.low) / range;
72        if upper >= 0.3 * range && lower >= 0.3 * range && (0.4..=0.6).contains(&pos) {
73            return Some(1.0);
74        }
75        Some(0.0)
76    }
77
78    fn reset(&mut self) {
79        self.has_emitted = false;
80    }
81
82    #[inline]
83    fn warmup_period(&self) -> usize {
84        1
85    }
86
87    #[inline]
88    fn is_ready(&self) -> bool {
89        self.has_emitted
90    }
91
92    #[inline]
93    fn name(&self) -> &'static str {
94        "RickshawMan"
95    }
96}
97
98#[cfg(test)]
99mod tests {
100    use super::*;
101    use crate::traits::BatchExt;
102
103    fn c(open: f64, high: f64, low: f64, close: f64, ts: i64) -> Candle {
104        Candle::new(open, high, low, close, 1.0, ts).unwrap()
105    }
106
107    #[test]
108    fn accessors_and_metadata() {
109        let t = RickshawMan::new();
110        assert_eq!(t.name(), "RickshawMan");
111        assert_eq!(t.warmup_period(), 1);
112        assert!(!t.is_ready());
113    }
114
115    #[test]
116    fn rickshaw_is_plus_one() {
117        let mut t = RickshawMan::new();
118        assert_eq!(t.update(c(10.0, 12.0, 8.0, 10.0, 0)), Some(1.0));
119    }
120
121    #[test]
122    fn off_centre_body_yields_zero() {
123        let mut t = RickshawMan::new();
124        // Long-legged but the body sits near the top, not the middle.
125        assert_eq!(t.update(c(11.4, 12.0, 8.0, 11.45, 0)), Some(0.0));
126    }
127
128    #[test]
129    fn one_sided_shadow_yields_zero() {
130        let mut t = RickshawMan::new();
131        // Dragonfly shape: no upper shadow -> not a rickshaw man.
132        assert_eq!(t.update(c(10.0, 10.05, 6.0, 10.0, 0)), Some(0.0));
133    }
134
135    #[test]
136    fn non_doji_yields_zero() {
137        let mut t = RickshawMan::new();
138        assert_eq!(t.update(c(9.0, 12.0, 8.0, 11.0, 0)), Some(0.0));
139    }
140
141    #[test]
142    fn zero_range_yields_zero() {
143        let mut t = RickshawMan::new();
144        assert_eq!(t.update(c(10.0, 10.0, 10.0, 10.0, 0)), Some(0.0));
145    }
146
147    #[test]
148    fn batch_equals_streaming() {
149        let candles: Vec<Candle> = (0..40)
150            .map(|i| {
151                let base = 100.0 + i as f64;
152                c(base, base + 2.0, base - 2.0, base + 0.05, i)
153            })
154            .collect();
155        let mut a = RickshawMan::new();
156        let mut b = RickshawMan::new();
157        assert_eq!(
158            a.batch(&candles),
159            candles.iter().map(|x| b.update(*x)).collect::<Vec<_>>()
160        );
161    }
162
163    #[test]
164    fn reset_clears_state() {
165        let mut t = RickshawMan::new();
166        t.update(c(10.0, 12.0, 8.0, 10.0, 0));
167        assert!(t.is_ready());
168        t.reset();
169        assert!(!t.is_ready());
170    }
171}