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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
//! Kaufman Adaptive Moving Average (KAMA) indicator.
//!
//! KAMA is an adaptive moving average that adjusts its smoothing speed based
//! on the market's efficiency ratio. In trending markets it becomes more
//! responsive; in choppy, ranging markets it slows down to filter noise.
//!
//! # Formula
//!
//! ```text
//! Efficiency Ratio (ER) = |Close - Close[N]| / Sum(|Close[i] - Close[i-1]|, N)
//! Smoothing Constant (SC) = [ER * (fast_sc - slow_sc) + slow_sc]^2
//! KAMA[t] = KAMA[t-1] + SC * (Close[t] - KAMA[t-1])
//! ```
//!
//! # Default parameters
//!
//! `KAMA::new(10)` with fast period 2 and slow period 30.
//!
//! # Example
//!
//! ```rust,ignore
//! use egui_charts::studies::{KAMA, Indicator};
//!
//! let mut kama = KAMA::new(10);
//! kama.calculate(&bars);
//! ```
use crate::model::Bar;
use crate::studies::{Indicator, IndicatorValue};
use crate::tokens::DESIGN_TOKENS;
use egui::Color32;
/// Kaufman Adaptive Moving Average indicator.
///
/// Adapts its smoothing speed to market conditions via the efficiency
/// ratio. Overlay indicator drawn on the price chart.
#[derive(Clone)]
pub struct KAMA {
/// Period for Efficiency Ratio calculation.
period: usize,
/// Fastest EMA period (typically 2).
fast_period: usize,
/// Slowest EMA period (typically 30).
slow_period: usize,
values: Vec<IndicatorValue>,
color: Color32,
visible: bool,
}
impl KAMA {
/// Create a new KAMA indicator.
///
/// # Arguments
/// * `period` -- Efficiency ratio lookback period (default: 10).
///
/// Fast/slow periods default to 2 and 30 respectively. Use
/// [`with_periods`](Self::with_periods) to customise.
pub fn new(period: usize) -> Self {
Self {
period,
fast_period: 2,
slow_period: 30,
values: Vec::new(),
color: DESIGN_TOKENS.semantic.extended.cyan, // Cyan
visible: true,
}
}
/// Set custom fast and slow EMA periods.
pub fn with_periods(mut self, fast: usize, slow: usize) -> Self {
self.fast_period = fast;
self.slow_period = slow;
self
}
/// Set a custom line colour (builder pattern).
pub fn with_color(mut self, color: Color32) -> Self {
self.color = color;
self
}
/// Calculate the Efficiency Ratio (ER) for a price window.
///
/// ER = |net change| / sum of |bar-to-bar changes|.
/// Returns a value between 0.0 (no net progress) and 1.0 (perfectly trending).
fn efficiency_ratio(prices: &[f64]) -> f64 {
if prices.len() < 2 {
return 0.0;
}
let change = (prices[prices.len() - 1] - prices[0]).abs();
let mut volatility = 0.0;
for i in 1..prices.len() {
volatility += (prices[i] - prices[i - 1]).abs();
}
if volatility > 0.0 {
change / volatility
} else {
0.0
}
}
/// Calculate smoothing constant from ER
fn smoothing_constant(&self, er: f64) -> f64 {
let fast_sc = 2.0 / (self.fast_period as f64 + 1.0);
let slow_sc = 2.0 / (self.slow_period as f64 + 1.0);
// SC = [ER * (FastSC - SlowSC) + SlowSC]^2
let sc = er * (fast_sc - slow_sc) + slow_sc;
sc * sc
}
}
/// Construct with the conventional default parameters.
impl Default for KAMA {
fn default() -> Self {
Self::new(10)
}
}
impl Indicator for KAMA {
fn name(&self) -> &str {
"KAMA"
}
fn desc(&self) -> &str {
"Kaufman Adaptive Moving Avg - Adjusts smoothing based on market efficiency"
}
fn calculate(&mut self, data: &[Bar]) {
self.values.clear();
if data.len() < self.period {
for _ in 0..data.len() {
self.values.push(IndicatorValue::None);
}
return;
}
// First values are None
for _ in 0..self.period - 1 {
self.values.push(IndicatorValue::None);
}
// Initialize KAMA with first valid SMA
let first_prices: Vec<f64> = data[0..self.period].iter().map(|bar| bar.close).collect();
let mut kama = first_prices.iter().sum::<f64>() / self.period as f64;
self.values.push(IndicatorValue::Single(kama));
// Calculate remaining KAMA values
for i in self.period..data.len() {
let prices: Vec<f64> = data[i + 1 - self.period..=i]
.iter()
.map(|bar| bar.close)
.collect();
let er = Self::efficiency_ratio(&prices);
let sc = self.smoothing_constant(er);
let close = data[i].close;
kama = kama + sc * (close - kama);
self.values.push(IndicatorValue::Single(kama));
}
}
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!(
"KAMA({}, {}, {})",
self.period, self.fast_period, self.slow_period
)]
}
}
#[cfg(test)]
mod tests {
use super::*;
use chrono::Utc;
fn make_bar(close: f64) -> Bar {
Bar {
time: Utc::now(),
open: close,
high: close,
low: close,
close,
volume: 1000.0,
}
}
#[test]
fn test_efficiency_ratio_trending() {
// Perfect uptrend: ER should be 1.0
let prices = vec![100.0, 101.0, 102.0, 103.0, 104.0];
let er = KAMA::efficiency_ratio(&prices);
assert!((er - 1.0).abs() < 0.001);
}
#[test]
fn test_efficiency_ratio_ranging() {
// Choppy market: ER should be close to 0
let prices = vec![100.0, 102.0, 100.0, 102.0, 100.0];
let er = KAMA::efficiency_ratio(&prices);
// Change = 0, so ER = 0
assert!((er - 0.0).abs() < 0.001);
}
#[test]
fn test_kama_calculation() {
let mut kama = KAMA::new(5);
let data = vec![
make_bar(100.0),
make_bar(101.0),
make_bar(102.0),
make_bar(103.0),
make_bar(104.0),
make_bar(105.0),
make_bar(106.0),
];
kama.calculate(&data);
assert_eq!(kama.values.len(), 7);
// First 4 values should be None
for i in 0..4 {
assert!(matches!(kama.values[i], IndicatorValue::None));
}
// Fifth value should be present (first KAMA)
assert!(matches!(kama.values[4], IndicatorValue::Single(_)));
}
#[test]
fn test_smoothing_constant() {
let kama = KAMA::new(10);
// ER = 0 (ranging) should give slow SC
let sc_slow = kama.smoothing_constant(0.0);
let expected_slow = (2.0_f64 / 31.0).powi(2);
assert!((sc_slow - expected_slow).abs() < 0.0001);
// ER = 1 (trending) should give fast SC
let sc_fast = kama.smoothing_constant(1.0);
let expected_fast = (2.0_f64 / 3.0).powi(2);
assert!((sc_fast - expected_fast).abs() < 0.0001);
}
}