Skip to main content

egui_charts/studies/builtin/
ma_presets.rs

1use super::ma_combination::impl_ma_preset;
2use super::{AverageLine, AverageType, MACombination};
3use crate::studies::palette;
4
5/// A simple average crossing an exponential one.
6#[derive(Clone)]
7pub struct MixedMACross {
8    inner: MACombination,
9}
10impl MixedMACross {
11    /// Simple fast and exponential slow lengths, each at least one.
12    pub fn new(fast: usize, slow: usize) -> Self {
13        Self {
14            inner: MACombination::new(vec![
15                AverageLine::new(AverageType::Simple, fast, palette::ORANGE),
16                AverageLine::new(AverageType::Exponential, slow, palette::DARK_GREEN),
17            ])
18            .unwrap(),
19        }
20    }
21}
22impl Default for MixedMACross {
23    fn default() -> Self {
24        Self::new(9, 21)
25    }
26}
27impl_ma_preset!(
28    MixedMACross,
29    "SMA/EMA Cross",
30    "Simple and exponential moving averages with crossing markers",
31    palette::LIGHT_BLUE
32);
33
34/// Three simple averages with independently chosen lengths.
35#[derive(Clone)]
36pub struct TripleMA {
37    inner: MACombination,
38}
39impl TripleMA {
40    /// Three lengths, each clamped to at least one. Marks crossings of the first two.
41    pub fn new(fast: usize, middle: usize, slow: usize) -> Self {
42        let colors = [palette::ORANGE, palette::LIGHT_BLUE, palette::CYAN];
43        Self {
44            inner: MACombination::new(
45                [fast, middle, slow]
46                    .into_iter()
47                    .zip(colors)
48                    .map(|(period, color)| AverageLine::new(AverageType::Simple, period, color))
49                    .collect(),
50            )
51            .unwrap(),
52        }
53    }
54}
55impl Default for TripleMA {
56    fn default() -> Self {
57        Self::new(9, 21, 50)
58    }
59}
60impl_ma_preset!(
61    TripleMA,
62    "Triple MA",
63    "Three simple moving averages with crossing markers"
64);
65
66/// Twelve evenly spaced exponential averages coloured by their individual direction.
67#[derive(Clone)]
68pub struct MARibbon {
69    inner: MACombination,
70}
71impl MARibbon {
72    /// Direction-coloured ribbon with a thicker slowest line and fading bottom fill.
73    pub fn new() -> Self {
74        Self {
75            inner: MACombination::ribbon(),
76        }
77    }
78    /// Dim magenta lines without a fill, each 1 px wide.
79    pub fn muted() -> Self {
80        Self {
81            inner: MACombination::muted_ribbon(),
82        }
83    }
84}
85impl Default for MARibbon {
86    fn default() -> Self {
87        Self::new()
88    }
89}
90impl_ma_preset!(
91    MARibbon,
92    "MA Ribbon",
93    "Twelve exponential averages of lengths 5 through 60"
94);