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
//! Andrews Pitchfork — median line and parallels off the last three swing pivots.
use std::collections::VecDeque;
use crate::error::{Error, Result};
use crate::ohlcv::Candle;
use crate::traits::Indicator;
/// Output of [`AndrewsPitchfork`]: the three pitchfork lines projected to the
/// current bar.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct AndrewsPitchforkOutput {
/// The median line — from the handle pivot through the midpoint of the other two.
pub median: f64,
/// The upper parallel (through the higher of the two anchor pivots).
pub upper: f64,
/// The lower parallel (through the lower of the two anchor pivots).
pub lower: f64,
}
/// A confirmed swing pivot: its bar index and price.
#[derive(Debug, Clone, Copy)]
struct Pivot {
index: f64,
price: f64,
is_high: bool,
}
/// Andrews Pitchfork — Alan Andrews' median-line tool drawn from the three most
/// recent **swing pivots**, projected forward to the current bar.
///
/// ```text
/// detect alternating swing highs/lows with a `strength`-bar fractal
/// P0 = handle (oldest of the last three), P1, P2 = the next two
/// M = midpoint of P1 and P2
/// median(t) = P0 + slope·(t − t0) slope = (M − P0) / (M_t − t0)
/// upper / lower = median(t) offset by the vertical gap to the higher / lower anchor
/// ```
///
/// The pitchfork projects a "fork" of three parallel lines: a central **median
/// line** drawn from a starting pivot through the midpoint of a later swing, plus
/// two parallels passing through that swing's high and low. Price tends to
/// oscillate around the median line and find support/resistance at the parallels.
/// This streaming version detects the pivots automatically with a symmetric
/// fractal of half-width `strength` (so each pivot is confirmed `strength` bars
/// late) and keeps the three most recent alternating swings.
///
/// Because it depends on swing structure, readiness is **data-dependent**: the
/// first output appears once three alternating pivots have been confirmed.
/// `warmup_period` returns the minimum bars to confirm a single pivot. Each
/// `update` is O(`strength`).
///
/// # Example
///
/// ```
/// use wickra_core::{Candle, Indicator, AndrewsPitchfork};
///
/// let mut indicator = AndrewsPitchfork::new(2).unwrap();
/// let mut last = None;
/// for i in 0..120 {
/// let base = 100.0 + (f64::from(i) * 0.4).sin() * 10.0;
/// let c = Candle::new(base, base + 1.0, base - 1.0, base, 1_000.0, 0).unwrap();
/// last = indicator.update(c);
/// }
/// // A swinging series eventually establishes a pitchfork.
/// let _ = last;
/// ```
#[derive(Debug, Clone)]
pub struct AndrewsPitchfork {
strength: usize,
window: VecDeque<Candle>,
pivots: Vec<Pivot>,
count: usize,
last: Option<AndrewsPitchforkOutput>,
}
impl AndrewsPitchfork {
/// Construct an Andrews Pitchfork with the given fractal `strength` (bars on
/// each side of a pivot).
///
/// # Errors
///
/// Returns [`Error::PeriodZero`] if `strength == 0`.
pub fn new(strength: usize) -> Result<Self> {
if strength == 0 {
return Err(Error::PeriodZero);
}
Ok(Self {
strength,
window: VecDeque::with_capacity(2 * strength + 1),
pivots: Vec::new(),
count: 0,
last: None,
})
}
/// Configured fractal strength.
pub const fn strength(&self) -> usize {
self.strength
}
/// Current value if available.
pub const fn value(&self) -> Option<AndrewsPitchforkOutput> {
self.last
}
/// Record a freshly confirmed pivot, keeping the last three alternating swings.
fn record_pivot(&mut self, pivot: Pivot) {
if let Some(last) = self.pivots.last_mut() {
if last.is_high == pivot.is_high {
// Same kind: keep the more extreme one (and its index).
let more_extreme = if pivot.is_high {
pivot.price > last.price
} else {
pivot.price < last.price
};
if more_extreme {
*last = pivot;
}
return;
}
}
self.pivots.push(pivot);
if self.pivots.len() > 3 {
self.pivots.remove(0);
}
}
fn project(&self, tc: f64) -> Option<AndrewsPitchforkOutput> {
let [p0, p1, p2] = self.pivots.as_slice() else {
return None;
};
let mid_t = f64::midpoint(p1.index, p2.index);
let mid_p = f64::midpoint(p1.price, p2.price);
let slope = (mid_p - p0.price) / (mid_t - p0.index);
let median = p0.price + slope * (tc - p0.index);
let off1 = p1.price - (p0.price + slope * (p1.index - p0.index));
let off2 = p2.price - (p0.price + slope * (p2.index - p0.index));
Some(AndrewsPitchforkOutput {
median,
upper: median + off1.max(off2),
lower: median + off1.min(off2),
})
}
}
impl Indicator for AndrewsPitchfork {
type Input = Candle;
type Output = AndrewsPitchforkOutput;
fn update(&mut self, candle: Candle) -> Option<AndrewsPitchforkOutput> {
self.count += 1;
let span = 2 * self.strength + 1;
if self.window.len() == span {
self.window.pop_front();
}
self.window.push_back(candle);
if self.window.len() == span {
let center = self.window[self.strength];
let is_high = self
.window
.iter()
.enumerate()
.all(|(i, c)| i == self.strength || c.high < center.high);
let is_low = self
.window
.iter()
.enumerate()
.all(|(i, c)| i == self.strength || c.low > center.low);
// Absolute index of the center bar (1-based count minus the right span).
let center_index = (self.count - 1 - self.strength) as f64;
if is_high && !is_low {
self.record_pivot(Pivot {
index: center_index,
price: center.high,
is_high: true,
});
} else if is_low && !is_high {
self.record_pivot(Pivot {
index: center_index,
price: center.low,
is_high: false,
});
}
}
let tc = (self.count - 1) as f64;
if let Some(out) = self.project(tc) {
self.last = Some(out);
return Some(out);
}
None
}
fn reset(&mut self) {
self.window.clear();
self.pivots.clear();
self.count = 0;
self.last = None;
}
fn warmup_period(&self) -> usize {
2 * self.strength + 1
}
fn is_ready(&self) -> bool {
self.last.is_some()
}
fn name(&self) -> &'static str {
"AndrewsPitchfork"
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::traits::BatchExt;
fn c(high: f64, low: f64) -> Candle {
Candle::new_unchecked(
f64::midpoint(high, low),
high,
low,
f64::midpoint(high, low),
1_000.0,
0,
)
}
/// A clean zig-zag that prints alternating swing highs and lows.
fn zigzag() -> Vec<Candle> {
let mut out = Vec::new();
for i in 0..120 {
let base = 100.0 + (f64::from(i) * 0.5).sin() * 10.0;
out.push(c(base + 1.0, base - 1.0));
}
out
}
#[test]
fn rejects_zero_strength() {
assert!(matches!(AndrewsPitchfork::new(0), Err(Error::PeriodZero)));
}
#[test]
fn accessors_and_metadata() {
let p = AndrewsPitchfork::new(2).unwrap();
assert_eq!(p.strength(), 2);
assert_eq!(p.warmup_period(), 5);
assert_eq!(p.name(), "AndrewsPitchfork");
assert!(!p.is_ready());
assert_eq!(p.value(), None);
}
#[test]
fn none_before_three_pivots() {
let mut p = AndrewsPitchfork::new(2).unwrap();
// Too few bars to ever confirm three alternating pivots.
let out = p.batch(&[c(101.0, 99.0), c(102.0, 100.0), c(101.0, 99.0)]);
assert!(out.iter().all(Option::is_none));
}
#[test]
fn eventually_emits_on_swings() {
let mut p = AndrewsPitchfork::new(2).unwrap();
let out = p.batch(&zigzag());
assert!(
out.iter().any(Option::is_some),
"a swinging series should form a pitchfork"
);
assert!(p.is_ready());
}
#[test]
fn upper_at_or_above_lower() {
let mut p = AndrewsPitchfork::new(2).unwrap();
for o in p.batch(&zigzag()).into_iter().flatten() {
assert!(
o.upper >= o.lower,
"upper {} below lower {}",
o.upper,
o.lower
);
}
}
#[test]
fn reset_clears_state() {
let mut p = AndrewsPitchfork::new(2).unwrap();
p.batch(&zigzag());
assert!(p.is_ready());
p.reset();
assert!(!p.is_ready());
assert_eq!(p.value(), None);
assert_eq!(p.strength(), 2);
}
#[test]
fn record_pivot_keeps_more_extreme_same_kind() {
let mut p = AndrewsPitchfork::new(2).unwrap();
p.record_pivot(Pivot {
index: 0.0,
price: 100.0,
is_high: true,
});
// A higher high of the same kind replaces the stored one.
p.record_pivot(Pivot {
index: 1.0,
price: 105.0,
is_high: true,
});
assert_eq!(p.pivots.len(), 1);
assert_eq!(p.pivots[0].price, 105.0);
// A lower high of the same kind is ignored.
p.record_pivot(Pivot {
index: 2.0,
price: 102.0,
is_high: true,
});
assert_eq!(p.pivots.len(), 1);
assert_eq!(p.pivots[0].price, 105.0);
// A low pivot of the other kind is appended.
p.record_pivot(Pivot {
index: 3.0,
price: 90.0,
is_high: false,
});
assert_eq!(p.pivots.len(), 2);
// A lower low of the same kind replaces the stored low.
p.record_pivot(Pivot {
index: 4.0,
price: 85.0,
is_high: false,
});
assert_eq!(p.pivots[1].price, 85.0);
// A higher low of the same kind is ignored.
p.record_pivot(Pivot {
index: 5.0,
price: 88.0,
is_high: false,
});
assert_eq!(p.pivots[1].price, 85.0);
}
#[test]
fn batch_equals_streaming() {
let candles = zigzag();
let batch = AndrewsPitchfork::new(2).unwrap().batch(&candles);
let mut b = AndrewsPitchfork::new(2).unwrap();
let streamed: Vec<_> = candles.iter().map(|x| b.update(*x)).collect();
assert_eq!(batch, streamed);
}
}