Skip to main content

flowsurface_data/chart/
indicator.rs

1use std::fmt::{self, Debug, Display};
2
3use enum_map::Enum;
4use exchange::adapter::MarketKind;
5use serde::{Deserialize, Serialize};
6
7pub trait Indicator: PartialEq + Display + 'static {
8    fn for_market(market: MarketKind) -> &'static [Self]
9    where
10        Self: Sized;
11}
12
13#[derive(Debug, Clone, Copy, PartialEq, Deserialize, Serialize, Eq, Enum)]
14pub enum KlineIndicator {
15    Volume,
16    BarAnalysis,
17    CumulativeDelta,
18    OpenInterest,
19}
20
21impl Indicator for KlineIndicator {
22    fn for_market(market: MarketKind) -> &'static [Self] {
23        match market {
24            MarketKind::Spot => &Self::FOR_SPOT,
25            MarketKind::LinearPerps | MarketKind::InversePerps => &Self::FOR_PERPS,
26        }
27    }
28}
29
30impl KlineIndicator {
31    // Indicator togglers on UI menus depend on these arrays.
32    // Every variant needs to be in either SPOT, PERPS or both.
33    /// Indicators that can be used with spot market tickers
34    const FOR_SPOT: [KlineIndicator; 3] = [
35        KlineIndicator::Volume,
36        KlineIndicator::BarAnalysis,
37        KlineIndicator::CumulativeDelta,
38    ];
39    /// Indicators that can be used with perpetual swap market tickers
40    const FOR_PERPS: [KlineIndicator; 4] = [
41        KlineIndicator::Volume,
42        KlineIndicator::BarAnalysis,
43        KlineIndicator::CumulativeDelta,
44        KlineIndicator::OpenInterest,
45    ];
46}
47
48impl Display for KlineIndicator {
49    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
50        match self {
51            KlineIndicator::Volume => write!(f, "Volume"),
52            KlineIndicator::BarAnalysis => write!(f, "Bar Analysis"),
53            KlineIndicator::CumulativeDelta => write!(f, "CVD"),
54            KlineIndicator::OpenInterest => write!(f, "Open Interest"),
55        }
56    }
57}
58
59#[derive(Debug, Clone, Copy, PartialEq, Deserialize, Serialize, Eq, Enum)]
60pub enum HeatmapIndicator {
61    Volume,
62}
63
64impl Indicator for HeatmapIndicator {
65    fn for_market(market: MarketKind) -> &'static [Self] {
66        match market {
67            MarketKind::Spot => &Self::FOR_SPOT,
68            MarketKind::LinearPerps | MarketKind::InversePerps => &Self::FOR_PERPS,
69        }
70    }
71}
72
73impl HeatmapIndicator {
74    // Indicator togglers on UI menus depend on these arrays.
75    // Every variant needs to be in either SPOT, PERPS or both.
76    /// Indicators that can be used with spot market tickers
77    const FOR_SPOT: [HeatmapIndicator; 1] = [HeatmapIndicator::Volume];
78    /// Indicators that can be used with perpetual swap market tickers
79    const FOR_PERPS: [HeatmapIndicator; 1] = [HeatmapIndicator::Volume];
80}
81
82impl Display for HeatmapIndicator {
83    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
84        match self {
85            HeatmapIndicator::Volume => write!(f, "Volume"),
86        }
87    }
88}
89
90#[derive(Debug, Clone, Copy)]
91/// Temporary workaround,
92/// represents any indicator type in the UI
93pub enum UiIndicator {
94    Heatmap(HeatmapIndicator),
95    Kline(KlineIndicator),
96}
97
98impl From<KlineIndicator> for UiIndicator {
99    fn from(k: KlineIndicator) -> Self {
100        UiIndicator::Kline(k)
101    }
102}
103
104impl From<HeatmapIndicator> for UiIndicator {
105    fn from(h: HeatmapIndicator) -> Self {
106        UiIndicator::Heatmap(h)
107    }
108}