use crate::regimes::MarketRegime;
use crate::traits::Next;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TAR {
pub thresholds: Vec<f64>,
}
impl TAR {
pub fn new(threshold: f64) -> Self {
Self {
thresholds: vec![threshold],
}
}
pub fn multi(thresholds: Vec<f64>) -> Self {
let mut t = thresholds;
t.sort_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));
Self { thresholds: t }
}
}
impl Next<f64> for TAR {
type Output = MarketRegime;
fn next(&mut self, signal: f64) -> Self::Output {
match self
.thresholds
.binary_search_by(|t| t.partial_cmp(&signal).unwrap_or(std::cmp::Ordering::Equal))
{
Ok(idx) => MarketRegime::Cluster(idx as u8 + 1), Err(idx) => {
if idx == 0 {
MarketRegime::Steady } else if idx >= self.thresholds.len() {
MarketRegime::Crisis } else {
MarketRegime::Cluster(idx as u8)
}
}
}
}
}