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
//! New Price Lines — the "eight/ten new price lines" exhaustion count.
use crate::error::{Error, Result};
use crate::ohlcv::Candle;
use crate::traits::Indicator;
/// New Price Lines — the Japanese "shinne" (new-price) exhaustion count: when the
/// close has made `count` consecutive new highs (or lows), the trend is considered
/// stretched and ripe for a pause or reversal.
///
/// ```text
/// consecutive higher closes form "new price lines" up
/// consecutive lower closes form "new price lines" down
/// signal = −1 once `count` consecutive higher closes (overbought / sell warning)
/// signal = +1 once `count` consecutive lower closes (oversold / buy warning)
/// signal = 0 otherwise
/// ```
///
/// Traditional Japanese practice flags **eight** new price lines (and a stronger
/// **ten** or twelve) as the point where a directional run becomes exhausted —
/// the market has gone up (or down) so many bars in a row that a corrective pause
/// is statistically due. The signal stays active for every bar the streak remains
/// at or above `count`, and clears the moment a close breaks the streak.
///
/// The first value lands on the second bar (one prior close is needed). The
/// output is `+1` / `0` / `−1`. Each `update` is O(1).
///
/// # Example
///
/// ```
/// use wickra_core::{Candle, Indicator, NewPriceLines};
///
/// let mut indicator = NewPriceLines::new(8).unwrap();
/// let mut last = None;
/// for i in 0..12 {
/// let close = 100.0 + f64::from(i); // 11 consecutive higher closes
/// let c = Candle::new(close, close, close, close, 1_000.0, 0).unwrap();
/// last = indicator.update(c);
/// }
/// assert_eq!(last, Some(-1.0));
/// ```
#[derive(Debug, Clone)]
pub struct NewPriceLines {
count: usize,
prev_close: Option<f64>,
consec_up: usize,
consec_down: usize,
last: Option<f64>,
}
impl NewPriceLines {
/// Construct a New Price Lines counter that fires at `count` consecutive new
/// closes (classic `8`, stronger `10`/`12`).
///
/// # Errors
///
/// Returns [`Error::InvalidPeriod`] if `count < 2`.
pub fn new(count: usize) -> Result<Self> {
if count < 2 {
return Err(Error::InvalidPeriod {
message: "new price lines count must be >= 2",
});
}
Ok(Self {
count,
prev_close: None,
consec_up: 0,
consec_down: 0,
last: None,
})
}
/// Configured count threshold.
pub const fn count(&self) -> usize {
self.count
}
/// Current consecutive streak `(up, down)`.
pub const fn streak(&self) -> (usize, usize) {
(self.consec_up, self.consec_down)
}
/// Current value if available.
pub const fn value(&self) -> Option<f64> {
self.last
}
}
impl Indicator for NewPriceLines {
type Input = Candle;
type Output = f64;
fn update(&mut self, candle: Candle) -> Option<f64> {
let close = candle.close;
let Some(prev) = self.prev_close else {
self.prev_close = Some(close);
return None;
};
if close > prev {
self.consec_up += 1;
self.consec_down = 0;
} else if close < prev {
self.consec_down += 1;
self.consec_up = 0;
} else {
self.consec_up = 0;
self.consec_down = 0;
}
self.prev_close = Some(close);
let v = if self.consec_up >= self.count {
-1.0
} else if self.consec_down >= self.count {
1.0
} else {
0.0
};
self.last = Some(v);
Some(v)
}
fn reset(&mut self) {
self.prev_close = None;
self.consec_up = 0;
self.consec_down = 0;
self.last = None;
}
fn warmup_period(&self) -> usize {
2
}
fn is_ready(&self) -> bool {
self.last.is_some()
}
fn name(&self) -> &'static str {
"NewPriceLines"
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::traits::BatchExt;
fn c(close: f64) -> Candle {
Candle::new_unchecked(close, close, close, close, 1_000.0, 0)
}
#[test]
fn rejects_small_count() {
assert!(matches!(
NewPriceLines::new(1),
Err(Error::InvalidPeriod { .. })
));
assert!(NewPriceLines::new(2).is_ok());
}
#[test]
fn accessors_and_metadata() {
let n = NewPriceLines::new(8).unwrap();
assert_eq!(n.count(), 8);
assert_eq!(n.streak(), (0, 0));
assert_eq!(n.warmup_period(), 2);
assert_eq!(n.name(), "NewPriceLines");
assert!(!n.is_ready());
assert_eq!(n.value(), None);
}
#[test]
fn first_bar_seeds_without_signal() {
let mut n = NewPriceLines::new(3).unwrap();
assert_eq!(n.update(c(100.0)), None);
assert!(n.update(c(101.0)).is_some());
}
#[test]
fn eight_higher_closes_signal_sell() {
let mut n = NewPriceLines::new(8).unwrap();
// 11 consecutive higher closes -> by the 9th the count reaches 8 -> -1.
let candles: Vec<Candle> = (0..12).map(|i| c(100.0 + f64::from(i))).collect();
let last = n.batch(&candles).into_iter().flatten().last().unwrap();
assert_eq!(last, -1.0);
}
#[test]
fn eight_lower_closes_signal_buy() {
let mut n = NewPriceLines::new(8).unwrap();
let candles: Vec<Candle> = (0..12).map(|i| c(200.0 - f64::from(i))).collect();
let last = n.batch(&candles).into_iter().flatten().last().unwrap();
assert_eq!(last, 1.0);
}
#[test]
fn break_in_streak_clears_signal() {
let mut n = NewPriceLines::new(3).unwrap();
n.batch(&[c(100.0), c(101.0), c(102.0), c(103.0)]); // streak 3 -> -1
assert_eq!(n.value(), Some(-1.0));
// A lower close breaks the up streak.
assert_eq!(n.update(c(102.0)), Some(0.0));
assert_eq!(n.streak(), (0, 1));
}
#[test]
fn unchanged_close_resets_streak() {
let mut n = NewPriceLines::new(3).unwrap();
n.batch(&[c(100.0), c(101.0), c(102.0)]);
assert_eq!(n.update(c(102.0)), Some(0.0)); // equal -> reset
assert_eq!(n.streak(), (0, 0));
}
#[test]
fn reset_clears_state() {
let mut n = NewPriceLines::new(3).unwrap();
n.batch(&[c(100.0), c(101.0), c(102.0), c(103.0)]);
assert!(n.is_ready());
n.reset();
assert!(!n.is_ready());
assert_eq!(n.value(), None);
assert_eq!(n.streak(), (0, 0));
}
#[test]
fn batch_equals_streaming() {
let candles: Vec<Candle> = (0..80)
.map(|i| c(100.0 + (f64::from(i) * 0.25).sin() * 9.0))
.collect();
let batch = NewPriceLines::new(8).unwrap().batch(&candles);
let mut b = NewPriceLines::new(8).unwrap();
let streamed: Vec<_> = candles.iter().map(|x| b.update(*x)).collect();
assert_eq!(batch, streamed);
}
}