use crate::error::FinError;
use crate::signals::{BarInput, Signal, SignalValue};
use rust_decimal::Decimal;
use std::collections::VecDeque;
pub struct Dpo {
name: String,
period: usize,
shift: usize,
window: VecDeque<Decimal>,
}
impl Dpo {
pub fn new(name: impl Into<String>, period: usize) -> Result<Self, FinError> {
if period < 2 {
return Err(FinError::InvalidPeriod(period));
}
let shift = period / 2 + 1;
Ok(Self {
name: name.into(),
period,
shift,
window: VecDeque::with_capacity(period + shift),
})
}
}
impl Signal for Dpo {
fn name(&self) -> &str {
&self.name
}
fn update(&mut self, bar: &BarInput) -> Result<SignalValue, FinError> {
self.window.push_back(bar.close);
let needed = self.period + self.shift;
if self.window.len() > needed {
self.window.pop_front();
}
if self.window.len() < needed {
return Ok(SignalValue::Unavailable);
}
let sma_sum: Decimal = self.window.iter().take(self.period).sum();
#[allow(clippy::cast_possible_truncation)]
let sma = sma_sum / Decimal::from(self.period as u32);
let compare_close = self.window[self.period - 1];
Ok(SignalValue::Scalar(compare_close - sma))
}
fn is_ready(&self) -> bool {
self.window.len() >= self.period + self.shift
}
fn period(&self) -> usize {
self.period
}
fn reset(&mut self) {
self.window.clear();
}
}
#[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(close: Decimal) -> OhlcvBar {
let p = Price::new(close).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_dpo_period_less_than_2_fails() {
assert!(Dpo::new("d", 0).is_err());
assert!(Dpo::new("d", 1).is_err());
}
#[test]
fn test_dpo_period_accessor() {
let d = Dpo::new("d4", 4).unwrap();
assert_eq!(d.period(), 4);
}
#[test]
fn test_dpo_unavailable_before_period() {
let mut d = Dpo::new("d4", 4).unwrap();
for _ in 0..6 {
assert_eq!(d.update_bar(&bar(dec!(100))).unwrap(), SignalValue::Unavailable);
}
assert!(!d.is_ready());
}
#[test]
fn test_dpo_flat_series_is_zero() {
let mut d = Dpo::new("d4", 4).unwrap();
let needed = 4 + (4 / 2 + 1); for _ in 0..needed {
d.update_bar(&bar(dec!(100))).unwrap();
}
let result = d.update_bar(&bar(dec!(100))).unwrap();
assert_eq!(result, SignalValue::Scalar(dec!(0)));
}
#[test]
fn test_dpo_reset() {
let mut d = Dpo::new("d4", 4).unwrap();
let needed = 4 + (4 / 2 + 1);
for _ in 0..needed {
d.update_bar(&bar(dec!(100))).unwrap();
}
assert!(d.is_ready());
d.reset();
assert!(!d.is_ready());
assert_eq!(d.update_bar(&bar(dec!(100))).unwrap(), SignalValue::Unavailable);
}
}