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
//! Bollinger bands on the spread of two series, for pairs mean-reversion trading.
use std::collections::VecDeque;
use crate::error::{Error, Result};
use crate::traits::Indicator;
/// Output of [`SpreadBollingerBands`].
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct SpreadBollingerBandsOutput {
/// Middle band: the rolling mean of the spread.
pub middle: f64,
/// Upper band: `middle + num_std · σ`.
pub upper: f64,
/// Lower band: `middle − num_std · σ`.
pub lower: f64,
/// `%b`: where the current spread sits across the band, `(s − lower) /
/// (upper − lower)`. `0` is the lower band, `1` the upper, `0.5` the middle.
/// Reported as `0.5` when the band has zero width (a flat spread).
pub percent_b: f64,
}
/// Bollinger bands on the spread `a − b` of two series.
///
/// Each `update` takes one `(a, b)` price pair and forms the spread
/// `sₜ = aₜ − bₜ`. Over the trailing window of `period` spreads it builds a
/// classic Bollinger envelope:
///
/// ```text
/// middle = mean(s) σ = stddev(s)
/// upper = middle + num_std · σ
/// lower = middle − num_std · σ
/// %b = (s_now − lower) / (upper − lower)
/// ```
///
/// Applied to a spread rather than a price, the bands are a ready-made pairs
/// mean-reversion signal: the spread riding the **upper** band is stretched
/// rich (a short-the-spread setup), the **lower** band stretched cheap, and a
/// return to the **middle** is the exit. `%b` compresses the location into one
/// number for thresholding. The spread is the raw difference `a − b`, so feed
/// already-comparable legs (e.g. a hedged pair, two yields, or log prices); pair
/// this with [`crate::BetaNeutralSpread`] when the legs need a hedge ratio first.
///
/// A flat spread yields a zero-width band; `%b` is then reported as the neutral
/// `0.5`. Each `update` is `O(1)`: the mean and variance come from two running
/// sums maintained as the window slides.
///
/// # Example
///
/// ```
/// use wickra_core::{Indicator, SpreadBollingerBands};
///
/// let mut bb = SpreadBollingerBands::new(20, 2.0).unwrap();
/// let mut last = None;
/// for t in 0..60 {
/// let b = 100.0 + f64::from(t);
/// let a = b + 2.0 * (f64::from(t) * 0.5).sin();
/// last = bb.update((a, b));
/// }
/// let out = last.unwrap();
/// assert!(out.lower <= out.middle && out.middle <= out.upper);
/// ```
#[derive(Debug, Clone)]
pub struct SpreadBollingerBands {
period: usize,
num_std: f64,
window: VecDeque<f64>,
sum: f64,
sum_sq: f64,
}
impl SpreadBollingerBands {
/// Construct new spread Bollinger bands.
///
/// `period` is the look-back window; `num_std` is the band width in standard
/// deviations.
///
/// # Errors
/// Returns [`Error::InvalidPeriod`] if `period < 2`, or
/// [`Error::InvalidParameter`] if `num_std` is not strictly positive (and
/// finite).
pub fn new(period: usize, num_std: f64) -> Result<Self> {
if period < 2 {
return Err(Error::InvalidPeriod {
message: "spread bollinger bands needs period >= 2",
});
}
if !num_std.is_finite() || num_std <= 0.0 {
return Err(Error::InvalidParameter {
message: "spread bollinger bands needs num_std > 0",
});
}
Ok(Self {
period,
num_std,
window: VecDeque::with_capacity(period),
sum: 0.0,
sum_sq: 0.0,
})
}
/// Configured look-back window.
pub const fn period(&self) -> usize {
self.period
}
/// Configured band width in standard deviations.
pub const fn num_std(&self) -> f64 {
self.num_std
}
}
impl Indicator for SpreadBollingerBands {
type Input = (f64, f64);
type Output = SpreadBollingerBandsOutput;
fn update(&mut self, input: (f64, f64)) -> Option<SpreadBollingerBandsOutput> {
let (a, b) = input;
let spread = a - b;
if self.window.len() == self.period {
let old = self.window.pop_front().expect("non-empty");
self.sum -= old;
self.sum_sq -= old * old;
}
self.window.push_back(spread);
self.sum += spread;
self.sum_sq += spread * spread;
if self.window.len() < self.period {
return None;
}
let n = self.period as f64;
let middle = self.sum / n;
let variance = (self.sum_sq / n - middle * middle).max(0.0);
let sigma = variance.sqrt();
let half_width = self.num_std * sigma;
let upper = middle + half_width;
let lower = middle - half_width;
let percent_b = if half_width == 0.0 {
0.5
} else {
(spread - lower) / (upper - lower)
};
Some(SpreadBollingerBandsOutput {
middle,
upper,
lower,
percent_b,
})
}
fn reset(&mut self) {
self.window.clear();
self.sum = 0.0;
self.sum_sq = 0.0;
}
fn warmup_period(&self) -> usize {
self.period
}
fn is_ready(&self) -> bool {
self.window.len() == self.period
}
fn name(&self) -> &'static str {
"SpreadBollingerBands"
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::traits::BatchExt;
use approx::assert_relative_eq;
#[test]
fn rejects_bad_parameters() {
assert!(SpreadBollingerBands::new(1, 2.0).is_err());
assert!(SpreadBollingerBands::new(20, 0.0).is_err());
assert!(SpreadBollingerBands::new(20, -1.0).is_err());
assert!(SpreadBollingerBands::new(20, f64::NAN).is_err());
assert!(SpreadBollingerBands::new(2, 2.0).is_ok());
}
#[test]
fn accessors_and_metadata() {
let bb = SpreadBollingerBands::new(20, 2.5).unwrap();
assert_eq!(bb.period(), 20);
assert_eq!(bb.num_std(), 2.5);
assert_eq!(bb.warmup_period(), 20);
assert_eq!(bb.name(), "SpreadBollingerBands");
assert!(!bb.is_ready());
}
#[test]
fn warmup_returns_none() {
let mut bb = SpreadBollingerBands::new(3, 2.0).unwrap();
assert_eq!(bb.update((1.0, 0.0)), None);
assert_eq!(bb.update((2.0, 0.0)), None);
assert!(bb.update((3.0, 0.0)).is_some());
assert!(bb.is_ready());
}
#[test]
fn hand_computed_value() {
// Spreads 1,2,3,4 (b = 0), period 4, num_std 2:
// mean = 2.5, σ = √1.25, upper = 2.5 + 2√1.25, lower = 2.5 − 2√1.25,
// %b at s = 4 ⇒ 0.8354102.
let pairs = [(1.0, 0.0), (2.0, 0.0), (3.0, 0.0), (4.0, 0.0)];
let out = SpreadBollingerBands::new(4, 2.0)
.unwrap()
.batch(&pairs)
.into_iter()
.flatten()
.last()
.unwrap();
assert_relative_eq!(out.middle, 2.5, epsilon = 1e-9);
assert_relative_eq!(out.upper, 4.736_067_977_499_79, epsilon = 1e-9);
assert_relative_eq!(out.lower, 0.263_932_022_500_21, epsilon = 1e-9);
assert_relative_eq!(out.percent_b, 0.835_410_196_624_97, epsilon = 1e-9);
}
#[test]
fn flat_spread_collapses_band() {
// a − b constant ⇒ σ = 0 ⇒ upper = middle = lower, %b = 0.5.
let pairs: Vec<(f64, f64)> = (0..10)
.map(|t| (5.0 + f64::from(t), f64::from(t)))
.collect();
let out = SpreadBollingerBands::new(5, 2.0)
.unwrap()
.batch(&pairs)
.into_iter()
.flatten()
.last()
.unwrap();
assert_relative_eq!(out.upper, out.middle, epsilon = 1e-12);
assert_relative_eq!(out.lower, out.middle, epsilon = 1e-12);
assert_relative_eq!(out.percent_b, 0.5, epsilon = 1e-12);
}
#[test]
fn bands_are_ordered() {
let pairs: Vec<(f64, f64)> = (0..80)
.map(|t| {
let b = 100.0 + f64::from(t);
(b + 3.0 * (f64::from(t) * 0.4).sin(), b)
})
.collect();
let mut bb = SpreadBollingerBands::new(20, 2.0).unwrap();
for out in bb.batch(&pairs).into_iter().flatten() {
assert!(out.lower <= out.middle && out.middle <= out.upper);
}
}
#[test]
fn reset_clears_state() {
let mut bb = SpreadBollingerBands::new(4, 2.0).unwrap();
bb.batch(&[(1.0, 0.0), (2.0, 0.0), (3.0, 0.0), (4.0, 0.0), (5.0, 0.0)]);
assert!(bb.is_ready());
bb.reset();
assert!(!bb.is_ready());
assert_eq!(bb.update((1.0, 0.0)), None);
}
#[test]
fn batch_equals_streaming() {
let pairs: Vec<(f64, f64)> = (0..60)
.map(|t| {
let b = 30.0 + 0.7 * f64::from(t);
(b + (f64::from(t) * 0.4).sin() * 1.5, b)
})
.collect();
let batch = SpreadBollingerBands::new(15, 2.0).unwrap().batch(&pairs);
let mut bb = SpreadBollingerBands::new(15, 2.0).unwrap();
let streamed: Vec<_> = pairs.iter().map(|p| bb.update(*p)).collect();
assert_eq!(batch, streamed);
}
}