nautilus_indicators/momentum/
roc.rs1use 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 RateOfChange {
36 pub period: usize,
37 pub use_log: bool,
38 pub value: f64,
39 pub initialized: bool,
40 has_inputs: bool,
41 prices: ArrayDeque<f64, MAX_PERIOD, Wrapping>,
42}
43
44impl Display for RateOfChange {
45 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
46 write!(f, "{}({})", self.name(), self.period)
47 }
48}
49
50impl Indicator for RateOfChange {
51 fn name(&self) -> String {
52 stringify!(RateOfChange).to_string()
53 }
54
55 fn has_inputs(&self) -> bool {
56 self.has_inputs
57 }
58
59 fn initialized(&self) -> bool {
60 self.initialized
61 }
62
63 fn handle_bar(&mut self, bar: &Bar) {
64 self.update_raw((&bar.close).into());
65 }
66
67 fn reset(&mut self) {
68 self.prices.clear();
69 self.value = 0.0;
70 self.has_inputs = false;
71 self.initialized = false;
72 }
73}
74
75impl RateOfChange {
76 #[must_use]
83 pub fn new(period: usize, use_log: Option<bool>) -> Self {
84 assert!(
85 period <= MAX_PERIOD,
86 "RateOfChange: period {period} exceeds MAX_PERIOD ({MAX_PERIOD})"
87 );
88
89 Self {
90 period,
91 use_log: use_log.unwrap_or(false),
92 value: 0.0,
93 prices: ArrayDeque::new(),
94 has_inputs: false,
95 initialized: false,
96 }
97 }
98
99 pub fn update_raw(&mut self, price: f64) {
100 let _ = self.prices.push_back(price);
101
102 if !self.initialized {
103 self.has_inputs = true;
104
105 if self.prices.len() >= self.period {
106 self.initialized = true;
107 }
108 }
109
110 if let Some(first) = self.prices.front() {
111 if self.use_log {
112 self.value = (price / first).log10();
113 } else {
114 self.value = (price - first) / first;
115 }
116 }
117 }
118}
119
120#[cfg(test)]
121mod tests {
122 use rstest::rstest;
123
124 use super::*;
125 use crate::stubs::roc_10;
126
127 #[rstest]
128 fn test_name_returns_expected_string(roc_10: RateOfChange) {
129 assert_eq!(roc_10.name(), "RateOfChange");
130 }
131
132 #[rstest]
133 fn test_str_repr_returns_expected_string(roc_10: RateOfChange) {
134 assert_eq!(format!("{roc_10}"), "RateOfChange(10)");
135 }
136
137 #[rstest]
138 fn test_period_returns_expected_value(roc_10: RateOfChange) {
139 assert_eq!(roc_10.period, 10);
140 assert!(roc_10.use_log);
141 }
142
143 #[rstest]
144 fn test_initialized_without_inputs_returns_false(roc_10: RateOfChange) {
145 assert!(!roc_10.initialized());
146 }
147
148 #[rstest]
149 fn test_value_with_all_higher_inputs_returns_expected_value(mut roc_10: RateOfChange) {
150 let close_values = [
151 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,
152 11.45,
153 ];
154
155 for close in &close_values {
156 roc_10.update_raw(*close);
157 }
158
159 assert!(roc_10.initialized());
160 assert_eq!(roc_10.value, 1.081_081_881_387_059);
161 }
162
163 #[rstest]
164 fn test_reset_successfully_returns_indicator_to_fresh_state(mut roc_10: RateOfChange) {
165 roc_10.update_raw(1.00020);
166 roc_10.update_raw(1.00030);
167 roc_10.update_raw(1.00070);
168
169 roc_10.reset();
170
171 assert!(!roc_10.initialized());
172 assert!(!roc_10.has_inputs);
173 assert_eq!(roc_10.value, 0.0);
174 }
175}