use crate::common::{validate_inputs, validate_options};
pub use crate::indicator_types::TIndicatorState;
use crate::indicators::linreg::calc as calc_linreg;
pub use crate::indicators::linreg::State;
use crate::types::{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::tsf_simd::indicator_by_assets;
#[cfg(feature = "simd_options")]
pub use crate::indicators::simd_indicators::tsf_simd::indicator_by_options;
#[cfg(feature = "simd_assets")]
pub mod by_assets {
pub use crate::indicators::simd_indicators::tsf_simd::indicator_by_assets as indicator;
}
#[cfg(feature = "simd_options")]
pub mod by_options {
pub use crate::indicators::simd_indicators::tsf_simd::indicator_by_options as indicator;
}
#[derive(Serialize, Deserialize)]
pub struct IndicatorState {
state: State,
real: Vec<f64>,
period: usize,
}
impl IndicatorState {
pub fn new(state: State, real: &[f64], period: usize) -> Self {
Self {
state,
real: real[real.len() - period + 1..].to_vec(),
period,
}
}
}
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 tsf_line, mut linreg_line, mut slope_line, mut intercept_line);
{
let capacity = inputs[0].len();
(linreg_line, slope_line, intercept_line) = crate::init_optional_outputs_eff!(
optional_outputs, &[false, false, false],
linreg_line: capacity,
slope_line: capacity,
intercept_line: capacity
);
tsf_line = crate::uninit_vec!(f64, capacity);
}
cycle_tsf(
&self.real,
&mut self.state,
self.period,
&mut tsf_line,
(&mut linreg_line, &mut slope_line, &mut intercept_line),
);
self.real.drain(..self.real.len() - self.period + 1);
Ok(vec![tsf_line, linreg_line, slope_line, intercept_line])
}
}
pub fn info() -> Info<'static> {
Info {
name: "tsf",
display_type: DisplayType::Overlay,
indicator_type: IndicatorType::Trend,
full_name: "Time Series Forecast",
inputs: &["real"],
options: &["period"],
outputs: &["tsf"],
optional_outputs: &["linreg", "linregslope", "linregintercept"],
}
}
pub fn min_data_accuracy(options: &[f64], _decimals: usize) -> usize {
min_data(options)
}
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 tsf_line, mut linreg_line, mut slope_line, mut intercept_line);
{
let capacity = output_length(real.len(), options);
(linreg_line, slope_line, intercept_line) = crate::init_optional_outputs_eff!(
optional_outputs, &[false, false, false],
linreg_line: capacity,
slope_line: capacity,
intercept_line: capacity
);
tsf_line = crate::uninit_vec!(f64, capacity); }
let mut state = State::init_state(&real[1..period], period);
cycle_tsf(
&real[1..],
&mut state,
period,
&mut tsf_line,
(&mut linreg_line, &mut slope_line, &mut intercept_line),
);
Ok((
vec![tsf_line, linreg_line, slope_line, intercept_line],
IndicatorState::new(state, real, period),
))
}
fn cycle_tsf(
real: &[f64],
state: &mut State,
period: usize,
tsf_line: &mut [f64],
out_vecs: (&mut [f64], &mut [f64], &mut [f64]),
) {
let (linreg_line, slope_line, intercept_line) = out_vecs;
let (has_optional, want_linreg, want_slope, want_intercept) =
crate::calc_want_flags!(linreg_line, slope_line, intercept_line);
for (j, i) in (period - 1..real.len()).enumerate() {
let (prev_value, value) = unsafe { (*real.get_unchecked(j), *real.get_unchecked(i)) };
let (tsf, linreg, slope, intercept) = calc(state, prev_value, value, period);
unsafe { *tsf_line.get_unchecked_mut(j) = tsf };
if has_optional {
crate::store_optional_outputs!(j,
want_linreg, linreg_line => linreg,
want_slope, slope_line => slope,
want_intercept, intercept_line => intercept
);
}
}
}
#[inline(always)]
pub fn calc(state: &mut State, prev_value: f64, value: f64, period: usize) -> (f64, f64, f64, f64) {
let (linreg, slope, intercept);
(linreg, slope, intercept) = calc_linreg(state, prev_value, value, period);
let tsf = slope.mul_add((period + 1) as f64, intercept);
(tsf, linreg, slope, intercept)
}