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
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
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
//! Moving-average Momentum Indicator Definitions.
//!
//! `APO` and `PPO` compare aligned fast and slow Period-based Moving Averages.
//! `MACD`, `MACDEXT`, and `MACDFIX` return aligned, named MACD, signal, and
//! histogram columns. `TRIX` is the one-observation percentage rate of change
//! of a triple-smoothed EMA. All Periods are explicit and every accepted moving
//! average kind is a [`PeriodMAType`]; MAMA is therefore not selectable.

use crate::common::{validate_finite_value, CompactPayloadLen};
use crate::overlap::{ema_multiplier, ema_seed, ema_step, MAConfig, MAStream, PeriodMAType};
use crate::{
    validate_finite_slice, validate_input_len, validate_output_len, CompactOutput, Float,
    IndicatorConfig, OutputRange, PreparedBatchRunner, Result, StreamingComputation, TalibError,
};

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

const MAX_PERIOD: usize = 100_000;
const DEFAULT_FAST_PERIOD: usize = 12;
const DEFAULT_SLOW_PERIOD: usize = 26;
const DEFAULT_SIGNAL_PERIOD: usize = 9;
const DEFAULT_TRIX_PERIOD: usize = 30;
const TA_EPSILON: Float = 1e-14 as Float;

#[inline]
fn validate_bounded_period(name: &str, period: usize, minimum: usize) -> Result<()> {
    if !(minimum..=MAX_PERIOD).contains(&period) {
        return Err(TalibError::invalid_period(
            period,
            if minimum == 1 {
                "Period must be in 1..=100000"
            } else {
                "Period must be in 2..=100000"
            },
        ));
    }
    let _ = name;
    Ok(())
}

#[inline]
fn validate_period_order(fast_period: usize, slow_period: usize) -> Result<()> {
    if fast_period >= slow_period {
        return Err(TalibError::invalid_input(
            "fast Period must be strictly less than slow Period",
        ));
    }
    Ok(())
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
struct PairSpec {
    fast: MAConfig,
    slow: MAConfig,
    lookback: usize,
}

impl PairSpec {
    fn new(
        fast_period: usize,
        fast_type: PeriodMAType,
        slow_period: usize,
        slow_type: PeriodMAType,
    ) -> Result<Self> {
        validate_bounded_period("fast_period", fast_period, 2)?;
        validate_bounded_period("slow_period", slow_period, 2)?;
        validate_period_order(fast_period, slow_period)?;
        let fast = MAConfig::new(fast_period, fast_type)?;
        let slow = MAConfig::new(slow_period, slow_type)?;
        let lookback = fast.lookback().max(slow.lookback());
        Ok(Self {
            fast,
            slow,
            lookback,
        })
    }
}

#[derive(Debug, Clone)]
struct PairStreamCore {
    fast: MAStream,
    slow: MAStream,
    fast_start: usize,
    slow_start: usize,
    observations: usize,
}

impl PairStreamCore {
    fn new(spec: PairSpec, align_initial_windows: bool) -> Result<Self> {
        Ok(Self {
            fast: spec.fast.stream()?,
            slow: spec.slow.stream()?,
            fast_start: if align_initial_windows {
                spec.lookback - spec.fast.lookback()
            } else {
                0
            },
            slow_start: if align_initial_windows {
                spec.lookback - spec.slow.lookback()
            } else {
                0
            },
            observations: 0,
        })
    }

    fn next(&mut self, input: Float) -> Result<Option<(Float, Float)>> {
        validate_finite_value("input", self.observations, input)?;
        let index = self.observations;
        let fast = if index >= self.fast_start {
            self.fast.next(input)?
        } else {
            None
        };
        let slow = if index >= self.slow_start {
            self.slow.next(input)?
        } else {
            None
        };
        self.observations += 1;
        Ok(match (fast, slow) {
            (Some(fast), Some(slow)) => Some((fast, slow)),
            _ => None,
        })
    }

    fn reset(&mut self) {
        self.fast.reset();
        self.slow.reset();
        self.observations = 0;
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum PriceOscillatorKind {
    Absolute,
    Percentage,
}

impl PriceOscillatorKind {
    #[inline]
    fn evaluate(self, fast: Float, slow: Float) -> Float {
        let difference = fast - slow;
        match self {
            Self::Absolute => difference,
            Self::Percentage if slow > -TA_EPSILON && slow < TA_EPSILON => 0.0 as Float,
            Self::Percentage => difference / slow * 100.0 as Float,
        }
    }
}

fn validate_single_output(
    real: &[Float],
    lookback: usize,
    output_name: &str,
    output_len: usize,
) -> Result<usize> {
    validate_finite_slice("real", real)?;
    let count = validate_input_len(real.len(), lookback)?;
    validate_output_len(output_name, output_len, count)?;
    Ok(count)
}

fn compute_pair_validated(
    real: &[Float],
    spec: PairSpec,
    kind: PriceOscillatorKind,
    out_real: &mut [Float],
) -> Result<OutputRange> {
    let mut core = PairStreamCore::new(spec, false)?;
    let mut output_index = 0;
    for input in real.iter().copied() {
        if let Some((fast, slow)) = core.next(input)? {
            out_real[output_index] = kind.evaluate(fast, slow);
            output_index += 1;
        }
    }
    debug_assert_eq!(output_index, real.len().saturating_sub(spec.lookback));
    Ok(if output_index == 0 {
        OutputRange::empty()
    } else {
        OutputRange::new(spec.lookback, output_index)
    })
}

fn compute_pair_into(
    real: &[Float],
    spec: PairSpec,
    kind: PriceOscillatorKind,
    output_name: &str,
    out_real: &mut [Float],
) -> Result<OutputRange> {
    validate_single_output(real, spec.lookback, output_name, out_real.len())?;
    compute_pair_validated(real, spec, kind, out_real)
}

macro_rules! define_price_oscillator {
    (
        $function:ident,
        $config:ident,
        $runner:ident,
        $stream:ident,
        $kind:expr,
        $title:literal
    ) => {
        #[doc = concat!("Immutable ", $title, " Indicator Configuration.")]
        #[allow(clippy::upper_case_acronyms)]
        #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
        pub struct $config {
            spec: PairSpec,
            ma_type: PeriodMAType,
        }

        impl $config {
            #[doc = concat!("Creates an explicit ", $title, " configuration.")]
            pub fn new(
                fast_period: usize,
                slow_period: usize,
                ma_type: PeriodMAType,
            ) -> Result<Self> {
                Ok(Self {
                    spec: PairSpec::new(fast_period, ma_type, slow_period, ma_type)?,
                    ma_type,
                })
            }

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

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

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

        impl Default for $config {
            fn default() -> Self {
                Self::new(DEFAULT_FAST_PERIOD, DEFAULT_SLOW_PERIOD, PeriodMAType::EMA)
                    .expect("default price-oscillator configuration must be valid")
            }
        }

        impl crate::traits::sealed::Sealed for $config {}

        impl IndicatorConfig for $config {
            type Input<'a> = &'a [Float];
            type Output = Vec<Float>;
            type OutputMut<'a> = &'a mut [Float];
            type BatchRunner = $runner;
            type Stream = $stream;

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

            fn compute<'a>(&self, input: Self::Input<'a>) -> Result<CompactOutput<Self::Output>> {
                validate_finite_slice("real", input)?;
                let count = validate_input_len(input.len(), self.spec.lookback)?;
                let mut values = vec![0.0 as Float; count];
                let range = compute_pair_validated(input, self.spec, $kind, &mut values)?;
                CompactOutput::new(input.len(), range, values)
            }

            fn compute_into<'a>(
                &self,
                input: Self::Input<'a>,
                output: Self::OutputMut<'a>,
            ) -> Result<OutputRange> {
                compute_pair_into(input, self.spec, $kind, stringify!($function), output)
            }

            fn prepare_batch(&self, max_input_len: usize) -> Result<Self::BatchRunner> {
                Ok($runner {
                    config: *self,
                    max_input_len,
                    core: PairStreamCore::new(self.spec, false)?,
                })
            }

            fn stream(&self) -> Result<Self::Stream> {
                Ok($stream {
                    core: PairStreamCore::new(self.spec, false)?,
                })
            }
        }

        #[doc = concat!("Reusable Prepared Batch Runner for ", $title, ".")]
        #[derive(Debug, Clone)]
        pub struct $runner {
            config: $config,
            max_input_len: usize,
            core: PairStreamCore,
        }

        impl crate::traits::sealed::Sealed for $runner {}

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

            fn compute_into<'a>(
                &mut self,
                input: <$config as IndicatorConfig>::Input<'a>,
                output: <$config as IndicatorConfig>::OutputMut<'a>,
            ) -> Result<OutputRange>
            where
                $config: 'a,
            {
                if input.len() > self.max_input_len {
                    return Err(TalibError::prepared_capacity_exceeded(
                        self.max_input_len,
                        input.len(),
                    ));
                }
                let count = validate_single_output(
                    input,
                    self.config.spec.lookback,
                    stringify!($function),
                    output.len(),
                )?;
                self.core.reset();
                let mut output_index = 0;
                for value in input.iter().copied() {
                    if let Some((fast, slow)) = self.core.next(value)? {
                        output[output_index] = $kind.evaluate(fast, slow);
                        output_index += 1;
                    }
                }
                debug_assert_eq!(output_index, count);
                Ok(if count == 0 {
                    OutputRange::empty()
                } else {
                    OutputRange::new(self.config.spec.lookback, count)
                })
            }
        }

        #[doc = concat!("Independent Streaming Computation for ", $title, ".")]
        #[derive(Debug, Clone)]
        pub struct $stream {
            core: PairStreamCore,
        }

        impl crate::traits::sealed::Sealed for $stream {}

        impl StreamingComputation<$config> for $stream {
            type Tick = Float;
            type TickOutput = Float;

            fn next(&mut self, input: Float) -> Result<Option<Float>> {
                Ok(self
                    .core
                    .next(input)?
                    .map(|(fast, slow)| $kind.evaluate(fast, slow)))
            }

            #[inline]
            fn reset(&mut self) {
                self.core.reset();
            }
        }

        #[doc = concat!("Computes ", $title, " into caller-owned Compact Output storage.")]
        #[allow(non_snake_case)]
        pub fn $function(
            real: &[Float],
            fast_period: usize,
            slow_period: usize,
            ma_type: PeriodMAType,
            out_real: &mut [Float],
        ) -> Result<OutputRange> {
            let config = $config::new(fast_period, slow_period, ma_type)?;
            config.compute_into(real, out_real)
        }
    };
}

define_price_oscillator!(
    APO,
    APOConfig,
    APOBatchRunner,
    APOStream,
    PriceOscillatorKind::Absolute,
    "Absolute Price Oscillator"
);
define_price_oscillator!(
    PPO,
    PPOConfig,
    PPOBatchRunner,
    PPOStream,
    PriceOscillatorKind::Percentage,
    "Percentage Price Oscillator"
);

/// One valid aligned MACD-family streaming output.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct MACDValue {
    /// Fast-minus-slow moving-average line.
    pub macd: Float,
    /// Moving average of the MACD line.
    pub signal: Float,
    /// Difference between `macd` and `signal`.
    pub histogram: Float,
}

/// Named aligned Compact Output columns for the MACD family.
#[derive(Debug, Clone, PartialEq)]
pub struct MACDValues {
    /// Fast-minus-slow moving-average column.
    pub macd: Vec<Float>,
    /// Signal moving-average column.
    pub signal: Vec<Float>,
    /// MACD-minus-signal histogram column.
    pub histogram: Vec<Float>,
}

impl CompactPayloadLen for MACDValues {
    fn compact_payload_len(&self) -> Result<usize> {
        if self.macd.len() != self.signal.len() || self.macd.len() != self.histogram.len() {
            return Err(TalibError::invalid_input(
                "MACD Compact Output columns must have equal lengths",
            ));
        }
        Ok(self.macd.len())
    }
}

/// Caller-owned named aligned Compact Output columns for the MACD family.
#[derive(Debug)]
pub struct MACDValuesMut<'a> {
    /// Fast-minus-slow moving-average output buffer.
    pub macd: &'a mut [Float],
    /// Signal moving-average output buffer.
    pub signal: &'a mut [Float],
    /// MACD-minus-signal histogram output buffer.
    pub histogram: &'a mut [Float],
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
struct MACDSpec {
    pair: PairSpec,
    signal: MAConfig,
    lookback: usize,
}

impl MACDSpec {
    fn new(
        fast_period: usize,
        fast_type: PeriodMAType,
        slow_period: usize,
        slow_type: PeriodMAType,
        signal_period: usize,
        signal_type: PeriodMAType,
    ) -> Result<Self> {
        let pair = PairSpec::new(fast_period, fast_type, slow_period, slow_type)?;
        validate_bounded_period("signal_period", signal_period, 1)?;
        let signal = MAConfig::new(signal_period, signal_type)?;
        let lookback = pair
            .lookback
            .checked_add(signal.lookback())
            .ok_or_else(|| {
                TalibError::invalid_period(signal_period, "MACD lookback would overflow")
            })?;
        Ok(Self {
            pair,
            signal,
            lookback,
        })
    }

    #[inline]
    fn uses_only_ema(self) -> bool {
        self.pair.fast.ma_type() == PeriodMAType::EMA
            && self.pair.slow.ma_type() == PeriodMAType::EMA
            && self.signal.ma_type() == PeriodMAType::EMA
    }
}

#[derive(Debug, Clone)]
struct MACDStreamCore {
    pair: PairStreamCore,
    signal: MAStream,
}

impl MACDStreamCore {
    fn new(spec: MACDSpec) -> Result<Self> {
        Ok(Self {
            pair: PairStreamCore::new(spec.pair, true)?,
            signal: spec.signal.stream()?,
        })
    }

    fn next(&mut self, input: Float) -> Result<Option<MACDValue>> {
        let Some((fast, slow)) = self.pair.next(input)? else {
            return Ok(None);
        };
        let macd = fast - slow;
        Ok(self.signal.next(macd)?.map(|signal| MACDValue {
            macd,
            signal,
            histogram: macd - signal,
        }))
    }

    fn reset(&mut self) {
        self.pair.reset();
        self.signal.reset();
    }
}

fn validate_macd_outputs(
    real: &[Float],
    lookback: usize,
    output_name: &str,
    output: &MACDValuesMut<'_>,
) -> Result<usize> {
    let names = match output_name {
        "MACDEXT" => ("MACDEXT MACD", "MACDEXT signal", "MACDEXT histogram"),
        "MACDFIX" => ("MACDFIX MACD", "MACDFIX signal", "MACDFIX histogram"),
        _ => ("MACD line", "MACD signal", "MACD histogram"),
    };
    validate_finite_slice("real", real)?;
    let count = validate_input_len(real.len(), lookback)?;
    validate_output_len(names.0, output.macd.len(), count)?;
    validate_output_len(names.1, output.signal.len(), count)?;
    validate_output_len(names.2, output.histogram.len(), count)?;
    Ok(count)
}

// Batch validation is complete before this fused path is entered. Keeping the
// three EMA states as scalars avoids repeating streaming validation, generic MA
// dispatch, and warm-up `Option` transitions for every observation.
fn compute_macd_ema_validated(
    real: &[Float],
    spec: MACDSpec,
    output: MACDValuesMut<'_>,
) -> OutputRange {
    let output_count = real.len().saturating_sub(spec.lookback);
    if output_count == 0 {
        return OutputRange::empty();
    }

    let fast_period = spec.pair.fast.period();
    let slow_period = spec.pair.slow.period();
    let signal_period = spec.signal.period();
    let pair_lookback = spec.pair.lookback;
    let fast_start = pair_lookback + 1 - fast_period;
    let fast_multiplier = ema_multiplier(fast_period);
    let slow_multiplier = ema_multiplier(slow_period);
    let signal_multiplier = ema_multiplier(signal_period);
    let mut fast = ema_seed(&real[fast_start..=pair_lookback], fast_period);
    let mut slow = ema_seed(real, slow_period);
    let mut signal_sum = 0.0 as Float;
    let mut macd = fast - slow;
    signal_sum += macd;

    for &input in &real[pair_lookback + 1..spec.lookback + 1] {
        fast = ema_step(fast, input, fast_multiplier);
        slow = ema_step(slow, input, slow_multiplier);
        macd = fast - slow;
        signal_sum += macd;
    }

    let mut signal = signal_sum / signal_period as Float;
    output.macd[0] = macd;
    output.signal[0] = signal;
    output.histogram[0] = macd - signal;

    for (((macd_output, signal_output), histogram_output), &input) in output.macd[1..output_count]
        .iter_mut()
        .zip(&mut output.signal[1..output_count])
        .zip(&mut output.histogram[1..output_count])
        .zip(&real[spec.lookback + 1..])
    {
        fast = ema_step(fast, input, fast_multiplier);
        slow = ema_step(slow, input, slow_multiplier);
        macd = fast - slow;
        signal = ema_step(signal, macd, signal_multiplier);
        *macd_output = macd;
        *signal_output = signal;
        *histogram_output = macd - signal;
    }

    OutputRange::new(spec.lookback, output_count)
}

fn compute_macd_stream_validated(
    real: &[Float],
    spec: MACDSpec,
    core: &mut MACDStreamCore,
    output: MACDValuesMut<'_>,
) -> Result<OutputRange> {
    let mut output_index = 0;
    for input in real.iter().copied() {
        if let Some(value) = core.next(input)? {
            output.macd[output_index] = value.macd;
            output.signal[output_index] = value.signal;
            output.histogram[output_index] = value.histogram;
            output_index += 1;
        }
    }
    debug_assert_eq!(output_index, real.len().saturating_sub(spec.lookback));
    Ok(if output_index == 0 {
        OutputRange::empty()
    } else {
        OutputRange::new(spec.lookback, output_index)
    })
}

fn compute_macd_validated(
    real: &[Float],
    spec: MACDSpec,
    output: MACDValuesMut<'_>,
) -> Result<OutputRange> {
    if spec.uses_only_ema() {
        return Ok(compute_macd_ema_validated(real, spec, output));
    }
    let mut core = MACDStreamCore::new(spec)?;
    compute_macd_stream_validated(real, spec, &mut core, output)
}

fn compute_macd_into(
    real: &[Float],
    spec: MACDSpec,
    output_name: &str,
    output: MACDValuesMut<'_>,
) -> Result<OutputRange> {
    validate_macd_outputs(real, spec.lookback, output_name, &output)?;
    compute_macd_validated(real, spec, output)
}

/// Immutable standard EMA MACD Indicator Configuration.
#[allow(clippy::upper_case_acronyms)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct MACDConfig {
    spec: MACDSpec,
}

impl MACDConfig {
    /// Creates a standard EMA MACD configuration.
    pub fn new(fast_period: usize, slow_period: usize, signal_period: usize) -> Result<Self> {
        Ok(Self {
            spec: MACDSpec::new(
                fast_period,
                PeriodMAType::EMA,
                slow_period,
                PeriodMAType::EMA,
                signal_period,
                PeriodMAType::EMA,
            )?,
        })
    }

    /// Returns the fast EMA Period.
    #[inline]
    pub const fn fast_period(&self) -> usize {
        self.spec.pair.fast.period()
    }

    /// Returns the slow EMA Period.
    #[inline]
    pub const fn slow_period(&self) -> usize {
        self.spec.pair.slow.period()
    }

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

impl Default for MACDConfig {
    fn default() -> Self {
        Self::new(
            DEFAULT_FAST_PERIOD,
            DEFAULT_SLOW_PERIOD,
            DEFAULT_SIGNAL_PERIOD,
        )
        .expect("default MACD configuration must be valid")
    }
}

/// Immutable extended MACD Indicator Configuration with explicit Period-based Moving Average kinds.
#[allow(clippy::upper_case_acronyms)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct MACDEXTConfig {
    spec: MACDSpec,
}

impl MACDEXTConfig {
    /// Creates an extended MACD configuration.
    #[allow(clippy::too_many_arguments)]
    pub fn new(
        fast_period: usize,
        fast_type: PeriodMAType,
        slow_period: usize,
        slow_type: PeriodMAType,
        signal_period: usize,
        signal_type: PeriodMAType,
    ) -> Result<Self> {
        Ok(Self {
            spec: MACDSpec::new(
                fast_period,
                fast_type,
                slow_period,
                slow_type,
                signal_period,
                signal_type,
            )?,
        })
    }

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

    /// Returns the fast Period-based Moving Average kind.
    #[inline]
    pub const fn fast_type(&self) -> PeriodMAType {
        self.spec.pair.fast.ma_type()
    }

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

    /// Returns the slow Period-based Moving Average kind.
    #[inline]
    pub const fn slow_type(&self) -> PeriodMAType {
        self.spec.pair.slow.ma_type()
    }

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

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

impl Default for MACDEXTConfig {
    fn default() -> Self {
        Self::new(
            DEFAULT_FAST_PERIOD,
            PeriodMAType::EMA,
            DEFAULT_SLOW_PERIOD,
            PeriodMAType::EMA,
            DEFAULT_SIGNAL_PERIOD,
            PeriodMAType::EMA,
        )
        .expect("default MACDEXT configuration must be valid")
    }
}

/// Immutable fixed-period (12, 26) EMA MACD Indicator Configuration.
#[allow(clippy::upper_case_acronyms)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct MACDFIXConfig {
    spec: MACDSpec,
}

impl MACDFIXConfig {
    /// Creates a fixed 12/26 EMA MACD configuration with an explicit signal Period.
    pub fn new(signal_period: usize) -> Result<Self> {
        Ok(Self {
            spec: MACDSpec::new(
                DEFAULT_FAST_PERIOD,
                PeriodMAType::EMA,
                DEFAULT_SLOW_PERIOD,
                PeriodMAType::EMA,
                signal_period,
                PeriodMAType::EMA,
            )?,
        })
    }

    /// Returns the fixed fast EMA Period (12).
    #[inline]
    pub const fn fast_period(&self) -> usize {
        DEFAULT_FAST_PERIOD
    }

    /// Returns the fixed slow EMA Period (26).
    #[inline]
    pub const fn slow_period(&self) -> usize {
        DEFAULT_SLOW_PERIOD
    }

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

impl Default for MACDFIXConfig {
    fn default() -> Self {
        Self::new(DEFAULT_SIGNAL_PERIOD).expect("default MACDFIX configuration must be valid")
    }
}

macro_rules! define_macd_execution {
    ($config:ident, $runner:ident, $stream:ident, $title:literal) => {
        impl crate::traits::sealed::Sealed for $config {}

        impl IndicatorConfig for $config {
            type Input<'a> = &'a [Float];
            type Output = MACDValues;
            type OutputMut<'a> = MACDValuesMut<'a>;
            type BatchRunner = $runner;
            type Stream = $stream;

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

            fn compute<'a>(&self, input: Self::Input<'a>) -> Result<CompactOutput<Self::Output>> {
                validate_finite_slice("real", input)?;
                let count = validate_input_len(input.len(), self.spec.lookback)?;
                let mut values = MACDValues {
                    macd: vec![0.0 as Float; count],
                    signal: vec![0.0 as Float; count],
                    histogram: vec![0.0 as Float; count],
                };
                let range = compute_macd_validated(
                    input,
                    self.spec,
                    MACDValuesMut {
                        macd: &mut values.macd,
                        signal: &mut values.signal,
                        histogram: &mut values.histogram,
                    },
                )?;
                CompactOutput::new(input.len(), range, values)
            }

            fn compute_into<'a>(
                &self,
                input: Self::Input<'a>,
                output: Self::OutputMut<'a>,
            ) -> Result<OutputRange> {
                compute_macd_into(input, self.spec, $title, output)
            }

            fn prepare_batch(&self, max_input_len: usize) -> Result<Self::BatchRunner> {
                Ok($runner {
                    config: *self,
                    max_input_len,
                    core: MACDStreamCore::new(self.spec)?,
                })
            }

            fn stream(&self) -> Result<Self::Stream> {
                Ok($stream {
                    core: MACDStreamCore::new(self.spec)?,
                })
            }
        }

        #[doc = concat!("Reusable Prepared Batch Runner for ", $title, ".")]
        #[derive(Debug, Clone)]
        pub struct $runner {
            config: $config,
            max_input_len: usize,
            core: MACDStreamCore,
        }

        impl crate::traits::sealed::Sealed for $runner {}

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

            fn compute_into<'a>(
                &mut self,
                input: <$config as IndicatorConfig>::Input<'a>,
                output: <$config as IndicatorConfig>::OutputMut<'a>,
            ) -> Result<OutputRange>
            where
                $config: 'a,
            {
                if input.len() > self.max_input_len {
                    return Err(TalibError::prepared_capacity_exceeded(
                        self.max_input_len,
                        input.len(),
                    ));
                }
                validate_macd_outputs(input, self.config.spec.lookback, $title, &output)?;
                if self.config.spec.uses_only_ema() {
                    return Ok(compute_macd_ema_validated(input, self.config.spec, output));
                }
                self.core.reset();
                compute_macd_stream_validated(input, self.config.spec, &mut self.core, output)
            }
        }

        #[doc = concat!("Independent Streaming Computation for ", $title, ".")]
        #[derive(Debug, Clone)]
        pub struct $stream {
            core: MACDStreamCore,
        }

        impl crate::traits::sealed::Sealed for $stream {}

        impl StreamingComputation<$config> for $stream {
            type Tick = Float;
            type TickOutput = MACDValue;

            #[inline]
            fn next(&mut self, input: Float) -> Result<Option<MACDValue>> {
                self.core.next(input)
            }

            #[inline]
            fn reset(&mut self) {
                self.core.reset();
            }
        }
    };
}

define_macd_execution!(MACDConfig, MACDBatchRunner, MACDStream, "MACD");
define_macd_execution!(MACDEXTConfig, MACDEXTBatchRunner, MACDEXTStream, "MACDEXT");
define_macd_execution!(MACDFIXConfig, MACDFIXBatchRunner, MACDFIXStream, "MACDFIX");

/// Computes standard EMA MACD into three caller-owned aligned Compact Output columns.
#[allow(non_snake_case, clippy::too_many_arguments)]
pub fn MACD(
    real: &[Float],
    fast_period: usize,
    slow_period: usize,
    signal_period: usize,
    out_macd: &mut [Float],
    out_signal: &mut [Float],
    out_histogram: &mut [Float],
) -> Result<OutputRange> {
    MACDConfig::new(fast_period, slow_period, signal_period)?.compute_into(
        real,
        MACDValuesMut {
            macd: out_macd,
            signal: out_signal,
            histogram: out_histogram,
        },
    )
}

/// Computes extended MACD into three caller-owned aligned Compact Output columns.
#[allow(non_snake_case, clippy::too_many_arguments)]
pub fn MACDEXT(
    real: &[Float],
    fast_period: usize,
    fast_type: PeriodMAType,
    slow_period: usize,
    slow_type: PeriodMAType,
    signal_period: usize,
    signal_type: PeriodMAType,
    out_macd: &mut [Float],
    out_signal: &mut [Float],
    out_histogram: &mut [Float],
) -> Result<OutputRange> {
    MACDEXTConfig::new(
        fast_period,
        fast_type,
        slow_period,
        slow_type,
        signal_period,
        signal_type,
    )?
    .compute_into(
        real,
        MACDValuesMut {
            macd: out_macd,
            signal: out_signal,
            histogram: out_histogram,
        },
    )
}

/// Computes fixed 12/26 EMA MACD into three caller-owned aligned Compact Output columns.
#[allow(non_snake_case)]
pub fn MACDFIX(
    real: &[Float],
    signal_period: usize,
    out_macd: &mut [Float],
    out_signal: &mut [Float],
    out_histogram: &mut [Float],
) -> Result<OutputRange> {
    MACDFIXConfig::new(signal_period)?.compute_into(
        real,
        MACDValuesMut {
            macd: out_macd,
            signal: out_signal,
            histogram: out_histogram,
        },
    )
}

#[inline]
fn trix_lookback(period: usize) -> Result<usize> {
    validate_bounded_period("timeperiod", period, 1)?;
    (period - 1)
        .checked_mul(3)
        .and_then(|lookback| lookback.checked_add(1))
        .ok_or_else(|| TalibError::invalid_period(period, "TRIX lookback would overflow"))
}

#[derive(Debug, Clone)]
struct TRIXStreamCore {
    ema1: MAStream,
    ema2: MAStream,
    ema3: MAStream,
    previous: Option<Float>,
    observations: usize,
}

impl TRIXStreamCore {
    fn new(period: usize) -> Result<Self> {
        let ema = MAConfig::new(period, PeriodMAType::EMA)?;
        Ok(Self {
            ema1: ema.stream()?,
            ema2: ema.stream()?,
            ema3: ema.stream()?,
            previous: None,
            observations: 0,
        })
    }

    fn next(&mut self, input: Float) -> Result<Option<Float>> {
        validate_finite_value("input", self.observations, input)?;
        self.observations += 1;
        let Some(first) = self.ema1.next(input)? else {
            return Ok(None);
        };
        let Some(second) = self.ema2.next(first)? else {
            return Ok(None);
        };
        let Some(third) = self.ema3.next(second)? else {
            return Ok(None);
        };
        let previous = self.previous.replace(third);
        Ok(previous.map(|previous| {
            if previous == 0.0 as Float {
                0.0 as Float
            } else {
                (third / previous - 1.0 as Float) * 100.0 as Float
            }
        }))
    }

    fn reset(&mut self) {
        self.ema1.reset();
        self.ema2.reset();
        self.ema3.reset();
        self.previous = None;
        self.observations = 0;
    }
}

/// Computes the one-observation percentage change of a triple EMA into caller-owned storage.
#[allow(non_snake_case)]
pub fn TRIX(real: &[Float], timeperiod: usize, out_real: &mut [Float]) -> Result<OutputRange> {
    let lookback = trix_lookback(timeperiod)?;
    let count = validate_single_output(real, lookback, "TRIX", out_real.len())?;
    let mut core = TRIXStreamCore::new(timeperiod)?;
    let mut output_index = 0;
    for input in real.iter().copied() {
        if let Some(value) = core.next(input)? {
            out_real[output_index] = value;
            output_index += 1;
        }
    }
    debug_assert_eq!(output_index, count);
    Ok(if count == 0 {
        OutputRange::empty()
    } else {
        OutputRange::new(lookback, count)
    })
}

/// Immutable Triple Exponential Average change Indicator Configuration.
#[allow(clippy::upper_case_acronyms)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct TRIXConfig {
    period: usize,
    lookback: usize,
}

impl TRIXConfig {
    /// Creates a TRIX configuration for the supplied Period.
    pub fn new(timeperiod: usize) -> Result<Self> {
        Ok(Self {
            period: timeperiod,
            lookback: trix_lookback(timeperiod)?,
        })
    }

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

impl Default for TRIXConfig {
    fn default() -> Self {
        Self::new(DEFAULT_TRIX_PERIOD).expect("default TRIX configuration must be valid")
    }
}

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

impl IndicatorConfig for TRIXConfig {
    type Input<'a> = &'a [Float];
    type Output = Vec<Float>;
    type OutputMut<'a> = &'a mut [Float];
    type BatchRunner = TRIXBatchRunner;
    type Stream = TRIXStream;

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

    fn compute<'a>(&self, input: Self::Input<'a>) -> Result<CompactOutput<Self::Output>> {
        validate_finite_slice("real", input)?;
        let count = validate_input_len(input.len(), self.lookback)?;
        let mut values = vec![0.0 as Float; count];
        let range = TRIX(input, self.period, &mut values)?;
        CompactOutput::new(input.len(), range, values)
    }

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

    fn prepare_batch(&self, max_input_len: usize) -> Result<Self::BatchRunner> {
        Ok(TRIXBatchRunner {
            config: *self,
            max_input_len,
            core: TRIXStreamCore::new(self.period)?,
        })
    }

    fn stream(&self) -> Result<Self::Stream> {
        Ok(TRIXStream {
            core: TRIXStreamCore::new(self.period)?,
        })
    }
}

/// Reusable Prepared Batch Runner for TRIX.
#[derive(Debug, Clone)]
pub struct TRIXBatchRunner {
    config: TRIXConfig,
    max_input_len: usize,
    core: TRIXStreamCore,
}

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

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

    fn compute_into<'a>(
        &mut self,
        input: <TRIXConfig as IndicatorConfig>::Input<'a>,
        output: <TRIXConfig as IndicatorConfig>::OutputMut<'a>,
    ) -> Result<OutputRange>
    where
        TRIXConfig: 'a,
    {
        if input.len() > self.max_input_len {
            return Err(TalibError::prepared_capacity_exceeded(
                self.max_input_len,
                input.len(),
            ));
        }
        let count = validate_single_output(input, self.config.lookback, "TRIX", output.len())?;
        self.core.reset();
        let mut output_index = 0;
        for input in input.iter().copied() {
            if let Some(value) = self.core.next(input)? {
                output[output_index] = value;
                output_index += 1;
            }
        }
        debug_assert_eq!(output_index, count);
        Ok(if count == 0 {
            OutputRange::empty()
        } else {
            OutputRange::new(self.config.lookback, count)
        })
    }
}

/// Independent Streaming Computation for TRIX.
#[derive(Debug, Clone)]
pub struct TRIXStream {
    core: TRIXStreamCore,
}

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

impl StreamingComputation<TRIXConfig> for TRIXStream {
    type Tick = Float;
    type TickOutput = Float;

    #[inline]
    fn next(&mut self, input: Float) -> Result<Option<Float>> {
        self.core.next(input)
    }

    #[inline]
    fn reset(&mut self) {
        self.core.reset();
    }
}