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
//! Ehlers Following Adaptive Moving Average (FAMA).
use crate::error::Result;
use crate::indicators::mama::Mama;
use crate::traits::Indicator;
/// Scalar wrapper that exposes only the FAMA line from a [`Mama`] indicator.
///
/// FAMA (Following Adaptive Moving Average) is MAMA's lagging companion in
/// Ehlers' MESA construction. It uses half MAMA's adaptive alpha, so it
/// reacts later than MAMA — MAMA crossing above FAMA marks a trend
/// confirmation, MAMA below FAMA a reversal. See [`Mama`] for the joint
/// `(mama, fama)` output; this wrapper exposes the slow line as a plain
/// scalar indicator so it can be chained directly.
///
/// # Example
///
/// ```
/// use wickra_core::{Indicator, Fama};
///
/// let mut fama = Fama::new(0.5, 0.05).unwrap();
/// let mut last = None;
/// for i in 0..80 {
/// last = fama.update(100.0 + (f64::from(i) * 0.3).sin() * 5.0);
/// }
/// assert!(last.is_some());
/// ```
#[derive(Debug, Clone)]
pub struct Fama {
inner: Mama,
last_value: Option<f64>,
}
impl Fama {
/// Construct with the same `(fast_limit, slow_limit)` semantics as [`Mama`].
///
/// # Errors
///
/// Forwards [`Mama::new`]'s validation errors.
pub fn new(fast_limit: f64, slow_limit: f64) -> Result<Self> {
Ok(Self {
inner: Mama::new(fast_limit, slow_limit)?,
last_value: None,
})
}
/// Default `(0.5, 0.05)` parameters.
pub fn classic() -> Self {
Self {
inner: Mama::classic(),
last_value: None,
}
}
/// Configured `(fast_limit, slow_limit)`.
pub const fn limits(&self) -> (f64, f64) {
self.inner.limits()
}
/// Current FAMA value if available.
pub const fn value(&self) -> Option<f64> {
self.last_value
}
}
impl Indicator for Fama {
type Input = f64;
type Output = f64;
fn update(&mut self, input: f64) -> Option<f64> {
let v = self.inner.update(input)?.fama;
self.last_value = Some(v);
Some(v)
}
fn reset(&mut self) {
self.inner.reset();
self.last_value = None;
}
fn warmup_period(&self) -> usize {
self.inner.warmup_period()
}
fn is_ready(&self) -> bool {
self.last_value.is_some()
}
fn name(&self) -> &'static str {
"FAMA"
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::error::Error;
use crate::traits::BatchExt;
#[test]
fn rejects_invalid_limits() {
assert!(matches!(
Fama::new(0.0, 0.05),
Err(Error::InvalidPeriod { .. })
));
assert!(matches!(
Fama::new(0.05, 0.5),
Err(Error::InvalidPeriod { .. })
));
}
#[test]
fn new_with_valid_limits_constructs_via_mama() {
// `classic()` bypasses `new` by going through `Mama::classic`; this
// test exercises the happy-path `Ok(Self { inner: Mama::new(..)? })`
// arm so the `?` doesn't only collapse to the error path.
let mut fama = Fama::new(0.5, 0.05).expect("valid Mama limits");
assert_eq!(fama.limits(), (0.5, 0.05));
for i in 0..60 {
fama.update(100.0 + (f64::from(i) * 0.3).sin() * 5.0);
}
assert!(fama.value().is_some());
}
#[test]
fn accessors_and_metadata() {
let mut fama = Fama::classic();
assert_eq!(fama.limits(), (0.5, 0.05));
assert_eq!(fama.warmup_period(), 33);
assert_eq!(fama.name(), "FAMA");
assert!(!fama.is_ready());
for i in 0..60 {
fama.update(100.0 + (f64::from(i) * 0.3).sin() * 5.0);
}
assert!(fama.is_ready());
assert!(fama.value().is_some());
}
#[test]
fn batch_equals_streaming() {
let prices: Vec<f64> = (0..120)
.map(|i| 100.0 + (f64::from(i) * 0.25).cos() * 5.0)
.collect();
let mut a = Fama::classic();
let mut b = Fama::classic();
let batch = a.batch(&prices);
let streamed: Vec<_> = prices.iter().map(|p| b.update(*p)).collect();
assert_eq!(batch, streamed);
}
#[test]
fn ignores_non_finite_input() {
let mut fama = Fama::classic();
let prices: Vec<f64> = (0..100)
.map(|i| 100.0 + (f64::from(i) * 0.3).sin() * 5.0)
.collect();
fama.batch(&prices);
let before = fama.value();
assert!(before.is_some());
assert_eq!(fama.update(f64::NAN), before);
}
#[test]
fn reset_clears_state() {
let mut fama = Fama::classic();
let prices: Vec<f64> = (0..100)
.map(|i| 100.0 + (f64::from(i) * 0.3).sin() * 5.0)
.collect();
fama.batch(&prices);
assert!(fama.is_ready());
fama.reset();
assert!(!fama.is_ready());
}
}