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 ADInput<'a> {
pub high: &'a [Float],
pub low: &'a [Float],
pub close: &'a [Float],
pub volume: &'a [Float],
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct ADTick {
pub high: Float,
pub low: Float,
pub close: Float,
pub volume: Float,
}
pub(super) fn validate_hlcv(
high: &[Float],
low: &[Float],
close: &[Float],
volume: &[Float],
) -> Result<usize> {
let len = validate_all_same_len(&[
("high", high.len()),
("low", low.len()),
("close", close.len()),
("volume", volume.len()),
])?;
validate_finite_slices(&[
("high", high),
("low", low),
("close", close),
("volume", volume),
])?;
Ok(len)
}
#[inline]
pub(super) fn money_flow_volume(high: Float, low: Float, close: Float, volume: Float) -> Float {
let range = high - low;
if range <= 0.0 as Float {
0.0 as Float
} else {
(((close - low) - (high - close)) / range) * volume
}
}
fn ad_kernel(input: ADInput<'_>, len: usize, output: &mut [Float]) -> OutputRange {
let mut cumulative = 0.0 as Float;
for (idx, output_value) in output.iter_mut().enumerate().take(len) {
cumulative += money_flow_volume(
input.high[idx],
input.low[idx],
input.close[idx],
input.volume[idx],
);
*output_value = cumulative;
}
OutputRange::new(0, len)
}
#[allow(non_snake_case)]
pub fn AD(
high: &[Float],
low: &[Float],
close: &[Float],
volume: &[Float],
out_real: &mut [Float],
) -> Result<OutputRange> {
let input = ADInput {
high,
low,
close,
volume,
};
let len = validate_hlcv(high, low, close, volume)?;
validate_output_len("AD", out_real.len(), len)?;
Ok(ad_kernel(input, len, out_real))
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash)]
pub struct ADConfig;
impl ADConfig {
pub const fn new() -> Self {
Self
}
}
impl crate::traits::sealed::Sealed for ADConfig {}
impl IndicatorConfig for ADConfig {
type Input<'a> = ADInput<'a>;
type Output = Vec<Float>;
type OutputMut<'a> = &'a mut [Float];
type BatchRunner = ADBatchRunner;
type Stream = ADStream;
#[inline]
fn lookback(&self) -> usize {
0
}
fn compute<'a>(&self, input: Self::Input<'a>) -> Result<CompactOutput<Self::Output>> {
let len = validate_hlcv(input.high, input.low, input.close, input.volume)?;
let mut values = Vec::with_capacity(len);
values.resize(len, 0.0 as Float);
let range = ad_kernel(input, len, &mut values);
CompactOutput::new(len, range, values)
}
#[inline]
fn compute_into<'a>(
&self,
input: Self::Input<'a>,
output: Self::OutputMut<'a>,
) -> Result<OutputRange> {
AD(input.high, input.low, input.close, input.volume, output)
}
#[inline]
fn prepare_batch(&self, max_input_len: usize) -> Result<Self::BatchRunner> {
Ok(ADBatchRunner {
config: *self,
max_input_len,
})
}
#[inline]
fn stream(&self) -> Result<Self::Stream> {
Ok(ADStream {
cumulative: 0.0 as Float,
})
}
}
#[derive(Debug, Clone)]
pub struct ADBatchRunner {
config: ADConfig,
max_input_len: usize,
}
impl crate::traits::sealed::Sealed for ADBatchRunner {}
impl PreparedBatchRunner<ADConfig> for ADBatchRunner {
#[inline]
fn max_input_len(&self) -> usize {
self.max_input_len
}
#[inline]
fn compute_into<'a>(
&mut self,
input: <ADConfig as IndicatorConfig>::Input<'a>,
output: <ADConfig as IndicatorConfig>::OutputMut<'a>,
) -> Result<OutputRange>
where
ADConfig: 'a,
{
let actual_input_len = input
.high
.len()
.max(input.low.len())
.max(input.close.len())
.max(input.volume.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)]
pub struct ADStream {
cumulative: Float,
}
impl crate::traits::sealed::Sealed for ADStream {}
impl StreamingComputation<ADConfig> for ADStream {
type Tick = ADTick;
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]),
("volume", &[input.volume]),
])?;
self.cumulative += money_flow_volume(input.high, input.low, input.close, input.volume);
Ok(Some(self.cumulative))
}
#[inline]
fn reset(&mut self) {
self.cumulative = 0.0 as Float;
}
}