#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use crate::core::{
Error, Method, MovingAverageConstructor, PeriodType, Source, ValueType, Window, OHLCV,
};
use crate::core::{IndicatorConfig, IndicatorInstance, IndicatorResult};
use crate::helpers::MA;
#[derive(Debug, Clone, Copy)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct DetrendedPriceOscillator<M: MovingAverageConstructor = MA> {
pub ma: M,
pub source: Source,
}
impl<M: MovingAverageConstructor> IndicatorConfig for DetrendedPriceOscillator<M> {
type Instance = DetrendedPriceOscillatorInstance<M>;
const NAME: &'static str = "DetrendedPriceOscillator";
fn init<T: OHLCV>(self, candle: &T) -> Result<Self::Instance, Error> {
if !self.validate() {
return Err(Error::WrongConfig);
}
let cfg = self;
let src = candle.source(cfg.source);
Ok(Self::Instance {
sma: cfg.ma.init(src)?, window: Window::new(cfg.ma.ma_period() / 2 + 1, src),
cfg,
})
}
fn validate(&self) -> bool {
self.ma.ma_period() > 1 && self.ma.ma_period() < PeriodType::MAX
}
fn set(&mut self, name: &str, value: String) -> Result<(), Error> {
match name {
"ma" => match value.parse() {
Err(_) => return Err(Error::ParameterParse(name.to_string(), value.to_string())),
Ok(value) => self.ma = value,
},
"source" => match value.parse() {
Err(_) => return Err(Error::ParameterParse(name.to_string(), value.to_string())),
Ok(value) => self.source = value,
},
_ => {
return Err(Error::ParameterParse(name.to_string(), value));
}
};
Ok(())
}
fn size(&self) -> (u8, u8) {
(1, 0)
}
}
impl Default for DetrendedPriceOscillator<MA> {
fn default() -> Self {
Self {
ma: MA::SMA(21),
source: Source::Close,
}
}
}
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct DetrendedPriceOscillatorInstance<M: MovingAverageConstructor = MA> {
cfg: DetrendedPriceOscillator<M>,
sma: M::Instance,
window: Window<ValueType>,
}
impl<M: MovingAverageConstructor> IndicatorInstance for DetrendedPriceOscillatorInstance<M> {
type Config = DetrendedPriceOscillator<M>;
fn config(&self) -> &Self::Config {
&self.cfg
}
fn next<T: OHLCV>(&mut self, candle: &T) -> IndicatorResult {
let src = candle.source(self.cfg.source);
let sma = self.sma.next(&src);
let left_src = self.window.push(src);
let dpo = left_src - sma;
IndicatorResult::new(&[dpo], &[])
}
}