use crate::error::FinError;
use crate::signals::{BarInput, Signal, SignalValue};
use rust_decimal::Decimal;
pub struct EmaBandWidth {
name: String,
fast_period: usize,
slow_period: usize,
fast_ema: Option<Decimal>,
slow_ema: Option<Decimal>,
fast_k: Decimal,
slow_k: Decimal,
bars: usize,
}
impl EmaBandWidth {
pub fn new(name: impl Into<String>, fast: usize, slow: usize) -> Result<Self, FinError> {
if fast == 0 { return Err(FinError::InvalidPeriod(fast)); }
if slow == 0 || fast >= slow { return Err(FinError::InvalidPeriod(slow)); }
#[allow(clippy::cast_possible_truncation)]
let fast_k = Decimal::from(2u32) / (Decimal::from(fast as u32) + Decimal::ONE);
#[allow(clippy::cast_possible_truncation)]
let slow_k = Decimal::from(2u32) / (Decimal::from(slow as u32) + Decimal::ONE);
Ok(Self {
name: name.into(),
fast_period: fast,
slow_period: slow,
fast_ema: None,
slow_ema: None,
fast_k,
slow_k,
bars: 0,
})
}
pub fn fast_period(&self) -> usize { self.fast_period }
pub fn slow_period(&self) -> usize { self.slow_period }
}
impl Signal for EmaBandWidth {
fn name(&self) -> &str { &self.name }
fn period(&self) -> usize { self.slow_period }
fn is_ready(&self) -> bool { self.bars > self.slow_period }
fn update(&mut self, bar: &BarInput) -> Result<SignalValue, FinError> {
self.bars += 1;
let close = bar.close;
self.fast_ema = Some(match self.fast_ema {
None => close,
Some(prev) => prev + self.fast_k * (close - prev),
});
self.slow_ema = Some(match self.slow_ema {
None => close,
Some(prev) => prev + self.slow_k * (close - prev),
});
if self.bars <= self.slow_period {
return Ok(SignalValue::Unavailable);
}
let fast = self.fast_ema.unwrap_or(Decimal::ZERO);
let slow = self.slow_ema.unwrap_or(Decimal::ZERO);
if slow.is_zero() { return Ok(SignalValue::Unavailable); }
Ok(SignalValue::Scalar((fast - slow) / slow * Decimal::ONE_HUNDRED))
}
fn reset(&mut self) {
self.fast_ema = None;
self.slow_ema = None;
self.bars = 0;
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::ohlcv::OhlcvBar;
use crate::signals::Signal;
use crate::types::{NanoTimestamp, Price, Quantity, Symbol};
use rust_decimal_macros::dec;
fn bar(c: &str) -> OhlcvBar {
let p = Price::new(c.parse().unwrap()).unwrap();
OhlcvBar {
symbol: Symbol::new("X").unwrap(),
open: p, high: p, low: p, close: p,
volume: Quantity::zero(),
ts_open: NanoTimestamp::new(0),
ts_close: NanoTimestamp::new(1),
tick_count: 1,
}
}
#[test]
fn test_ebw_invalid_periods() {
assert!(EmaBandWidth::new("e", 0, 26).is_err());
assert!(EmaBandWidth::new("e", 26, 12).is_err()); assert!(EmaBandWidth::new("e", 26, 26).is_err()); }
#[test]
fn test_ebw_unavailable_before_slow_period() {
let mut e = EmaBandWidth::new("e", 3, 5).unwrap();
for _ in 0..5 {
assert_eq!(e.update_bar(&bar("100")).unwrap(), SignalValue::Unavailable);
}
}
#[test]
fn test_ebw_constant_price_zero_width() {
let mut e = EmaBandWidth::new("e", 3, 5).unwrap();
for _ in 0..20 {
e.update_bar(&bar("100")).unwrap();
}
if let SignalValue::Scalar(v) = e.update_bar(&bar("100")).unwrap() {
assert!(v.abs() < dec!(0.0001), "expected ~0, got {v}");
} else {
panic!("expected Scalar");
}
}
#[test]
fn test_ebw_uptrend_positive() {
let mut e = EmaBandWidth::new("e", 3, 5).unwrap();
for i in 0u32..20 {
e.update_bar(&bar(&(100 + i).to_string())).unwrap();
}
if let SignalValue::Scalar(v) = e.update_bar(&bar("120")).unwrap() {
assert!(v > dec!(0), "expected positive band width in uptrend, got {v}");
} else {
panic!("expected Scalar");
}
}
#[test]
fn test_ebw_reset() {
let mut e = EmaBandWidth::new("e", 3, 5).unwrap();
for _ in 0..20 { e.update_bar(&bar("100")).unwrap(); }
assert!(e.is_ready());
e.reset();
assert!(!e.is_ready());
}
}