use crate::error::FinError;
use crate::signals::{BarInput, Signal, SignalValue};
use rust_decimal::Decimal;
use std::collections::VecDeque;
pub struct RocAcceleration {
name: String,
period: usize,
closes: VecDeque<Decimal>,
rocs: VecDeque<Decimal>,
}
impl RocAcceleration {
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);
if self.closes.len() > self.period + 1 {
self.closes.pop_front();
}
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();
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 {
assert!(v.abs() < dec!(0.1), "near-constant ROC → near-zero acceleration: {v}");
}
}
#[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());
}
}