fast-ta 0.2.1

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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
//! Rolling midpoint Overlap Studies: MIDPOINT and MIDPRICE.

use crate::common::validate_finite_value;
use crate::{
    validate_all_same_len, validate_finite_slice, validate_finite_slices, validate_input_len,
    validate_output_len, CompactOutput, Float, IndicatorConfig, OutputRange, PreparedBatchRunner,
    Result, StreamingComputation, TalibError,
};

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

const MAX_PERIOD: usize = 100_000;

#[inline]
fn midpoint_lookback(timeperiod: usize) -> Result<usize> {
    if !(2..=MAX_PERIOD).contains(&timeperiod) {
        return Err(TalibError::invalid_period(
            timeperiod,
            format!("timeperiod must be in 2..={MAX_PERIOD}"),
        ));
    }
    Ok(timeperiod - 1)
}

#[derive(Debug, Clone)]
struct ExtremaScratch {
    min: Vec<usize>,
    max: Vec<usize>,
}

impl ExtremaScratch {
    fn with_capacity(max_input_len: usize) -> Self {
        Self {
            min: Vec::with_capacity(max_input_len),
            max: Vec::with_capacity(max_input_len),
        }
    }
}

#[allow(clippy::too_many_arguments)]
fn midpoint_kernel(
    low: &[Float],
    high: &[Float],
    timeperiod: usize,
    lookback: usize,
    count: usize,
    output: &mut [Float],
    min_queue: &mut Vec<usize>,
    max_queue: &mut Vec<usize>,
) -> OutputRange {
    min_queue.clear();
    max_queue.clear();
    if count == 0 {
        return OutputRange::empty();
    }

    let mut min_head = 0usize;
    let mut max_head = 0usize;
    for idx in 0..low.len() {
        if idx >= timeperiod {
            let expired_through = idx - timeperiod;
            while min_head < min_queue.len() && min_queue[min_head] <= expired_through {
                min_head += 1;
            }
            while max_head < max_queue.len() && max_queue[max_head] <= expired_through {
                max_head += 1;
            }
        }

        while min_queue.len() > min_head {
            let last = min_queue[min_queue.len() - 1];
            if low[idx] < low[last] {
                min_queue.pop();
            } else {
                break;
            }
        }
        while max_queue.len() > max_head {
            let last = max_queue[max_queue.len() - 1];
            if high[idx] > high[last] {
                max_queue.pop();
            } else {
                break;
            }
        }
        min_queue.push(idx);
        max_queue.push(idx);

        if idx >= lookback {
            let output_idx = idx - lookback;
            output[output_idx] =
                (low[min_queue[min_head]] + high[max_queue[max_head]]) / 2.0 as Float;
        }
    }

    OutputRange::new(lookback, count)
}

fn validate_midpoint(real: &[Float], timeperiod: usize) -> Result<(usize, usize)> {
    let lookback = midpoint_lookback(timeperiod)?;
    validate_finite_slice("real", real)?;
    let count = validate_input_len(real.len(), lookback)?;
    Ok((lookback, count))
}

/// TA-Lib-defined rolling midpoint of the highest and lowest real observations.
#[allow(non_snake_case)]
pub fn MIDPOINT(real: &[Float], timeperiod: usize, out_real: &mut [Float]) -> Result<OutputRange> {
    let (lookback, count) = validate_midpoint(real, timeperiod)?;
    validate_output_len("MIDPOINT", out_real.len(), count)?;
    let mut scratch = ExtremaScratch::with_capacity(real.len());
    Ok(midpoint_kernel(
        real,
        real,
        timeperiod,
        lookback,
        count,
        out_real,
        &mut scratch.min,
        &mut scratch.max,
    ))
}

/// Immutable rolling MIDPOINT Indicator Configuration.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct MIDPOINTConfig {
    period: usize,
}

impl MIDPOINTConfig {
    /// Creates a MIDPOINT configuration for a Period in `2..=100_000`.
    pub fn new(timeperiod: usize) -> Result<Self> {
        midpoint_lookback(timeperiod)?;
        Ok(Self { period: timeperiod })
    }

    /// Returns the configured Period.
    #[inline]
    pub const fn period(&self) -> usize {
        self.period
    }
}

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

impl IndicatorConfig for MIDPOINTConfig {
    type Input<'a> = &'a [Float];
    type Output = Vec<Float>;
    type OutputMut<'a> = &'a mut [Float];
    type BatchRunner = MIDPOINTBatchRunner;
    type Stream = MIDPOINTStream;

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

    fn compute<'a>(&self, input: Self::Input<'a>) -> Result<CompactOutput<Self::Output>> {
        let (lookback, count) = validate_midpoint(input, self.period)?;
        let mut values = vec![0.0 as Float; count];
        let mut scratch = ExtremaScratch::with_capacity(input.len());
        let range = midpoint_kernel(
            input,
            input,
            self.period,
            lookback,
            count,
            &mut values,
            &mut scratch.min,
            &mut scratch.max,
        );
        CompactOutput::new(input.len(), range, values)
    }

    fn compute_into<'a>(
        &self,
        input: Self::Input<'a>,
        output: Self::OutputMut<'a>,
    ) -> Result<OutputRange> {
        MIDPOINT(input, self.period, output)
    }

    fn prepare_batch(&self, max_input_len: usize) -> Result<Self::BatchRunner> {
        let scratch = ExtremaScratch::with_capacity(max_input_len);
        Ok(MIDPOINTBatchRunner {
            config: *self,
            max_input_len,
            min_scratch: scratch.min,
            max_scratch: scratch.max,
        })
    }

    fn stream(&self) -> Result<Self::Stream> {
        MIDPOINTStream::new(self.period)
    }
}

/// Reusable Prepared Batch Runner for MIDPOINT.
#[derive(Debug)]
pub struct MIDPOINTBatchRunner {
    config: MIDPOINTConfig,
    max_input_len: usize,
    min_scratch: Vec<usize>,
    max_scratch: Vec<usize>,
}

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

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

    fn compute_into<'a>(
        &mut self,
        input: <MIDPOINTConfig as IndicatorConfig>::Input<'a>,
        output: <MIDPOINTConfig as IndicatorConfig>::OutputMut<'a>,
    ) -> Result<OutputRange>
    where
        MIDPOINTConfig: 'a,
    {
        if input.len() > self.max_input_len {
            return Err(TalibError::prepared_capacity_exceeded(
                self.max_input_len,
                input.len(),
            ));
        }
        let (lookback, count) = validate_midpoint(input, self.config.period)?;
        validate_output_len("MIDPOINT", output.len(), count)?;
        Ok(midpoint_kernel(
            input,
            input,
            self.config.period,
            lookback,
            count,
            output,
            &mut self.min_scratch,
            &mut self.max_scratch,
        ))
    }
}

/// Independent Streaming Computation state for MIDPOINT.
#[derive(Debug, Clone)]
pub struct MIDPOINTStream {
    period: usize,
    buffer: Vec<Float>,
    index: usize,
    count: usize,
}

impl MIDPOINTStream {
    fn new(period: usize) -> Result<Self> {
        midpoint_lookback(period)?;
        Ok(Self {
            period,
            buffer: vec![0.0 as Float; period],
            index: 0,
            count: 0,
        })
    }
}

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

impl StreamingComputation<MIDPOINTConfig> for MIDPOINTStream {
    type Tick = Float;
    type TickOutput = Float;

    fn next(&mut self, input: Self::Tick) -> Result<Option<Self::TickOutput>> {
        validate_finite_value("input", 0, input)?;
        self.buffer[self.index] = input;
        if self.count < self.period {
            self.count += 1;
        }
        self.index = (self.index + 1) % self.period;
        if self.count < self.period {
            return Ok(None);
        }

        let values = &self.buffer[..self.count];
        let low = values.iter().copied().fold(values[0], Float::min);
        let high = values.iter().copied().fold(values[0], Float::max);
        Ok(Some((low + high) / 2.0 as Float))
    }

    fn reset(&mut self) {
        self.buffer.fill(0.0 as Float);
        self.index = 0;
        self.count = 0;
    }
}

/// Borrowed aligned high/low Observation Series for MIDPRICE.
#[derive(Debug, Clone, Copy)]
pub struct MIDPRICEInput<'a> {
    /// High price series.
    pub high: &'a [Float],
    /// Low price series.
    pub low: &'a [Float],
}

/// One aligned high/low MIDPRICE tick.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct MIDPRICETick {
    /// High price.
    pub high: Float,
    /// Low price.
    pub low: Float,
}

fn validate_midprice(input: MIDPRICEInput<'_>, timeperiod: usize) -> Result<(usize, usize)> {
    let lookback = midpoint_lookback(timeperiod)?;
    let len = validate_all_same_len(&[("high", input.high.len()), ("low", input.low.len())])?;
    validate_finite_slices(&[("high", input.high), ("low", input.low)])?;
    let count = validate_input_len(len, lookback)?;
    Ok((lookback, count))
}

/// TA-Lib-defined rolling midpoint of the highest high and lowest low.
#[allow(non_snake_case)]
pub fn MIDPRICE(
    high: &[Float],
    low: &[Float],
    timeperiod: usize,
    out_real: &mut [Float],
) -> Result<OutputRange> {
    let input = MIDPRICEInput { high, low };
    let (lookback, count) = validate_midprice(input, timeperiod)?;
    validate_output_len("MIDPRICE", out_real.len(), count)?;
    let mut scratch = ExtremaScratch::with_capacity(high.len());
    Ok(midpoint_kernel(
        low,
        high,
        timeperiod,
        lookback,
        count,
        out_real,
        &mut scratch.min,
        &mut scratch.max,
    ))
}

/// Immutable rolling MIDPRICE Indicator Configuration.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct MIDPRICEConfig {
    period: usize,
}

impl MIDPRICEConfig {
    /// Creates a MIDPRICE configuration for a Period in `2..=100_000`.
    pub fn new(timeperiod: usize) -> Result<Self> {
        midpoint_lookback(timeperiod)?;
        Ok(Self { period: timeperiod })
    }

    /// Returns the configured Period.
    #[inline]
    pub const fn period(&self) -> usize {
        self.period
    }
}

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

impl IndicatorConfig for MIDPRICEConfig {
    type Input<'a> = MIDPRICEInput<'a>;
    type Output = Vec<Float>;
    type OutputMut<'a> = &'a mut [Float];
    type BatchRunner = MIDPRICEBatchRunner;
    type Stream = MIDPRICEStream;

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

    fn compute<'a>(&self, input: Self::Input<'a>) -> Result<CompactOutput<Self::Output>> {
        let (lookback, count) = validate_midprice(input, self.period)?;
        let mut values = vec![0.0 as Float; count];
        let mut scratch = ExtremaScratch::with_capacity(input.high.len());
        let range = midpoint_kernel(
            input.low,
            input.high,
            self.period,
            lookback,
            count,
            &mut values,
            &mut scratch.min,
            &mut scratch.max,
        );
        CompactOutput::new(input.high.len(), range, values)
    }

    fn compute_into<'a>(
        &self,
        input: Self::Input<'a>,
        output: Self::OutputMut<'a>,
    ) -> Result<OutputRange> {
        let (lookback, count) = validate_midprice(input, self.period)?;
        validate_output_len("MIDPRICE", output.len(), count)?;
        let mut scratch = ExtremaScratch::with_capacity(input.high.len());
        Ok(midpoint_kernel(
            input.low,
            input.high,
            self.period,
            lookback,
            count,
            output,
            &mut scratch.min,
            &mut scratch.max,
        ))
    }

    fn prepare_batch(&self, max_input_len: usize) -> Result<Self::BatchRunner> {
        let scratch = ExtremaScratch::with_capacity(max_input_len);
        Ok(MIDPRICEBatchRunner {
            config: *self,
            max_input_len,
            min_scratch: scratch.min,
            max_scratch: scratch.max,
        })
    }

    fn stream(&self) -> Result<Self::Stream> {
        MIDPRICEStream::new(self.period)
    }
}

/// Reusable Prepared Batch Runner for MIDPRICE.
#[derive(Debug)]
pub struct MIDPRICEBatchRunner {
    config: MIDPRICEConfig,
    max_input_len: usize,
    min_scratch: Vec<usize>,
    max_scratch: Vec<usize>,
}

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

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

    fn compute_into<'a>(
        &mut self,
        input: <MIDPRICEConfig as IndicatorConfig>::Input<'a>,
        output: <MIDPRICEConfig as IndicatorConfig>::OutputMut<'a>,
    ) -> Result<OutputRange>
    where
        MIDPRICEConfig: 'a,
    {
        let actual_input_len = input.high.len().max(input.low.len());
        if actual_input_len > self.max_input_len {
            return Err(TalibError::prepared_capacity_exceeded(
                self.max_input_len,
                actual_input_len,
            ));
        }
        let (lookback, count) = validate_midprice(input, self.config.period)?;
        validate_output_len("MIDPRICE", output.len(), count)?;
        Ok(midpoint_kernel(
            input.low,
            input.high,
            self.config.period,
            lookback,
            count,
            output,
            &mut self.min_scratch,
            &mut self.max_scratch,
        ))
    }
}

/// Independent Streaming Computation state for MIDPRICE.
#[derive(Debug, Clone)]
pub struct MIDPRICEStream {
    period: usize,
    highs: Vec<Float>,
    lows: Vec<Float>,
    index: usize,
    count: usize,
}

impl MIDPRICEStream {
    fn new(period: usize) -> Result<Self> {
        midpoint_lookback(period)?;
        Ok(Self {
            period,
            highs: vec![0.0 as Float; period],
            lows: vec![0.0 as Float; period],
            index: 0,
            count: 0,
        })
    }
}

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

impl StreamingComputation<MIDPRICEConfig> for MIDPRICEStream {
    type Tick = MIDPRICETick;
    type TickOutput = Float;

    fn next(&mut self, input: Self::Tick) -> Result<Option<Self::TickOutput>> {
        validate_finite_value("high", 0, input.high)?;
        validate_finite_value("low", 0, input.low)?;

        self.highs[self.index] = input.high;
        self.lows[self.index] = input.low;
        if self.count < self.period {
            self.count += 1;
        }
        self.index = (self.index + 1) % self.period;
        if self.count < self.period {
            return Ok(None);
        }

        let highs = &self.highs[..self.count];
        let lows = &self.lows[..self.count];
        let high = highs.iter().copied().fold(highs[0], Float::max);
        let low = lows.iter().copied().fold(lows[0], Float::min);
        Ok(Some((high + low) / 2.0 as Float))
    }

    fn reset(&mut self) {
        self.highs.fill(0.0 as Float);
        self.lows.fill(0.0 as Float);
        self.index = 0;
        self.count = 0;
    }
}