use crate::common::{validate_inputs, validate_options};
pub use crate::indicator_types::TIndicatorState;
use crate::indicators::max::{
calc as calc_max, calc_unchecked as calc_max_uncheked, output_length as max_output_length,
State as MaxState,
};
use crate::indicators::min::{
calc as calc_min, calc_unchecked as calc_min_uncheked, State as MinState,
};
use crate::types::{DisplayGroup, DisplayType, IndicatorError, IndicatorType, Info};
use serde::{Deserialize, Serialize};
pub const INPUTS_WIDTH: usize = 3;
pub const OPTIONS_WIDTH: usize = 1;
#[cfg(feature = "simd_assets")]
pub use crate::indicators::simd_indicators::willr_simd::indicator_by_assets;
#[cfg(feature = "simd_options")]
pub use crate::indicators::simd_indicators::willr_simd::indicator_by_options;
#[cfg(feature = "simd_assets")]
pub mod by_assets {
pub use crate::indicators::simd_indicators::willr_simd::indicator_by_assets as indicator;
}
#[cfg(feature = "simd_options")]
pub mod by_options {
pub use crate::indicators::simd_indicators::willr_simd::indicator_by_options as indicator;
}
#[derive(Serialize, Deserialize)]
pub struct IndicatorState {
state: State,
high: Vec<f64>,
low: Vec<f64>,
period: usize,
}
impl IndicatorState {
pub fn new(state: State, high: &[f64], low: &[f64], period: usize) -> Self {
Self {
state,
high: high[high.len() - period..].to_vec(),
low: low[low.len() - period..].to_vec(),
period,
}
}
}
impl TIndicatorState<3> 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] = *inputs;
self.high.extend_from_slice(high);
self.low.extend_from_slice(low);
let (mut willr_line, (mut min_line, mut max_line)) = {
let len = high.len();
(
crate::uninit_vec!(f64, len),
crate::init_optional_outputs_eff!(
optional_outputs, &[false, false],
min_line: len,
max_line: len
),
)
};
match self.period {
1..=13 => {
cycle_willr::<1>(
&self.high,
&self.low,
close,
self.period,
&mut self.state,
&mut willr_line,
(&mut min_line, &mut max_line),
);
}
14..30 => {
cycle_willr::<4>(
&self.high,
&self.low,
close,
self.period,
&mut self.state,
&mut willr_line,
(&mut min_line, &mut max_line),
);
}
_ => {
cycle_willr::<8>(
&self.high,
&self.low,
close,
self.period,
&mut self.state,
&mut willr_line,
(&mut min_line, &mut max_line),
);
}
}
self.high.drain(..self.high.len() - self.period);
self.low.drain(..self.low.len() - self.period);
Ok(vec![willr_line, min_line, max_line])
}
}
#[derive(Serialize, Deserialize)]
pub struct State {
pub min_state: MinState,
pub max_state: MaxState,
}
impl State {
pub fn new(min_state: (f64, usize), max_state: (f64, usize)) -> Self {
State {
min_state: MinState::new(min_state.0, min_state.1),
max_state: MaxState::new(max_state.0, max_state.1),
}
}
pub fn init_state(
high: &[f64],
low: &[f64],
period: usize,
min_max: (&mut [f64], &mut [f64]),
) -> Self {
let (min_line, max_line) = min_max;
let min_state = MinState::init_state(low, period, period - 1, min_line);
let max_state = MaxState::init_state(high, period, period - 1, max_line);
Self {
min_state,
max_state,
}
}
}
pub const INFO: Info = Info {
name: "willr",
full_name: "Williams %R",
indicator_type: IndicatorType::Momentum,
inputs: &["high", "low", "close"],
options: &["period"],
outputs: &["willr"],
optional_outputs: &["min", "max"],
display_groups: &[
DisplayGroup {
offset: None,
id: "willr",
label: "WILLR",
display_type: DisplayType::Indicator,
outputs: &["willr"],
},
DisplayGroup {
offset: None,
id: "min_max",
label: "Min & Max",
display_type: DisplayType::Overlay,
outputs: &["min", "max"],
},
],
};
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 high = inputs[0];
let low = inputs[1];
let close = inputs[2];
let (mut willr_line, (mut min_line, mut max_line)) = {
let len = high.len();
let capacity = output_length(len, options);
let min_max_capacity = max_output_length(len, options);
(
crate::uninit_vec!(f64, capacity),
crate::init_optional_outputs_eff!(
optional_outputs, &[false, false],
min_line: min_max_capacity,
max_line: min_max_capacity
),
)
};
let mut state = State::init_state(high, low, period, (&mut min_line, &mut max_line));
let optional_outputs = {
let (min_offset, max_offset) =
crate::slice_outputs_start!(willr_line.len(), min_line, max_line);
(&mut min_line[min_offset..], &mut max_line[max_offset..])
};
match period {
1..=13 => {
cycle_willr::<1>(
high,
low,
&close[period..],
period,
&mut state,
&mut willr_line,
optional_outputs,
);
}
14..25 => {
cycle_willr::<4>(
high,
low,
&close[period..],
period,
&mut state,
&mut willr_line,
optional_outputs,
);
}
_ => {
cycle_willr::<8>(
high,
low,
&close[period..],
period,
&mut state,
&mut willr_line,
optional_outputs,
);
}
}
Ok((
vec![willr_line, min_line, max_line],
IndicatorState::new(state, high, low, period),
))
}
fn cycle_willr<const N: usize>(
high: &[f64],
low: &[f64],
close: &[f64],
period: usize,
state: &mut State,
willr_line: &mut [f64],
optional_outputs: (&mut [f64], &mut [f64]),
) {
let (min_line, max_line) = optional_outputs;
let (has_optional, want_min, want_max) = crate::calc_want_flags!(min_line, max_line);
let periods = (period, period - 1);
let mut i = period;
for (j, (close, willr)) in close.iter().zip(willr_line.iter_mut()).enumerate() {
let (min, max);
unsafe {
(*willr, min, max) = calc_unchecked::<N>(state, high, low, close, i, periods);
}
if has_optional {
crate::store_optional_outputs!(j,
want_min, min_line => min,
want_max, max_line => max
);
}
i += 1;
}
}
#[inline(always)]
pub fn calc(
state: &mut State,
high: &[f64],
low: &[f64],
close: &f64,
i: usize,
periods: (usize, usize),
) -> (f64, f64, f64) {
let (min, _) = calc_min(&mut state.min_state, low, i, periods);
let (max, _) = calc_max(&mut state.max_state, high, i, periods);
if (max - min).abs() < f64::EPSILON {
return (0.0, min, max);
}
(100.0 * (max - close) / (max - min), min, max)
}
#[inline(always)]
pub unsafe fn calc_unchecked<const N: usize>(
state: &mut State,
high: &[f64],
low: &[f64],
close: &f64,
i: usize,
periods: (usize, usize),
) -> (f64, f64, f64) {
let (min, _) = calc_min_uncheked::<N>(&mut state.min_state, low, i, periods);
let (max, _) = calc_max_uncheked::<N>(&mut state.max_state, high, i, periods);
if (max - min).abs() < f64::EPSILON {
return (0.0, min, max);
}
(100.0 * (max - close) / (max - min), min, max)
}