Documentation
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 // 买入资产1,卖出资产2
        } else if *asset1_price > *asset2_price {
            -1.0 // 卖出资产1,买入资产2
        } 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; // 如果价格队列为空,返回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
    }
}