use std::collections::VecDeque;
use crate::strategy_engine::Strategy;
#[derive(Debug, Clone)]
pub struct Volatility {
prices: VecDeque<f64>,
}
impl Volatility {
pub fn new() -> Self {
Self {
prices: VecDeque::new(),
}
}
pub fn update_price(&mut self, price: f64) {
self.prices.push_back(price);
}
}
impl Strategy for Volatility {
fn evaluate(&self, _price: &f64) -> f64 {
if self.prices.len() < 2 {
return 0.0;
}
let last_price = self.prices[self.prices.len() - 2];
let current_price = self.prices.back().unwrap();
if *current_price > last_price {
1.0 } else if *current_price < last_price {
-1.0 } else {
0.0 }
}
fn calculate_indicator(&self, _prices: &Vec<f64>) -> f64 {
let mut volatility = 0.0;
for i in 1..self.prices.len() {
let diff = self.prices[i] - self.prices[i - 1];
volatility += diff.abs();
}
volatility / self.prices.len() as f64
}
}