nautilus_indicators/momentum/
cci.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2025 Posei Systems Pty Ltd. All rights reserved.
3//  https://poseitrader.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 = "posei_trader.core.nautilus_pyo3.indicators", unsendable)
33)]
34pub struct CommodityChannelIndex {
35    pub period: usize,
36    pub ma_type: MovingAverageType,
37    pub scalar: f64,
38    pub value: f64,
39    pub initialized: bool,
40    ma: Box<dyn MovingAverage + Send + 'static>,
41    has_inputs: bool,
42    mad: f64,
43    prices: ArrayDeque<f64, MAX_PERIOD, Wrapping>,
44}
45
46impl Display for CommodityChannelIndex {
47    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
48        write!(f, "{}({},{})", self.name(), self.period, self.ma_type,)
49    }
50}
51
52impl Indicator for CommodityChannelIndex {
53    fn name(&self) -> String {
54        stringify!(CommodityChannelIndex).to_string()
55    }
56
57    fn has_inputs(&self) -> bool {
58        self.has_inputs
59    }
60
61    fn initialized(&self) -> bool {
62        self.initialized
63    }
64
65    fn handle_bar(&mut self, bar: &Bar) {
66        self.update_raw((&bar.high).into(), (&bar.low).into(), (&bar.close).into());
67    }
68
69    fn reset(&mut self) {
70        self.ma.reset();
71        self.mad = 0.0;
72        self.prices.clear();
73        self.value = 0.0;
74        self.has_inputs = false;
75        self.initialized = false;
76    }
77}
78
79impl CommodityChannelIndex {
80    /// Creates a new [`CommodityChannelIndex`] instance.
81    ///
82    /// # Panics
83    ///
84    /// - If `period` is less than or equal to 0.
85    /// - If `period` exceeds `MAX_PERIOD`.
86    #[must_use]
87    pub fn new(period: usize, scalar: f64, ma_type: Option<MovingAverageType>) -> Self {
88        assert!(period > 0, "CommodityChannelIndex: period must be > 0");
89        assert!(
90            period <= MAX_PERIOD,
91            "CommodityChannelIndex: period exceeds MAX_PERIOD"
92        );
93
94        Self {
95            period,
96            scalar,
97            ma_type: ma_type.unwrap_or(MovingAverageType::Simple),
98            value: 0.0,
99            prices: ArrayDeque::new(),
100            ma: MovingAverageFactory::create(ma_type.unwrap_or(MovingAverageType::Simple), period),
101            has_inputs: false,
102            initialized: false,
103            mad: 0.0,
104        }
105    }
106
107    pub fn update_raw(&mut self, high: f64, low: f64, close: f64) {
108        let typical_price = (high + low + close) / 3.0;
109
110        if self.prices.len() == self.period {
111            let _ = self.prices.pop_front();
112        }
113        let _ = self.prices.push_back(typical_price);
114
115        self.ma.update_raw(typical_price);
116
117        self.mad = fast_mad_with_mean(self.prices.iter().copied(), self.ma.value());
118
119        if self.ma.initialized() && self.mad != 0.0 {
120            self.value = (typical_price - self.ma.value()) / (self.scalar * self.mad);
121        }
122
123        if !self.initialized {
124            self.has_inputs = true;
125            if self.ma.initialized() {
126                self.initialized = true;
127            }
128        }
129    }
130}
131
132pub fn fast_mad_with_mean<I>(values: I, mean: f64) -> f64
133where
134    I: IntoIterator<Item = f64>,
135{
136    let mut acc = 0.0_f64;
137    let mut count = 0_usize;
138
139    for v in values {
140        acc += (v - mean).abs();
141        count += 1;
142    }
143
144    if count == 0 { 0.0 } else { acc / count as f64 }
145}
146
147////////////////////////////////////////////////////////////////////////////////
148// Tests
149////////////////////////////////////////////////////////////////////////////////
150#[cfg(test)]
151mod tests {
152    use nautilus_model::data::Bar;
153    use rstest::rstest;
154
155    use crate::{
156        indicator::Indicator,
157        momentum::cci::CommodityChannelIndex,
158        stubs::{bar_ethusdt_binance_minute_bid, cci_10},
159    };
160
161    #[rstest]
162    fn test_psl_initialized(cci_10: CommodityChannelIndex) {
163        let display_str = format!("{cci_10}");
164        assert_eq!(display_str, "CommodityChannelIndex(10,SIMPLE)");
165        assert_eq!(cci_10.period, 10);
166        assert!(!cci_10.initialized);
167        assert!(!cci_10.has_inputs);
168    }
169
170    #[rstest]
171    fn test_value_with_one_input(mut cci_10: CommodityChannelIndex) {
172        cci_10.update_raw(1.0, 0.9, 0.95);
173        assert_eq!(cci_10.value, 0.0);
174    }
175
176    #[rstest]
177    fn test_value_with_three_inputs(mut cci_10: CommodityChannelIndex) {
178        cci_10.update_raw(1.0, 0.9, 0.95);
179        cci_10.update_raw(2.0, 1.9, 1.95);
180        cci_10.update_raw(3.0, 2.9, 2.95);
181        assert_eq!(cci_10.value, 0.0);
182    }
183
184    #[rstest]
185    fn test_value_with_ten_inputs(mut cci_10: CommodityChannelIndex) {
186        cci_10.update_raw(1.00000, 0.90000, 1.00000);
187        cci_10.update_raw(1.00010, 0.90010, 1.00010);
188        cci_10.update_raw(1.00030, 0.90020, 1.00020);
189        cci_10.update_raw(1.00040, 0.90030, 1.00030);
190        cci_10.update_raw(1.00050, 0.90040, 1.00040);
191        cci_10.update_raw(1.00060, 0.90050, 1.00050);
192        cci_10.update_raw(1.00050, 0.90040, 1.00040);
193        cci_10.update_raw(1.00040, 0.90030, 1.00030);
194        cci_10.update_raw(1.00030, 0.90020, 1.00020);
195        cci_10.update_raw(1.00010, 0.90010, 1.00010);
196        cci_10.update_raw(1.00000, 0.90000, 1.00000);
197        assert_eq!(cci_10.value, -0.976_190_476_190_006_1);
198    }
199
200    #[rstest]
201    fn test_initialized_with_required_input(mut cci_10: CommodityChannelIndex) {
202        for i in 1..10 {
203            cci_10.update_raw(f64::from(i), f64::from(i), f64::from(i));
204        }
205        assert!(!cci_10.initialized);
206        cci_10.update_raw(10.0, 10.0, 10.0);
207        assert!(cci_10.initialized);
208    }
209
210    #[rstest]
211    fn test_handle_bar(mut cci_10: CommodityChannelIndex, bar_ethusdt_binance_minute_bid: Bar) {
212        cci_10.handle_bar(&bar_ethusdt_binance_minute_bid);
213        assert_eq!(cci_10.value, 0.0);
214        assert!(cci_10.has_inputs);
215        assert!(!cci_10.initialized);
216    }
217
218    #[rstest]
219    fn test_reset(mut cci_10: CommodityChannelIndex) {
220        cci_10.update_raw(1.0, 0.9, 0.95);
221        cci_10.reset();
222        assert_eq!(cci_10.value, 0.0);
223        assert_eq!(cci_10.prices.len(), 0);
224        assert_eq!(cci_10.mad, 0.0);
225        assert!(!cci_10.has_inputs);
226        assert!(!cci_10.initialized);
227    }
228}