Skip to main content

quantwave_core/indicators/patterns/
cdlshortline.rs

1//! Native O(1) streaming CDLSHORTLINE (matches `talib_rs::pattern::cdl_shortline`).
2//! Source: TA-Lib
3
4use crate::indicators::patterns::candle_settings::{
5    BODY_SHORT, CandleWindow, RollingCandleAvg, SHADOW_SHORT, candle_color, lower_shadow,
6    real_body, upper_shadow,
7};
8use crate::traits::Next;
9
10const LOOKBACK: usize = 10; // max(10, 10)
11
12#[derive(Debug, Clone)]
13#[allow(non_camel_case_types)]
14pub struct CDLSHORTLINE {
15    bars_seen: usize,
16    window: CandleWindow,
17    body_short: RollingCandleAvg,
18    shadow_short: RollingCandleAvg,
19}
20
21impl CDLSHORTLINE {
22    pub fn new() -> Self {
23        Self {
24            bars_seen: 0,
25            window: CandleWindow::new(1),
26            body_short: RollingCandleAvg::new(BODY_SHORT),
27            shadow_short: RollingCandleAvg::new(SHADOW_SHORT),
28        }
29    }
30}
31
32impl Default for CDLSHORTLINE {
33    fn default() -> Self {
34        Self::new()
35    }
36}
37
38impl Next<(f64, f64, f64, f64)> for CDLSHORTLINE {
39    type Output = f64;
40
41    fn next(&mut self, (open, high, low, close): (f64, f64, f64, f64)) -> Self::Output {
42        self.bars_seen += 1;
43        self.window.push(open, high, low, close);
44        self.body_short.push(open, high, low, close);
45        self.shadow_short.push(open, high, low, close);
46
47        if self.bars_seen <= LOOKBACK {
48            return 0.0;
49        }
50
51        let curr = self.window.bar(0);
52
53        let out = (real_body(curr.open, curr.close) < self.body_short.val(0)
54            && upper_shadow(curr.open, curr.high, curr.close) < self.shadow_short.val(0)
55            && lower_shadow(curr.open, curr.low, curr.close) < self.shadow_short.val(0))
56            as i32
57            * candle_color(curr.open, curr.close)
58            * 100;
59
60        out as f64
61    }
62}
63
64#[cfg(test)]
65mod tests {
66    use super::*;
67    crate::test_pattern_parity!(
68        test_cdlshortline_parity,
69        CDLSHORTLINE,
70        talib_rs::pattern::cdl_shortline
71    );
72}