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
//! Three-Line-Break bar builder — line-break chart segments driven by close prices.
use std::collections::VecDeque;
use crate::error::{Error, Result};
use crate::ohlcv::Candle;
use crate::traits::BarBuilder;
/// One completed line-break line.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct LineBreakBar {
/// Price where the line began (the previous line's far edge).
pub open: f64,
/// Price where the line ended (the new close that drew it).
pub close: f64,
/// `+1` for a rising line, `-1` for a falling line.
pub direction: i8,
}
/// Three-Line-Break bar builder using the classic close-based reversal rule.
///
/// A line-break chart draws a new line in the trend direction whenever the close
/// makes a new extreme, and only reverses when the close breaks the extreme of the
/// previous `lines` lines (three by default — hence "three-line break"). This filters
/// minor noise: a pullback that fails to exceed the last three lines is ignored
/// entirely, so the chart isolates meaningful reversals.
///
/// This is the **bar-builder** counterpart of the
/// [`ThreeLineBreak`](crate::ThreeLineBreak) indicator: the indicator reports the
/// current line *state* as a streaming value, whereas this builder emits each
/// completed line as a [`LineBreakBar`] so you can reconstruct the full line-break
/// chart. At most one line forms per candle, so [`BarBuilder::update`] returns either
/// an empty vector or a single bar.
///
/// Construction rules:
///
/// - The first candle seeds a reference close and prints nothing.
/// - The first subsequent move (up or down) draws the first line.
/// - In an up-trend a close above the last line's top extends it (a new up line); a
/// close below the lowest low of the last `lines` lines reverses to a down line.
/// The down-trend is symmetric.
///
/// # Example
///
/// ```
/// use wickra_core::{BarBuilder, Candle, ThreeLineBreakBars};
///
/// let flat = |price: f64| Candle::new(price, price, price, price, 1.0, 0).unwrap();
/// let mut bars = ThreeLineBreakBars::new(3).unwrap();
/// bars.update(flat(10.0)); // seed
/// let first = bars.update(flat(11.0)); // first up line
/// assert_eq!(first.len(), 1);
/// assert_eq!(first[0].direction, 1);
/// ```
#[derive(Debug, Clone)]
pub struct ThreeLineBreakBars {
lines: usize,
seed: Option<f64>,
recent: VecDeque<LineBreakBar>,
}
impl ThreeLineBreakBars {
/// Construct a line-break builder that reverses on a break of the last `lines`
/// lines (3 for the classic three-line break).
///
/// # Errors
///
/// Returns [`Error::PeriodZero`] if `lines == 0`.
pub fn new(lines: usize) -> Result<Self> {
if lines == 0 {
return Err(Error::PeriodZero);
}
Ok(Self {
lines,
seed: None,
recent: VecDeque::with_capacity(lines),
})
}
/// Configured number of lines a reversal must break.
pub const fn lines(&self) -> usize {
self.lines
}
/// Number of recent lines currently tracked for the reversal test.
pub fn tracked(&self) -> usize {
self.recent.len()
}
fn push_line(&mut self, bar: LineBreakBar) {
if self.recent.len() == self.lines {
self.recent.pop_front();
}
self.recent.push_back(bar);
}
fn lowest_low(&self) -> f64 {
self.recent
.iter()
.map(|bar| bar.open.min(bar.close))
.fold(f64::INFINITY, f64::min)
}
fn highest_high(&self) -> f64 {
self.recent
.iter()
.map(|bar| bar.open.max(bar.close))
.fold(f64::NEG_INFINITY, f64::max)
}
}
impl BarBuilder for ThreeLineBreakBars {
type Bar = LineBreakBar;
fn update(&mut self, candle: Candle) -> Vec<LineBreakBar> {
let close = candle.close;
let Some(last) = self.recent.back().copied() else {
// No line yet: seed, then draw the first line on the first move.
let Some(seed) = self.seed else {
self.seed = Some(close);
return Vec::new();
};
let bar = if close > seed {
LineBreakBar {
open: seed,
close,
direction: 1,
}
} else if close < seed {
LineBreakBar {
open: seed,
close,
direction: -1,
}
} else {
return Vec::new();
};
self.push_line(bar);
return vec![bar];
};
let new_bar = if last.direction > 0 {
if close > last.close {
Some(LineBreakBar {
open: last.close,
close,
direction: 1,
})
} else if close < self.lowest_low() {
Some(LineBreakBar {
open: last.close,
close,
direction: -1,
})
} else {
None
}
} else if close < last.close {
Some(LineBreakBar {
open: last.close,
close,
direction: -1,
})
} else if close > self.highest_high() {
Some(LineBreakBar {
open: last.close,
close,
direction: 1,
})
} else {
None
};
if let Some(bar) = new_bar {
self.push_line(bar);
vec![bar]
} else {
Vec::new()
}
}
fn reset(&mut self) {
self.seed = None;
self.recent.clear();
}
fn name(&self) -> &'static str {
"ThreeLineBreakBars"
}
}
#[cfg(test)]
mod tests {
use super::*;
use approx::assert_relative_eq;
fn flat(price: f64) -> Candle {
Candle::new(price, price, price, price, 1.0, 0).unwrap()
}
#[test]
fn rejects_zero_lines() {
assert!(matches!(ThreeLineBreakBars::new(0), Err(Error::PeriodZero)));
}
#[test]
fn accessors_and_metadata() {
let bars = ThreeLineBreakBars::new(3).unwrap();
assert_eq!(bars.lines(), 3);
assert_eq!(bars.tracked(), 0);
assert_eq!(bars.name(), "ThreeLineBreakBars");
}
#[test]
fn seed_then_first_line() {
let mut bars = ThreeLineBreakBars::new(3).unwrap();
assert!(bars.update(flat(10.0)).is_empty()); // seed
let first = bars.update(flat(11.0));
assert_eq!(first.len(), 1);
assert_eq!(first[0].direction, 1);
assert_relative_eq!(first[0].open, 10.0, epsilon = 1e-12);
assert_relative_eq!(first[0].close, 11.0, epsilon = 1e-12);
}
#[test]
fn new_high_extends_up_line() {
let mut bars = ThreeLineBreakBars::new(3).unwrap();
bars.update(flat(10.0));
bars.update(flat(11.0)); // line 1 up
let cont = bars.update(flat(12.0)); // new high -> extend
assert_eq!(cont.len(), 1);
assert_eq!(cont[0].direction, 1);
assert_relative_eq!(cont[0].open, 11.0, epsilon = 1e-12);
}
#[test]
fn small_pullback_prints_nothing() {
let mut bars = ThreeLineBreakBars::new(3).unwrap();
bars.update(flat(10.0));
bars.update(flat(11.0)); // line 1
bars.update(flat(12.0)); // line 2
bars.update(flat(13.0)); // line 3, lows are 10/11/12
assert!(bars.update(flat(10.5)).is_empty()); // not > 13, not < 10
}
#[test]
fn reversal_breaks_three_lines() {
let mut bars = ThreeLineBreakBars::new(3).unwrap();
bars.update(flat(10.0));
bars.update(flat(11.0)); // line 1, low 10
bars.update(flat(12.0)); // line 2, low 11
bars.update(flat(13.0)); // line 3, low 12
let rev = bars.update(flat(9.0)); // 9 < lowest low 10 -> reverse
assert_eq!(rev.len(), 1);
assert_eq!(rev[0].direction, -1);
assert_relative_eq!(rev[0].open, 13.0, epsilon = 1e-12);
assert_relative_eq!(rev[0].close, 9.0, epsilon = 1e-12);
}
#[test]
fn reset_clears_state() {
let mut bars = ThreeLineBreakBars::new(3).unwrap();
bars.update(flat(10.0));
bars.update(flat(11.0));
bars.reset();
assert_eq!(bars.tracked(), 0);
assert!(bars.update(flat(50.0)).is_empty()); // re-seeds
}
#[test]
fn flat_first_move_prints_nothing() {
let mut bars = ThreeLineBreakBars::new(3).unwrap();
assert!(bars.update(flat(10.0)).is_empty()); // seed
assert!(bars.update(flat(10.0)).is_empty()); // equal to seed -> no line
}
#[test]
fn first_line_down_then_down_trend_and_reversal() {
let mut bars = ThreeLineBreakBars::new(3).unwrap();
assert!(bars.update(flat(10.0)).is_empty()); // seed
let first = bars.update(flat(9.0)); // first line down
assert_eq!(first.len(), 1);
assert_eq!(first[0].direction, -1);
assert_relative_eq!(first[0].open, 10.0, epsilon = 1e-12);
assert_relative_eq!(first[0].close, 9.0, epsilon = 1e-12);
let cont = bars.update(flat(8.0)); // new low extends the down line
assert_eq!(cont.len(), 1);
assert_eq!(cont[0].direction, -1);
assert_relative_eq!(cont[0].open, 9.0, epsilon = 1e-12);
bars.update(flat(7.0)); // third down line; highs are 10/9/8
assert!(bars.update(flat(7.5)).is_empty()); // not < 7, not > highest high 10
let rev = bars.update(flat(11.0)); // > highest high 10 -> reverse up
assert_eq!(rev.len(), 1);
assert_eq!(rev[0].direction, 1);
assert_relative_eq!(rev[0].open, 7.0, epsilon = 1e-12);
}
#[test]
fn batch_concatenates_completed_lines() {
let mut bars = ThreeLineBreakBars::new(3).unwrap();
let candles = [flat(10.0), flat(11.0), flat(12.0), flat(13.0)];
let out = bars.batch(&candles);
// seed at 10, then three rising lines.
assert_eq!(out.len(), 3);
assert!(out.iter().all(|b| b.direction == 1));
}
}