1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
use crate::common::{validate_inputs, validate_options};
pub use crate::indicator_types::TIndicatorState;
use crate::indicators::mom::calc as calc_mom;
use crate::indicators::rocr::calc as calc_rocr;
use crate::types::{DisplayGroup, DisplayType, IndicatorError, IndicatorType, Info};
use serde::{Deserialize, Serialize};
/// Number of input price series required by this indicator.
pub const INPUTS_WIDTH: usize = 1;
/// Number of option parameters required by this indicator.
pub const OPTIONS_WIDTH: usize = 1;
/// SIMD-parallel variant that processes `N` assets with identical options simultaneously.
/// Requires the `simd_assets` Cargo feature. See [`by_assets`] for the module form.
#[cfg(feature = "simd_assets")]
pub use crate::indicators::simd_indicators::roc_simd::indicator_by_assets;
/// SIMD-parallel variant that processes a single asset with `N` different option
/// sets simultaneously. Requires the `simd_options` Cargo feature. See [`by_options`].
#[cfg(feature = "simd_options")]
pub use crate::indicators::simd_indicators::roc_simd::indicator_by_options;
/// Convenience module that re-exports [`indicator_by_assets`] as `indicator`,
/// allowing SIMD multi-asset computation to be used as a drop-in replacement
/// for the standard single-asset [`indicator`] function.
/// Requires the `simd_assets` Cargo feature.
#[cfg(feature = "simd_assets")]
pub mod by_assets {
/// Processes `N` assets in parallel with shared options.
/// See the parent module's [`super::indicator_by_assets`] for full documentation.
pub use crate::indicators::simd_indicators::roc_simd::indicator_by_assets as indicator;
}
/// Convenience module that re-exports [`indicator_by_options`] as `indicator`,
/// allowing SIMD multi-option computation to be used as a drop-in replacement
/// for the standard single-asset [`indicator`] function.
/// Requires the `simd_options` Cargo feature.
#[cfg(feature = "simd_options")]
pub mod by_options {
/// Processes a single asset with `N` different option sets in parallel.
/// See the parent module's [`super::indicator_by_options`] for full documentation.
pub use crate::indicators::simd_indicators::roc_simd::indicator_by_options as indicator;
}
/// Returns information about the Rate of Change (ROC) indicator.
pub const INFO: Info = Info {
name: "roc",
full_name: "Rate of Change",
indicator_type: IndicatorType::Momentum,
inputs: &["real"],
options: &["period"],
outputs: &["roc"],
optional_outputs: &["mom"],
display_groups: &[
DisplayGroup {
offset: None,
id: "roc",
label: "ROC",
display_type: DisplayType::Indicator,
outputs: &["roc"],
},
DisplayGroup {
offset: None,
id: "mom",
label: "Momentum",
display_type: DisplayType::Indicator,
outputs: &["mom"],
},
],
};
#[derive(Serialize, Deserialize)]
pub struct IndicatorState {
real: Vec<f64>,
period: usize,
}
impl IndicatorState {
pub fn new(real: &[f64], period: usize) -> Self {
Self {
period,
real: real[real.len() - period..].to_vec(),
}
}
}
impl TIndicatorState<1> for IndicatorState {
fn batch_indicator(
&mut self,
inputs: &[&[f64]; INPUTS_WIDTH],
optional_outputs: Option<&[bool]>,
) -> Result<Vec<Vec<f64>>, IndicatorError> {
validate_inputs(inputs, 1)?;
self.real.extend_from_slice(inputs[0]);
let (mut roc_line, mut mom_line) = {
let capacity = inputs[0].len();
(
crate::uninit_vec!(f64, capacity),
crate::init_optional_outputs_eff!(
optional_outputs, &[false],
mom_line: capacity
),
)
};
cycle_roc(&self.real, self.period, &mut roc_line, &mut mom_line);
self.real.drain(..self.real.len() - self.period);
Ok(vec![roc_line, mom_line])
}
}
/// Returns the minimum amount of data required for the ROC indicator.
pub fn min_data(options: &[f64]) -> usize {
options[0] as usize + 1
}
/// Calculates the output length for the ROC indicator given the input data length and options.
///
/// # Arguments
///
/// * `data_len` - The length of the input data.
/// * `options` - A slice containing the options for the ROC calculation.
///
/// # Returns
///
/// The output length.
pub fn output_length(data_len: usize, options: &[f64]) -> usize {
data_len - min_data(options) + 1
}
/// Calculates the Rate of Change (ROC) indicator over the full input dataset.
///
/// # Inputs
///
/// * `inputs[0]` — real (source) values
///
/// # Options
///
/// * `options[0]` — period
///
/// # Arguments
///
/// * `inputs` - Array of input price slices (see Inputs above).
/// * `options` - Array of indicator options (see Options above).
/// * `optional_outputs` - Optional slice of booleans enabling optional outputs.
/// Pass `Some(&[true])` to also compute `mom`.
///
/// # Returns
///
/// `Ok((outputs, state))` where:
/// - `outputs[0]` — `roc`
/// - `outputs[1]` — `mom` (only populated when `optional_outputs[0]` is `true`)
///
/// `state` can be passed to `IndicatorState::batch_indicator` for streaming.
/// Returns `Err(IndicatorError)` if inputs are too short or options are invalid.
pub fn indicator(
inputs: &[&[f64]; INPUTS_WIDTH],
options: &[f64; OPTIONS_WIDTH],
optional_outputs: Option<&[bool]>,
) -> Result<(Vec<Vec<f64>>, IndicatorState), IndicatorError> {
validate_options(options)?;
let period = options[0] as usize;
validate_inputs(inputs, min_data(options))?;
let real = inputs[0];
let (mut roc_line, mut mom_line) = {
let capacity = output_length(real.len(), options);
(
crate::uninit_vec!(f64, capacity),
crate::init_optional_outputs_eff!(
optional_outputs, &[false],
mom_line: capacity
),
)
};
cycle_roc(real, period, &mut roc_line, &mut mom_line);
Ok((
vec![roc_line, mom_line],
IndicatorState {
period,
real: real[real.len() - period..].to_vec(),
},
))
}
/// Iterates over the input data and applies the calc function.
fn cycle_roc(real: &[f64], period: usize, roc_line: &mut [f64], mom_line: &mut [f64]) {
let (_, want_mom) = crate::calc_want_flags!(mom_line);
for (j, i) in (period..real.len()).enumerate() {
let (roc, mom) = unsafe { calc(*real.get_unchecked(i), *real.get_unchecked(j)) };
unsafe { *roc_line.get_unchecked_mut(j) = roc };
crate::store_optional_outputs_safe!(j,
want_mom, mom_line => mom
);
}
}
/// Performs the core calculation for the Rate of Change (ROC) indicator.
#[inline(always)]
pub fn calc(real: f64, prev_real: f64) -> (f64, f64) {
let mom = calc_mom(real, prev_real);
(calc_rocr(mom, prev_real), mom)
}