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
//! Heikin-Ashi Oscillator — the (smoothed) Heikin-Ashi candle body as a zero-line oscillator.
use crate::error::{Error, Result};
use crate::indicators::ema::Ema;
use crate::indicators::heikin_ashi::HeikinAshi;
use crate::ohlcv::Candle;
use crate::traits::Indicator;
/// Heikin-Ashi Oscillator — the body of the [`HeikinAshi`](crate::HeikinAshi)
/// candle (`ha_close − ha_open`), optionally EMA-smoothed, as an oscillator around
/// zero.
///
/// ```text
/// body = ha_close − ha_open
/// HAO = EMA(body, period)
/// ```
///
/// A Heikin-Ashi candle is bullish when its close is above its open and bearish
/// when below; the size of that body measures conviction. Plotting the body as an
/// oscillator turns the visual HA colour/strength into a number: positive =
/// bullish HA candles, negative = bearish, and the magnitude is trend strength.
/// Smoothing the body with an EMA (`period`) damps single-bar noise so zero-line
/// crosses mark cleaner trend changes. With `period == 1` the oscillator is the raw
/// HA body.
///
/// The output is centred on zero (price units). The first value lands after
/// `period` inputs (the HA transform itself needs only one). Each `update` is O(1).
///
/// # Example
///
/// ```
/// use wickra_core::{Candle, Indicator, HeikinAshiOscillator};
///
/// let mut indicator = HeikinAshiOscillator::new(5).unwrap();
/// let mut last = None;
/// for i in 0..40 {
/// let base = 100.0 + f64::from(i);
/// let c = Candle::new(base, base + 1.0, base - 1.0, base + 0.5, 1_000.0, 0).unwrap();
/// last = indicator.update(c);
/// }
/// assert!(last.is_some());
/// ```
#[derive(Debug, Clone)]
pub struct HeikinAshiOscillator {
period: usize,
ha: HeikinAshi,
ema: Ema,
last: Option<f64>,
}
impl HeikinAshiOscillator {
/// Construct a Heikin-Ashi Oscillator with the given EMA smoothing `period`
/// (use `1` for the raw body).
///
/// # Errors
///
/// Returns [`Error::PeriodZero`] if `period == 0`.
pub fn new(period: usize) -> Result<Self> {
if period == 0 {
return Err(Error::PeriodZero);
}
Ok(Self {
period,
ha: HeikinAshi::new(),
ema: Ema::new(period)?,
last: None,
})
}
/// Configured smoothing period.
pub const fn period(&self) -> usize {
self.period
}
/// Current value if available.
pub const fn value(&self) -> Option<f64> {
self.last
}
}
impl Indicator for HeikinAshiOscillator {
type Input = Candle;
type Output = f64;
fn update(&mut self, candle: Candle) -> Option<f64> {
let ha = self.ha.update(candle).expect("HeikinAshi emits every bar");
let body = ha.close - ha.open;
let v = self.ema.update(body)?;
self.last = Some(v);
Some(v)
}
fn reset(&mut self) {
self.ha.reset();
self.ema.reset();
self.last = None;
}
fn warmup_period(&self) -> usize {
self.period
}
fn is_ready(&self) -> bool {
self.last.is_some()
}
fn name(&self) -> &'static str {
"HeikinAshiOscillator"
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::traits::BatchExt;
use approx::assert_relative_eq;
fn c(open: f64, high: f64, low: f64, close: f64) -> Candle {
Candle::new_unchecked(open, high, low, close, 1_000.0, 0)
}
#[test]
fn rejects_zero_period() {
assert!(matches!(
HeikinAshiOscillator::new(0),
Err(Error::PeriodZero)
));
}
#[test]
fn accessors_and_metadata() {
let h = HeikinAshiOscillator::new(5).unwrap();
assert_eq!(h.period(), 5);
assert_eq!(h.warmup_period(), 5);
assert_eq!(h.name(), "HeikinAshiOscillator");
assert!(!h.is_ready());
assert_eq!(h.value(), None);
}
#[test]
fn first_emission_at_warmup_period() {
let mut h = HeikinAshiOscillator::new(3).unwrap();
let candles: Vec<Candle> = (0..6)
.map(|i| {
let b = 100.0 + f64::from(i);
c(b, b + 1.0, b - 1.0, b + 0.5)
})
.collect();
let out = h.batch(&candles);
for v in out.iter().take(2) {
assert!(v.is_none());
}
assert!(out[2].is_some());
}
#[test]
fn uptrend_is_positive() {
let mut h = HeikinAshiOscillator::new(3).unwrap();
let candles: Vec<Candle> = (0..40)
.map(|i| {
let b = 100.0 + 2.0 * f64::from(i);
c(b, b + 1.0, b - 1.0, b + 1.5)
})
.collect();
let last = h.batch(&candles).into_iter().flatten().last().unwrap();
assert!(
last > 0.0,
"uptrend should give a positive HA body, got {last}"
);
}
#[test]
fn downtrend_is_negative() {
let mut h = HeikinAshiOscillator::new(3).unwrap();
let candles: Vec<Candle> = (0..40)
.map(|i| {
let b = 200.0 - 2.0 * f64::from(i);
c(b, b + 1.0, b - 1.0, b - 1.5)
})
.collect();
let last = h.batch(&candles).into_iter().flatten().last().unwrap();
assert!(
last < 0.0,
"downtrend should give a negative HA body, got {last}"
);
}
#[test]
fn flat_market_near_zero() {
let mut h = HeikinAshiOscillator::new(3).unwrap();
let last = h
.batch(&[c(100.0, 100.5, 99.5, 100.0); 30])
.into_iter()
.flatten()
.last()
.unwrap();
assert_relative_eq!(last, 0.0, epsilon = 1e-9);
}
#[test]
fn reset_clears_state() {
let mut h = HeikinAshiOscillator::new(3).unwrap();
h.batch(
&(0..10)
.map(|i| {
let b = 100.0 + f64::from(i);
c(b, b + 1.0, b - 1.0, b)
})
.collect::<Vec<_>>(),
);
assert!(h.is_ready());
h.reset();
assert!(!h.is_ready());
assert_eq!(h.value(), None);
assert_eq!(h.update(c(100.0, 101.0, 99.0, 100.0)), None);
}
#[test]
fn batch_equals_streaming() {
let candles: Vec<Candle> = (0..80)
.map(|i| {
let b = 100.0 + (f64::from(i) * 0.25).sin() * 9.0;
c(b, b + 1.0, b - 1.0, b + 0.3)
})
.collect();
let batch = HeikinAshiOscillator::new(5).unwrap().batch(&candles);
let mut b = HeikinAshiOscillator::new(5).unwrap();
let streamed: Vec<_> = candles.iter().map(|x| b.update(*x)).collect();
assert_eq!(batch, streamed);
}
}