nautilus_indicators/volatility/
kc.rs1use std::fmt::{Debug, Display};
17
18use nautilus_model::data::Bar;
19
20use crate::{
21 average::{MovingAverageFactory, MovingAverageType},
22 indicator::{Indicator, MovingAverage},
23 volatility::atr::AverageTrueRange,
24};
25
26#[repr(C)]
27#[derive(Debug)]
28#[cfg_attr(
29 feature = "python",
30 pyo3::pyclass(module = "nautilus_trader.core.nautilus_pyo3.indicators", unsendable)
31)]
32pub struct KeltnerChannel {
33 pub period: usize,
34 pub k_multiplier: f64,
35 pub ma_type: MovingAverageType,
36 pub ma_type_atr: MovingAverageType,
37 pub use_previous: bool,
38 pub atr_floor: f64,
39 pub upper: f64,
40 pub middle: f64,
41 pub lower: f64,
42 pub initialized: bool,
43 has_inputs: bool,
44 ma: Box<dyn MovingAverage + Send + 'static>,
45 atr: AverageTrueRange,
46}
47
48impl Display for KeltnerChannel {
49 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
50 write!(f, "{}({})", self.name(), self.period)
51 }
52}
53
54impl Indicator for KeltnerChannel {
55 fn name(&self) -> String {
56 stringify!(KeltnerChannel).to_string()
57 }
58
59 fn has_inputs(&self) -> bool {
60 self.has_inputs
61 }
62
63 fn initialized(&self) -> bool {
64 self.initialized
65 }
66
67 fn handle_bar(&mut self, bar: &Bar) {
68 self.update_raw((&bar.high).into(), (&bar.low).into(), (&bar.close).into());
69 }
70
71 fn reset(&mut self) {
72 self.ma.reset();
73 self.atr.reset();
74 self.upper = 0.0;
75 self.middle = 0.0;
76 self.lower = 0.0;
77 self.has_inputs = false;
78 self.initialized = false;
79 }
80}
81
82impl KeltnerChannel {
83 #[must_use]
85 pub fn new(
86 period: usize,
87 k_multiplier: f64,
88 ma_type: Option<MovingAverageType>,
89 ma_type_atr: Option<MovingAverageType>,
90 use_previous: Option<bool>,
91 atr_floor: Option<f64>,
92 ) -> Self {
93 Self {
94 period,
95 k_multiplier,
96 ma_type: ma_type.unwrap_or(MovingAverageType::Simple),
97 ma_type_atr: ma_type_atr.unwrap_or(MovingAverageType::Simple),
98 use_previous: use_previous.unwrap_or(true),
99 atr_floor: atr_floor.unwrap_or(0.0),
100 upper: 0.0,
101 middle: 0.0,
102 lower: 0.0,
103 has_inputs: false,
104 initialized: false,
105 ma: MovingAverageFactory::create(ma_type.unwrap_or(MovingAverageType::Simple), period),
106 atr: AverageTrueRange::new(period, ma_type_atr, use_previous, atr_floor),
107 }
108 }
109
110 pub fn update_raw(&mut self, high: f64, low: f64, close: f64) {
111 let typical_price = (high + low + close) / 3.0;
112
113 self.ma.update_raw(typical_price);
114 self.atr.update_raw(high, low, close);
115
116 self.upper = self.atr.value.mul_add(self.k_multiplier, self.ma.value());
117 self.middle = self.ma.value();
118 self.lower = self.atr.value.mul_add(-self.k_multiplier, self.ma.value());
119
120 if !self.initialized {
122 self.has_inputs = true;
123 if self.ma.initialized() {
124 self.initialized = true;
125 }
126 }
127 }
128}
129
130#[cfg(test)]
134mod tests {
135 use rstest::rstest;
136
137 use super::*;
138 use crate::stubs::kc_10;
139
140 #[rstest]
141 fn test_name_returns_expected_string(kc_10: KeltnerChannel) {
142 assert_eq!(kc_10.name(), "KeltnerChannel");
143 }
144
145 #[rstest]
146 fn test_str_repr_returns_expected_string(kc_10: KeltnerChannel) {
147 assert_eq!(format!("{kc_10}"), "KeltnerChannel(10)");
148 }
149
150 #[rstest]
151 fn test_period_returns_expected_value(kc_10: KeltnerChannel) {
152 assert_eq!(kc_10.period, 10);
153 assert_eq!(kc_10.k_multiplier, 2.0);
154 }
155
156 #[rstest]
157 fn test_initialized_without_inputs_returns_false(kc_10: KeltnerChannel) {
158 assert!(!kc_10.initialized());
159 }
160
161 #[rstest]
162 fn test_value_with_all_higher_inputs_returns_expected_value(mut kc_10: KeltnerChannel) {
163 let high_values = [
164 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,
165 ];
166 let low_values = [
167 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,
168 ];
169
170 let close_values = [
171 0.95, 1.95, 2.95, 3.95, 4.95, 5.95, 6.95, 7.95, 8.95, 9.95, 10.05, 10.15, 10.25, 11.05,
172 11.45,
173 ];
174
175 for i in 0..15 {
176 kc_10.update_raw(high_values[i], low_values[i], close_values[i]);
177 }
178
179 assert!(kc_10.initialized());
180 assert_eq!(kc_10.upper, 13.436_666_666_666_666);
181 assert_eq!(kc_10.middle, 9.676_666_666_666_666);
182 assert_eq!(kc_10.lower, 5.916_666_666_666_666);
183 }
184
185 #[rstest]
186 fn test_reset_successfully_returns_indicator_to_fresh_state(mut kc_10: KeltnerChannel) {
187 kc_10.update_raw(1.00020, 1.00050, 1.00030);
188 kc_10.update_raw(1.00030, 1.00060, 1.00040);
189 kc_10.update_raw(1.00070, 1.00080, 1.00075);
190
191 kc_10.reset();
192
193 assert!(!kc_10.initialized());
194 assert!(!kc_10.has_inputs);
195 assert_eq!(kc_10.upper, 0.0);
196 assert_eq!(kc_10.middle, 0.0);
197 assert_eq!(kc_10.lower, 0.0);
198 }
199}