use crate::{AggregationRule, By, Error, ModularCandle, Result, TakerTrade};
#[derive(Debug, Clone)]
pub struct VolumeRule {
init: bool,
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 {
init: true,
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.init {
self.cum_vol = 0.0;
self.init = false;
}
self.cum_vol += match self.by {
By::Quote => trade.size().abs(),
By::Base => trade.size().abs() / trade.price(),
};
let should_trigger = self.cum_vol > self.threshold_vol;
if should_trigger {
self.init = true;
}
should_trigger
}
}