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
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use crate::core::{IndicatorConfig, IndicatorInitializer, IndicatorInstance, IndicatorResult};
use crate::core::{Method, PeriodType, ValueType, OHLC};
use crate::methods::{Cross, HighestIndex, LowestIndex};
use std::marker::PhantomData;
#[derive(Debug, Clone, Copy)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Aroon {
pub period: PeriodType,
pub signal_zone: ValueType,
pub over_zone_period: PeriodType,
}
impl IndicatorConfig for Aroon {
fn validate(&self) -> bool {
self.signal_zone >= 0.0
&& self.signal_zone <= 1.0
&& self.period > 1
&& self.over_zone_period > 0
}
fn set(&mut self, name: &str, value: String) {
match name {
"signal_zone" => self.signal_zone = value.parse().unwrap(),
"over_zone_period" => self.over_zone_period = value.parse().unwrap(),
"period" => self.period = value.parse().unwrap(),
_ => {
dbg!(format!(
"Unknown attribute `{:}` with value `{:}` for `{:}`",
name,
value,
std::any::type_name::<Self>(),
));
}
};
}
fn size(&self) -> (u8, u8) {
(2, 3)
}
}
impl<T: OHLC> IndicatorInitializer<T> for Aroon {
type Instance = AroonInstance<T>;
fn init(self, candle: T) -> Self::Instance
where
Self: Sized,
{
let cfg = self;
Self::Instance {
lowest_index: LowestIndex::new(cfg.period, candle.low()),
highest_index: HighestIndex::new(cfg.period, candle.high()),
cross: Cross::default(),
uptrend: 0,
downtrend: 0,
cfg,
phantom: PhantomData::default(),
}
}
}
impl Default for Aroon {
fn default() -> Self {
Self {
signal_zone: 0.3,
period: 14,
over_zone_period: 7,
}
}
}
#[derive(Debug)]
pub struct AroonInstance<T: OHLC> {
cfg: Aroon,
lowest_index: LowestIndex,
highest_index: HighestIndex,
cross: Cross,
phantom: PhantomData<T>,
uptrend: isize,
downtrend: isize,
}
impl<T: OHLC> IndicatorInstance<T> for AroonInstance<T> {
type Config = Aroon;
fn name(&self) -> &'static str {
"Aroon"
}
fn config(&self) -> &Self::Config {
&self.cfg
}
fn next(&mut self, candle: T) -> IndicatorResult {
let highest_index = self.highest_index.next(candle.high());
let lowest_index = self.lowest_index.next(candle.low());
let aroon_up =
(self.cfg.period - highest_index) as ValueType / self.cfg.period as ValueType;
let aroon_down =
(self.cfg.period - lowest_index) as ValueType / self.cfg.period as ValueType;
let trend_signal = self.cross.next((aroon_up, aroon_down));
let edge_signal = (highest_index == 0) as i8 - (lowest_index == 0) as i8;
let is_up_over = (aroon_up >= (1.0 - self.cfg.signal_zone)) as isize;
let is_up_under = (aroon_up <= self.cfg.signal_zone) as isize;
let is_down_over = (aroon_down >= (1.0 - self.cfg.signal_zone)) as isize;
let is_down_under = (aroon_down <= self.cfg.signal_zone) as isize;
self.uptrend = (self.uptrend + 1) * is_up_over * is_down_under;
self.downtrend = (self.downtrend + 1) * is_down_over * is_up_under;
let trend_value =
(self.uptrend - self.downtrend) as ValueType / self.cfg.over_zone_period as ValueType;
IndicatorResult::new(
&[aroon_up, aroon_down],
&[trend_signal, edge_signal.into(), trend_value.into()],
)
}
}