use crate::{AggregationRule, By, Error, ModularCandle, Result, TakerTrade};
#[derive(Debug, Clone)]
pub struct VolumeRule {
by: By,
cum_vol: f64,
threshold_vol: f64,
}
impl VolumeRule {
pub fn new(threshold_vol: f64, by: By) -> Result<Self> {
if threshold_vol <= 0.0 {
return Err(Error::InvalidParam);
}
Ok(Self {
by,
cum_vol: 0.0,
threshold_vol,
})
}
}
impl<C, T> AggregationRule<C, T> for VolumeRule
where
C: ModularCandle<T>,
T: TakerTrade,
{
fn should_trigger(&mut self, trade: &T, _candle: &C) -> bool {
if self.cum_vol >= self.threshold_vol {
self.cum_vol = self.cum_vol - self.threshold_vol;
debug_assert!(self.cum_vol >= 0.0);
}
self.cum_vol += match self.by {
By::Quote => trade.size().abs(),
By::Base => trade.size().abs() / trade.price(),
};
self.cum_vol >= self.threshold_vol
}
}