use crate::common::validate_inputs;
pub use crate::indicator_types::TIndicatorState;
use crate::indicators::typprice::calc as calc_typprice;
pub use crate::indicators::typprice::{min_data, output_length};
use crate::types::{DisplayGroup, DisplayType, IndicatorError, IndicatorType, Info};
use serde::{Deserialize, Serialize};
pub const INPUTS_WIDTH: usize = 4;
pub const OPTIONS_WIDTH: usize = 0;
#[cfg(feature = "simd_assets")]
pub use crate::indicators::simd_indicators::vwap_simd::indicator_by_assets;
#[cfg(feature = "simd_assets")]
pub mod by_assets {
pub use crate::indicators::simd_indicators::vwap_simd::indicator_by_assets as indicator;
}
pub const INFO: Info = Info {
name: "vwap",
full_name: "Volume Weighted Average Price",
indicator_type: IndicatorType::Trend,
inputs: &["high", "low", "close", "volume"],
options: &[],
outputs: &["vwap"],
optional_outputs: &["typprice"],
display_groups: &[DisplayGroup {
offset: None,
id: "vwap",
label: "Price",
display_type: DisplayType::Overlay,
outputs: &["vwap", "typprice"],
}],
};
#[derive(Serialize, Deserialize)]
pub struct IndicatorState {
pub pv_sum: f64,
pub vol_sum: f64,
}
impl IndicatorState {
pub fn new() -> Self {
Self {
pv_sum: 0.0,
vol_sum: 0.0,
}
}
#[inline(always)]
pub fn calc(&mut self, high: f64, low: f64, close: f64, volume: f64) -> (f64, f64) {
let tp = calc_typprice(&high, &low, &close);
self.pv_sum += tp * volume;
self.vol_sum += volume;
(self.pv_sum / self.vol_sum, tp)
}
}
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 [high, low, close, volume] = *inputs;
let (mut vwap_line, mut typprice_line) = {
let len = high.len();
(
crate::uninit_vec!(f64, len),
crate::init_optional_outputs_eff!(
optional_outputs, &[false],
tp: len
),
)
};
cycle(
high,
low,
close,
volume,
self,
&mut vwap_line,
&mut typprice_line,
);
Ok(vec![vwap_line, typprice_line])
}
}
pub fn indicator(
inputs: &[&[f64]; INPUTS_WIDTH],
_options: &[f64; OPTIONS_WIDTH],
optional_outputs: Option<&[bool]>,
) -> Result<(Vec<Vec<f64>>, IndicatorState), IndicatorError> {
validate_inputs(inputs, min_data(_options))?;
let [high, low, close, volume] = *inputs;
let (mut vwap_line, mut typprice_line) = {
let capacity = output_length(high.len(), &[]);
(
crate::uninit_vec!(f64, capacity),
crate::init_optional_outputs_eff!(
optional_outputs, &[false],
tp: capacity
),
)
};
let mut state = IndicatorState::new();
cycle(
high,
low,
close,
volume,
&mut state,
&mut vwap_line,
&mut typprice_line,
);
Ok((vec![vwap_line, typprice_line], state))
}
fn cycle(
high: &[f64],
low: &[f64],
close: &[f64],
volume: &[f64],
state: &mut IndicatorState,
vwap_line: &mut [f64],
typprice_line: &mut [f64],
) {
let (_, want_tp) = crate::calc_want_flags!(typprice_line);
for i in 0..close.len() {
let tp;
unsafe {
(*vwap_line.get_unchecked_mut(i), tp) = state.calc(
*high.get_unchecked(i),
*low.get_unchecked(i),
*close.get_unchecked(i),
*volume.get_unchecked(i),
);
}
crate::store_optional_outputs!(i,
want_tp, typprice_line => tp
);
}
}
#[inline(always)]
pub fn calc(
high: f64,
low: f64,
close: f64,
volume: f64,
state: &mut IndicatorState,
) -> (f64, f64) {
state.calc(high, low, close, volume)
}