wickra_core/indicators/
cci.rs1use std::collections::VecDeque;
4
5use crate::error::{Error, Result};
6use crate::ohlcv::Candle;
7use crate::traits::Indicator;
8
9#[derive(Debug, Clone)]
30pub struct Cci {
31 period: usize,
32 factor: f64,
33 window: VecDeque<f64>,
34 sum: f64,
35}
36
37impl Cci {
38 pub fn new(period: usize) -> Result<Self> {
43 Self::with_factor(period, 0.015)
44 }
45
46 pub fn with_factor(period: usize, factor: f64) -> Result<Self> {
53 if period == 0 {
54 return Err(Error::PeriodZero);
55 }
56 if !factor.is_finite() || factor <= 0.0 {
57 return Err(Error::NonPositiveMultiplier);
58 }
59 Ok(Self {
60 period,
61 factor,
62 window: VecDeque::with_capacity(period),
63 sum: 0.0,
64 })
65 }
66
67 pub const fn period(&self) -> usize {
69 self.period
70 }
71}
72
73impl Indicator for Cci {
74 type Input = Candle;
75 type Output = f64;
76
77 fn update(&mut self, candle: Candle) -> Option<f64> {
78 let tp = candle.typical_price();
79 if self.window.len() == self.period {
80 let old = self.window.pop_front().expect("non-empty");
81 self.sum -= old;
82 }
83 self.window.push_back(tp);
84 self.sum += tp;
85 if self.window.len() < self.period {
86 return None;
87 }
88 let n = self.period as f64;
89 let mean = self.sum / n;
90 let mad: f64 = self.window.iter().map(|v| (v - mean).abs()).sum::<f64>() / n;
91 if mad == 0.0 {
92 return Some(0.0);
93 }
94 Some((tp - mean) / (self.factor * mad))
95 }
96
97 fn reset(&mut self) {
98 self.window.clear();
99 self.sum = 0.0;
100 }
101
102 fn warmup_period(&self) -> usize {
103 self.period
104 }
105
106 fn is_ready(&self) -> bool {
107 self.window.len() == self.period
108 }
109
110 fn name(&self) -> &'static str {
111 "CCI"
112 }
113}
114
115#[cfg(test)]
116mod tests {
117 use super::*;
118 use crate::traits::BatchExt;
119 use approx::assert_relative_eq;
120
121 fn c(h: f64, l: f64, cl: f64) -> Candle {
122 Candle::new(cl, h, l, cl, 1.0, 0).unwrap()
123 }
124
125 #[test]
126 fn flat_candles_yield_zero() {
127 let candles: Vec<Candle> = (0..30).map(|_| c(10.0, 10.0, 10.0)).collect();
128 let mut cci = Cci::new(20).unwrap();
129 for v in cci.batch(&candles).into_iter().flatten() {
130 assert_relative_eq!(v, 0.0, epsilon = 1e-12);
131 }
132 }
133
134 #[test]
135 fn rejects_invalid_input() {
136 assert!(Cci::new(0).is_err());
137 assert!(Cci::with_factor(20, 0.0).is_err());
138 assert!(Cci::with_factor(20, -1.0).is_err());
139 }
140
141 #[test]
145 fn accessors_and_metadata() {
146 let cci = Cci::new(20).unwrap();
147 assert_eq!(cci.period(), 20);
148 assert_eq!(cci.warmup_period(), 20);
149 assert_eq!(cci.name(), "CCI");
150 }
151
152 #[test]
153 fn batch_equals_streaming() {
154 let candles: Vec<Candle> = (0..60)
155 .map(|i| {
156 let m = 50.0 + (f64::from(i) * 0.2).sin() * 10.0;
157 c(m + 1.0, m - 1.0, m)
158 })
159 .collect();
160 let mut a = Cci::new(20).unwrap();
161 let mut b = Cci::new(20).unwrap();
162 assert_eq!(
163 a.batch(&candles),
164 candles.iter().map(|x| b.update(*x)).collect::<Vec<_>>()
165 );
166 }
167
168 #[test]
169 fn reset_clears_state() {
170 let candles: Vec<Candle> = (0..30).map(|_| c(10.0, 10.0, 10.0)).collect();
171 let mut cci = Cci::new(20).unwrap();
172 cci.batch(&candles);
173 assert!(cci.is_ready());
174 cci.reset();
175 assert!(!cci.is_ready());
176 }
177}