quantwave_core/regimes/
tar.rs1use crate::traits::Next;
9use crate::regimes::MarketRegime;
10use serde::{Deserialize, Serialize};
11
12#[derive(Debug, Clone, Serialize, Deserialize)]
14pub struct TAR {
15 pub thresholds: Vec<f64>,
17}
18
19impl TAR {
20 pub fn new(threshold: f64) -> Self {
22 Self { thresholds: vec![threshold] }
23 }
24
25 pub fn multi(thresholds: Vec<f64>) -> Self {
27 let mut t = thresholds;
28 t.sort_by(|a, b| a.partial_cmp(b).unwrap());
29 Self { thresholds: t }
30 }
31}
32
33impl Next<f64> for TAR {
34 type Output = MarketRegime;
35
36 fn next(&mut self, signal: f64) -> Self::Output {
37 match self.thresholds.binary_search_by(|t| t.partial_cmp(&signal).unwrap()) {
39 Ok(idx) => MarketRegime::Cluster(idx as u8 + 1), Err(idx) => {
41 if idx == 0 {
42 MarketRegime::Steady } else if idx >= self.thresholds.len() {
44 MarketRegime::Crisis } else {
46 MarketRegime::Cluster(idx as u8)
47 }
48 }
49 }
50 }
51}