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
//! Rate-of-Change Acceleration indicator.
//!
//! Measures the change in momentum by computing the difference between the
//! current ROC and the ROC `period` bars ago (second derivative of price).
use crate::error::FinError;
use crate::signals::{BarInput, Signal, SignalValue};
use rust_decimal::Decimal;
use std::collections::VecDeque;
/// ROC Acceleration: `ROC(N)[t] - ROC(N)[t - N]`.
///
/// Computes the N-bar Rate-of-Change for each bar, then returns the change
/// in that ROC value over the last N ROC readings. This is the second
/// derivative of price (acceleration of momentum):
///
/// - **Positive and growing**: momentum is accelerating upward.
/// - **Negative and falling**: momentum is decelerating into negative territory.
/// - **Crossing zero**: momentum trend is reversing direction.
///
/// Returns [`SignalValue::Unavailable`] until `2 * period` bars have been
/// accumulated (enough to produce a ROC *and* then measure its change over
/// another `period` bars).
///
/// # Errors
/// Returns [`FinError::InvalidPeriod`] if `period == 0`.
///
/// # Example
/// ```rust
/// use fin_primitives::signals::indicators::RocAcceleration;
/// use fin_primitives::signals::Signal;
///
/// let ra = RocAcceleration::new("roc_acc", 10).unwrap();
/// assert_eq!(ra.period(), 10);
/// assert!(!ra.is_ready());
/// ```
pub struct RocAcceleration {
name: String,
period: usize,
closes: VecDeque<Decimal>,
rocs: VecDeque<Decimal>,
}
impl RocAcceleration {
/// Constructs a new `RocAcceleration`.
///
/// # Errors
/// Returns [`FinError::InvalidPeriod`] if `period == 0`.
pub fn new(name: impl Into<String>, period: usize) -> Result<Self, FinError> {
if period == 0 {
return Err(FinError::InvalidPeriod(period));
}
Ok(Self {
name: name.into(),
period,
closes: VecDeque::with_capacity(period + 1),
rocs: VecDeque::with_capacity(period + 1),
})
}
}
impl Signal for RocAcceleration {
fn name(&self) -> &str {
&self.name
}
fn period(&self) -> usize {
self.period
}
fn is_ready(&self) -> bool {
self.rocs.len() > self.period
}
fn update(&mut self, bar: &BarInput) -> Result<SignalValue, FinError> {
let close = bar.close;
self.closes.push_back(close);
// Keep closes window at period+1 to compute N-bar ROC
if self.closes.len() > self.period + 1 {
self.closes.pop_front();
}
// Compute current ROC when we have enough closes
if self.closes.len() == self.period + 1 {
let old_close = *self.closes.front().unwrap_or(&close);
if old_close.is_zero() {
return Ok(SignalValue::Unavailable);
}
let roc = (close - old_close)
.checked_div(old_close)
.ok_or(FinError::ArithmeticOverflow)?
* Decimal::ONE_HUNDRED;
self.rocs.push_back(roc);
if self.rocs.len() > self.period + 1 {
self.rocs.pop_front();
}
}
if self.rocs.len() <= self.period {
return Ok(SignalValue::Unavailable);
}
let roc_now = *self.rocs.back().unwrap_or(&Decimal::ZERO);
let roc_prev = *self.rocs.front().unwrap_or(&Decimal::ZERO);
Ok(SignalValue::Scalar(roc_now - roc_prev))
}
fn reset(&mut self) {
self.closes.clear();
self.rocs.clear();
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::ohlcv::OhlcvBar;
use crate::signals::Signal;
use crate::types::{NanoTimestamp, Price, Quantity, Symbol};
use rust_decimal_macros::dec;
fn bar(close: &str) -> OhlcvBar {
let p = Price::new(close.parse().unwrap()).unwrap();
OhlcvBar {
symbol: Symbol::new("X").unwrap(),
open: p, high: p, low: p, close: p,
volume: Quantity::zero(),
ts_open: NanoTimestamp::new(0),
ts_close: NanoTimestamp::new(1),
tick_count: 1,
}
}
#[test]
fn test_ra_invalid_period() {
assert!(RocAcceleration::new("ra", 0).is_err());
}
#[test]
fn test_ra_unavailable_during_warmup() {
let mut ra = RocAcceleration::new("ra", 3).unwrap();
for i in 1u32..=5 {
assert_eq!(
ra.update_bar(&bar(&(100 + i).to_string())).unwrap(),
SignalValue::Unavailable,
"expected Unavailable at bar {i}"
);
}
}
#[test]
fn test_ra_constant_roc_zero_acceleration() {
let mut ra = RocAcceleration::new("ra", 2).unwrap();
// Steady +1% per bar → ROC stays constant → acceleration = 0
let prices = ["100", "101", "102.01", "103.0301", "104.060401", "105.10100401"];
let mut last = SignalValue::Unavailable;
for p in &prices {
last = ra.update_bar(&bar(p)).unwrap();
}
if let SignalValue::Scalar(v) = last {
// acceleration should be very close to 0 for constant ROC
assert!(v.abs() < dec!(0.1), "near-constant ROC → near-zero acceleration: {v}");
}
// (Unavailable is also acceptable if not yet warm)
}
#[test]
fn test_ra_produces_scalar_after_warmup() {
let mut ra = RocAcceleration::new("ra", 2).unwrap();
for i in 1u32..=5 {
ra.update_bar(&bar(&(100 + i * 2).to_string())).unwrap();
}
let v = ra.update_bar(&bar("120")).unwrap();
assert!(matches!(v, SignalValue::Scalar(_)), "expected Scalar after warmup");
}
#[test]
fn test_ra_reset() {
let mut ra = RocAcceleration::new("ra", 2).unwrap();
for i in 1u32..=5 {
ra.update_bar(&bar(&(100 + i).to_string())).unwrap();
}
assert!(ra.is_ready());
ra.reset();
assert!(!ra.is_ready());
}
}