use crate::error::FinError;
use crate::signals::{BarInput, Signal, SignalValue};
use rust_decimal::Decimal;
pub struct VolumeAtClose {
name: String,
ready: bool,
}
impl VolumeAtClose {
pub fn new(name: impl Into<String>) -> Result<Self, FinError> {
Ok(Self { name: name.into(), ready: false })
}
}
impl Signal for VolumeAtClose {
fn name(&self) -> &str {
&self.name
}
fn period(&self) -> usize {
1
}
fn is_ready(&self) -> bool {
self.ready
}
fn update(&mut self, bar: &BarInput) -> Result<SignalValue, FinError> {
let vol = bar.volume;
if vol.is_zero() {
return Ok(SignalValue::Unavailable);
}
self.ready = true;
let clv = bar.close_location_value();
let fraction = (Decimal::ONE + clv)
.checked_div(Decimal::from(2u32))
.ok_or(FinError::ArithmeticOverflow)?;
let vac = vol
.checked_mul(fraction)
.ok_or(FinError::ArithmeticOverflow)?;
Ok(SignalValue::Scalar(vac))
}
fn reset(&mut self) {
self.ready = false;
}
}
#[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(open: &str, high: &str, low: &str, close: &str, vol: &str) -> OhlcvBar {
OhlcvBar {
symbol: Symbol::new("X").unwrap(),
open: Price::new(open.parse().unwrap()).unwrap(),
high: Price::new(high.parse().unwrap()).unwrap(),
low: Price::new(low.parse().unwrap()).unwrap(),
close: Price::new(close.parse().unwrap()).unwrap(),
volume: Quantity::new(vol.parse().unwrap()).unwrap(),
ts_open: NanoTimestamp::new(0),
ts_close: NanoTimestamp::new(1),
tick_count: 1,
}
}
#[test]
fn test_vac_zero_volume_unavailable() {
let mut vac = VolumeAtClose::new("vac").unwrap();
let v = vac.update_bar(&bar("100", "110", "90", "105", "0")).unwrap();
assert_eq!(v, SignalValue::Unavailable);
}
#[test]
fn test_vac_close_at_high_full_volume() {
let mut vac = VolumeAtClose::new("vac").unwrap();
let v = vac.update_bar(&bar("100", "110", "90", "110", "1000")).unwrap();
assert_eq!(v, SignalValue::Scalar(dec!(1000)));
}
#[test]
fn test_vac_close_at_low_zero_volume() {
let mut vac = VolumeAtClose::new("vac").unwrap();
let v = vac.update_bar(&bar("100", "110", "90", "90", "1000")).unwrap();
assert_eq!(v, SignalValue::Scalar(dec!(0)));
}
#[test]
fn test_vac_close_at_midpoint_half_volume() {
let mut vac = VolumeAtClose::new("vac").unwrap();
let v = vac.update_bar(&bar("100", "110", "90", "100", "1000")).unwrap();
assert_eq!(v, SignalValue::Scalar(dec!(500)));
}
#[test]
fn test_vac_flat_bar_zero_clv() {
let mut vac = VolumeAtClose::new("vac").unwrap();
let v = vac.update_bar(&bar("100", "100", "100", "100", "500")).unwrap();
assert_eq!(v, SignalValue::Scalar(dec!(250)));
}
#[test]
fn test_vac_ready_after_first_bar() {
let mut vac = VolumeAtClose::new("vac").unwrap();
vac.update_bar(&bar("100", "110", "90", "105", "500")).unwrap();
assert!(vac.is_ready());
}
#[test]
fn test_vac_period_is_one() {
let vac = VolumeAtClose::new("vac").unwrap();
assert_eq!(vac.period(), 1);
}
#[test]
fn test_vac_reset() {
let mut vac = VolumeAtClose::new("vac").unwrap();
vac.update_bar(&bar("100", "110", "90", "105", "500")).unwrap();
assert!(vac.is_ready());
vac.reset();
assert!(!vac.is_ready());
}
}