1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
use rust_decimal::Decimal;
use rust_decimal_macros::dec;

pub trait OHLC {
    type Type;

    fn open(&self) -> Self::Type;
    fn high(&self) -> Self::Type;
    fn low(&self) -> Self::Type;
    fn close(&self) -> Self::Type;
}

pub trait OHLCMath: OHLC {
    fn ohlc4(&self) -> Self::Type;
    fn hlc3(&self) -> Self::Type;
    fn hl2(&self) -> Self::Type;
}

impl OHLCMath for dyn OHLC<Type = Decimal> {
    fn ohlc4(&self) -> Decimal {
        (self.open() + self.high() + self.low() + self.close()) / dec!(4)
    }

    fn hlc3(&self) -> Decimal {
        (self.high() + self.low() + self.close()) / dec!(3)
    }

    fn hl2(&self) -> Decimal {
        (self.high() + self.low()) / dec!(2)
    }
}

impl OHLCMath for dyn OHLC<Type = f64> {
    fn ohlc4(&self) -> f64 {
        (self.open() + self.high() + self.low() + self.close()) / 4.0
    }

    fn hlc3(&self) -> f64 {
        (self.high() + self.low() + self.close()) / 3.0
    }

    fn hl2(&self) -> f64 {
        (self.high() + self.low()) / 2.0
    }
}

pub trait Volume {
    type Type;

    fn volume(&self) -> Self::Type;
}