use std::collections::VecDeque;
use crate::bar_indicators::indicator_value::IndicatorValue;
use crate::bar_indicators::BlockTradeConsumer;
use crate::core::types::BlockTrade;
#[derive(Clone)]
pub struct BlockTradeImpact {
events: VecDeque<i64>,
window_ms: i64,
last_rate: f64,
}
impl BlockTradeImpact {
pub fn new(window_ms: i64) -> Self {
Self {
events: VecDeque::new(),
window_ms: window_ms.max(1),
last_rate: 0.0,
}
}
fn compute_rate(count: usize, window_ms: i64) -> f64 {
let window_minutes = window_ms as f64 / 60_000.0;
count as f64 / window_minutes
}
}
impl Default for BlockTradeImpact {
fn default() -> Self {
Self::new(60_000) }
}
impl BlockTradeConsumer for BlockTradeImpact {
fn update_block_trade(&mut self, bt: &BlockTrade) -> IndicatorValue {
let cutoff = bt.timestamp - self.window_ms;
while self.events.front().map_or(false, |&ts| ts < cutoff) {
self.events.pop_front();
}
self.events.push_back(bt.timestamp);
self.last_rate = Self::compute_rate(self.events.len(), self.window_ms);
IndicatorValue::Single(self.last_rate)
}
fn value(&self) -> IndicatorValue {
IndicatorValue::Single(self.last_rate)
}
fn reset(&mut self) {
self.events.clear();
self.last_rate = 0.0;
}
fn is_ready(&self) -> bool {
!self.events.is_empty()
}
}
#[cfg(test)]
mod tests {
use super::*;
fn make_bt(timestamp: i64) -> BlockTrade {
BlockTrade {
block_id: "test".to_string(),
price: 100.0,
quantity: 1.0,
is_buy: true,
timestamp,
is_iv: false,
}
}
#[test]
fn rate_equals_events_per_minute() {
let mut ind = BlockTradeImpact::new(60_000);
for i in 0..6 {
ind.update_block_trade(&make_bt(i * 10_000));
}
if let IndicatorValue::Single(r) = ind.value() {
assert!((r - 6.0).abs() < 1e-9, "expected 6.0 events/min, got {r}");
} else {
panic!("expected Single");
}
}
#[test]
fn expired_events_excluded() {
let mut ind = BlockTradeImpact::new(60_000);
ind.update_block_trade(&make_bt(0));
ind.update_block_trade(&make_bt(70_000));
if let IndicatorValue::Single(r) = ind.value() {
let expected = 1.0 / 1.0; assert!((r - expected).abs() < 1e-9, "got {r}");
} else {
panic!("expected Single");
}
}
#[test]
fn reset_clears_state() {
let mut ind = BlockTradeImpact::new(60_000);
ind.update_block_trade(&make_bt(1000));
ind.reset();
assert!(!ind.is_ready());
if let IndicatorValue::Single(v) = ind.value() {
assert_eq!(v, 0.0);
}
}
}