Skip to main content

wickra_core/indicators/
starc_bands.rs

1//! STARC Bands (Stoller Average Range Channel).
2
3use crate::error::{Error, Result};
4use crate::indicators::atr::Atr;
5use crate::indicators::sma::Sma;
6use crate::ohlcv::Candle;
7use crate::traits::Indicator;
8
9/// STARC Bands output.
10#[derive(Debug, Clone, Copy, PartialEq)]
11pub struct StarcBandsOutput {
12    /// Upper band: `middle + multiplier · ATR`.
13    pub upper: f64,
14    /// Middle band: SMA of close.
15    pub middle: f64,
16    /// Lower band: `middle − multiplier · ATR`.
17    pub lower: f64,
18}
19
20/// STARC Bands (Stoller Average Range Channel): a close-SMA centerline with
21/// bands sized by ATR.
22///
23/// ```text
24/// middle = SMA(close, sma_period)
25/// upper  = middle + multiplier · ATR(atr_period)
26/// lower  = middle − multiplier · ATR(atr_period)
27/// ```
28///
29/// STARC and [`Keltner`](crate::Keltner) share the same skeleton — moving
30/// average plus an ATR offset — but Keltner's centerline is an `EMA` of the
31/// typical price while STARC uses an `SMA` of the close. The SMA gives a
32/// flatter, less reactive midline that traders use to pick the larger swing
33/// targets; Stoller's reference parameters are `SMA(6)` over the close with
34/// `ATR(15)` and a multiplier of `2.0`.
35///
36/// # Example
37///
38/// ```
39/// use wickra_core::{Candle, Indicator, StarcBands};
40///
41/// let mut indicator = StarcBands::new(6, 15, 2.0).unwrap();
42/// let mut last = None;
43/// for i in 0..40 {
44///     let base = 100.0 + f64::from(i);
45///     let candle =
46///         Candle::new(base, base + 2.0, base - 2.0, base + 1.0, 10.0, i64::from(i)).unwrap();
47///     last = indicator.update(candle);
48/// }
49/// assert!(last.is_some());
50/// ```
51#[derive(Debug, Clone)]
52pub struct StarcBands {
53    sma: Sma,
54    atr: Atr,
55    multiplier: f64,
56    sma_period: usize,
57    atr_period: usize,
58}
59
60impl StarcBands {
61    /// # Errors
62    /// Returns [`Error::PeriodZero`] / [`Error::NonPositiveMultiplier`] on
63    /// invalid inputs.
64    pub fn new(sma_period: usize, atr_period: usize, multiplier: f64) -> Result<Self> {
65        if !multiplier.is_finite() || multiplier <= 0.0 {
66            return Err(Error::NonPositiveMultiplier);
67        }
68        Ok(Self {
69            sma: Sma::new(sma_period)?,
70            atr: Atr::new(atr_period)?,
71            multiplier,
72            sma_period,
73            atr_period,
74        })
75    }
76
77    /// Stoller's classic configuration: SMA(6), ATR(15), multiplier 2.0.
78    pub fn classic() -> Self {
79        Self::new(6, 15, 2.0).expect("classic STARC parameters are valid")
80    }
81
82    /// Configured `(sma_period, atr_period, multiplier)`.
83    pub const fn parameters(&self) -> (usize, usize, f64) {
84        (self.sma_period, self.atr_period, self.multiplier)
85    }
86}
87
88impl Indicator for StarcBands {
89    type Input = Candle;
90    type Output = StarcBandsOutput;
91
92    fn update(&mut self, candle: Candle) -> Option<StarcBandsOutput> {
93        // Feed both unconditionally so SMA and ATR warm up in parallel.
94        let mid = self.sma.update(candle.close);
95        let atr = self.atr.update(candle);
96        let (mid, atr) = (mid?, atr?);
97        Some(StarcBandsOutput {
98            upper: mid + self.multiplier * atr,
99            middle: mid,
100            lower: mid - self.multiplier * atr,
101        })
102    }
103
104    fn reset(&mut self) {
105        self.sma.reset();
106        self.atr.reset();
107    }
108
109    fn warmup_period(&self) -> usize {
110        self.sma_period.max(self.atr_period)
111    }
112
113    fn is_ready(&self) -> bool {
114        self.sma.is_ready() && self.atr.is_ready()
115    }
116
117    fn name(&self) -> &'static str {
118        "StarcBands"
119    }
120}
121
122#[cfg(test)]
123mod tests {
124    use super::*;
125    use crate::traits::BatchExt;
126    use approx::assert_relative_eq;
127
128    fn c(h: f64, l: f64, cl: f64) -> Candle {
129        Candle::new(cl, h, l, cl, 1.0, 0).unwrap()
130    }
131
132    #[test]
133    fn rejects_invalid_input() {
134        assert!(StarcBands::new(0, 14, 2.0).is_err());
135        assert!(StarcBands::new(6, 0, 2.0).is_err());
136        assert!(StarcBands::new(6, 14, 0.0).is_err());
137        assert!(StarcBands::new(6, 14, -1.0).is_err());
138        assert!(StarcBands::new(6, 14, f64::NAN).is_err());
139    }
140
141    #[test]
142    fn accessors_and_metadata() {
143        let s = StarcBands::new(6, 15, 2.0).unwrap();
144        let (sp, ap, m) = s.parameters();
145        assert_eq!(sp, 6);
146        assert_eq!(ap, 15);
147        assert_relative_eq!(m, 2.0, epsilon = 1e-12);
148        assert_eq!(s.warmup_period(), 15);
149        assert_eq!(s.name(), "StarcBands");
150    }
151
152    #[test]
153    fn flat_market_collapses_bands() {
154        let candles: Vec<Candle> = (0..50).map(|_| c(10.0, 10.0, 10.0)).collect();
155        let mut s = StarcBands::new(6, 15, 2.0).unwrap();
156        let last = s.batch(&candles).into_iter().flatten().last().unwrap();
157        assert_relative_eq!(last.upper, last.middle, epsilon = 1e-9);
158        assert_relative_eq!(last.lower, last.middle, epsilon = 1e-9);
159    }
160
161    #[test]
162    fn upper_above_middle_above_lower() {
163        let candles: Vec<Candle> = (0..80)
164            .map(|i| {
165                let m = 100.0 + (f64::from(i) * 0.2).sin() * 5.0;
166                c(m + 1.0, m - 1.0, m)
167            })
168            .collect();
169        let mut s = StarcBands::classic();
170        for o in s.batch(&candles).into_iter().flatten() {
171            assert!(o.upper >= o.middle);
172            assert!(o.middle >= o.lower);
173        }
174    }
175
176    #[test]
177    fn batch_equals_streaming() {
178        let candles: Vec<Candle> = (0..40)
179            .map(|i| c(f64::from(i) + 1.0, f64::from(i) - 1.0, f64::from(i)))
180            .collect();
181        let mut a = StarcBands::classic();
182        let mut b = StarcBands::classic();
183        assert_eq!(
184            a.batch(&candles),
185            candles.iter().map(|x| b.update(*x)).collect::<Vec<_>>()
186        );
187    }
188
189    #[test]
190    fn reset_clears_state() {
191        let candles: Vec<Candle> = (0..30)
192            .map(|i| c(f64::from(i) + 1.0, f64::from(i) - 1.0, f64::from(i)))
193            .collect();
194        let mut s = StarcBands::classic();
195        s.batch(&candles);
196        assert!(s.is_ready());
197        s.reset();
198        assert!(!s.is_ready());
199        assert_eq!(s.update(candles[0]), None);
200    }
201
202    /// STARC must equal feeding independent SMA(close) and ATR siblings and
203    /// combining them.
204    #[test]
205    fn matches_independent_sma_and_atr() {
206        let candles: Vec<Candle> = (0..60)
207            .map(|i| {
208                let m = 100.0 + (f64::from(i) * 0.2).sin() * 5.0;
209                c(m + 1.5, m - 1.5, m)
210            })
211            .collect();
212        let mut s = StarcBands::new(6, 15, 2.0).unwrap();
213        let mut sma = Sma::new(6).unwrap();
214        let mut atr = Atr::new(15).unwrap();
215        for candle in &candles {
216            let got = s.update(*candle);
217            let mid = sma.update(candle.close);
218            let a = atr.update(*candle);
219            if let (Some(m), Some(av)) = (mid, a) {
220                let o = got.expect("STARC emits once both ready");
221                assert_relative_eq!(o.middle, m, epsilon = 1e-9);
222                assert_relative_eq!(o.upper, m + 2.0 * av, epsilon = 1e-9);
223                assert_relative_eq!(o.lower, m - 2.0 * av, epsilon = 1e-9);
224            } else {
225                assert!(got.is_none());
226            }
227        }
228    }
229}