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
308
309
310
311
312
313
314
315
//! Volume-Weighted Average Price (VWAP).
//!
//! Two variants are offered: a cumulative `Vwap` that runs forever (the
//! intraday convention), and a rolling-window `RollingVwap` for streaming bots
//! that need a finite-memory price benchmark.
use std::collections::VecDeque;
use crate::error::{Error, Result};
use crate::ohlcv::Candle;
use crate::traits::Indicator;
/// Cumulative session VWAP. Call [`Indicator::reset`] at the start of each
/// session (e.g. trading-day boundary) to restart the accumulation.
///
/// # Example
///
/// ```
/// use wickra_core::{Candle, Indicator, Vwap};
///
/// let mut indicator = Vwap::new();
/// let mut last = None;
/// for i in 0..80 {
/// let base = 100.0 + f64::from(i);
/// let candle =
/// Candle::new(base, base + 2.0, base - 2.0, base + 1.0, 10.0, i64::from(i)).unwrap();
/// last = indicator.update(candle);
/// }
/// assert!(last.is_some());
/// ```
#[derive(Debug, Clone, Default)]
pub struct Vwap {
sum_pv: f64,
sum_v: f64,
has_emitted: bool,
}
impl Vwap {
/// Construct a fresh cumulative VWAP.
pub const fn new() -> Self {
Self {
sum_pv: 0.0,
sum_v: 0.0,
has_emitted: false,
}
}
/// Current VWAP if at least one candle with non-zero volume has been observed.
pub fn value(&self) -> Option<f64> {
if self.sum_v == 0.0 {
None
} else {
Some(self.sum_pv / self.sum_v)
}
}
}
impl Indicator for Vwap {
type Input = Candle;
type Output = f64;
fn update(&mut self, candle: Candle) -> Option<f64> {
let tp = candle.typical_price();
self.sum_pv += tp * candle.volume;
self.sum_v += candle.volume;
if self.sum_v == 0.0 {
return None;
}
self.has_emitted = true;
Some(self.sum_pv / self.sum_v)
}
fn reset(&mut self) {
self.sum_pv = 0.0;
self.sum_v = 0.0;
self.has_emitted = false;
}
fn warmup_period(&self) -> usize {
1
}
fn is_ready(&self) -> bool {
self.has_emitted
}
fn name(&self) -> &'static str {
"VWAP"
}
}
/// Rolling-window VWAP: a finite-memory variant for bots that don't want
/// unbounded accumulation.
///
/// # Example
///
/// ```
/// use wickra_core::{Candle, Indicator, RollingVwap};
///
/// let mut indicator = RollingVwap::new(5).unwrap();
/// let mut last = None;
/// for i in 0..80 {
/// let base = 100.0 + f64::from(i);
/// let candle =
/// Candle::new(base, base + 2.0, base - 2.0, base + 1.0, 10.0, i64::from(i)).unwrap();
/// last = indicator.update(candle);
/// }
/// assert!(last.is_some());
/// ```
#[derive(Debug, Clone)]
pub struct RollingVwap {
period: usize,
window: VecDeque<(f64, f64)>, // (typical_price * volume, volume)
sum_pv: f64,
sum_v: f64,
}
impl RollingVwap {
/// # Errors
/// Returns [`Error::PeriodZero`] if `period == 0`.
pub fn new(period: usize) -> Result<Self> {
if period == 0 {
return Err(Error::PeriodZero);
}
Ok(Self {
period,
window: VecDeque::with_capacity(period),
sum_pv: 0.0,
sum_v: 0.0,
})
}
/// Configured rolling window length.
pub const fn period(&self) -> usize {
self.period
}
}
impl Indicator for RollingVwap {
type Input = Candle;
type Output = f64;
fn update(&mut self, candle: Candle) -> Option<f64> {
let pv = candle.typical_price() * candle.volume;
if self.window.len() == self.period {
let (old_pv, old_v) = self.window.pop_front().expect("non-empty");
self.sum_pv -= old_pv;
self.sum_v -= old_v;
}
self.window.push_back((pv, candle.volume));
self.sum_pv += pv;
self.sum_v += candle.volume;
if self.window.len() < self.period || self.sum_v == 0.0 {
return None;
}
Some(self.sum_pv / self.sum_v)
}
fn reset(&mut self) {
self.window.clear();
self.sum_pv = 0.0;
self.sum_v = 0.0;
}
fn warmup_period(&self) -> usize {
self.period
}
fn is_ready(&self) -> bool {
self.window.len() == self.period && self.sum_v > 0.0
}
fn name(&self) -> &'static str {
"RollingVWAP"
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::traits::BatchExt;
use approx::assert_relative_eq;
fn c(price: f64, volume: f64) -> Candle {
Candle::new(price, price, price, price, volume, 0).unwrap()
}
#[test]
fn cumulative_vwap_equal_volumes_equals_mean() {
let candles = vec![c(10.0, 1.0), c(20.0, 1.0), c(30.0, 1.0)];
let mut v = Vwap::new();
let out = v.batch(&candles);
assert_relative_eq!(out[2].unwrap(), 20.0, epsilon = 1e-12);
}
/// Cover the `Some` branch of `Vwap::value()` (line 53). The only other
/// test that calls `value()` is `cumulative_reset_clears_state`, which
/// calls it after `reset()` so `sum_v == 0` and the `None` branch fires.
#[test]
fn cumulative_value_some_branch_after_update() {
let mut v = Vwap::new();
// typical_price of a flat OHLC bar equals the price itself.
v.update(c(42.0, 5.0));
assert_relative_eq!(v.value().expect("non-zero volume"), 42.0, epsilon = 1e-12);
}
/// Cover the `return None` early-out inside `Vwap::update` (line 67),
/// reached when the running `sum_v` is still 0 after adding the latest
/// candle's volume — i.e. the first candle has volume 0. Existing tests
/// only use strictly positive volumes, so the early-return never fired.
#[test]
fn cumulative_zero_volume_first_candle_returns_none() {
let mut v = Vwap::new();
let out = v.update(c(42.0, 0.0));
assert_eq!(out, None);
assert!(!v.is_ready());
// Adding a non-zero candle afterwards still works as expected.
let out2 = v.update(c(10.0, 4.0));
assert_relative_eq!(out2.expect("now warmed"), 10.0, epsilon = 1e-12);
}
/// Cover the cumulative `Vwap` Indicator-impl metadata: `warmup_period`
/// (lines 79-81) and `name` (lines 87-89). Existing tests inspected
/// only the numeric output, never the metadata surface.
#[test]
fn cumulative_metadata() {
let v = Vwap::new();
assert_eq!(v.warmup_period(), 1);
assert_eq!(v.name(), "VWAP");
}
#[test]
fn cumulative_vwap_weighted() {
// Two candles: 10@1 and 20@3 -> (10*1 + 20*3) / (1+3) = 70/4 = 17.5
let candles = vec![c(10.0, 1.0), c(20.0, 3.0)];
let mut v = Vwap::new();
let out = v.batch(&candles);
assert_relative_eq!(out[1].unwrap(), 17.5, epsilon = 1e-12);
}
/// Cover the `RollingVwap` accessors and metadata: `period`
/// (lines 134-136), `warmup_period` (165-167), `name` (173-175).
/// Existing rolling tests called `update`/`batch`/`reset`/`is_ready`
/// only, never queried the configuration or metadata.
#[test]
fn rolling_accessors_and_metadata() {
let v = RollingVwap::new(7).unwrap();
assert_eq!(v.period(), 7);
assert_eq!(v.warmup_period(), 7);
assert_eq!(v.name(), "RollingVWAP");
}
#[test]
fn rolling_vwap_window_slides() {
let candles = vec![c(10.0, 1.0), c(20.0, 1.0), c(30.0, 1.0), c(40.0, 1.0)];
let mut v = RollingVwap::new(3).unwrap();
let out = v.batch(&candles);
assert!(out[1].is_none());
// index 2 -> (10+20+30)/3 = 20
assert_relative_eq!(out[2].unwrap(), 20.0, epsilon = 1e-12);
// index 3 -> (20+30+40)/3 = 30
assert_relative_eq!(out[3].unwrap(), 30.0, epsilon = 1e-12);
}
#[test]
fn batch_equals_streaming_cumulative() {
let candles: Vec<Candle> = (1..20).map(|i| c(f64::from(i), 1.0)).collect();
let mut a = Vwap::new();
let mut b = Vwap::new();
assert_eq!(
a.batch(&candles),
candles.iter().map(|x| b.update(*x)).collect::<Vec<_>>()
);
}
#[test]
fn batch_equals_streaming_rolling() {
let candles: Vec<Candle> = (1..30)
.map(|i| c(f64::from(i), f64::from(i % 5 + 1)))
.collect();
let mut a = RollingVwap::new(10).unwrap();
let mut b = RollingVwap::new(10).unwrap();
assert_eq!(
a.batch(&candles),
candles.iter().map(|x| b.update(*x)).collect::<Vec<_>>()
);
}
#[test]
fn rolling_rejects_zero_period() {
assert!(RollingVwap::new(0).is_err());
}
#[test]
fn cumulative_reset_clears_state() {
let candles = vec![c(10.0, 1.0), c(20.0, 1.0), c(30.0, 1.0)];
let mut v = Vwap::new();
v.batch(&candles);
assert!(v.is_ready());
v.reset();
assert!(!v.is_ready());
assert_eq!(v.value(), None);
}
#[test]
fn rolling_reset_clears_state() {
let candles: Vec<Candle> = (1..=10).map(|i| c(f64::from(i), 1.0)).collect();
let mut v = RollingVwap::new(5).unwrap();
v.batch(&candles);
assert!(v.is_ready());
v.reset();
assert!(!v.is_ready());
assert_eq!(v.update(candles[0]), None);
}
}