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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
//! Simple Moving Average (SMA) indicator.
//!
//! The SMA is the most fundamental moving average, computed as the
//! unweighted arithmetic mean of the closing prices over the last `N`
//! periods. It is an overlay indicator drawn directly on the price chart.
//!
//! # Formula
//!
//! ```text
//! SMA(t) = (Close[t] + Close[t-1] + ... + Close[t-N+1]) / N
//! ```
//!
//! # Interpretation
//!
//! - Price above SMA: bullish bias.
//! - Price below SMA: bearish bias.
//! - Popular periods: 20 (short-term), 50 (medium-term), 200 (long-term).
//! - SMA crossovers (e.g. 50/200 "golden cross") are widely-used signals.
//!
//! # Default colour
//!
//! Orange (from the design-token `indicators.ma`).
//!
//! # Example
//!
//! ```rust,ignore
//! use egui_charts::studies::{SMA, Indicator};
//!
//! let mut sma = SMA::new(20);
//! sma.calculate(&bars);
//! // First 19 values are IndicatorValue::None (warmup)
//! ```
use crate::model::Bar;
use crate::studies::{Indicator, IndicatorValue};
use crate::tokens::DESIGN_TOKENS;
use egui::Color32;
/// Simple Moving Average indicator.
///
/// Computes the arithmetic mean of closing prices over a rolling window of
/// `period` bars. This is an overlay indicator (drawn on the price chart).
#[derive(Clone)]
pub struct SMA {
period: usize,
values: Vec<IndicatorValue>,
color: Color32,
visible: bool,
}
impl SMA {
/// Create a new SMA indicator.
///
/// # Arguments
/// * `period` -- The number of bars in the averaging window (e.g. 20, 50, 200).
pub fn new(period: usize) -> Self {
Self {
period,
values: Vec::new(),
color: DESIGN_TOKENS.semantic.indicators.ma, // Orange for MA
visible: true,
}
}
/// Set a custom line colour (builder pattern).
pub fn with_color(mut self, color: Color32) -> Self {
self.color = color;
self
}
}
/// Construct with the conventional default parameters.
impl Default for SMA {
fn default() -> Self {
Self::new(20)
}
}
impl Indicator for SMA {
fn name(&self) -> &str {
"SMA"
}
fn desc(&self) -> &str {
"Simple Moving Avg - Avg price over N periods"
}
fn calculate(&mut self, data: &[Bar]) {
self.values.clear();
if data.len() < self.period {
return;
}
for i in 0..data.len() {
if i + 1 < self.period {
self.values.push(IndicatorValue::None);
} else {
let start = i + 1 - self.period;
let sum: f64 = data[start..=i].iter().map(|bar| bar.close).sum();
let sma = sum / self.period as f64;
self.values.push(IndicatorValue::Single(sma));
}
}
}
fn values(&self) -> &[IndicatorValue] {
&self.values
}
fn colors(&self) -> Vec<Color32> {
vec![self.color]
}
fn set_colors(&mut self, colors: Vec<Color32>) {
if !colors.is_empty() {
self.color = colors[0];
}
}
fn is_overlay(&self) -> bool {
true
}
fn is_visible(&self) -> bool {
self.visible
}
fn set_visible(&mut self, visible: bool) {
self.visible = visible;
}
fn clone_box(&self) -> Box<dyn Indicator> {
Box::new(self.clone())
}
fn line_names(&self) -> Vec<String> {
vec![format!("SMA({})", self.period)]
}
}