use std::collections::VecDeque;
use crate::strategy_engine::Strategy;
#[derive(Debug, Clone)]
pub struct Arbitrage {
asset1_prices: VecDeque<f64>,
asset2_prices: VecDeque<f64>,
}
impl Arbitrage {
pub fn new() -> Self {
Self {
asset1_prices: VecDeque::new(),
asset2_prices: VecDeque::new(),
}
}
pub fn update_prices(&mut self, asset1_price: f64, asset2_price: f64) {
self.asset1_prices.push_back(asset1_price);
self.asset2_prices.push_back(asset2_price);
}
}
impl Strategy for Arbitrage {
fn evaluate(&self, _price: &f64) -> f64 {
if self.asset1_prices.is_empty() || self.asset2_prices.is_empty() {
return 0.0; }
let asset1_price = self.asset1_prices.back().unwrap();
let asset2_price = self.asset2_prices.back().unwrap();
if *asset1_price < *asset2_price {
1.0 } else if *asset1_price > *asset2_price {
-1.0 } else {
0.0 }
}
fn calculate_indicator(&self, _prices: &Vec<f64>) -> f64 {
if self.asset1_prices.is_empty() || self.asset2_prices.is_empty() {
return 0.0; }
let mut profits = 0.0;
for (asset1_price, asset2_price) in self.asset1_prices.iter().zip(self.asset2_prices.iter()) {
if *asset1_price < *asset2_price {
profits += *asset2_price - *asset1_price;
} else if *asset1_price > *asset2_price {
profits += *asset1_price - *asset2_price;
}
}
profits / self.asset1_prices.len() as f64
}
}