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
//! `McGinley` Dynamic — self-adjusting moving average.
use std::collections::VecDeque;
use crate::error::{Error, Result};
use crate::traits::Indicator;
/// John `McGinley`'s "Dynamic" — a self-adjusting moving average that speeds up
/// in downtrends and slows down in uptrends to track price more closely than
/// a fixed-period MA.
///
/// The recurrence is
///
/// ```text
/// MD_t = MD_{t-1} + (price_t - MD_{t-1}) / (K * period * (price_t / MD_{t-1})^4)
/// ```
///
/// where `K = 0.6` is `McGinley`'s original constant. The fourth-power ratio
/// term shrinks the divisor when price falls below the indicator (faster
/// catch-up) and inflates it when price runs above (more smoothing). The
/// indicator is seeded with the simple average of the first `period` inputs.
///
/// Reference: John R. `McGinley` Jr., *Technical Analysis of Stocks &
/// Commodities*, 1990.
///
/// # Example
///
/// ```
/// use wickra_core::{Indicator, McGinleyDynamic};
///
/// let mut md = McGinleyDynamic::new(10).unwrap();
/// let mut last = None;
/// for i in 0..40 {
/// last = md.update(100.0 + f64::from(i));
/// }
/// assert!(last.is_some());
/// ```
#[derive(Debug, Clone)]
pub struct McGinleyDynamic {
period: usize,
seed: VecDeque<f64>,
seed_sum: f64,
current: Option<f64>,
}
/// `McGinley`'s original constant `K` in the recurrence denominator.
const K: f64 = 0.6;
impl McGinleyDynamic {
/// # Errors
/// Returns [`Error::PeriodZero`] if `period == 0`.
pub fn new(period: usize) -> Result<Self> {
if period == 0 {
return Err(Error::PeriodZero);
}
Ok(Self {
period,
seed: VecDeque::with_capacity(period),
seed_sum: 0.0,
current: None,
})
}
/// Configured period.
pub const fn period(&self) -> usize {
self.period
}
/// Current value if available.
pub const fn value(&self) -> Option<f64> {
self.current
}
}
impl Indicator for McGinleyDynamic {
type Input = f64;
type Output = f64;
fn update(&mut self, input: f64) -> Option<f64> {
if !input.is_finite() {
return self.current;
}
if let Some(prev) = self.current {
// The recurrence divides by `(price / prev)^4`; if either side is
// zero or negative the formula blows up, so we hold the previous
// value as a defensive fallback against degenerate price series.
if prev <= 0.0 || input <= 0.0 {
return self.current;
}
let ratio = input / prev;
let divisor = K * (self.period as f64) * ratio.powi(4);
let next = prev + (input - prev) / divisor;
self.current = Some(next);
} else {
self.seed.push_back(input);
self.seed_sum += input;
if self.seed.len() == self.period {
self.current = Some(self.seed_sum / self.period as f64);
}
}
self.current
}
fn reset(&mut self) {
self.seed.clear();
self.seed_sum = 0.0;
self.current = None;
}
fn warmup_period(&self) -> usize {
self.period
}
fn is_ready(&self) -> bool {
self.current.is_some()
}
fn name(&self) -> &'static str {
"McGinleyDynamic"
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::traits::BatchExt;
use approx::assert_relative_eq;
#[test]
fn rejects_zero_period() {
assert!(matches!(McGinleyDynamic::new(0), Err(Error::PeriodZero)));
}
#[test]
fn accessors_and_metadata() {
let mut md = McGinleyDynamic::new(10).unwrap();
assert_eq!(md.period(), 10);
assert_eq!(md.warmup_period(), 10);
assert_eq!(md.name(), "McGinleyDynamic");
assert_eq!(md.value(), None);
for i in 1..=10 {
md.update(f64::from(i));
}
assert!(md.value().is_some());
}
#[test]
fn constant_series_yields_the_constant() {
// ratio = 1, so the recurrence collapses to MD + 0 / divisor = MD.
let mut md = McGinleyDynamic::new(5).unwrap();
let out = md.batch(&[42.0_f64; 30]);
for v in out.iter().skip(4).flatten() {
assert_relative_eq!(*v, 42.0, epsilon = 1e-12);
}
}
#[test]
fn warmup_emits_first_value_at_period() {
let mut md = McGinleyDynamic::new(3).unwrap();
// Seed = SMA([10, 20, 30]) = 20.0.
assert_eq!(md.update(10.0), None);
assert_eq!(md.update(20.0), None);
assert_eq!(md.update(30.0), Some(20.0));
}
#[test]
fn reference_value_recurrence() {
// Period 3, seed = SMA([10, 20, 30]) = 20.0. Then on price = 40.0:
// ratio = 40 / 20 = 2
// divisor = 0.6 * 3 * 2^4 = 0.6 * 3 * 16 = 28.8
// next = 20 + (40 - 20) / 28.8 = 20.694444...
let mut md = McGinleyDynamic::new(3).unwrap();
md.batch(&[10.0_f64, 20.0, 30.0]);
let v = md.update(40.0).unwrap();
let expected = 20.0 + 20.0 / (0.6 * 3.0 * 16.0);
assert_relative_eq!(v, expected, epsilon = 1e-12);
}
#[test]
fn batch_equals_streaming() {
let prices: Vec<f64> = (1..=80)
.map(|i| 100.0 + (f64::from(i) * 0.2).sin() * 5.0)
.collect();
let mut a = McGinleyDynamic::new(10).unwrap();
let mut b = McGinleyDynamic::new(10).unwrap();
assert_eq!(
a.batch(&prices),
prices.iter().map(|p| b.update(*p)).collect::<Vec<_>>()
);
}
#[test]
fn reset_clears_state() {
let mut md = McGinleyDynamic::new(5).unwrap();
md.batch(&(1..=30).map(f64::from).collect::<Vec<_>>());
assert!(md.is_ready());
md.reset();
assert!(!md.is_ready());
assert_eq!(md.update(1.0), None);
}
#[test]
fn ignores_non_finite_input() {
let mut md = McGinleyDynamic::new(3).unwrap();
md.batch(&[10.0_f64, 20.0, 30.0]);
let before = md.value().unwrap();
assert_eq!(md.update(f64::NAN), Some(before));
assert_eq!(md.update(f64::INFINITY), Some(before));
}
#[test]
fn holds_value_when_input_is_non_positive() {
// Defensive: a zero or negative price would make the (price/prev)^4
// divisor zero or otherwise blow up; the recurrence holds steady.
let mut md = McGinleyDynamic::new(3).unwrap();
md.batch(&[10.0_f64, 20.0, 30.0]);
let before = md.value().unwrap();
assert_eq!(md.update(0.0), Some(before));
assert_eq!(md.update(-5.0), Some(before));
// Once a positive price arrives the recurrence resumes normally.
let after = md.update(40.0).unwrap();
assert!(after > before);
}
}