use rust_decimal::Decimal;
use std::collections::VecDeque;
use crate::error::FinError;
use crate::signals::{BarInput, Signal, SignalValue};
pub struct VolatilitySpike {
period: usize,
multiplier_pct: u32,
window: VecDeque<Decimal>,
sum: Decimal,
}
impl VolatilitySpike {
pub fn new(period: usize, multiplier_pct: u32) -> Result<Self, FinError> {
if period == 0 {
return Err(FinError::InvalidPeriod(period));
}
Ok(Self {
period,
multiplier_pct,
window: VecDeque::with_capacity(period),
sum: Decimal::ZERO,
})
}
}
impl Signal for VolatilitySpike {
fn update(&mut self, bar: &BarInput) -> Result<SignalValue, FinError> {
let range = bar.high - bar.low;
self.window.push_back(range);
self.sum += range;
if self.window.len() > self.period {
if let Some(old) = self.window.pop_front() {
self.sum -= old;
}
}
if self.window.len() < self.period {
return Ok(SignalValue::Unavailable);
}
let avg_range = self.sum / Decimal::from(self.period as u32);
let threshold = avg_range * Decimal::from(self.multiplier_pct) / Decimal::ONE_HUNDRED;
let spike: i32 = if range > threshold { 1 } else { 0 };
Ok(SignalValue::Scalar(Decimal::from(spike)))
}
fn is_ready(&self) -> bool { self.window.len() >= self.period }
fn period(&self) -> usize { self.period }
fn reset(&mut self) { self.window.clear(); self.sum = Decimal::ZERO; }
fn name(&self) -> &str { "VolatilitySpike" }
}
#[cfg(test)]
mod tests {
use super::*;
use rust_decimal_macros::dec;
fn bar(h: &str, l: &str) -> BarInput {
BarInput {
open: dec!(100),
high: h.parse().unwrap(),
low: l.parse().unwrap(),
close: dec!(100),
volume: dec!(1000),
}
}
#[test]
fn test_vs_no_spike() {
let mut sig = VolatilitySpike::new(3, 150).unwrap();
sig.update(&bar("110", "90")).unwrap();
sig.update(&bar("110", "90")).unwrap();
let v = sig.update(&bar("110", "90")).unwrap();
assert_eq!(v, SignalValue::Scalar(dec!(0)));
}
#[test]
fn test_vs_spike_detected() {
let mut sig = VolatilitySpike::new(3, 150).unwrap();
sig.update(&bar("110", "90")).unwrap(); sig.update(&bar("110", "90")).unwrap(); sig.update(&bar("110", "90")).unwrap(); let v = sig.update(&bar("130", "70")).unwrap(); assert_eq!(v, SignalValue::Scalar(dec!(1)));
}
}