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