quantwave_core/regimes/
tar.rs1use crate::regimes::MarketRegime;
9use crate::traits::Next;
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 {
23 thresholds: vec![threshold],
24 }
25 }
26
27 pub fn multi(thresholds: Vec<f64>) -> Self {
29 let mut t = thresholds;
30 t.sort_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));
31 Self { thresholds: t }
32 }
33}
34
35impl Next<f64> for TAR {
36 type Output = MarketRegime;
37
38 fn next(&mut self, signal: f64) -> Self::Output {
39 match self
41 .thresholds
42 .binary_search_by(|t| t.partial_cmp(&signal).unwrap_or(std::cmp::Ordering::Equal))
43 {
44 Ok(idx) => MarketRegime::Cluster(idx as u8 + 1), Err(idx) => {
46 if idx == 0 {
47 MarketRegime::Steady } else if idx >= self.thresholds.len() {
49 MarketRegime::Crisis } else {
51 MarketRegime::Cluster(idx as u8)
52 }
53 }
54 }
55 }
56}