use crate::common::validate_inputs;
pub use crate::indicator_types::TIndicatorState;
use crate::types::{DisplayType, IndicatorError, IndicatorType, Info};
use serde::{Deserialize, Serialize};
pub const INPUTS_WIDTH: usize = 3;
pub const OPTIONS_WIDTH: usize = 0;
#[cfg(feature = "simd_assets")]
pub use crate::indicators::simd_indicators::typprice_simd::indicator_by_assets;
#[cfg(feature = "simd_assets")]
pub mod by_assets {
pub use crate::indicators::simd_indicators::typprice_simd::indicator_by_assets as indicator;
}
#[derive(Serialize, Deserialize, Clone)]
pub struct IndicatorState;
impl TIndicatorState<3> for IndicatorState {
fn batch_indicator(
&mut self,
inputs: &[&[f64]; INPUTS_WIDTH],
_optional_outputs: Option<&[bool]>,
) -> Result<Vec<Vec<f64>>, IndicatorError> {
process(inputs)
}
}
pub fn info() -> Info<'static> {
Info {
name: "TYPPRICE",
full_name: "Typical Price",
display_type: DisplayType::Overlay,
indicator_type: IndicatorType::Price,
inputs: &["high", "low", "close"],
options: &[],
outputs: &["typprice"],
optional_outputs: &[],
}
}
pub fn min_data_accuracy(options: &[f64], _decimals: usize) -> usize {
min_data(options)
}
pub fn min_data(_options: &[f64]) -> usize {
1
}
pub fn output_length(data_len: usize, _options: &[f64]) -> usize {
data_len
}
pub fn indicator(
inputs: &[&[f64]; INPUTS_WIDTH],
_options: &[f64; OPTIONS_WIDTH],
_optional_outputs: Option<&[bool]>,
) -> Result<(Vec<Vec<f64>>, IndicatorState), IndicatorError> {
let outputs = process(inputs)?;
Ok((outputs, IndicatorState))
}
#[inline(always)]
fn process(inputs: &[&[f64]; INPUTS_WIDTH]) -> Result<Vec<Vec<f64>>, IndicatorError> {
validate_inputs(inputs, 1)?;
let high = inputs[0];
let low = inputs[1];
let close = inputs[2];
let mut typprice_line = crate::uninit_vec!(f64, high.len());
cycle_typprice((high, low, close), &mut typprice_line);
Ok(vec![typprice_line])
}
#[inline(always)]
fn cycle_typprice(inputs: (&[f64], &[f64], &[f64]), typprice_line: &mut [f64]) {
let (high, low, close) = inputs;
for i in 0..high.len() {
unsafe {
*typprice_line.get_unchecked_mut(i) = calc(
high.get_unchecked(i),
low.get_unchecked(i),
close.get_unchecked(i),
);
}
}
}
const DIV: f64 = 1.0 / 3.0;
#[inline(always)]
pub fn calc(high: &f64, low: &f64, close: &f64) -> f64 {
(high + low + close) * DIV
}