Skip to main content

nautilus_indicators/momentum/
cci.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2026 Nautech Systems Pty Ltd. All rights reserved.
3//  https://nautechsystems.io
4//
5//  Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
6//  You may not use this file except in compliance with the License.
7//  You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
8//
9//  Unless required by applicable law or agreed to in writing, software
10//  distributed under the License is distributed on an "AS IS" BASIS,
11//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12//  See the License for the specific language governing permissions and
13//  limitations under the License.
14// -------------------------------------------------------------------------------------------------
15
16use std::fmt::{Debug, Display};
17
18use arraydeque::{ArrayDeque, Wrapping};
19use nautilus_model::data::Bar;
20
21use crate::{
22    average::{MovingAverageFactory, MovingAverageType},
23    indicator::{Indicator, MovingAverage},
24};
25
26const MAX_PERIOD: usize = 1024;
27
28#[repr(C)]
29#[derive(Debug)]
30#[cfg_attr(
31    feature = "python",
32    pyo3::pyclass(module = "nautilus_trader.core.nautilus_pyo3.indicators", unsendable)
33)]
34#[cfg_attr(
35    feature = "python",
36    pyo3_stub_gen::derive::gen_stub_pyclass(module = "nautilus_trader.indicators")
37)]
38pub struct CommodityChannelIndex {
39    pub period: usize,
40    pub ma_type: MovingAverageType,
41    pub scalar: f64,
42    pub value: f64,
43    pub initialized: bool,
44    ma: Box<dyn MovingAverage + Send + 'static>,
45    has_inputs: bool,
46    mad: f64,
47    prices: ArrayDeque<f64, MAX_PERIOD, Wrapping>,
48}
49
50impl Display for CommodityChannelIndex {
51    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
52        write!(f, "{}({},{})", self.name(), self.period, self.ma_type,)
53    }
54}
55
56impl Indicator for CommodityChannelIndex {
57    fn name(&self) -> String {
58        stringify!(CommodityChannelIndex).to_string()
59    }
60
61    fn has_inputs(&self) -> bool {
62        self.has_inputs
63    }
64
65    fn initialized(&self) -> bool {
66        self.initialized
67    }
68
69    fn handle_bar(&mut self, bar: &Bar) {
70        self.update_raw((&bar.high).into(), (&bar.low).into(), (&bar.close).into());
71    }
72
73    fn reset(&mut self) {
74        self.ma.reset();
75        self.mad = 0.0;
76        self.prices.clear();
77        self.value = 0.0;
78        self.has_inputs = false;
79        self.initialized = false;
80    }
81}
82
83impl CommodityChannelIndex {
84    /// Creates a new [`CommodityChannelIndex`] instance.
85    ///
86    /// # Panics
87    ///
88    /// - If `period` is less than or equal to 0.
89    /// - If `period` exceeds `MAX_PERIOD`.
90    #[must_use]
91    pub fn new(period: usize, scalar: f64, ma_type: Option<MovingAverageType>) -> Self {
92        assert!(period > 0, "CommodityChannelIndex: period must be > 0");
93        assert!(
94            period <= MAX_PERIOD,
95            "CommodityChannelIndex: period exceeds MAX_PERIOD"
96        );
97
98        Self {
99            period,
100            scalar,
101            ma_type: ma_type.unwrap_or(MovingAverageType::Simple),
102            value: 0.0,
103            prices: ArrayDeque::new(),
104            ma: MovingAverageFactory::create(ma_type.unwrap_or(MovingAverageType::Simple), period),
105            has_inputs: false,
106            initialized: false,
107            mad: 0.0,
108        }
109    }
110
111    pub fn update_raw(&mut self, high: f64, low: f64, close: f64) {
112        let typical_price = (high + low + close) / 3.0;
113
114        if self.prices.len() == self.period {
115            let _ = self.prices.pop_front();
116        }
117        let _ = self.prices.push_back(typical_price);
118
119        self.ma.update_raw(typical_price);
120
121        self.mad = fast_mad_with_mean(self.prices.iter().copied(), self.ma.value());
122
123        if self.ma.initialized() && self.mad != 0.0 {
124            self.value = (typical_price - self.ma.value()) / (self.scalar * self.mad);
125        }
126
127        if !self.initialized {
128            self.has_inputs = true;
129
130            if self.ma.initialized() {
131                self.initialized = true;
132            }
133        }
134    }
135}
136
137pub fn fast_mad_with_mean<I>(values: I, mean: f64) -> f64
138where
139    I: IntoIterator<Item = f64>,
140{
141    let mut acc = 0.0_f64;
142    let mut count = 0_usize;
143
144    for v in values {
145        acc += (v - mean).abs();
146        count += 1;
147    }
148
149    if count == 0 { 0.0 } else { acc / count as f64 }
150}
151
152#[cfg(test)]
153mod tests {
154    use nautilus_model::data::Bar;
155    use rstest::rstest;
156
157    use crate::{
158        indicator::Indicator,
159        momentum::cci::CommodityChannelIndex,
160        stubs::{bar_ethusdt_binance_minute_bid, cci_10},
161    };
162
163    #[rstest]
164    fn test_psl_initialized(cci_10: CommodityChannelIndex) {
165        let display_str = format!("{cci_10}");
166        assert_eq!(display_str, "CommodityChannelIndex(10,SIMPLE)");
167        assert_eq!(cci_10.period, 10);
168        assert!(!cci_10.initialized);
169        assert!(!cci_10.has_inputs);
170    }
171
172    #[rstest]
173    fn test_value_with_one_input(mut cci_10: CommodityChannelIndex) {
174        cci_10.update_raw(1.0, 0.9, 0.95);
175        assert_eq!(cci_10.value, 0.0);
176    }
177
178    #[rstest]
179    fn test_value_with_three_inputs(mut cci_10: CommodityChannelIndex) {
180        cci_10.update_raw(1.0, 0.9, 0.95);
181        cci_10.update_raw(2.0, 1.9, 1.95);
182        cci_10.update_raw(3.0, 2.9, 2.95);
183        assert_eq!(cci_10.value, 0.0);
184    }
185
186    #[rstest]
187    fn test_value_with_ten_inputs(mut cci_10: CommodityChannelIndex) {
188        cci_10.update_raw(1.00000, 0.90000, 1.00000);
189        cci_10.update_raw(1.00010, 0.90010, 1.00010);
190        cci_10.update_raw(1.00030, 0.90020, 1.00020);
191        cci_10.update_raw(1.00040, 0.90030, 1.00030);
192        cci_10.update_raw(1.00050, 0.90040, 1.00040);
193        cci_10.update_raw(1.00060, 0.90050, 1.00050);
194        cci_10.update_raw(1.00050, 0.90040, 1.00040);
195        cci_10.update_raw(1.00040, 0.90030, 1.00030);
196        cci_10.update_raw(1.00030, 0.90020, 1.00020);
197        cci_10.update_raw(1.00010, 0.90010, 1.00010);
198        cci_10.update_raw(1.00000, 0.90000, 1.00000);
199        assert_eq!(cci_10.value, -0.976_190_476_190_006_1);
200    }
201
202    #[rstest]
203    fn test_initialized_with_required_input(mut cci_10: CommodityChannelIndex) {
204        for i in 1..10 {
205            cci_10.update_raw(f64::from(i), f64::from(i), f64::from(i));
206        }
207        assert!(!cci_10.initialized);
208        cci_10.update_raw(10.0, 10.0, 10.0);
209        assert!(cci_10.initialized);
210    }
211
212    #[rstest]
213    fn test_handle_bar(mut cci_10: CommodityChannelIndex, bar_ethusdt_binance_minute_bid: Bar) {
214        cci_10.handle_bar(&bar_ethusdt_binance_minute_bid);
215        assert_eq!(cci_10.value, 0.0);
216        assert!(cci_10.has_inputs);
217        assert!(!cci_10.initialized);
218    }
219
220    #[rstest]
221    fn test_reset(mut cci_10: CommodityChannelIndex) {
222        cci_10.update_raw(1.0, 0.9, 0.95);
223        cci_10.reset();
224        assert_eq!(cci_10.value, 0.0);
225        assert_eq!(cci_10.prices.len(), 0);
226        assert_eq!(cci_10.mad, 0.0);
227        assert!(!cci_10.has_inputs);
228        assert!(!cci_10.initialized);
229    }
230}