use super::{IndicatorInstance, IndicatorResult};
use crate::core::{Error, OHLCV};
pub trait IndicatorConfig: Clone {
type Instance: IndicatorInstance<Config = Self>;
const NAME: &'static str;
fn validate(&self) -> bool;
fn set(&mut self, name: &str, value: String) -> Result<(), Error>;
fn size(&self) -> (u8, u8);
fn init<T: OHLCV>(self, initial_value: &T) -> Result<Self::Instance, Error>;
fn name(&self) -> &'static str {
Self::NAME
}
fn init_fn<'a, T: OHLCV>(
self,
initial_value: &'a T,
) -> Result<Box<dyn FnMut(&'a T) -> IndicatorResult>, Error>
where
Self: 'static,
{
let instance = self.init(initial_value)?;
Ok(instance.into_fn())
}
fn over<T, S>(self, inputs: S) -> Result<Vec<IndicatorResult>, Error>
where
T: OHLCV,
S: AsRef<[T]>,
Self: Sized,
{
let inputs_ref = inputs.as_ref();
if inputs_ref.is_empty() {
return Ok(Vec::new());
}
let mut state = self.init(&inputs_ref[0])?;
Ok(IndicatorInstance::over(&mut state, inputs))
}
}