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
//! Short Line candlestick pattern.
use crate::error::{Error, Result};
use crate::ohlcv::Candle;
use crate::traits::Indicator;
use std::collections::VecDeque;
/// Short Line — a single candle whose range is *shorter* than the recent average
/// while its body still dominates that (small) range: a compact directional bar.
/// As with [`LongLine`](crate::LongLine), "short" only has meaning relative to
/// recent activity, so 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
/// short line = range < avg AND |close − open| >= 0.5 * range
/// white -> +1.0, black -> −1.0
/// ```
///
/// Output is `+1.0` (short white line), `−1.0` (short 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`. 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, ShortLine};
///
/// let mut indicator = ShortLine::new();
/// // Five wide bars fill the rolling average.
/// for ts in 0..5 {
/// indicator.update(Candle::new(10.0, 13.0, 9.5, 12.9, 1.0, ts).unwrap());
/// }
/// // A compact solid white bar is a short white line.
/// let out = indicator
/// .update(Candle::new(10.0, 11.0, 9.9, 10.9, 1.0, 5).unwrap());
/// assert_eq!(out, Some(1.0));
/// ```
#[derive(Debug, Clone)]
pub struct ShortLine {
period: usize,
ranges: VecDeque<f64>,
}
impl Default for ShortLine {
fn default() -> Self {
Self::new()
}
}
impl ShortLine {
/// Construct a Short Line detector with the default 5-candle rolling average.
pub const fn new() -> Self {
Self {
period: 5,
ranges: VecDeque::new(),
}
}
/// Construct a Short 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 ShortLine {
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 {
"ShortLine"
}
}
#[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 ShortLine) {
for ts in 0..5 {
assert_eq!(t.update(c(10.0, 13.0, 9.5, 12.9, ts)), Some(0.0));
}
}
#[test]
fn rejects_zero_period() {
assert!(ShortLine::with_period(0).is_err());
}
#[test]
fn accepts_valid_period() {
let t = ShortLine::with_period(10).unwrap();
assert_eq!(t.period(), 10);
}
#[test]
fn accessors_and_metadata() {
let t = ShortLine::new();
assert_eq!(t.name(), "ShortLine");
assert_eq!(t.warmup_period(), 5);
assert!(!t.is_ready());
assert_eq!(t.period(), 5);
}
#[test]
fn short_white_line_is_plus_one() {
let mut t = ShortLine::new();
warm(&mut t);
assert!(t.is_ready());
assert_eq!(t.update(c(10.0, 11.0, 9.9, 10.9, 5)), Some(1.0));
}
#[test]
fn short_black_line_is_minus_one() {
let mut t = ShortLine::new();
warm(&mut t);
assert_eq!(t.update(c(10.9, 11.0, 9.9, 10.0, 5)), Some(-1.0));
}
#[test]
fn wide_range_yields_zero() {
let mut t = ShortLine::new();
warm(&mut t);
// Range as wide as the average -> not a short line.
assert_eq!(t.update(c(10.0, 13.0, 9.5, 12.9, 5)), Some(0.0));
}
#[test]
fn short_range_small_body_yields_zero() {
let mut t = ShortLine::new();
warm(&mut t);
// Compact range but a tiny body -> not a solid short line.
assert_eq!(t.update(c(10.4, 11.0, 9.9, 10.5, 5)), Some(0.0));
}
#[test]
fn warmup_returns_zero() {
let mut t = ShortLine::new();
for ts in 0..5 {
assert_eq!(t.update(c(10.0, 11.0, 9.9, 10.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 + 0.6, base - 0.1, base + 0.5, i)
} else {
c(base, base + 3.0, base - 1.0, base + 2.8, i)
}
})
.collect();
let mut a = ShortLine::new();
let mut b = ShortLine::new();
assert_eq!(
a.batch(&candles),
candles.iter().map(|x| b.update(*x)).collect::<Vec<_>>()
);
}
#[test]
fn reset_clears_state() {
let mut t = ShortLine::new();
warm(&mut t);
t.update(c(10.0, 11.0, 9.9, 10.9, 5));
assert!(t.is_ready());
t.reset();
assert!(!t.is_ready());
assert_eq!(t.update(c(10.0, 11.0, 9.9, 10.9, 0)), Some(0.0));
}
#[test]
fn default_matches_new() {
assert_eq!(ShortLine::default().period(), ShortLine::new().period());
}
}