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 = 1;
pub const OPTIONS_WIDTH: usize = 1;
#[cfg(feature = "simd_assets")]
pub use crate::indicators::simd_indicators::ef_simd::indicator_by_assets;
#[cfg(feature = "simd_options")]
pub use crate::indicators::simd_indicators::ef_simd::indicator_by_options;
#[cfg(feature = "simd_assets")]
pub mod by_assets {
pub use crate::indicators::simd_indicators::ef_simd::indicator_by_assets as indicator;
}
#[cfg(feature = "simd_options")]
pub mod by_options {
pub use crate::indicators::simd_indicators::ef_simd::indicator_by_options as indicator;
}
pub const INFO: Info = Info {
name: "ef",
indicator_type: IndicatorType::Trend,
full_name: "Efficiency Ratio",
inputs: &["real"],
options: &["period"],
outputs: &["ef"],
optional_outputs: &[],
display_groups: &[DisplayGroup {
offset: None,
id: "ef",
label: "EF",
display_type: DisplayType::Indicator,
outputs: &["ef"],
}],
};
#[derive(Serialize, Deserialize)]
pub struct IndicatorState {
real: Vec<f64>,
period: usize,
sum: f64,
}
impl IndicatorState {
pub fn new(real: &[f64], sum: f64, period: usize) -> Self {
Self {
period,
sum,
real: real[real.len() - period - 1..].to_vec(),
}
}
}
impl TIndicatorState<1> for IndicatorState {
fn batch_indicator(
&mut self,
inputs: &[&[f64]; INPUTS_WIDTH],
_optional_outputs: Option<&[bool]>,
) -> Result<Vec<Vec<f64>>, IndicatorError> {
validate_inputs(inputs, 1)?;
self.real.extend_from_slice(inputs[0]);
let mut ef_line = {
let capacity = inputs[0].len();
crate::uninit_vec!(f64, capacity)
};
cycle_ef(&self.real, &mut self.sum, self.period, &mut ef_line);
self.real.drain(..self.real.len() - self.period - 1);
Ok(vec![ef_line])
}
}
pub fn init(real: &[f64], period: usize, ef_line: &mut [f64]) -> f64 {
let mut sum = (1..period).map(|i| (real[i] - real[i - 1]).abs()).sum();
let values = unsafe {
(
real.get_unchecked(period),
real.get_unchecked(period - 1),
real.get_unchecked(0),
&0.0,
)
};
let ef = calc(&mut sum, values, period, period);
ef_line[0] = ef;
sum
}
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 real = inputs[0];
let mut ef_line = {
let capacity = output_length(real.len(), options);
crate::uninit_vec!(f64, capacity)
};
let mut sum = init(real, period, &mut ef_line);
cycle_ef(real, &mut sum, period, &mut ef_line[1..]);
Ok((vec![ef_line], IndicatorState::new(real, sum, period)))
}
fn cycle_ef(real: &[f64], sum: &mut f64, period: usize, ef_line: &mut [f64]) {
for (j, i) in (period + 1..real.len()).enumerate() {
let values = unsafe {
(
real.get_unchecked(i),
real.get_unchecked(i - 1),
real.get_unchecked(j + 1),
real.get_unchecked(j),
)
};
let ef = calc(sum, values, period, i);
unsafe { *ef_line.get_unchecked_mut(j) = ef };
}
}
#[inline(always)]
pub fn calc(sum: &mut f64, values: (&f64, &f64, &f64, &f64), period: usize, i: usize) -> f64 {
let mut s = *sum;
let (value, prev_value, last_value, old_value) = values;
s += (value - prev_value).abs();
if i > period {
s -= (last_value - old_value).abs();
}
*sum = s;
if s != 0.0 {
(value - last_value).abs() / s
} else {
0.0
}
}