use crate::common::validate_inputs;
pub use crate::indicator_types::TIndicatorState;
use crate::indicators::{cybercycle, homodynediscriminator};
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::trendmode_simd::indicator_by_assets;
#[cfg(feature = "simd_options")]
pub use crate::indicators::simd_indicators::trendmode_simd::indicator_by_options;
#[cfg(feature = "simd_assets")]
pub mod by_assets {
pub use crate::indicators::simd_indicators::trendmode_simd::indicator_by_assets as indicator;
}
#[cfg(feature = "simd_options")]
pub mod by_options {
pub use crate::indicators::simd_indicators::trendmode_simd::indicator_by_options as indicator;
}
pub const INFO: Info = Info {
name: "trendmode",
indicator_type: IndicatorType::Trend,
full_name: "Ehlers TrendMode",
inputs: &["real"],
options: &["alpha"],
outputs: &["trendmode"],
optional_outputs: &["cycle", "peak"],
display_groups: &[
DisplayGroup {
offset: None,
id: "trendmode",
label: "Ehlers TrendMode",
display_type: DisplayType::Indicator,
outputs: &["trendmode"],
},
DisplayGroup {
offset: None,
id: "trendmode_cycle",
label: "TrendMode CyberCycle",
display_type: DisplayType::Indicator,
outputs: &["cycle"],
},
DisplayGroup {
offset: None,
id: "trendmode_peak",
label: "TrendMode Peak",
display_type: DisplayType::Indicator,
outputs: &["peak"],
},
],
};
#[derive(Serialize, Deserialize)]
pub struct State {
pub hd: homodynediscriminator::State,
pub cc: cybercycle::State,
pub pk: f64,
}
impl State {
pub fn new() -> Self {
Self {
hd: homodynediscriminator::State::new(),
cc: cybercycle::State::new(),
pk: 0.0,
}
}
pub fn init_state(
real: &[f64],
alpha: f64, trendmode_line: &mut [f64],
cycle_line: &mut [f64],
peak_line: &mut [f64],
) -> Self {
let mut state = Self::new();
let fixed_mults = if alpha > 0.0 {
Some(cybercycle::multiplier(alpha))
} else {
None
};
for i in 0..6 {
state.cc.price_buf.push(real[i]);
if state.cc.price_buf.len() >= 4 {
let ab = 2.0_f64.mul_add(state.cc.price_buf[1], state.cc.price_buf[0]);
let cd = 2.0_f64.mul_add(state.cc.price_buf[2], state.cc.price_buf[3]);
state.cc.smooth_buf.push((ab + cd) * (1.0 / 6.0));
}
if state.cc.price_buf.len() >= 3 {
let seed = (state.cc.price_buf[0] - 2.0 * state.cc.price_buf[1]
+ state.cc.price_buf[2])
/ 4.0;
state.cc.cycle_prev2 = state.cc.cycle_prev;
state.cc.cycle_prev = seed;
}
state.hd.calc(real[i]);
}
for i in 6..22 {
state.hd.calc(real[i]);
let mults = match fixed_mults {
Some(m) => m,
None => cybercycle::multiplier(cybercycle::adaptive_alpha(state.hd.smooth_period)),
};
let cycle = unsafe { state.cc.calc_unchecked(real[i], mults) };
state.pk = (state.pk * 0.991).max(cycle.abs());
}
for i in 22..55 {
unsafe { state.hd.calc_unchecked(real[i]) };
let mults = match fixed_mults {
Some(m) => m,
None => cybercycle::multiplier(cybercycle::adaptive_alpha(state.hd.smooth_period)),
};
let cycle = unsafe { state.cc.calc_unchecked(real[i], mults) };
state.pk = (state.pk * 0.991).max(cycle.abs());
}
let trendmode = if alpha == 0.0 {
unsafe { state.calc_unchecked_adaptive(real[55]) }
} else {
unsafe { state.calc_unchecked(real[55], fixed_mults.unwrap()) }
};
trendmode_line[0] = trendmode;
if !cycle_line.is_empty() {
cycle_line[0] = state.cc.cycle_prev;
}
if !peak_line.is_empty() {
peak_line[0] = state.pk;
}
state
}
#[inline(always)]
pub unsafe fn calc_unchecked(&mut self, price: f64, multipliers: (f64, f64, f64)) -> f64 {
self.hd.calc_unchecked(price);
let cycle = self.cc.calc_unchecked(price, multipliers);
self.pk = (self.pk * 0.991).max(cycle.abs());
if self.pk > 0.0 && cycle.abs() < 0.2 * self.pk {
1.0
} else {
0.0
}
}
#[inline(always)]
pub unsafe fn calc_unchecked_adaptive(&mut self, price: f64) -> f64 {
self.hd.calc_unchecked(price);
let alpha = cybercycle::adaptive_alpha(self.hd.smooth_period);
let multipliers = cybercycle::multiplier(alpha);
let cycle = self.cc.calc_unchecked(price, multipliers);
self.pk = (self.pk * 0.991).max(cycle.abs());
if self.pk > 0.0 && cycle.abs() < 0.2 * self.pk {
1.0
} else {
0.0
}
}
}
impl Default for State {
fn default() -> Self {
Self::new()
}
}
#[derive(Serialize, Deserialize)]
pub struct IndicatorState {
pub(crate) alpha: f64,
pub(crate) multipliers: (f64, f64, f64),
pub(crate) state: State,
}
impl IndicatorState {
pub fn new(state: State, alpha: f64) -> Self {
let multipliers = if alpha > 0.0 {
cybercycle::multiplier(alpha)
} else {
(0.0, 0.0, 0.0)
};
Self {
alpha,
multipliers,
state,
}
}
}
impl TIndicatorState<INPUTS_WIDTH> 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 real = inputs[0];
let n = real.len();
let mut trendmode_line = crate::uninit_vec!(f64, n);
let (mut cycle_line, mut peak_line) = crate::init_optional_outputs_eff!(
optional_outputs, &[false, false],
cycle_line: n,
peak_line: n
);
run_trendmode(
real,
&mut self.state,
self.alpha,
self.multipliers,
&mut trendmode_line,
&mut cycle_line,
&mut peak_line,
);
Ok(vec![trendmode_line, cycle_line, peak_line])
}
}
pub fn min_data(_options: &[f64]) -> usize {
56
}
pub fn output_length(data_len: usize, _options: &[f64]) -> usize {
data_len.saturating_sub(55)
}
pub(crate) fn validate_options(options: &[f64; OPTIONS_WIDTH]) -> Result<(), IndicatorError> {
if options[0] < 0.0 || options[0] >= 1.0 {
return Err(IndicatorError::InvalidOptions);
}
Ok(())
}
pub fn indicator(
inputs: &[&[f64]; INPUTS_WIDTH],
options: &[f64; OPTIONS_WIDTH],
optional_outputs: Option<&[bool]>,
) -> Result<(Vec<Vec<f64>>, IndicatorState), IndicatorError> {
validate_options(options)?;
validate_inputs(inputs, min_data(options))?;
let alpha = options[0];
let multipliers = if alpha > 0.0 {
cybercycle::multiplier(alpha)
} else {
(0.0, 0.0, 0.0)
};
let real = inputs[0];
let n = real.len();
let capacity = output_length(n, options);
let mut trendmode_line = crate::uninit_vec!(f64, capacity);
let (mut cycle_line, mut peak_line) = crate::init_optional_outputs_eff!(
optional_outputs, &[false, false],
cycle_line: capacity,
peak_line: capacity
);
let mut state = State::init_state(
real,
alpha,
&mut trendmode_line,
&mut cycle_line,
&mut peak_line,
);
let (cycle_tail, peak_tail) = {
let o = crate::slice_outputs_start!(capacity - 1, cycle_line, peak_line);
(&mut cycle_line[o.0..], &mut peak_line[o.1..])
};
run_trendmode(
&real[min_data(options)..],
&mut state,
alpha,
multipliers,
&mut trendmode_line[1..],
cycle_tail,
peak_tail,
);
Ok((
vec![trendmode_line, cycle_line, peak_line],
IndicatorState::new(state, alpha),
))
}
fn run_trendmode(
real: &[f64],
state: &mut State,
alpha: f64,
multipliers: (f64, f64, f64),
trendmode_line: &mut [f64],
cycle_line: &mut [f64],
peak_line: &mut [f64],
) {
let (has_optional, want_cycle, want_peak) = crate::calc_want_flags!(cycle_line, peak_line);
if alpha == 0.0 {
for i in 0..real.len() {
let trendmode = unsafe { state.calc_unchecked_adaptive(*real.get_unchecked(i)) };
unsafe {
*trendmode_line.get_unchecked_mut(i) = trendmode;
}
if has_optional {
crate::store_optional_outputs!(i,
want_cycle, cycle_line => state.cc.cycle_prev,
want_peak, peak_line => state.pk
);
}
}
} else {
for i in 0..real.len() {
let trendmode = unsafe { state.calc_unchecked(*real.get_unchecked(i), multipliers) };
unsafe {
*trendmode_line.get_unchecked_mut(i) = trendmode;
}
if has_optional {
crate::store_optional_outputs!(i,
want_cycle, cycle_line => state.cc.cycle_prev,
want_peak, peak_line => state.pk
);
}
}
}
}