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
282
283
//! Aroon Indicator
//!
//! Aroon identifies trend changes and trend strength by measuring the time
//! since the highest high and lowest low within a lookback period.
//!
//! # Components
//! - Aroon Up: ((period - days since highest high) / period) * 100
//! - Aroon Down: ((period - days since lowest low) / period) * 100
//! - Aroon Oscillator: Aroon Up - Aroon Down (optional)
//!
//! # Interpretation
//! - Aroon Up > 70 and Aroon Down < 30: Strong uptrend
//! - Aroon Down > 70 and Aroon Up < 30: Strong downtrend
//! - Crossovers indicate potential trend changes
//!
//! # Example
//! ```ignore
//! use egui_charts::Aroon;
//!
//! let mut aroon = Aroon::new(25);
//! aroon.calculate(&bars);
//! ```
use crate::model::Bar;
use crate::studies::{Indicator, IndicatorValue};
use crate::tokens::DESIGN_TOKENS;
use egui::Color32;
/// Aroon indicator
#[derive(Clone)]
pub struct Aroon {
period: usize,
values: Vec<IndicatorValue>,
up_color: Color32,
down_color: Color32,
visible: bool,
}
impl Aroon {
/// Create a new Aroon indicator
///
/// # Arguments
/// * `period` - Lookback period (typically 25)
pub fn new(period: usize) -> Self {
Self {
period: period.max(1),
values: Vec::new(),
up_color: DESIGN_TOKENS.semantic.extended.success,
down_color: DESIGN_TOKENS.semantic.extended.error,
visible: true,
}
}
/// Create with default param (25)
pub fn default_params() -> Self {
Self::new(25)
}
/// Set colors for up and down lines
pub fn with_colors(mut self, up: Color32, down: Color32) -> Self {
self.up_color = up;
self.down_color = down;
self
}
}
/// Construct with the conventional default parameters.
impl Default for Aroon {
fn default() -> Self {
Self::new(14)
}
}
impl Indicator for Aroon {
fn name(&self) -> &str {
"Aroon"
}
fn desc(&self) -> &str {
"Aroon - Trend identification indicator"
}
fn calculate(&mut self, data: &[Bar]) {
self.values.clear();
if data.is_empty() {
return;
}
for i in 0..data.len() {
if i < self.period {
self.values.push(IndicatorValue::None);
continue;
}
// Find highest high and lowest low positions in lookback
let start = i - self.period;
let mut highest_idx = start;
let mut lowest_idx = start;
let mut highest = data[start].high;
let mut lowest = data[start].low;
for j in (start + 1)..=i {
if data[j].high >= highest {
highest = data[j].high;
highest_idx = j;
}
if data[j].low <= lowest {
lowest = data[j].low;
lowest_idx = j;
}
}
// Days since highest/lowest
let days_since_high = i - highest_idx;
let days_since_low = i - lowest_idx;
// Calculate Aroon Up and Down (0-100 scale)
let aroon_up = ((self.period - days_since_high) as f64 / self.period as f64) * 100.0;
let aroon_down = ((self.period - days_since_low) as f64 / self.period as f64) * 100.0;
self.values
.push(IndicatorValue::Multiple(vec![aroon_up, aroon_down]));
}
}
fn values(&self) -> &[IndicatorValue] {
&self.values
}
fn colors(&self) -> Vec<Color32> {
vec![self.up_color, self.down_color]
}
fn set_colors(&mut self, colors: Vec<Color32>) {
if !colors.is_empty() {
self.up_color = colors[0];
}
if colors.len() > 1 {
self.down_color = colors[1];
}
}
fn is_overlay(&self) -> bool {
false // Aroon is a separate oscillator pane
}
fn line_cnt(&self) -> usize {
2 // Aroon Up and Aroon Down
}
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!("Aroon Up({})", self.period),
format!("Aroon Down({})", self.period),
]
}
}
#[cfg(test)]
mod tests {
use super::*;
use chrono::{Duration, Utc};
fn create_uptrending_bars() -> Vec<Bar> {
let start = Utc::now();
(0..50)
.map(|i| {
let price = 100.0 + i as f64 * 0.5;
Bar {
time: start + Duration::minutes(i * 5),
open: price - 0.2,
high: price + 1.0,
low: price - 1.0,
close: price,
volume: 1000.0,
}
})
.collect()
}
fn create_downtrending_bars() -> Vec<Bar> {
let start = Utc::now();
(0..50)
.map(|i| {
let price = 150.0 - i as f64 * 0.5;
Bar {
time: start + Duration::minutes(i * 5),
open: price + 0.2,
high: price + 1.0,
low: price - 1.0,
close: price,
volume: 1000.0,
}
})
.collect()
}
#[test]
fn test_aroon_range() {
let bars = create_uptrending_bars();
let mut aroon = Aroon::new(25);
aroon.calculate(&bars);
for value in aroon.values() {
if let IndicatorValue::Multiple(vals) = value {
assert!(
vals[0] >= 0.0 && vals[0] <= 100.0,
"Aroon Up should be 0-100"
);
assert!(
vals[1] >= 0.0 && vals[1] <= 100.0,
"Aroon Down should be 0-100"
);
}
}
}
#[test]
fn test_aroon_uptrend() {
let bars = create_uptrending_bars();
let mut aroon = Aroon::new(25);
aroon.calculate(&bars);
// In uptrend, Aroon Up should be higher than Aroon Down
if let Some(IndicatorValue::Multiple(vals)) = aroon.values().last() {
assert!(
vals[0] > vals[1],
"In uptrend, Aroon Up ({}) should be > Aroon Down ({})",
vals[0],
vals[1]
);
}
}
#[test]
fn test_aroon_downtrend() {
let bars = create_downtrending_bars();
let mut aroon = Aroon::new(25);
aroon.calculate(&bars);
// In downtrend, Aroon Down should be higher than Aroon Up
if let Some(IndicatorValue::Multiple(vals)) = aroon.values().last() {
assert!(
vals[1] > vals[0],
"In downtrend, Aroon Down ({}) should be > Aroon Up ({})",
vals[1],
vals[0]
);
}
}
#[test]
fn test_aroon_is_not_overlay() {
let aroon = Aroon::new(25);
assert!(!aroon.is_overlay());
}
#[test]
fn test_aroon_line_cnt() {
let aroon = Aroon::new(25);
assert_eq!(aroon.line_cnt(), 2);
}
#[test]
fn test_aroon_empty_data() {
let mut aroon = Aroon::new(25);
aroon.calculate(&[]);
assert!(aroon.values().is_empty());
}
}