use crate::error::FinError;
use crate::signals::{BarInput, Signal, SignalValue};
use rust_decimal::Decimal;
use std::collections::VecDeque;
pub struct VolumeConsistency {
name: String,
period: usize,
ups: VecDeque<bool>,
prev_volume: Option<Decimal>,
}
impl VolumeConsistency {
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,
ups: VecDeque::with_capacity(period),
prev_volume: None,
})
}
}
impl Signal for VolumeConsistency {
fn name(&self) -> &str { &self.name }
fn period(&self) -> usize { self.period }
fn is_ready(&self) -> bool { self.ups.len() >= self.period }
fn update(&mut self, bar: &BarInput) -> Result<SignalValue, FinError> {
if let Some(pv) = self.prev_volume {
let up = bar.volume > pv;
self.ups.push_back(up);
if self.ups.len() > self.period {
self.ups.pop_front();
}
}
self.prev_volume = Some(bar.volume);
if self.ups.len() < self.period {
return Ok(SignalValue::Unavailable);
}
let count = self.ups.iter().filter(|&&u| u).count();
let pct = Decimal::from(count as u32)
.checked_div(Decimal::from(self.period as u32))
.ok_or(FinError::ArithmeticOverflow)?
* Decimal::ONE_HUNDRED;
Ok(SignalValue::Scalar(pct))
}
fn reset(&mut self) {
self.ups.clear();
self.prev_volume = None;
}
}
#[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(vol: &str) -> OhlcvBar {
let p = Price::new(dec!(100)).unwrap();
OhlcvBar {
symbol: Symbol::new("X").unwrap(),
open: p, high: p, low: p, close: p,
volume: Quantity::new(vol.parse().unwrap()).unwrap(),
ts_open: NanoTimestamp::new(0),
ts_close: NanoTimestamp::new(1),
tick_count: 1,
}
}
#[test]
fn test_vc_invalid_period() {
assert!(VolumeConsistency::new("vc", 0).is_err());
}
#[test]
fn test_vc_unavailable_during_warmup() {
let mut vc = VolumeConsistency::new("vc", 3).unwrap();
for v in &["100", "200", "300"] {
assert_eq!(vc.update_bar(&bar(v)).unwrap(), SignalValue::Unavailable);
}
assert!(!vc.is_ready());
}
#[test]
fn test_vc_always_increasing_is_100() {
let mut vc = VolumeConsistency::new("vc", 3).unwrap();
vc.update_bar(&bar("100")).unwrap();
vc.update_bar(&bar("200")).unwrap();
vc.update_bar(&bar("300")).unwrap();
if let SignalValue::Scalar(v) = vc.update_bar(&bar("400")).unwrap() {
assert_eq!(v, dec!(100), "always up volume → 100%");
} else {
panic!("expected Scalar");
}
}
#[test]
fn test_vc_always_decreasing_is_zero() {
let mut vc = VolumeConsistency::new("vc", 3).unwrap();
vc.update_bar(&bar("400")).unwrap();
vc.update_bar(&bar("300")).unwrap();
vc.update_bar(&bar("200")).unwrap();
if let SignalValue::Scalar(v) = vc.update_bar(&bar("100")).unwrap() {
assert_eq!(v, dec!(0), "always down volume → 0%");
} else {
panic!("expected Scalar");
}
}
#[test]
fn test_vc_alternating_fifty() {
let mut vc = VolumeConsistency::new("vc", 4).unwrap();
vc.update_bar(&bar("100")).unwrap();
vc.update_bar(&bar("200")).unwrap(); vc.update_bar(&bar("100")).unwrap(); vc.update_bar(&bar("200")).unwrap(); if let SignalValue::Scalar(v) = vc.update_bar(&bar("100")).unwrap() { assert_eq!(v, dec!(50), "alternating → 50%: {v}");
} else {
panic!("expected Scalar");
}
}
#[test]
fn test_vc_reset() {
let mut vc = VolumeConsistency::new("vc", 3).unwrap();
for v in &["100","200","300","400"] { vc.update_bar(&bar(v)).unwrap(); }
assert!(vc.is_ready());
vc.reset();
assert!(!vc.is_ready());
}
}