fast-ta 0.2.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
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
538
539
540
//! Crate-private generic execution engine for Pattern Recognition definitions.

use super::{
    Candle, CandleInput, CandleRangeKind, CandleSettingType, CandleSettings, PatternDirection,
    PatternSignal,
};
use crate::{common::validate_finite_value, TalibError};
use crate::{
    validate_all_same_len, validate_finite_slices, validate_input_len, validate_output_len,
    CompactOutput, Float, OutputRange, Result,
};

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

/// Pinned C evaluates f32-rounded inputs in double-precision arithmetic.
pub(crate) type PatternFloat = f64;

#[inline]
pub(crate) fn widen(value: Float) -> PatternFloat {
    value as PatternFloat
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum CandleColor {
    White,
    Black,
}

impl CandleColor {
    #[inline]
    pub(crate) const fn direction(self) -> PatternDirection {
        match self {
            Self::White => PatternDirection::Bullish,
            Self::Black => PatternDirection::Bearish,
        }
    }

    #[inline]
    pub(crate) const fn opposite_direction(self) -> PatternDirection {
        match self {
            Self::White => PatternDirection::Bearish,
            Self::Black => PatternDirection::Bullish,
        }
    }
}

#[derive(Debug, Clone, Copy)]
pub(crate) struct WideCandle {
    pub(crate) open: PatternFloat,
    pub(crate) high: PatternFloat,
    pub(crate) low: PatternFloat,
    pub(crate) close: PatternFloat,
}

impl From<Candle> for WideCandle {
    fn from(candle: Candle) -> Self {
        Self {
            open: widen(candle.open),
            high: widen(candle.high),
            low: widen(candle.low),
            close: widen(candle.close),
        }
    }
}

impl Candle {
    #[inline]
    pub(crate) fn real_body(self) -> PatternFloat {
        (widen(self.close) - widen(self.open)).abs()
    }

    #[inline]
    fn high_low_range(self) -> PatternFloat {
        widen(self.high) - widen(self.low)
    }

    #[inline]
    fn upper_shadow(self) -> PatternFloat {
        let body_high = if self.close >= self.open {
            self.close
        } else {
            self.open
        };
        widen(self.high) - widen(body_high)
    }

    #[inline]
    pub(crate) fn lower_shadow(self) -> PatternFloat {
        let body_low = if self.close >= self.open {
            self.open
        } else {
            self.close
        };
        widen(body_low) - widen(self.low)
    }

    #[inline]
    fn shadows(self) -> PatternFloat {
        self.upper_shadow() + self.lower_shadow()
    }

    #[inline]
    pub(crate) fn color(self) -> CandleColor {
        if self.close >= self.open {
            CandleColor::White
        } else {
            CandleColor::Black
        }
    }

    #[inline]
    pub(crate) fn range(self, range_kind: CandleRangeKind) -> PatternFloat {
        match range_kind {
            CandleRangeKind::RealBody => self.real_body(),
            CandleRangeKind::HighLow => self.high_low_range(),
            CandleRangeKind::Shadows => self.shadows(),
        }
    }
}

#[derive(Debug, Clone, Copy)]
struct CandleFrame {
    candle: Candle,
    averages: [PatternFloat; 11],
}

#[derive(Debug, Clone)]
struct CandleHistory {
    frames: Vec<CandleFrame>,
    first: usize,
    capacity: usize,
}

impl CandleHistory {
    fn new(capacity: usize) -> Self {
        Self {
            frames: Vec::with_capacity(capacity),
            first: 0,
            capacity,
        }
    }

    fn clear(&mut self) {
        self.frames.clear();
        self.first = 0;
    }

    fn push(&mut self, frame: CandleFrame) {
        if self.frames.len() < self.capacity {
            self.frames.push(frame);
        } else {
            self.frames[self.first] = frame;
            self.first = (self.first + 1) % self.capacity;
        }
    }

    #[inline]
    fn frame(&self, offset: usize) -> &CandleFrame {
        assert!(
            offset < self.frames.len(),
            "pattern requested unavailable Candle offset"
        );
        let newest = (self.first + self.frames.len() - 1) % self.capacity;
        let index = (newest + self.capacity - offset) % self.capacity;
        &self.frames[index]
    }
}

/// Offset-aware Candle geometry and Candle Average access for local definitions.
pub(crate) struct RecognitionContext<'a> {
    history: &'a CandleHistory,
}

impl RecognitionContext<'_> {
    #[inline]
    pub(crate) fn raw_candle(&self, offset: usize) -> Candle {
        self.history.frame(offset).candle
    }

    /// Returns a widened Candle, where offset zero is the current source position.
    #[inline]
    pub(crate) fn candle(&self, offset: usize) -> WideCandle {
        self.raw_candle(offset).into()
    }

    #[inline]
    pub(crate) fn low(&self, offset: usize) -> PatternFloat {
        self.candle(offset).low
    }

    #[inline]
    pub(crate) fn body_high(&self, offset: usize) -> PatternFloat {
        let candle = self.raw_candle(offset);
        widen(if candle.close >= candle.open {
            candle.close
        } else {
            candle.open
        })
    }

    #[inline]
    pub(crate) fn body_low(&self, offset: usize) -> PatternFloat {
        let candle = self.raw_candle(offset);
        widen(if candle.close >= candle.open {
            candle.open
        } else {
            candle.close
        })
    }

    #[inline]
    pub(crate) fn real_body(&self, offset: usize) -> PatternFloat {
        self.raw_candle(offset).real_body()
    }

    #[inline]
    pub(crate) fn high_low_range(&self, offset: usize) -> PatternFloat {
        self.raw_candle(offset).high_low_range()
    }

    #[inline]
    pub(crate) fn upper_shadow(&self, offset: usize) -> PatternFloat {
        self.raw_candle(offset).upper_shadow()
    }

    #[inline]
    pub(crate) fn lower_shadow(&self, offset: usize) -> PatternFloat {
        self.raw_candle(offset).lower_shadow()
    }

    #[inline]
    pub(crate) fn color(&self, offset: usize) -> CandleColor {
        self.raw_candle(offset).color()
    }

    /// Returns the setting's already source-aligned Candle Average.
    #[inline]
    pub(crate) fn average(&self, setting_type: CandleSettingType, offset: usize) -> PatternFloat {
        self.history.frame(offset).averages[setting_type.index()]
    }

    #[inline]
    pub(crate) fn real_body_gap_up(&self, second: usize, first: usize) -> bool {
        let second = self.candle(second);
        let first = self.candle(first);
        let second_low = if second.close >= second.open {
            second.open
        } else {
            second.close
        };
        let first_high = if first.close >= first.open {
            first.close
        } else {
            first.open
        };
        second_low > first_high
    }

    #[inline]
    pub(crate) fn real_body_gap_down(&self, second: usize, first: usize) -> bool {
        let second = self.candle(second);
        let first = self.candle(first);
        let second_high = if second.close >= second.open {
            second.close
        } else {
            second.open
        };
        let first_low = if first.close >= first.open {
            first.open
        } else {
            first.close
        };
        second_high < first_low
    }

    #[inline]
    pub(crate) fn candle_gap_up(&self, second: usize, first: usize) -> bool {
        self.candle(second).low > self.candle(first).high
    }

    #[inline]
    pub(crate) fn candle_gap_down(&self, second: usize, first: usize) -> bool {
        self.candle(second).high < self.candle(first).low
    }
}

pub(crate) fn maximum_average_period(
    settings: CandleSettings,
    referenced: &[CandleSettingType],
) -> usize {
    referenced
        .iter()
        .map(|&setting_type| settings.setting(setting_type).average_period())
        .max()
        .unwrap_or(0)
}

/// Static, crate-owned definition seam. Predicates and formulas stay local.
pub(crate) trait PatternDefinition: Copy {
    type State;

    fn name(&self) -> &'static str;
    fn settings(&self) -> CandleSettings;
    fn referenced_settings(&self) -> &'static [CandleSettingType];
    fn lookback(&self) -> usize;
    fn transition_start(&self) -> usize;
    fn initial_state(&self) -> Self::State;
    fn transition(
        &self,
        context: &RecognitionContext<'_>,
        state: &mut Self::State,
    ) -> PatternSignal;

    fn compute_batch_into(&self, _input: CandleInput<'_>, _output: &mut [PatternSignal]) -> bool {
        false
    }
}

/// Shared rolling lifecycle used by all four public execution modes.
#[derive(Debug, Clone)]
pub(crate) struct RecognitionEngine<D: PatternDefinition> {
    definition: D,
    state: D::State,
    history: CandleHistory,
    totals: [PatternFloat; 11],
    position: usize,
}

impl<D> RecognitionEngine<D>
where
    D: PatternDefinition,
{
    pub(crate) fn new(definition: D) -> Self {
        let history_capacity = definition.lookback().saturating_add(1).max(1);
        Self {
            definition,
            state: definition.initial_state(),
            history: CandleHistory::new(history_capacity),
            totals: [0.0; 11],
            position: 0,
        }
    }

    #[inline]
    pub(crate) const fn definition(&self) -> D {
        self.definition
    }

    pub(crate) fn reset(&mut self) {
        self.state = self.definition.initial_state();
        self.history.clear();
        self.totals.fill(0.0);
        self.position = 0;
    }

    pub(crate) fn next(&mut self, candle: Candle) -> Result<Option<PatternSignal>> {
        validate_candle(candle)?;
        Ok(self.next_validated(candle))
    }

    fn next_validated(&mut self, candle: Candle) -> Option<PatternSignal> {
        let mut averages = [0.0; 11];
        let settings = self.definition.settings();
        for &setting_type in self.definition.referenced_settings() {
            let setting = settings.setting(setting_type);
            let range = if setting.average_period() == 0 {
                candle.range(setting.range_kind())
            } else {
                self.totals[setting_type.index()] / setting.average_period() as PatternFloat
            };
            let divisor = if setting.range_kind() == CandleRangeKind::Shadows {
                2.0
            } else {
                1.0
            };
            averages[setting_type.index()] = widen(setting.factor()) * range / divisor;
        }

        self.history.push(CandleFrame { candle, averages });

        let signal = if self.position >= self.definition.transition_start() {
            let context = RecognitionContext {
                history: &self.history,
            };
            self.definition.transition(&context, &mut self.state)
        } else {
            PatternSignal::NoMatch
        };

        for &setting_type in self.definition.referenced_settings() {
            let setting = settings.setting(setting_type);
            let period = setting.average_period();
            if period == 0 {
                continue;
            }
            let current_range = candle.range(setting.range_kind());
            let outgoing_range = if self.position >= period {
                self.history
                    .frame(period)
                    .candle
                    .range(setting.range_kind())
            } else {
                0.0
            };
            self.totals[setting_type.index()] += current_range - outgoing_range;
        }

        let source_position = self.position;
        self.position += 1;
        (source_position >= self.definition.lookback()).then_some(signal)
    }

    pub(crate) fn run_validated(
        &mut self,
        input: CandleInput<'_>,
        output: &mut [PatternSignal],
        shape: BatchShape,
    ) -> OutputRange {
        self.reset();
        let mut output_index = 0;
        for index in 0..shape.source_len {
            if let Some(signal) = self.next_validated(input.candle(index)) {
                output[output_index] = signal;
                output_index += 1;
            }
        }
        shape.output_range()
    }
}

#[derive(Debug, Clone, Copy)]
pub(crate) struct BatchShape {
    lookback: usize,
    source_len: usize,
    output_count: usize,
}

impl BatchShape {
    fn output_range(self) -> OutputRange {
        if self.output_count == 0 {
            OutputRange::empty()
        } else {
            OutputRange::new(self.lookback, self.output_count)
        }
    }
}

pub(crate) fn validate_batch<D: PatternDefinition>(
    definition: D,
    input: CandleInput<'_>,
) -> Result<BatchShape> {
    let source_len = validate_all_same_len(&[
        ("open", input.open.len()),
        ("high", input.high.len()),
        ("low", input.low.len()),
        ("close", input.close.len()),
    ])?;
    validate_finite_slices(&[
        ("open", input.open),
        ("high", input.high),
        ("low", input.low),
        ("close", input.close),
    ])?;
    let lookback = definition.lookback();
    let output_count = validate_input_len(source_len, lookback)?;
    Ok(BatchShape {
        lookback,
        source_len,
        output_count,
    })
}

pub(crate) fn validate_prepared_capacity(
    input: CandleInput<'_>,
    max_input_len: usize,
) -> Result<()> {
    let actual_input_len = input.max_len();
    if actual_input_len > max_input_len {
        return Err(TalibError::prepared_capacity_exceeded(
            max_input_len,
            actual_input_len,
        ));
    }
    Ok(())
}

pub(crate) fn compute_owned<D: PatternDefinition>(
    definition: D,
    input: CandleInput<'_>,
) -> Result<CompactOutput<Vec<PatternSignal>>> {
    let shape = validate_batch(definition, input)?;
    let mut values = Vec::with_capacity(shape.output_count);
    values.resize(shape.output_count, PatternSignal::NoMatch);
    let range = if definition.compute_batch_into(input, &mut values) {
        shape.output_range()
    } else {
        RecognitionEngine::new(definition).run_validated(input, &mut values, shape)
    };
    CompactOutput::new(shape.source_len, range, values)
}

pub(crate) fn compute_into<D: PatternDefinition>(
    definition: D,
    input: CandleInput<'_>,
    output: &mut [PatternSignal],
) -> Result<OutputRange> {
    let shape = validate_batch(definition, input)?;
    validate_output_len(definition.name(), output.len(), shape.output_count)?;
    if definition.compute_batch_into(input, output) {
        return Ok(shape.output_range());
    }
    let mut engine = RecognitionEngine::new(definition);
    Ok(engine.run_validated(input, output, shape))
}

pub(crate) fn prepared_compute_into<D: PatternDefinition>(
    engine: &mut RecognitionEngine<D>,
    max_input_len: usize,
    input: CandleInput<'_>,
    output: &mut [PatternSignal],
) -> Result<OutputRange> {
    validate_prepared_capacity(input, max_input_len)?;
    let shape = validate_batch(engine.definition(), input)?;
    validate_output_len(engine.definition().name(), output.len(), shape.output_count)?;
    if engine.definition().compute_batch_into(input, output) {
        return Ok(shape.output_range());
    }
    Ok(engine.run_validated(input, output, shape))
}

#[inline]
fn validate_candle(candle: Candle) -> Result<()> {
    validate_finite_value("open", 0, candle.open)?;
    validate_finite_value("high", 0, candle.high)?;
    validate_finite_value("low", 0, candle.low)?;
    validate_finite_value("close", 0, candle.close)
}