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