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
//! Wedge (rising / falling) reversal chart pattern.
use crate::indicators::pattern_swing::{recent_legs, SwingTracker, SWING_THRESHOLD};
use crate::ohlcv::Candle;
use crate::traits::Indicator;
/// Wedge — a pattern where both trendlines slope the same way but converge,
/// signalling exhaustion of the prevailing move.
///
/// Built on confirmed swing pivots (`SWING_THRESHOLD` = 5%); evaluated from the
/// last two swing highs and lows:
///
/// ```text
/// rising wedge : highs rising AND lows rising, lows rising faster → -1 (bearish)
/// falling wedge : highs falling AND lows falling, highs falling faster → +1 (bullish)
/// ```
///
/// Convergence is the key: in a rising wedge the lower trendline climbs faster
/// than the upper (the range narrows from below); in a falling wedge the upper
/// trendline drops faster than the lower. Output is `+1.0` / `-1.0` / `0.0`;
/// never `None`.
#[derive(Debug, Clone)]
pub struct Wedge {
swing: SwingTracker,
has_emitted: bool,
}
impl Wedge {
/// Construct a new Wedge detector.
pub const fn new() -> Self {
Self {
swing: SwingTracker::new(SWING_THRESHOLD, 4),
has_emitted: false,
}
}
}
impl Default for Wedge {
fn default() -> Self {
Self::new()
}
}
impl Indicator for Wedge {
type Input = Candle;
type Output = f64;
#[inline]
fn update(&mut self, candle: Candle) -> Option<f64> {
let advanced = self.swing.update(candle);
let pivots = self.swing.pivots();
// Too few pivots to form the shape at all: the indicator cannot
// judge yet, which is what `None` means.
if pivots.len() < 4 {
return None;
}
self.has_emitted = true;
// Armed, but this bar did not close a new pivot, so there is
// nothing new to match against.
if !advanced {
return Some(0.0);
}
let (high_old, high_new, low_old, low_new) = recent_legs(pivots);
let high_slope = high_new - high_old;
let low_slope = low_new - low_old;
// Rising wedge: both lines slope up, lower line steeper (converging) → bearish.
if high_slope > 0.0 && low_slope > 0.0 && low_slope > high_slope {
return Some(-1.0);
}
// Falling wedge: both lines slope down, upper line steeper → bullish.
if high_slope < 0.0 && low_slope < 0.0 && high_slope < low_slope {
return Some(1.0);
}
Some(0.0)
}
fn reset(&mut self) {
self.swing.reset();
self.has_emitted = false;
}
#[inline]
fn warmup_period(&self) -> usize {
// Four confirmed pivots; the earliest confirmation of the fourth is bar 5.
5
}
#[inline]
fn is_ready(&self) -> bool {
self.has_emitted
}
#[inline]
fn name(&self) -> &'static str {
"Wedge"
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::indicators::pattern_swing::candles_for_pivots;
use crate::traits::BatchExt;
fn run(pivots: &[f64]) -> Vec<f64> {
let mut indicator = Wedge::new();
candles_for_pivots(pivots)
.into_iter()
.filter_map(|c| indicator.update(c))
.collect()
}
#[test]
fn accessors_and_metadata() {
let indicator = Wedge::new();
assert_eq!(indicator.name(), "Wedge");
assert_eq!(indicator.warmup_period(), 5);
assert!(!indicator.is_ready());
assert!(!Wedge::default().is_ready());
}
#[test]
fn rising_wedge_is_minus_one() {
// Highs 100 → 103 (+3), lows 90 → 94 (+4, steeper) → rising wedge.
let out = run(&[110.0, 90.0, 100.0, 94.0, 103.0]);
assert_eq!(*out.last().unwrap(), -1.0);
}
#[test]
fn falling_wedge_is_plus_one() {
// Highs 120 → 106 (-14, steeper), lows 100 → 99 (-1) → falling wedge.
let out = run(&[120.0, 100.0, 106.0, 99.0]);
assert_eq!(*out.last().unwrap(), 1.0);
}
#[test]
fn diverging_swings_are_not_a_wedge() {
// Rising highs but falling lows (broadening) → no wedge.
let out = run(&[110.0, 100.0, 130.0, 80.0]);
assert_eq!(*out.last().unwrap(), 0.0);
}
#[test]
fn reset_clears_state() {
let mut indicator = Wedge::new();
for c in candles_for_pivots(&[110.0, 90.0, 100.0]) {
let _ = indicator.update(c);
}
indicator.reset();
assert!(!indicator.is_ready());
let c = Candle::new(99.5, 100.0, 99.5, 99.5, 1.0, 0).unwrap();
assert_eq!(indicator.update(c), None);
}
#[test]
fn batch_equals_streaming() {
let candles = candles_for_pivots(&[110.0, 90.0, 100.0, 94.0, 103.0]);
let mut a = Wedge::new();
let mut b = Wedge::new();
assert_eq!(
a.batch(&candles),
candles.iter().map(|x| b.update(*x)).collect::<Vec<_>>()
);
}
}