1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
//! Commodity Channel Index (CCI) Indicator
//!
//! CCI measures the current price level relative to an avg price level over
//! a given period. It oscillates above and below zero, helping identify cyclical
//! trends in a commodity or stock.
//!
//! # Interpretation
//! - Readings above +100: Overbought, potential reversal
//! - Readings below -100: Oversold, potential reversal
//! - Zero crossings can signal trend changes
//!
//! # Calculation
//! Typical Price (TP) = (High + Low + Close) / 3
//! SMA = Simple Moving Avg of TP
//! Mean Deviation = Avg of |TP - SMA|
//! CCI = (TP - SMA) / (0.015 * Mean Deviation)
//!
//! # Example
//! ```ignore
//! use egui_charts::CCI;
//!
//! let mut cci = CCI::new(20);
//! cci.calculate(&bars);
//! ```
use crate::model::Bar;
use crate::studies::{Indicator, IndicatorValue};
use crate::tokens::DESIGN_TOKENS;
use egui::Color32;
/// Commodity Channel Index indicator
#[derive(Clone)]
pub struct CCI {
period: usize,
values: Vec<IndicatorValue>,
color: Color32,
visible: bool,
}
impl CCI {
/// Create a new CCI indicator
///
/// # Arguments
/// * `period` - Lookback period (typically 20)
pub fn new(period: usize) -> Self {
Self {
period: period.max(1),
values: Vec::new(),
color: DESIGN_TOKENS.semantic.extended.cyan, // Cyan
visible: true,
}
}
/// Set the indicator color
pub fn with_color(mut self, color: Color32) -> Self {
self.color = color;
self
}
/// Calculate typical price (TP) for a bar
#[inline]
fn typical_price(bar: &Bar) -> f64 {
(bar.high + bar.low + bar.close) / 3.0
}
}
/// Construct with the conventional default parameters.
impl Default for CCI {
fn default() -> Self {
Self::new(20)
}
}
impl Indicator for CCI {
fn name(&self) -> &str {
"CCI"
}
fn desc(&self) -> &str {
"Commodity Channel Index - Cyclical trend indicator"
}
fn calculate(&mut self, data: &[Bar]) {
self.values.clear();
if data.len() < self.period {
for _ in 0..data.len() {
self.values.push(IndicatorValue::None);
}
return;
}
// Calculate typical prices
let tp: Vec<f64> = data.iter().map(Self::typical_price).collect();
for i in 0..data.len() {
if i + 1 < self.period {
self.values.push(IndicatorValue::None);
} else {
// Calculate SMA of typical price
let start = i + 1 - self.period;
let sma: f64 = tp[start..=i].iter().sum::<f64>() / self.period as f64;
// Calculate mean deviation
let mean_dev: f64 =
tp[start..=i].iter().map(|p| (p - sma).abs()).sum::<f64>() / self.period as f64;
// Calculate CCI
if mean_dev.abs() < 1e-10 {
self.values.push(IndicatorValue::Single(0.0));
} else {
// Lambert's constant 0.015 ensures ~70-80% of values fall between -100 and +100
let cci = (tp[i] - sma) / (0.015 * mean_dev);
self.values.push(IndicatorValue::Single(cci));
}
}
}
}
fn values(&self) -> &[IndicatorValue] {
&self.values
}
fn colors(&self) -> Vec<Color32> {
vec![self.color]
}
fn set_colors(&mut self, colors: Vec<Color32>) {
if !colors.is_empty() {
self.color = colors[0];
}
}
fn is_overlay(&self) -> bool {
false // CCI is drawn in separate pane
}
fn is_visible(&self) -> bool {
self.visible
}
fn set_visible(&mut self, visible: bool) {
self.visible = visible;
}
fn clone_box(&self) -> Box<dyn Indicator> {
Box::new(self.clone())
}
fn line_names(&self) -> Vec<String> {
vec![format!("CCI({})", self.period)]
}
}
#[cfg(test)]
mod tests {
use super::*;
use chrono::{Duration, Utc};
fn create_test_bars() -> Vec<Bar> {
let start = Utc::now();
(0..50)
.map(|i| {
let price = 100.0 + (i as f64 * 0.2).sin() * 10.0;
Bar {
time: start + Duration::minutes(i * 5),
open: price,
high: price + 2.0,
low: price - 2.0,
close: price + 0.5,
volume: 1000.0,
}
})
.collect()
}
#[test]
fn test_cci_calculation() {
let bars = create_test_bars();
let mut cci = CCI::new(20);
cci.calculate(&bars);
assert_eq!(cci.values().len(), bars.len());
// First period-1 values should be None
for i in 0..19 {
assert!(matches!(cci.values()[i], IndicatorValue::None));
}
// Remaining values should be valid
for value in cci.values().iter().skip(19) {
assert!(matches!(value, IndicatorValue::Single(_)));
}
}
#[test]
fn test_cci_typical_price() {
let bar = Bar {
time: Utc::now(),
open: 100.0,
high: 110.0,
low: 90.0,
close: 105.0,
volume: 1000.0,
};
let tp = CCI::typical_price(&bar);
// (110 + 90 + 105) / 3 = 101.67
assert!((tp - 101.666666).abs() < 0.001);
}
#[test]
fn test_cci_flat_prices() {
// With flat prices, CCI should be around 0
let start = Utc::now();
let bars: Vec<Bar> = (0..30)
.map(|i| Bar {
time: start + Duration::minutes(i * 5),
open: 100.0,
high: 100.0,
low: 100.0,
close: 100.0,
volume: 1000.0,
})
.collect();
let mut cci = CCI::new(20);
cci.calculate(&bars);
for value in cci.values().iter().skip(19) {
if let IndicatorValue::Single(v) = value {
assert!(v.abs() < 0.001, "CCI should be ~0 for flat prices");
}
}
}
#[test]
fn test_cci_empty_data() {
let mut cci = CCI::new(20);
cci.calculate(&[]);
assert!(cci.values().is_empty());
}
}