indexes_rs/v1/ma/
main.rs

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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
use crate::v1::{
    ema::main::ExponentialMovingAverage,
    macd::{main::MACD, types::MACDResult},
    sma::main::SimpleMovingAverage,
};

pub struct MovingAverages {
    pub sma: SMAPeriods,
    pub ema: EMAPeriods,
    pub macd: MACD,
}

pub struct SMAPeriods {
    pub short: SimpleMovingAverage,  // 20 periods
    pub medium: SimpleMovingAverage, // 50 periods
    pub long: SimpleMovingAverage,   // 200 periods
}

pub struct EMAPeriods {
    pub short: ExponentialMovingAverage,  // 20 periods
    pub medium: ExponentialMovingAverage, // 50 periods
    pub long: ExponentialMovingAverage,   // 200 periods
}

#[derive(Debug)]
pub struct MovingAverageResults {
    pub sma: SMAValues,
    pub ema: EMAValues,
    pub macd: Option<MACDResult>,
}

#[derive(Debug)]
pub struct SMAValues {
    pub short: Option<f64>,
    pub medium: Option<f64>,
    pub long: Option<f64>,
}

#[derive(Debug)]
pub struct EMAValues {
    pub short: Option<f64>,
    pub medium: Option<f64>,
    pub long: Option<f64>,
}

impl MovingAverages {
    pub fn new() -> Self {
        MovingAverages {
            sma: SMAPeriods::new(),
            ema: EMAPeriods::new(),
            macd: MACD::new(12, 26, 9),
        }
    }

    pub fn calculate(&mut self, price: f64) -> MovingAverageResults {
        self.sma.update(price);
        self.ema.update(price);

        MovingAverageResults {
            sma: self.sma.get_values(),
            ema: self.ema.get_values(),
            macd: self.macd.calculate(price),
        }
    }
}

impl SMAPeriods {
    fn new() -> Self {
        SMAPeriods {
            short: SimpleMovingAverage::new(20),
            medium: SimpleMovingAverage::new(50),
            long: SimpleMovingAverage::new(200),
        }
    }

    fn update(&mut self, price: f64) {
        self.short.add_value(price);
        self.medium.add_value(price);
        self.long.add_value(price);
    }

    fn get_values(&self) -> SMAValues {
        SMAValues {
            short: self.short.calculate(),
            medium: self.medium.calculate(),
            long: self.long.calculate(),
        }
    }
}

impl EMAPeriods {
    fn new() -> Self {
        EMAPeriods {
            short: ExponentialMovingAverage::new(20),
            medium: ExponentialMovingAverage::new(50),
            long: ExponentialMovingAverage::new(200),
        }
    }

    fn update(&mut self, price: f64) {
        self.short.add_value(price);
        self.medium.add_value(price);
        self.long.add_value(price);
    }

    fn get_values(&self) -> EMAValues {
        EMAValues {
            short: self.short.get_current_value(),
            medium: self.medium.get_current_value(),
            long: self.long.get_current_value(),
        }
    }
}