use crate::common::{validate_inputs, validate_options};
pub use crate::indicator_types::TIndicatorState;
use crate::types::{
DisplayGroup, DisplayType, IndicatorError, IndicatorType, Info,
};
use serde::{Deserialize, Serialize};
pub const INPUTS_WIDTH: usize = 2;
pub const OPTIONS_WIDTH: usize = 1;
#[cfg(feature = "simd_assets")]
pub use crate::indicators::simd_indicators::dm_simd::indicator_by_assets;
#[cfg(feature = "simd_options")]
pub use crate::indicators::simd_indicators::dm_simd::indicator_by_options;
#[cfg(feature = "simd_assets")]
pub mod by_assets {
pub use crate::indicators::simd_indicators::dm_simd::indicator_by_assets as indicator;
}
#[cfg(feature = "simd_options")]
pub mod by_options {
pub use crate::indicators::simd_indicators::dm_simd::indicator_by_options as indicator;
}
pub const INFO: Info = Info {
name: "dm",
full_name: "Directional Movement",
indicator_type: IndicatorType::Trend,
inputs: &["high", "low"],
options: &["period"],
outputs: &["plus_dm", "minus_dm"],
optional_outputs: &[],
display_groups: &[DisplayGroup {
offset: None,
id: "dm",
label: "DM",
display_type: DisplayType::Indicator,
outputs: &["plus_dm", "minus_dm"],
}],
};
#[derive(Serialize, Deserialize)]
pub struct State {
pub dmup: f64,
pub dmdown: f64,
pub prev_high: f64,
pub prev_low: f64,
}
#[derive(Serialize, Deserialize)]
pub struct IndicatorState {
state: State,
multiplier: f64,
}
impl IndicatorState {
pub fn new(state: State, multiplier: f64) -> Self {
Self { state, multiplier }
}
}
impl TIndicatorState<2> for IndicatorState {
fn batch_indicator(
&mut self,
inputs: &[&[f64]; INPUTS_WIDTH],
_optional_outputs: Option<&[bool]>,
) -> Result<Vec<Vec<f64>>, IndicatorError> {
validate_inputs(inputs, 1)?;
let (mut plus_dm_line, mut minus_dm_line) = {
let capacity = inputs[0].len();
(
crate::uninit_vec!(f64, capacity),
crate::uninit_vec!(f64, capacity),
)
};
let [high, low] = inputs;
cycle_calc(
high,
low,
&mut self.state,
self.multiplier,
&mut plus_dm_line,
&mut minus_dm_line,
);
Ok(vec![plus_dm_line, minus_dm_line])
}
}
impl State {
pub fn new(dmup: f64, dmdown: f64, prev_high: f64, prev_low: f64) -> Self {
Self {
dmup,
dmdown,
prev_high,
prev_low,
}
}
pub fn init_state(high: &[f64], low: &[f64], period: usize) -> State {
let mut state = State::new(0.0, 0.0, high[0], low[0]);
for (&h, &l) in high.iter().zip(low.iter()).take(period).skip(1) {
let (dp, dm) = calc_dp_dm(&mut state, h, l);
state.dmup += dp;
state.dmdown += dm;
}
state
}
}
pub fn min_data(options: &[f64]) -> usize {
options[0] as usize + 1
}
pub fn output_length(data_len: usize, options: &[f64]) -> usize {
data_len - min_data(options) + 1
}
pub fn indicator(
inputs: &[&[f64]; INPUTS_WIDTH],
options: &[f64; OPTIONS_WIDTH],
_optional_outputs: Option<&[bool]>,
) -> Result<(Vec<Vec<f64>>, IndicatorState), IndicatorError> {
validate_options(options)?;
let period = options[0] as usize;
validate_inputs(inputs, min_data(options))?;
let multiplier = multiplier(period);
let (mut plus_dm_line, mut minus_dm_line) = {
let capacity: usize = output_length(inputs[0].len(), options);
(
crate::uninit_vec!(f64, capacity),
crate::uninit_vec!(f64, capacity),
)
};
let mut state = State::init_state(inputs[0], inputs[1], period);
let (high, low) = (&inputs[0][period..], &inputs[1][period..]);
cycle_calc(
high,
low,
&mut state,
multiplier,
&mut plus_dm_line,
&mut minus_dm_line,
);
Ok((
vec![plus_dm_line, minus_dm_line],
IndicatorState::new(state, multiplier),
))
}
fn cycle_calc(
high: &[f64],
low: &[f64],
state: &mut State,
multiplier: f64,
plus_dm_line: &mut [f64],
minus_dm_line: &mut [f64],
) {
for i in 0..high.len() {
unsafe {
let (h, l) = (*high.get_unchecked(i), *low.get_unchecked(i));
let (dmup, dmdown) = calc(state, h, l, multiplier);
*plus_dm_line.get_unchecked_mut(i) = dmup;
*minus_dm_line.get_unchecked_mut(i) = dmdown;
}
}
}
#[inline(always)]
pub fn calc(state: &mut State, high: f64, low: f64, multiplier: f64) -> (f64, f64) {
let (dp, dm) = calc_dp_dm(state, high, low);
let (_, _) = calc_dmup_dmdown(state, dp, dm, multiplier);
(state.dmup, state.dmdown)
}
#[inline(always)]
fn calc_dmup_dmdown(state: &mut State, dp: f64, dm: f64, multiplier: f64) -> (f64, f64) {
state.dmup = state.dmup.mul_add(multiplier, dp);
state.dmdown = state.dmdown.mul_add(multiplier, dm);
(state.dmup, state.dmdown)
}
#[inline(always)]
pub fn calc_dp_dm(state: &mut State, high: f64, low: f64) -> (f64, f64) {
let mut dp = high - state.prev_high;
let mut dm = state.prev_low - low;
(state.prev_high, state.prev_low) = (high, low);
if dp < 0.0 {
dp = 0.0;
} else if dp > dm {
dm = 0.0;
}
if dm < 0.0 {
dm = 0.0;
} else if dm > dp {
dp = 0.0;
}
if dp > dm {
dm = 0.0;
} else if dm > dp {
dp = 0.0;
}
(dp, dm)
}
#[inline]
pub fn multiplier(period: usize) -> f64 {
((period - 1) as f64) / period as f64
}