fast-ta 0.1.0

High-performance technical analysis indicators with batch, prepared, and streaming APIs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
//! Moving Average with Variable Period (MAVP).
//!
//! Each Observation is paired with an aligned integer Period selection. The
//! selection is clamped to immutable configured bounds and evaluated with one
//! qualified [`PeriodMAType`](super::PeriodMAType).

use super::ma::{ma_kernel, ma_lookback, MAConfig, MAStream, PeriodMAType};
use crate::{
    validate_finite_slice, validate_input_len, validate_output_len, validate_same_len,
    CompactOutput, Float, IndicatorConfig, OutputRange, PreparedBatchRunner, Result,
    StreamingComputation, TalibError,
};

#[cfg(not(feature = "std"))]
use alloc::vec::Vec;
#[cfg(feature = "std")]
use std::vec::Vec;

/// Borrowed aligned input series for MAVP Batch Computation.
#[derive(Debug, Clone, Copy)]
pub struct MAVPInput<'a> {
    /// Observation Series.
    pub real: &'a [Float],
    /// Aligned integer Period Selection Series.
    pub periods: &'a [usize],
}

/// One aligned MAVP streaming Tick.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct MAVPTick {
    /// Observation value.
    pub real: Float,
    /// Integer Period selection, clamped to the configured bounds.
    pub period: usize,
}

#[inline]
fn clamp_period(period: usize, minimum: usize, maximum: usize) -> usize {
    period.clamp(minimum, maximum)
}

#[inline]
fn requires_fallible_preflight(ma_type: PeriodMAType) -> bool {
    match ma_type {
        PeriodMAType::DEMA | PeriodMAType::TEMA | PeriodMAType::T3 => true,
        PeriodMAType::SMA
        | PeriodMAType::EMA
        | PeriodMAType::WMA
        | PeriodMAType::TRIMA
        | PeriodMAType::KAMA => false,
    }
}

fn validate_mavp_config(
    minimum_period: usize,
    maximum_period: usize,
    ma_type: PeriodMAType,
) -> Result<usize> {
    if minimum_period > maximum_period {
        return Err(TalibError::invalid_input(
            "minimum_period must not exceed maximum_period",
        ));
    }
    ma_lookback(minimum_period, ma_type)?;
    ma_lookback(maximum_period, ma_type)
}

fn validate_mavp_input(input: MAVPInput<'_>, lookback: usize) -> Result<usize> {
    validate_same_len("real", input.real.len(), "periods", input.periods.len())?;
    validate_finite_slice("real", input.real)?;
    validate_input_len(input.real.len(), lookback)
}

fn collect_used_periods(
    periods: &[usize],
    lookback: usize,
    minimum: usize,
    maximum: usize,
    used_periods: &mut Vec<usize>,
) {
    used_periods.clear();
    for &selection in &periods[lookback..] {
        let period = clamp_period(selection, minimum, maximum);
        if !used_periods.contains(&period) {
            used_periods.push(period);
        }
    }
}

fn mavp_kernel(
    input: MAVPInput<'_>,
    config: MAVPConfig,
    lookback: usize,
    count: usize,
    output: &mut [Float],
    ma_scratch: &mut [Float],
    used_periods: &mut Vec<usize>,
) -> Result<OutputRange> {
    if count == 0 {
        used_periods.clear();
        return Ok(OutputRange::empty());
    }

    collect_used_periods(
        input.periods,
        lookback,
        config.minimum_period,
        config.maximum_period,
        used_periods,
    );
    if requires_fallible_preflight(config.ma_type) {
        for &period in used_periods.iter() {
            let period_lookback = ma_lookback(period, config.ma_type)?;
            let start_offset = lookback - period_lookback;
            ma_kernel(
                &input.real[start_offset..],
                period,
                config.ma_type,
                period_lookback,
                count,
                &mut ma_scratch[..count],
            )?;
        }
    }

    for &period in used_periods.iter() {
        let period_lookback = ma_lookback(period, config.ma_type)?;
        let start_offset = lookback - period_lookback;
        ma_kernel(
            &input.real[start_offset..],
            period,
            config.ma_type,
            period_lookback,
            count,
            &mut ma_scratch[..count],
        )?;

        for source_idx in lookback..input.real.len() {
            if clamp_period(
                input.periods[source_idx],
                config.minimum_period,
                config.maximum_period,
            ) == period
            {
                output[source_idx - lookback] = ma_scratch[source_idx - lookback];
            }
        }
    }

    Ok(OutputRange::new(lookback, count))
}

/// Variable-period Period-based Moving Average Batch Computation.
///
/// `periods` must align exactly with `real`. Each integer selection is clamped
/// to `[minimum_period, maximum_period]` without floating-point conversion.
/// Each selected definition is seeded so its first result aligns with the
/// maximum-Period MAVP Lookback, matching recursive MAVP restart semantics.
/// This caller-owned convenience function allocates two algorithm scratch
/// buffers; use [`MAVPBatchRunner`] to reserve and reuse them.
#[allow(non_snake_case)]
pub fn MAVP(
    real: &[Float],
    periods: &[usize],
    minimum_period: usize,
    maximum_period: usize,
    ma_type: PeriodMAType,
    out_real: &mut [Float],
) -> Result<OutputRange> {
    let config = MAVPConfig::new(minimum_period, maximum_period, ma_type)?;
    let input = MAVPInput { real, periods };
    let lookback = config.lookback;
    let count = validate_mavp_input(input, lookback)?;
    validate_output_len("MAVP", out_real.len(), count)?;

    let mut ma_scratch = Vec::new();
    ma_scratch.resize(count, 0.0 as Float);
    let mut used_periods = Vec::with_capacity(count);
    mavp_kernel(
        input,
        config,
        lookback,
        count,
        out_real,
        &mut ma_scratch,
        &mut used_periods,
    )
}

/// Immutable MAVP Indicator Configuration.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct MAVPConfig {
    minimum_period: usize,
    maximum_period: usize,
    ma_type: PeriodMAType,
    lookback: usize,
}

impl MAVPConfig {
    /// Creates a bounded variable-period configuration.
    pub fn new(
        minimum_period: usize,
        maximum_period: usize,
        ma_type: PeriodMAType,
    ) -> Result<Self> {
        let lookback = validate_mavp_config(minimum_period, maximum_period, ma_type)?;
        Ok(Self {
            minimum_period,
            maximum_period,
            ma_type,
            lookback,
        })
    }

    /// Returns the immutable minimum Period bound.
    #[inline]
    pub const fn minimum_period(&self) -> usize {
        self.minimum_period
    }

    /// Returns the immutable maximum Period bound.
    #[inline]
    pub const fn maximum_period(&self) -> usize {
        self.maximum_period
    }

    /// Returns the selected Period-based Moving Average definition.
    #[inline]
    pub const fn ma_type(&self) -> PeriodMAType {
        self.ma_type
    }
}

impl crate::traits::sealed::Sealed for MAVPConfig {}

impl IndicatorConfig for MAVPConfig {
    type Input<'a> = MAVPInput<'a>;
    type Output = Vec<Float>;
    type OutputMut<'a> = &'a mut [Float];
    type BatchRunner = MAVPBatchRunner;
    type Stream = MAVPStream;

    #[inline]
    fn lookback(&self) -> usize {
        self.lookback
    }

    fn compute<'a>(&self, input: Self::Input<'a>) -> Result<CompactOutput<Self::Output>> {
        let count = validate_mavp_input(input, self.lookback)?;
        let mut values = Vec::with_capacity(count);
        values.resize(count, 0.0 as Float);
        let mut ma_scratch = Vec::new();
        ma_scratch.resize(count, 0.0 as Float);
        let mut used_periods = Vec::with_capacity(count);
        let range = mavp_kernel(
            input,
            *self,
            self.lookback,
            count,
            &mut values,
            &mut ma_scratch,
            &mut used_periods,
        )?;
        CompactOutput::new(input.real.len(), range, values)
    }

    fn compute_into<'a>(
        &self,
        input: Self::Input<'a>,
        output: Self::OutputMut<'a>,
    ) -> Result<OutputRange> {
        let count = validate_mavp_input(input, self.lookback)?;
        validate_output_len("MAVP", output.len(), count)?;
        let mut ma_scratch = Vec::new();
        ma_scratch.resize(count, 0.0 as Float);
        let mut used_periods = Vec::with_capacity(count);
        mavp_kernel(
            input,
            *self,
            self.lookback,
            count,
            output,
            &mut ma_scratch,
            &mut used_periods,
        )
    }

    fn prepare_batch(&self, max_input_len: usize) -> Result<Self::BatchRunner> {
        let scratch_capacity = max_input_len.saturating_sub(self.lookback);
        let mut ma_scratch = Vec::new();
        ma_scratch.resize(scratch_capacity, 0.0 as Float);
        Ok(MAVPBatchRunner {
            config: *self,
            max_input_len,
            ma_scratch,
            used_periods: Vec::with_capacity(scratch_capacity),
        })
    }

    fn stream(&self) -> Result<Self::Stream> {
        MAVPStream::new(*self)
    }
}

/// Prepared Batch Runner with all MAVP algorithm scratch reserved at creation.
#[derive(Debug, Clone)]
pub struct MAVPBatchRunner {
    config: MAVPConfig,
    max_input_len: usize,
    ma_scratch: Vec<Float>,
    used_periods: Vec<usize>,
}

impl crate::traits::sealed::Sealed for MAVPBatchRunner {}

impl PreparedBatchRunner<MAVPConfig> for MAVPBatchRunner {
    #[inline]
    fn max_input_len(&self) -> usize {
        self.max_input_len
    }

    fn compute_into<'a>(
        &mut self,
        input: <MAVPConfig as IndicatorConfig>::Input<'a>,
        output: <MAVPConfig as IndicatorConfig>::OutputMut<'a>,
    ) -> Result<OutputRange>
    where
        MAVPConfig: 'a,
    {
        let actual_input_len = input.real.len().max(input.periods.len());
        if actual_input_len > self.max_input_len {
            return Err(TalibError::prepared_capacity_exceeded(
                self.max_input_len,
                actual_input_len,
            ));
        }
        let count = validate_mavp_input(input, self.config.lookback)?;
        validate_output_len("MAVP", output.len(), count)?;
        mavp_kernel(
            input,
            self.config,
            self.config.lookback,
            count,
            output,
            &mut self.ma_scratch,
            &mut self.used_periods,
        )
    }
}

/// Independent MAVP Streaming Computation.
///
/// One independent selected-MA stream is prepared for every Period in the
/// configured inclusive bounds. Creation owns that storage; ticks allocate no
/// heap memory.
#[derive(Debug, Clone)]
pub struct MAVPStream {
    config: MAVPConfig,
    streams: Vec<MAStream>,
    count: usize,
}

impl MAVPStream {
    fn new(config: MAVPConfig) -> Result<Self> {
        let period_count = config.maximum_period - config.minimum_period + 1;
        let mut streams = Vec::with_capacity(period_count);
        for period in config.minimum_period..=config.maximum_period {
            streams.push(IndicatorConfig::stream(&MAConfig::new(
                period,
                config.ma_type,
            )?)?);
        }
        Ok(Self {
            config,
            streams,
            count: 0,
        })
    }
}

impl crate::traits::sealed::Sealed for MAVPStream {}

impl StreamingComputation<MAVPConfig> for MAVPStream {
    type Tick = MAVPTick;
    type TickOutput = Float;

    fn next(&mut self, input: Self::Tick) -> Result<Option<Self::TickOutput>> {
        validate_finite_slice("real", &[input.real])?;
        let selected_period = clamp_period(
            input.period,
            self.config.minimum_period,
            self.config.maximum_period,
        );
        let selected_index = selected_period - self.config.minimum_period;
        let source_idx = self.count;
        let mut selected_value = None;
        for (index, stream) in self.streams.iter_mut().enumerate() {
            let period = self.config.minimum_period + index;
            let period_lookback = ma_lookback(period, self.config.ma_type)
                .expect("validated MAVP Period must retain a valid lookback");
            let start_offset = self.config.lookback - period_lookback;
            let value = if source_idx < start_offset {
                None
            } else {
                StreamingComputation::<MAConfig>::next(stream, input.real)?
            };
            if index == selected_index {
                selected_value = value;
            }
        }
        self.count += 1;
        if source_idx < self.config.lookback {
            Ok(None)
        } else {
            Ok(selected_value)
        }
    }

    fn reset(&mut self) {
        for stream in &mut self.streams {
            StreamingComputation::<MAConfig>::reset(stream);
        }
        self.count = 0;
    }
}