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
//! Long Line candlestick pattern.
use crate::error::{Error, Result};
use crate::ohlcv::Candle;
use crate::traits::Indicator;
use std::collections::VecDeque;
/// Long Line — a single candle whose range is *longer* than the recent average and
/// whose body dominates that range (a solid directional bar). Because "long" only
/// has meaning relative to recent activity, the detector compares each candle's
/// range against a rolling average of the previous `period` ranges.
///
/// ```text
/// avg = mean range of the previous `period` candles
/// long line = range > avg AND |close − open| >= 0.5 * range
/// white -> +1.0, black -> −1.0
/// ```
///
/// Output is `+1.0` (long white line), `−1.0` (long black line), or `0.0`
/// otherwise. The first `period` candles return `0.0` while the rolling average
/// fills. `period` defaults to `5` and must be at least `1`. This rolling baseline
/// is the one place the family departs from a purely intra-candle rule, since a
/// short/long classification is inherently scale-relative. Pattern-shape check
/// only — no trend filter is applied; combine with a trend indicator for
/// actionable signals.
///
/// # Signed ±1 encoding
///
/// This detector emits the uniform candlestick sign convention shared across the
/// pattern family — `+1.0` bullish, `−1.0` bearish, `0.0` no pattern — so it
/// drops straight into a machine-learning feature matrix as a single dimension.
///
/// # Example
///
/// ```
/// use wickra_core::{Candle, Indicator, LongLine};
///
/// let mut indicator = LongLine::new();
/// // Five quiet bars fill the rolling average.
/// for ts in 0..5 {
/// indicator.update(Candle::new(10.0, 10.5, 9.5, 10.2, 1.0, ts).unwrap());
/// }
/// // A wide solid white bar is a long white line.
/// let out = indicator
/// .update(Candle::new(10.0, 13.0, 9.9, 12.9, 1.0, 5).unwrap());
/// assert_eq!(out, Some(1.0));
/// ```
#[derive(Debug, Clone)]
pub struct LongLine {
period: usize,
ranges: VecDeque<f64>,
}
impl Default for LongLine {
fn default() -> Self {
Self::new()
}
}
impl LongLine {
/// Construct a Long Line detector with the default 5-candle rolling average.
pub const fn new() -> Self {
Self {
period: 5,
ranges: VecDeque::new(),
}
}
/// Construct a Long Line detector with a custom averaging period.
///
/// `period` must be at least `1`.
pub fn with_period(period: usize) -> Result<Self> {
if period == 0 {
return Err(Error::PeriodZero);
}
Ok(Self {
period,
ranges: VecDeque::new(),
})
}
/// Configured averaging period.
pub fn period(&self) -> usize {
self.period
}
}
impl Indicator for LongLine {
type Input = Candle;
type Output = f64;
fn update(&mut self, candle: Candle) -> Option<f64> {
let range = candle.high - candle.low;
let body = candle.close - candle.open;
if self.ranges.len() < self.period {
self.ranges.push_back(range);
return Some(0.0);
}
let avg = self.ranges.iter().sum::<f64>() / self.period as f64;
self.ranges.push_back(range);
self.ranges.pop_front();
if range > avg && body.abs() >= 0.5 * range {
return Some(if body > 0.0 { 1.0 } else { -1.0 });
}
Some(0.0)
}
fn reset(&mut self) {
self.ranges.clear();
}
fn warmup_period(&self) -> usize {
self.period
}
fn is_ready(&self) -> bool {
self.ranges.len() >= self.period
}
fn name(&self) -> &'static str {
"LongLine"
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::traits::BatchExt;
fn c(open: f64, high: f64, low: f64, close: f64, ts: i64) -> Candle {
Candle::new(open, high, low, close, 1.0, ts).unwrap()
}
fn warm(t: &mut LongLine) {
for ts in 0..5 {
assert_eq!(t.update(c(10.0, 10.5, 9.5, 10.2, ts)), Some(0.0));
}
}
#[test]
fn rejects_zero_period() {
assert!(LongLine::with_period(0).is_err());
}
#[test]
fn accepts_valid_period() {
let t = LongLine::with_period(10).unwrap();
assert_eq!(t.period(), 10);
}
#[test]
fn accessors_and_metadata() {
let t = LongLine::new();
assert_eq!(t.name(), "LongLine");
assert_eq!(t.warmup_period(), 5);
assert!(!t.is_ready());
assert_eq!(t.period(), 5);
}
#[test]
fn long_white_line_is_plus_one() {
let mut t = LongLine::new();
warm(&mut t);
assert!(t.is_ready());
assert_eq!(t.update(c(10.0, 13.0, 9.9, 12.9, 5)), Some(1.0));
}
#[test]
fn long_black_line_is_minus_one() {
let mut t = LongLine::new();
warm(&mut t);
assert_eq!(t.update(c(13.0, 13.1, 9.9, 10.0, 5)), Some(-1.0));
}
#[test]
fn short_range_yields_zero() {
let mut t = LongLine::new();
warm(&mut t);
// Range no bigger than the average -> not a long line.
assert_eq!(t.update(c(10.0, 10.5, 9.5, 10.2, 5)), Some(0.0));
}
#[test]
fn wide_range_small_body_yields_zero() {
let mut t = LongLine::new();
warm(&mut t);
// Wide range but a tiny body -> a spinning top, not a long line.
assert_eq!(t.update(c(10.5, 13.0, 9.9, 10.6, 5)), Some(0.0));
}
#[test]
fn warmup_returns_zero() {
let mut t = LongLine::new();
for ts in 0..5 {
assert_eq!(t.update(c(10.0, 13.0, 9.9, 12.9, ts)), Some(0.0));
}
}
#[test]
fn batch_equals_streaming() {
let candles: Vec<Candle> = (0..40)
.map(|i| {
let base = 100.0 + i as f64;
if i % 7 == 0 {
c(base, base + 4.0, base - 0.1, base + 3.9, i)
} else {
c(base, base + 0.5, base - 0.5, base + 0.2, i)
}
})
.collect();
let mut a = LongLine::new();
let mut b = LongLine::new();
assert_eq!(
a.batch(&candles),
candles.iter().map(|x| b.update(*x)).collect::<Vec<_>>()
);
}
#[test]
fn reset_clears_state() {
let mut t = LongLine::new();
warm(&mut t);
t.update(c(10.0, 13.0, 9.9, 12.9, 5));
assert!(t.is_ready());
t.reset();
assert!(!t.is_ready());
assert_eq!(t.update(c(10.0, 13.0, 9.9, 12.9, 0)), Some(0.0));
}
#[test]
fn default_matches_new() {
assert_eq!(LongLine::default().period(), LongLine::new().period());
}
}