use std::fmt;
use crate::errors::*;
use crate::indicators::StandardDeviation as Sd;
use crate::{Close, Next, Reset};
#[derive(Debug, Clone)]
pub struct BollingerBands {
length: u32,
multiplier: f64,
sd: Sd,
}
#[derive(Debug, Clone, PartialEq)]
pub struct BollingerBandsOutput {
pub average: f64,
pub upper: f64,
pub lower: f64,
}
impl BollingerBands {
pub fn new(length: u32, multiplier: f64) -> Result<Self> {
if multiplier <= 0.0 {
return Err(Error::from_kind(ErrorKind::InvalidParameter));
}
Ok(Self {
length,
multiplier,
sd: Sd::new(length)?,
})
}
pub fn length(&self) -> u32 {
self.length
}
pub fn multiplier(&self) -> f64 {
self.multiplier
}
}
impl Next<f64> for BollingerBands {
type Output = BollingerBandsOutput;
fn next(&mut self, input: f64) -> Self::Output {
let sd = self.sd.next(input);
let mean = self.sd.mean();
Self::Output {
average: mean,
upper: mean + sd * self.multiplier,
lower: mean - sd * self.multiplier,
}
}
}
impl<'a, T: Close> Next<&'a T> for BollingerBands {
type Output = BollingerBandsOutput;
fn next(&mut self, input: &'a T) -> Self::Output {
self.next(input.close())
}
}
impl Reset for BollingerBands {
fn reset(&mut self) {
self.sd.reset();
}
}
impl Default for BollingerBands {
fn default() -> Self {
Self::new(9, 2_f64).unwrap()
}
}
impl fmt::Display for BollingerBands {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "BB({}, {})", self.length, self.multiplier)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::test_helper::*;
macro_rules! test_indicator {
($i:tt) => {
#[test]
fn test_indicator() {
let bar = Bar::new();
let mut indicator = $i::default();
let first_output = indicator.next(12.3);
indicator.next(&bar);
indicator.reset();
assert_eq!(indicator.next(12.3), first_output);
format!("{}", indicator);
}
};
}
test_indicator!(BollingerBands);
#[test]
fn test_new() {
assert!(BollingerBands::new(0, 2_f64).is_err());
assert!(BollingerBands::new(1, 2_f64).is_ok());
assert!(BollingerBands::new(2, 2_f64).is_ok());
}
#[test]
fn test_next() {
let mut bb = BollingerBands::new(3, 2.0_f64).unwrap();
let a = bb.next(2.0);
let b = bb.next(5.0);
let c = bb.next(1.0);
let d = bb.next(6.25);
assert_eq!(round(a.average), 2.0);
assert_eq!(round(b.average), 3.5);
assert_eq!(round(c.average), 2.667);
assert_eq!(round(d.average), 4.083);
assert_eq!(round(a.upper), 2.0);
assert_eq!(round(b.upper), 6.5);
assert_eq!(round(c.upper), 6.066);
assert_eq!(round(d.upper), 8.562);
assert_eq!(round(a.lower), 2.0);
assert_eq!(round(b.lower), 0.5);
assert_eq!(round(c.lower), -0.733);
assert_eq!(round(d.lower), -0.395);
}
#[test]
fn test_reset() {
let mut bb = BollingerBands::new(5, 2.0_f64).unwrap();
let out = bb.next(3.0);
assert_eq!(out.average, 3.0);
assert_eq!(out.upper, 3.0);
assert_eq!(out.lower, 3.0);
bb.next(2.5);
bb.next(3.5);
bb.next(4.0);
let out = bb.next(2.0);
assert_eq!(out.average, 3.0);
assert_eq!(round(out.upper), 4.414);
assert_eq!(round(out.lower), 1.586);
bb.reset();
let out = bb.next(3.0);
assert_eq!(out.average, 3.0);
assert_eq!(out.upper, 3.0);
assert_eq!(out.lower, 3.0);
}
#[test]
fn test_default() {
BollingerBands::default();
}
#[test]
fn test_display() {
let bb = BollingerBands::new(10, 3.0_f64).unwrap();
println!("{:#?}", bb);
assert_eq!(format!("{}", bb), "BB(10, 3)");
}
}