Skip to main content

nautilus_indicators/volatility/
dc.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::Display;
17
18use arraydeque::{ArrayDeque, Wrapping};
19use nautilus_model::data::Bar;
20
21use crate::indicator::Indicator;
22
23const MAX_PERIOD: usize = 1_024;
24
25#[repr(C)]
26#[derive(Debug)]
27#[cfg_attr(
28    feature = "python",
29    pyo3::pyclass(module = "nautilus_trader.core.nautilus_pyo3.indicators")
30)]
31#[cfg_attr(
32    feature = "python",
33    pyo3_stub_gen::derive::gen_stub_pyclass(module = "nautilus_trader.indicators")
34)]
35pub struct DonchianChannel {
36    pub period: usize,
37    pub upper: f64,
38    pub middle: f64,
39    pub lower: f64,
40    pub initialized: bool,
41    has_inputs: bool,
42    upper_prices: ArrayDeque<f64, MAX_PERIOD, Wrapping>,
43    lower_prices: ArrayDeque<f64, MAX_PERIOD, Wrapping>,
44}
45
46impl Display for DonchianChannel {
47    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
48        write!(f, "{}({})", self.name(), self.period)
49    }
50}
51
52impl Indicator for DonchianChannel {
53    fn name(&self) -> String {
54        stringify!(DonchianChannel).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());
67    }
68
69    fn reset(&mut self) {
70        self.upper_prices.clear();
71        self.lower_prices.clear();
72        self.upper = 0.0;
73        self.middle = 0.0;
74        self.lower = 0.0;
75        self.has_inputs = false;
76        self.initialized = false;
77    }
78}
79
80impl DonchianChannel {
81    /// Creates a new [`DonchianChannel`] instance.
82    ///
83    /// # Panics
84    ///
85    /// This function panics if:
86    /// - `period` is not in the range of 1 to `MAX_PERIOD` (inclusive).
87    #[must_use]
88    pub fn new(period: usize) -> Self {
89        assert!(
90            period > 0 && period <= MAX_PERIOD,
91            "DonchianChannel: period {period} exceeds MAX_PERIOD ({MAX_PERIOD})"
92        );
93
94        Self {
95            period,
96            upper: 0.0,
97            middle: 0.0,
98            lower: 0.0,
99            upper_prices: ArrayDeque::new(),
100            lower_prices: ArrayDeque::new(),
101            has_inputs: false,
102            initialized: false,
103        }
104    }
105
106    pub fn update_raw(&mut self, high: f64, low: f64) {
107        let _ = self.upper_prices.push_back(high);
108        let _ = self.lower_prices.push_back(low);
109
110        if !self.initialized {
111            self.has_inputs = true;
112
113            if self.upper_prices.len() >= self.period && self.lower_prices.len() >= self.period {
114                self.initialized = true;
115            }
116        }
117
118        self.upper = self
119            .upper_prices
120            .iter()
121            .copied()
122            .fold(f64::NEG_INFINITY, f64::max);
123        self.lower = self
124            .lower_prices
125            .iter()
126            .copied()
127            .fold(f64::INFINITY, f64::min);
128        self.middle = 0.5 * (self.upper + self.lower);
129    }
130}
131
132#[cfg(test)]
133mod tests {
134    use nautilus_model::data::Bar;
135    use rstest::rstest;
136
137    use crate::{
138        indicator::Indicator,
139        stubs::{bar_ethusdt_binance_minute_bid, dc_10},
140        volatility::dc::DonchianChannel,
141    };
142
143    #[rstest]
144    fn test_psl_initialized(dc_10: DonchianChannel) {
145        let display_str = format!("{dc_10}");
146        assert_eq!(display_str, "DonchianChannel(10)");
147        assert_eq!(dc_10.period, 10);
148        assert!(!dc_10.initialized);
149        assert!(!dc_10.has_inputs);
150    }
151
152    #[rstest]
153    fn test_value_with_one_input(mut dc_10: DonchianChannel) {
154        dc_10.update_raw(1.0, 0.9);
155        assert_eq!(dc_10.upper, 1.0);
156        assert_eq!(dc_10.middle, 0.95);
157        assert_eq!(dc_10.lower, 0.9);
158    }
159
160    #[rstest]
161    fn test_value_with_three_inputs(mut dc_10: DonchianChannel) {
162        dc_10.update_raw(1.0, 0.9);
163        dc_10.update_raw(2.0, 1.8);
164        dc_10.update_raw(3.0, 2.7);
165        assert_eq!(dc_10.upper, 3.0);
166        assert_eq!(dc_10.middle, 1.95);
167        assert_eq!(dc_10.lower, 0.9);
168    }
169
170    #[rstest]
171    fn test_value_with_ten_inputs(mut dc_10: DonchianChannel) {
172        let high_values = [
173            1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0,
174        ];
175        let low_values = [
176            0.9, 1.9, 2.9, 3.9, 4.9, 5.9, 6.9, 7.9, 8.9, 9.9, 10.1, 10.2, 10.3, 11.1, 11.4,
177        ];
178
179        for i in 0..15 {
180            dc_10.update_raw(high_values[i], low_values[i]);
181        }
182
183        assert_eq!(dc_10.upper, 15.0);
184        assert_eq!(dc_10.middle, 7.95);
185        assert_eq!(dc_10.lower, 0.9);
186    }
187
188    #[rstest]
189    fn test_handle_bar(mut dc_10: DonchianChannel, bar_ethusdt_binance_minute_bid: Bar) {
190        dc_10.handle_bar(&bar_ethusdt_binance_minute_bid);
191        assert_eq!(dc_10.upper, 1550.0);
192        assert_eq!(dc_10.middle, 1522.5);
193        assert_eq!(dc_10.lower, 1495.0);
194        assert!(dc_10.has_inputs);
195        assert!(!dc_10.initialized);
196    }
197
198    #[rstest]
199    fn test_reset(mut dc_10: DonchianChannel) {
200        dc_10.update_raw(1.0, 0.9);
201        dc_10.reset();
202        assert_eq!(dc_10.upper_prices.len(), 0);
203        assert_eq!(dc_10.lower_prices.len(), 0);
204        assert_eq!(dc_10.upper, 0.0);
205        assert_eq!(dc_10.middle, 0.0);
206        assert_eq!(dc_10.lower, 0.0);
207        assert!(!dc_10.has_inputs);
208        assert!(!dc_10.initialized);
209    }
210}