#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use crate::core::{Error, Method, PeriodType, OHLCV};
use crate::core::{IndicatorConfig, IndicatorInstance, IndicatorResult};
use crate::methods::{Highest, Lowest};
#[derive(Debug, Clone, Copy)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct DonchianChannel {
pub period: PeriodType,
}
impl IndicatorConfig for DonchianChannel {
type Instance = DonchianChannelInstance;
const NAME: &'static str = "DonchianChannel";
fn init<T: OHLCV>(self, candle: &T) -> Result<Self::Instance, Error> {
if !self.validate() {
return Err(Error::WrongConfig);
}
let cfg = self;
Ok(Self::Instance {
highest: Highest::new(cfg.period, &candle.high())?,
lowest: Lowest::new(cfg.period, &candle.low())?,
cfg,
})
}
fn validate(&self) -> bool {
self.period > 1
}
fn set(&mut self, name: &str, value: String) -> Result<(), Error> {
match name {
"period" => match value.parse() {
Err(_) => return Err(Error::ParameterParse(name.to_string(), value.to_string())),
Ok(value) => self.period = value,
},
_ => {
return Err(Error::ParameterParse(name.to_string(), value));
}
};
Ok(())
}
fn size(&self) -> (u8, u8) {
(3, 1)
}
}
impl Default for DonchianChannel {
fn default() -> Self {
Self { period: 20 }
}
}
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct DonchianChannelInstance {
cfg: DonchianChannel,
highest: Highest,
lowest: Lowest,
}
impl IndicatorInstance for DonchianChannelInstance {
type Config = DonchianChannel;
fn config(&self) -> &Self::Config {
&self.cfg
}
#[inline]
fn next<T: OHLCV>(&mut self, candle: &T) -> IndicatorResult {
let (high, low) = (candle.high(), candle.low());
let highest = self.highest.next(&high);
let lowest = self.lowest.next(&low);
let middle = (highest + lowest) * 0.5;
let signal1 = (high >= highest) as i8 - (low <= lowest) as i8;
IndicatorResult::new(&[lowest, middle, highest], &[signal1.into()])
}
}