Skip to main content

egui_charts/studies/builtin/
ma_multiple.rs

1use super::ma_combination::impl_ma_preset;
2use super::{AverageLine, AverageType, MACombination};
3use egui::Color32;
4
5/// Simple averages of lengths 20, 50, 100 and 200, as a combination preset, without crossing
6/// markers until a pair is chosen.
7#[derive(Clone)]
8pub struct MultipleMA {
9    inner: MACombination,
10}
11
12impl MultipleMA {
13    /// Create the conventional four-average preset.
14    pub fn new() -> Self {
15        let colors = [
16            Color32::from_rgb(246, 195, 9),
17            Color32::from_rgb(251, 152, 0),
18            Color32::from_rgb(251, 101, 0),
19            Color32::from_rgb(246, 12, 12),
20        ];
21        Self {
22            inner: MACombination::new(
23                [20, 50, 100, 200]
24                    .into_iter()
25                    .zip(colors)
26                    .map(|(period, color)| AverageLine::new(AverageType::Simple, period, color))
27                    .collect(),
28            )
29            .unwrap()
30            .without_crossings(),
31        }
32    }
33}
34impl Default for MultipleMA {
35    fn default() -> Self {
36        Self::new()
37    }
38}
39impl_ma_preset!(
40    MultipleMA,
41    "Multi MA",
42    "Multiple Moving Averages - SMA 20, 50, 100, 200"
43);