Skip to main content

fin_primitives/signals/indicators/
support_test_count.rs

1//! Support Test Count indicator.
2
3use rust_decimal::Decimal;
4use std::collections::VecDeque;
5use crate::error::FinError;
6use crate::signals::{BarInput, Signal, SignalValue};
7
8/// Rolling count of bars whose low is within 0.5% of the period's lowest low.
9///
10/// Measures how many times price has tested the support level in the recent window.
11/// Higher counts suggest a stronger, well-tested support zone.
12pub struct SupportTestCount {
13    period: usize,
14    lows: VecDeque<Decimal>,
15    threshold_pct: Decimal,
16}
17
18impl SupportTestCount {
19    /// Creates a new `SupportTestCount` with the given rolling period and threshold percentage.
20    pub fn new(period: usize, threshold_pct: Decimal) -> Result<Self, FinError> {
21        if period == 0 {
22            return Err(FinError::InvalidPeriod(period));
23        }
24        Ok(Self { period, lows: VecDeque::with_capacity(period), threshold_pct })
25    }
26}
27
28impl Signal for SupportTestCount {
29    fn update(&mut self, bar: &BarInput) -> Result<SignalValue, FinError> {
30        self.lows.push_back(bar.low);
31        if self.lows.len() > self.period {
32            self.lows.pop_front();
33        }
34        if self.lows.len() < self.period {
35            return Ok(SignalValue::Unavailable);
36        }
37
38        let period_low = self.lows.iter().copied().fold(Decimal::MAX, Decimal::min);
39        let threshold = period_low * self.threshold_pct / Decimal::ONE_HUNDRED;
40        let count = self.lows.iter()
41            .filter(|&&l| (l - period_low).abs() <= threshold)
42            .count();
43        Ok(SignalValue::Scalar(Decimal::from(count as u32)))
44    }
45
46    fn is_ready(&self) -> bool { self.lows.len() >= self.period }
47    fn period(&self) -> usize { self.period }
48    fn reset(&mut self) { self.lows.clear(); }
49    fn name(&self) -> &str { "SupportTestCount" }
50}
51
52#[cfg(test)]
53mod tests {
54    use super::*;
55    use rust_decimal_macros::dec;
56
57    fn bar(l: &str) -> BarInput {
58        BarInput {
59            open: dec!(100),
60            high: dec!(110),
61            low: l.parse().unwrap(),
62            close: dec!(100),
63            volume: dec!(1000),
64        }
65    }
66
67    #[test]
68    fn test_support_test_count_all_at_support() {
69        let mut sig = SupportTestCount::new(3, dec!(0.5)).unwrap();
70        sig.update(&bar("90")).unwrap();
71        sig.update(&bar("90")).unwrap();
72        let v = sig.update(&bar("90")).unwrap();
73        // All 3 at same low => all 3 tests
74        assert_eq!(v, SignalValue::Scalar(dec!(3)));
75    }
76
77    #[test]
78    fn test_support_test_count_one_test() {
79        let mut sig = SupportTestCount::new(3, dec!(0.5)).unwrap();
80        sig.update(&bar("90")).unwrap();
81        sig.update(&bar("100")).unwrap();
82        let v = sig.update(&bar("110")).unwrap();
83        // Only bar at 90 is the period low, others are far above
84        assert_eq!(v, SignalValue::Scalar(dec!(1)));
85    }
86}