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
306
307
//! High/Low Volume Nodes (HVN / LVN) — the busiest and quietest price levels.
use std::collections::VecDeque;
use crate::error::{Error, Result};
use crate::ohlcv::Candle;
use crate::traits::Indicator;
/// Output of [`HighLowVolumeNodes`]: the price of the highest- and lowest-volume
/// node in the profile.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct HighLowVolumeNodesOutput {
/// High Volume Node — the price level (bin centre) with the most volume.
pub hvn: f64,
/// Low Volume Node — the traded price level with the least volume.
pub lvn: f64,
}
/// High/Low Volume Nodes — the price levels of greatest and least acceptance in a
/// rolling volume profile.
///
/// ```text
/// build a `bins`-bucket volume profile over the last `period` candles
/// HVN = bin centre of the bucket with the most volume
/// LVN = bin centre of the traded bucket with the least volume
/// ```
///
/// A volume profile reveals where the market spent the most effort. A **High Volume
/// Node** (HVN) is a price the market accepted and traded heavily — it acts as a
/// magnet and as strong support/resistance. A **Low Volume Node** (LVN) is a price
/// the market rejected quickly — moves tend to accelerate through LVNs and they
/// often mark the edges between balance areas. Each candle's volume is spread
/// across the price bins its high-low range spans (as in
/// [`VolumeProfile`](crate::VolumeProfile)).
///
/// The first value lands after `period` candles; each `update` rebuilds the profile
/// in O(`period · bins`). A degenerate flat window puts both nodes at the price.
///
/// # Example
///
/// ```
/// use wickra_core::{Candle, Indicator, HighLowVolumeNodes};
///
/// let mut indicator = HighLowVolumeNodes::new(20, 24).unwrap();
/// let mut last = None;
/// for i in 0..40 {
/// let base = 100.0 + (f64::from(i) * 0.3).sin() * 5.0;
/// let c = Candle::new(base, base + 1.0, base - 1.0, base, 1_000.0, 0).unwrap();
/// last = indicator.update(c);
/// }
/// assert!(last.is_some());
/// ```
#[derive(Debug, Clone)]
pub struct HighLowVolumeNodes {
period: usize,
bins: usize,
window: VecDeque<Candle>,
last: Option<HighLowVolumeNodesOutput>,
}
impl HighLowVolumeNodes {
/// Construct a High/Low Volume Nodes indicator.
///
/// # Errors
///
/// Returns [`Error::PeriodZero`] if `period` or `bins` is zero.
pub fn new(period: usize, bins: usize) -> Result<Self> {
if period == 0 || bins == 0 {
return Err(Error::PeriodZero);
}
Ok(Self {
period,
bins,
window: VecDeque::with_capacity(period),
last: None,
})
}
/// Configured `(period, bins)`.
pub const fn params(&self) -> (usize, usize) {
(self.period, self.bins)
}
/// Current value if available.
pub const fn value(&self) -> Option<HighLowVolumeNodesOutput> {
self.last
}
/// Build the volume histogram; returns `(low, bin_width, bins)`.
#[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
fn profile(&self) -> (f64, f64, Vec<f64>) {
let mut low = f64::INFINITY;
let mut high = f64::NEG_INFINITY;
for c in &self.window {
low = low.min(c.low);
high = high.max(c.high);
}
let mut hist = vec![0.0; self.bins];
let span = high - low;
if span <= 0.0 {
hist[0] = self.window.iter().map(|c| c.volume).sum();
return (low, 0.0, hist);
}
let width = span / self.bins as f64;
for c in &self.window {
if c.volume == 0.0 {
continue;
}
let lo_idx = (((c.low - low) / width).floor() as usize).min(self.bins - 1);
let hi_idx = (((c.high - low) / width).floor() as usize).min(self.bins - 1);
let touched = hi_idx - lo_idx + 1;
let share = c.volume / touched as f64;
for bin in hist.iter_mut().take(hi_idx + 1).skip(lo_idx) {
*bin += share;
}
}
(low, width, hist)
}
}
impl Indicator for HighLowVolumeNodes {
type Input = Candle;
type Output = HighLowVolumeNodesOutput;
#[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
fn update(&mut self, candle: Candle) -> Option<HighLowVolumeNodesOutput> {
if self.window.len() == self.period {
self.window.pop_front();
}
self.window.push_back(candle);
if self.window.len() < self.period {
return None;
}
let (low, width, hist) = self.profile();
let centre = |idx: usize| low + (idx as f64 + 0.5) * width;
let mut hvn_idx = 0;
let mut hvn_vol = f64::NEG_INFINITY;
let mut lvn_idx = 0;
let mut lvn_vol = f64::INFINITY;
for (idx, &vol) in hist.iter().enumerate() {
if vol > hvn_vol {
hvn_vol = vol;
hvn_idx = idx;
}
if vol > 0.0 && vol < lvn_vol {
lvn_vol = vol;
lvn_idx = idx;
}
}
// If no traded bin was found (all zero volume), both default to bin 0.
if !lvn_vol.is_finite() {
lvn_idx = hvn_idx;
}
let out = HighLowVolumeNodesOutput {
hvn: centre(hvn_idx),
lvn: centre(lvn_idx),
};
self.last = Some(out);
Some(out)
}
fn reset(&mut self) {
self.window.clear();
self.last = None;
}
fn warmup_period(&self) -> usize {
self.period
}
fn is_ready(&self) -> bool {
self.last.is_some()
}
fn name(&self) -> &'static str {
"HighLowVolumeNodes"
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::traits::BatchExt;
fn c(high: f64, low: f64, volume: f64) -> Candle {
Candle::new_unchecked(
f64::midpoint(high, low),
high,
low,
f64::midpoint(high, low),
volume,
0,
)
}
#[test]
fn rejects_zero_params() {
assert!(matches!(
HighLowVolumeNodes::new(0, 24),
Err(Error::PeriodZero)
));
assert!(matches!(
HighLowVolumeNodes::new(20, 0),
Err(Error::PeriodZero)
));
}
#[test]
fn accessors_and_metadata() {
let h = HighLowVolumeNodes::new(20, 24).unwrap();
assert_eq!(h.params(), (20, 24));
assert_eq!(h.warmup_period(), 20);
assert_eq!(h.name(), "HighLowVolumeNodes");
assert!(!h.is_ready());
assert_eq!(h.value(), None);
}
#[test]
fn first_emission_at_warmup_period() {
let mut h = HighLowVolumeNodes::new(4, 8).unwrap();
let candles: Vec<Candle> = (0..6).map(|_| c(110.0, 90.0, 1_000.0)).collect();
let out = h.batch(&candles);
for v in out.iter().take(3) {
assert!(v.is_none());
}
assert!(out[3].is_some());
}
#[test]
fn hvn_at_heavy_price() {
// Most bars cluster at ~100 (heavy volume); one bar pokes up to 120 lightly.
let mut h = HighLowVolumeNodes::new(6, 24).unwrap();
let mut candles: Vec<Candle> = (0..5).map(|_| c(101.0, 99.0, 5_000.0)).collect();
candles.push(c(121.0, 119.0, 100.0));
let out = h.batch(&candles).into_iter().flatten().last().unwrap();
// HVN should sit near the heavy 100 cluster, well below the light 120 poke.
assert!(
out.hvn < 110.0,
"HVN should be at the heavy cluster, got {}",
out.hvn
);
assert!(out.lvn >= out.hvn - 1e9); // lvn is a valid level
}
#[test]
fn hvn_at_or_above_low() {
let mut h = HighLowVolumeNodes::new(10, 24).unwrap();
let candles: Vec<Candle> = (0..30)
.map(|i| {
c(
110.0 + (f64::from(i) * 0.3).sin() * 5.0,
90.0,
1_000.0 + f64::from(i),
)
})
.collect();
for o in h.batch(&candles).into_iter().flatten() {
assert!(o.hvn.is_finite() && o.lvn.is_finite());
}
}
#[test]
fn reset_clears_state() {
let mut h = HighLowVolumeNodes::new(4, 8).unwrap();
h.batch(&[c(110.0, 90.0, 1_000.0); 6]);
assert!(h.is_ready());
h.reset();
assert!(!h.is_ready());
assert_eq!(h.value(), None);
assert_eq!(h.update(c(110.0, 90.0, 1_000.0)), None);
}
#[test]
fn batch_equals_streaming() {
let candles: Vec<Candle> = (0..80)
.map(|i| {
c(
110.0 + (f64::from(i) * 0.25).sin() * 9.0,
90.0,
1_000.0 + f64::from(i),
)
})
.collect();
let batch = HighLowVolumeNodes::new(20, 24).unwrap().batch(&candles);
let mut b = HighLowVolumeNodes::new(20, 24).unwrap();
let streamed: Vec<_> = candles.iter().map(|x| b.update(*x)).collect();
assert_eq!(batch, streamed);
}
#[test]
fn flat_window_is_handled() {
// Zero high-low span dumps all volume into bin 0 and returns early.
let mut h = HighLowVolumeNodes::new(2, 4).unwrap();
h.update(c(50.0, 50.0, 10.0));
assert!(h.update(c(50.0, 50.0, 10.0)).is_some());
}
#[test]
fn zero_volume_window_falls_back() {
// All-zero volume leaves no traded bin; the LVN falls back to the HVN.
let mut h = HighLowVolumeNodes::new(2, 4).unwrap();
h.update(c(60.0, 40.0, 0.0));
let out = h.update(c(60.0, 40.0, 0.0)).unwrap();
assert_eq!(out.hvn, out.lvn);
}
}