nautilus_indicators/momentum/
roc.rs1use std::{
17 collections::VecDeque,
18 fmt::{Debug, Display},
19};
20
21use nautilus_model::data::Bar;
22
23use crate::indicator::Indicator;
24
25#[repr(C)]
26#[derive(Debug)]
27#[cfg_attr(
28 feature = "python",
29 pyo3::pyclass(module = "nautilus_trader.core.nautilus_pyo3.indicators")
30)]
31pub struct RateOfChange {
32 pub period: usize,
33 pub use_log: bool,
34 pub value: f64,
35 pub initialized: bool,
36 has_inputs: bool,
37 prices: VecDeque<f64>,
38}
39
40impl Display for RateOfChange {
41 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
42 write!(f, "{}({})", self.name(), self.period)
43 }
44}
45
46impl Indicator for RateOfChange {
47 fn name(&self) -> String {
48 stringify!(RateOfChange).to_string()
49 }
50
51 fn has_inputs(&self) -> bool {
52 self.has_inputs
53 }
54
55 fn initialized(&self) -> bool {
56 self.initialized
57 }
58
59 fn handle_bar(&mut self, bar: &Bar) {
60 self.update_raw((&bar.close).into());
61 }
62
63 fn reset(&mut self) {
64 self.prices.clear();
65 self.value = 0.0;
66 self.has_inputs = false;
67 self.initialized = false;
68 }
69}
70
71impl RateOfChange {
72 #[must_use]
74 pub fn new(period: usize, use_log: Option<bool>) -> Self {
75 Self {
76 period,
77 use_log: use_log.unwrap_or(false),
78 value: 0.0,
79 prices: VecDeque::with_capacity(period),
80 has_inputs: false,
81 initialized: false,
82 }
83 }
84
85 pub fn update_raw(&mut self, price: f64) {
86 self.prices.push_back(price);
87
88 if !self.initialized {
89 self.has_inputs = true;
90 if self.prices.len() >= self.period {
91 self.initialized = true;
92 }
93 }
94
95 if self.use_log {
96 self.value = (price / self.prices[0]).log10();
98 } else {
99 self.value = (price - self.prices[0]) / self.prices[0];
100 }
101 }
102}
103
104#[cfg(test)]
108mod tests {
109 use rstest::rstest;
110
111 use super::*;
112 use crate::stubs::roc_10;
113
114 #[rstest]
115 fn test_name_returns_expected_string(roc_10: RateOfChange) {
116 assert_eq!(roc_10.name(), "RateOfChange");
117 }
118
119 #[rstest]
120 fn test_str_repr_returns_expected_string(roc_10: RateOfChange) {
121 assert_eq!(format!("{roc_10}"), "RateOfChange(10)");
122 }
123
124 #[rstest]
125 fn test_period_returns_expected_value(roc_10: RateOfChange) {
126 assert_eq!(roc_10.period, 10);
127 assert!(roc_10.use_log);
128 }
129
130 #[rstest]
131 fn test_initialized_without_inputs_returns_false(roc_10: RateOfChange) {
132 assert!(!roc_10.initialized());
133 }
134
135 #[rstest]
136 fn test_value_with_all_higher_inputs_returns_expected_value(mut roc_10: RateOfChange) {
137 let close_values = [
138 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,
139 11.45,
140 ];
141
142 for close in &close_values {
143 roc_10.update_raw(*close);
144 }
145
146 assert!(roc_10.initialized());
147 assert_eq!(roc_10.value, 1.081_081_881_387_059);
148 }
149
150 #[rstest]
151 fn test_reset_successfully_returns_indicator_to_fresh_state(mut roc_10: RateOfChange) {
152 roc_10.update_raw(1.00020);
153 roc_10.update_raw(1.00030);
154 roc_10.update_raw(1.00070);
155
156 roc_10.reset();
157
158 assert!(!roc_10.initialized());
159 assert!(!roc_10.has_inputs);
160 assert_eq!(roc_10.value, 0.0);
161 }
162}