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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
//! Parabolic SAR (Wilder).
use crate::error::{Error, Result};
use crate::ohlcv::Candle;
use crate::traits::Indicator;
/// Trade direction in the SAR state machine.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Trend {
Up,
Down,
}
/// Parabolic Stop And Reverse.
///
/// Implementation follows Wilder's original recursion: each step computes a new
/// SAR from the previous SAR, extreme point (EP) and acceleration factor (AF);
/// the trend flips when price crosses the SAR.
///
/// # Example
///
/// ```
/// use wickra_core::{Candle, Indicator, Psar};
///
/// let mut indicator = Psar::new(0.02, 0.02, 0.2).unwrap();
/// let mut last = None;
/// for i in 0..80 {
/// let base = 100.0 + f64::from(i);
/// let candle =
/// Candle::new(base, base + 2.0, base - 2.0, base + 1.0, 10.0, i64::from(i)).unwrap();
/// last = indicator.update(candle);
/// }
/// assert!(last.is_some());
/// ```
#[derive(Debug, Clone)]
pub struct Psar {
af_start: f64,
af_step: f64,
af_max: f64,
/// `true` once the first candle has been observed and the seed values
/// (`prev_high`, `prev_low`, `sar`, `ep`) are valid. `false` is the
/// constructor / `reset()` state in which the compute-fields hold
/// `f64::NAN` sentinels.
initialised: bool,
/// `true` once `update` has returned the first `Some(sar)`. Drives
/// [`Indicator::is_ready`] so it matches the convention of every other
/// indicator: `is_ready() == true` ↔ the most recent `update` produced
/// (or could produce) a real value. PSAR's seed candle returns `None`
/// while `initialised` flips to `true`, which is why `is_ready` cannot
/// just mirror `initialised`.
has_emitted: bool,
prev_high: f64,
prev_low: f64,
trend: Trend,
sar: f64,
ep: f64,
af: f64,
}
impl Psar {
/// Construct PSAR with explicit acceleration parameters.
///
/// # Errors
/// Returns [`Error::NonPositiveMultiplier`] / [`Error::InvalidPeriod`] for invalid params.
pub fn new(af_start: f64, af_step: f64, af_max: f64) -> Result<Self> {
if !af_start.is_finite() || !af_step.is_finite() || !af_max.is_finite() {
return Err(Error::NonPositiveMultiplier);
}
if af_start <= 0.0 || af_step <= 0.0 || af_max <= 0.0 {
return Err(Error::NonPositiveMultiplier);
}
if af_start > af_max {
return Err(Error::InvalidPeriod {
message: "af_start must be <= af_max",
});
}
Ok(Self {
af_start,
af_step,
af_max,
initialised: false,
has_emitted: false,
// NaN sentinels: any read of these fields before the seed candle
// overwrites them is a logic bug. The `initialised` flag gates
// every read, and the `debug_assert!` in `update` makes the
// invariant explicit so a future refactor cannot silently treat a
// sentinel as a real price.
prev_high: f64::NAN,
prev_low: f64::NAN,
trend: Trend::Up,
sar: f64::NAN,
ep: f64::NAN,
af: af_start,
})
}
/// Wilder's defaults: `(0.02, 0.02, 0.20)`.
pub fn classic() -> Self {
Self::new(0.02, 0.02, 0.20).expect("classic PSAR params are valid")
}
}
impl Indicator for Psar {
type Input = Candle;
type Output = f64;
fn update(&mut self, candle: Candle) -> Option<f64> {
if !self.initialised {
// Seed on the first candle; the first SAR is emitted on the second.
// The initial trend is assumed Up — PSAR's reversal logic flips it
// within the first few bars if the market is actually falling.
self.prev_high = candle.high;
self.prev_low = candle.low;
self.sar = candle.low;
self.ep = candle.high;
self.trend = Trend::Up;
self.af = self.af_start;
self.initialised = true;
// `has_emitted` stays false — this is the seed bar; the first
// `Some` lands on the next call.
return None;
}
// After `initialised` flips to `true`, every compute field is guaranteed
// finite. This guards against a future refactor that changes the seed
// gate but leaves a NaN sentinel reachable.
debug_assert!(
self.prev_high.is_finite()
&& self.prev_low.is_finite()
&& self.sar.is_finite()
&& self.ep.is_finite(),
"PSAR seed state must be finite once initialised"
);
// Predicted SAR for this period (before clamping to prior two extremes).
let mut new_sar = self.sar + self.af * (self.ep - self.sar);
// Wilder rule: SAR cannot penetrate today's or yesterday's range.
let prev_h = self.prev_high;
let prev_l = self.prev_low;
new_sar = match self.trend {
Trend::Up => new_sar.min(prev_l).min(candle.low),
Trend::Down => new_sar.max(prev_h).max(candle.high),
};
let mut output_sar = new_sar;
// Check for trend reversal.
let reversed = match self.trend {
Trend::Up => candle.low <= new_sar,
Trend::Down => candle.high >= new_sar,
};
if reversed {
// Flip trend, reset AF and EP, place SAR at prior EP.
output_sar = self.ep;
self.trend = match self.trend {
Trend::Up => Trend::Down,
Trend::Down => Trend::Up,
};
self.ep = match self.trend {
Trend::Up => candle.high,
Trend::Down => candle.low,
};
self.af = self.af_start;
} else {
// Update EP and AF if a new extreme has been reached.
match self.trend {
Trend::Up => {
if candle.high > self.ep {
self.ep = candle.high;
self.af = (self.af + self.af_step).min(self.af_max);
}
}
Trend::Down => {
if candle.low < self.ep {
self.ep = candle.low;
self.af = (self.af + self.af_step).min(self.af_max);
}
}
}
}
self.sar = output_sar;
self.prev_high = candle.high;
self.prev_low = candle.low;
self.has_emitted = true;
Some(output_sar)
}
fn reset(&mut self) {
// Restore every field to its constructor state. The compute fields
// return to `f64::NAN` sentinels so a future refactor that reads them
// before re-seeding cannot silently treat `0.0` as a real price.
self.initialised = false;
self.has_emitted = false;
self.prev_high = f64::NAN;
self.prev_low = f64::NAN;
self.trend = Trend::Up;
self.sar = f64::NAN;
self.ep = f64::NAN;
self.af = self.af_start;
}
fn warmup_period(&self) -> usize {
2
}
fn is_ready(&self) -> bool {
// Match the convention of every other indicator: `is_ready` flips to
// `true` only once a real value has been returned. The previous
// implementation returned `self.initialised`, which is `true` *after*
// the seed candle (which itself returns `None`) — so a streaming
// consumer that wrote `if ind.is_ready() { use(ind.update(c)?) }`
// would hit a `None` it didn't expect. (Audit finding R6.)
self.has_emitted
}
fn name(&self) -> &'static str {
"PSAR"
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::traits::BatchExt;
fn c(h: f64, l: f64, cl: f64) -> Candle {
Candle::new(cl, h, l, cl, 1.0, 0).unwrap()
}
#[test]
fn first_candle_returns_none() {
let mut psar = Psar::classic();
assert_eq!(psar.update(c(11.0, 9.0, 10.0)), None);
}
#[test]
fn pure_uptrend_sar_below_lows() {
let candles: Vec<Candle> = (0..40)
.map(|i| {
let base = 100.0 + f64::from(i);
c(base + 0.5, base - 0.5, base)
})
.collect();
let mut psar = Psar::classic();
// `all()` with `is_none_or` keeps every reachable arm on the hot path —
// the previous filter_map / violation-Vec construction had a cold
// "violation found" tuple branch that was unreachable on a clean
// uptrend, leaving its line uncovered by Codecov.
let ok = psar
.batch(&candles)
.iter()
.enumerate()
.all(|(i, sar)| sar.is_none_or(|s| s <= candles[i].low + 1e-9));
assert!(ok, "SAR sat above a candle's low on a pure uptrend");
}
#[test]
fn pure_downtrend_sar_above_highs() {
let candles: Vec<Candle> = (0..40)
.rev()
.map(|i| {
let base = 100.0 + f64::from(i);
c(base + 0.5, base - 0.5, base)
})
.collect();
let mut psar = Psar::classic();
// After the trend establishes downward, SAR should sit above highs.
// Same `all()` + `is_none_or` shape as `pure_uptrend_sar_below_lows`
// so the violation-tuple branch never appears as a cold path.
let ok = psar
.batch(&candles)
.iter()
.enumerate()
.skip(5)
.all(|(i, sar)| sar.is_none_or(|s| s >= candles[i].high - 1e-9));
assert!(ok, "SAR sat below a candle's high on a pure downtrend");
}
#[test]
fn batch_equals_streaming() {
let candles: Vec<Candle> = (0..60)
.map(|i| {
let m = 100.0 + (f64::from(i) * 0.3).sin() * 8.0;
c(m + 1.0, m - 1.0, m)
})
.collect();
let mut a = Psar::classic();
let mut b = Psar::classic();
assert_eq!(
a.batch(&candles),
candles.iter().map(|x| b.update(*x)).collect::<Vec<_>>()
);
}
/// Cover the Indicator-impl `warmup_period` (206-208) and `name`
/// (220-222). PSAR's warmup is the constant 2 (seed candle + first
/// emitting candle); the name is the literal "PSAR".
#[test]
fn accessors_and_metadata() {
let psar = Psar::classic();
assert_eq!(psar.warmup_period(), 2);
assert_eq!(psar.name(), "PSAR");
}
#[test]
fn rejects_invalid_params() {
assert!(Psar::new(0.0, 0.02, 0.20).is_err());
assert!(Psar::new(0.02, 0.0, 0.20).is_err());
assert!(Psar::new(0.30, 0.02, 0.20).is_err());
assert!(Psar::new(f64::NAN, 0.02, 0.20).is_err());
}
#[test]
fn is_ready_only_after_first_some_value() {
// Audit R6: the previous implementation flipped `is_ready` to true on
// the seed candle (which returns `None`), making the convention
// `is_ready == last_value.is_some()` a lie. The new gate is
// `has_emitted`, set when `update` returns its first `Some`.
let mut psar = Psar::classic();
assert!(!psar.is_ready(), "fresh PSAR must not be ready");
let first = psar.update(c(11.0, 9.0, 10.0));
assert!(first.is_none(), "seed candle returns None by design");
assert!(
!psar.is_ready(),
"is_ready must stay false until a Some value is produced"
);
let second = psar.update(c(12.0, 10.0, 11.0));
assert!(second.is_some(), "second candle must emit");
assert!(
psar.is_ready(),
"is_ready must flip to true once a real value has been returned"
);
}
#[test]
fn reset_allows_clean_reuse() {
let candles: Vec<Candle> = (0..40)
.map(|i| {
let base = 100.0 + f64::from(i);
c(base + 0.5, base - 0.5, base)
})
.collect();
let mut psar = Psar::classic();
let first = psar.batch(&candles);
assert!(psar.is_ready());
psar.reset();
assert!(!psar.is_ready());
// A reset instance must reproduce a pristine run bit for bit.
let second = psar.batch(&candles);
assert_eq!(first, second);
}
}