use crate::{
validate_all_same_len, validate_finite_slices, validate_output_len, CompactOutput, Float,
IndicatorConfig, OutputRange, PreparedBatchRunner, Result, StreamingComputation, TalibError,
};
#[cfg(not(feature = "std"))]
use alloc::vec::Vec;
#[cfg(feature = "std")]
use std::vec::Vec;
#[derive(Debug, Clone, Copy)]
pub struct WCLPRICEInput<'a> {
pub high: &'a [Float],
pub low: &'a [Float],
pub close: &'a [Float],
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct WCLPRICETick {
pub high: Float,
pub low: Float,
pub close: Float,
}
fn validate_wclprice_input(input: WCLPRICEInput<'_>) -> Result<usize> {
let len = validate_all_same_len(&[
("high", input.high.len()),
("low", input.low.len()),
("close", input.close.len()),
])?;
validate_finite_slices(&[
("high", input.high),
("low", input.low),
("close", input.close),
])?;
Ok(len)
}
fn wclprice_kernel(input: WCLPRICEInput<'_>, len: usize, output: &mut [Float]) -> OutputRange {
for (idx, output_value) in output.iter_mut().enumerate().take(len) {
*output_value =
(input.high[idx] + input.low[idx] + 2.0 as Float * input.close[idx]) / 4.0 as Float;
}
OutputRange::new(0, len)
}
#[allow(non_snake_case)]
pub fn WCLPRICE(
high: &[Float],
low: &[Float],
close: &[Float],
out_real: &mut [Float],
) -> Result<OutputRange> {
let input = WCLPRICEInput { high, low, close };
let len = validate_wclprice_input(input)?;
validate_output_len("WCLPRICE", out_real.len(), len)?;
Ok(wclprice_kernel(input, len, out_real))
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash)]
pub struct WCLPRICEConfig;
impl WCLPRICEConfig {
#[inline]
pub const fn new() -> Self {
Self
}
}
impl crate::traits::sealed::Sealed for WCLPRICEConfig {}
impl IndicatorConfig for WCLPRICEConfig {
type Input<'a> = WCLPRICEInput<'a>;
type Output = Vec<Float>;
type OutputMut<'a> = &'a mut [Float];
type BatchRunner = WCLPRICEBatchRunner;
type Stream = WCLPRICEStream;
fn lookback(&self) -> usize {
0
}
fn compute<'a>(&self, input: Self::Input<'a>) -> Result<CompactOutput<Self::Output>> {
let len = validate_wclprice_input(input)?;
let mut values = Vec::with_capacity(len);
values.resize(len, 0.0 as Float);
let range = wclprice_kernel(input, len, &mut values);
CompactOutput::new(len, range, values)
}
fn compute_into<'a>(
&self,
input: Self::Input<'a>,
output: Self::OutputMut<'a>,
) -> Result<OutputRange> {
let len = validate_wclprice_input(input)?;
validate_output_len("WCLPRICE", output.len(), len)?;
Ok(wclprice_kernel(input, len, output))
}
fn prepare_batch(&self, max_input_len: usize) -> Result<Self::BatchRunner> {
Ok(WCLPRICEBatchRunner {
config: *self,
max_input_len,
})
}
fn stream(&self) -> Result<Self::Stream> {
Ok(WCLPRICEStream)
}
}
#[derive(Debug, Clone)]
pub struct WCLPRICEBatchRunner {
config: WCLPRICEConfig,
max_input_len: usize,
}
impl crate::traits::sealed::Sealed for WCLPRICEBatchRunner {}
impl PreparedBatchRunner<WCLPRICEConfig> for WCLPRICEBatchRunner {
fn max_input_len(&self) -> usize {
self.max_input_len
}
fn compute_into<'a>(
&mut self,
input: <WCLPRICEConfig as IndicatorConfig>::Input<'a>,
output: <WCLPRICEConfig as IndicatorConfig>::OutputMut<'a>,
) -> Result<OutputRange>
where
WCLPRICEConfig: 'a,
{
let actual_input_len = input.high.len().max(input.low.len()).max(input.close.len());
if actual_input_len > self.max_input_len {
return Err(TalibError::prepared_capacity_exceeded(
self.max_input_len,
actual_input_len,
));
}
IndicatorConfig::compute_into(&self.config, input, output)
}
}
#[derive(Debug, Clone, Copy, Default)]
pub struct WCLPRICEStream;
impl crate::traits::sealed::Sealed for WCLPRICEStream {}
impl StreamingComputation<WCLPRICEConfig> for WCLPRICEStream {
type Tick = WCLPRICETick;
type TickOutput = Float;
fn next(&mut self, input: Self::Tick) -> Result<Option<Self::TickOutput>> {
validate_finite_slices(&[
("high", &[input.high]),
("low", &[input.low]),
("close", &[input.close]),
])?;
Ok(Some(
(input.high + input.low + 2.0 as Float * input.close) / 4.0 as Float,
))
}
fn reset(&mut self) {}
}