Expand description
§Bollinger Bands
Middle band = SMA(period). Upper/lower = middle ± num_std × window standard deviation.
§Word problem
A trader plots Bollinger(20, 2) on daily closes. After 20 days of identical closes at 100, where are the bands?
Expect: middle = 100, upper = lower = 100 (zero width → %B is None, not a fake 0.5).
use finance_solution::stocks::ta::{bollinger, BollingerParams};
let c = vec![100.0; 25];
let s = bollinger(&c, BollingerParams::standard()).unwrap();
assert_eq!(s.middle[19], Some(100.0));
assert_eq!(s.upper[19], Some(100.0));
assert_eq!(s.pct_b[19], None);§Stdev: sample vs population
| Kind | Denominator | Use |
|---|---|---|
StdevKind::Sample (default) | n − 1 | Recommended for Bollinger in most finance software |
StdevKind::Population | n | Matches some charting packages / “full window” definitions |
Sample is slightly wider for small n. Both are available so you are not locked in.
use finance_solution::stocks::ta::{bollinger, BollingerParams, StdevKind};
let closes: Vec<f64> = (1..=30).map(|x| 100.0 + x as f64).collect();
let sample = bollinger(&closes, BollingerParams::standard()).unwrap();
let pop = bollinger(
&closes,
BollingerParams { stdev: StdevKind::Population, ..BollingerParams::standard() },
).unwrap();
// Same middle (SMA); upper band: sample ≥ population for n>1 when variance > 0
assert!(sample.upper[29].unwrap() >= pop.upper[29].unwrap() - 1e-12);§Quant pattern
use finance_solution::stocks::ta::{BollingerParams, ValidatedBollinger, BollingerState};
const BB20_2: BollingerParams = BollingerParams::standard(); // sample stdev
let bb = ValidatedBollinger::new(BB20_2).unwrap();
let s = bb.compute(&closes).unwrap();
// Live:
let mut st = BollingerState::new(BB20_2).unwrap();
let _ = st.push_bars(&closes).unwrap();§Sample solution table
period close middle upper lower pct_b
------ ------ ------ ------ ------ --------
18 100.50 n/a n/a n/a n/a
19 100.60 100.20 101.10 99.30 0.6667§%B
%B = (close − lower) / (upper − lower) when width > 0; otherwise None.
Structs§
- Bollinger
Params - Bollinger parameter pack.
- Bollinger
Series - Middle / upper / lower / %B series.
- Bollinger
Solution - Teaching solution + table.
- Validated
Bollinger - Validated Bollinger config.
Functions§
- bollinger
- bollinger_
solution - Teaching solution with formulas + table.