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
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
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
//! Composite-input Momentum Indicator Definitions.
//!
//! `BOP` and `CCI` consume OHLC observations, `MFI` consumes OHLCV
//! observations, and `ULTOSC` consumes HLC observations. Every batch input is a
//! typed Structure-of-Arrays view whose columns are validated for source
//! alignment and finite values before output is mutated. Streaming ticks model
//! the same observation shapes and validate before state transitions.

use crate::{
    validate_finite_slices, validate_input_len, validate_output_len, validate_same_len,
    CompactOutput, Float, IndicatorConfig, OutputRange, PreparedBatchRunner, Result,
    StreamingComputation, TalibError,
};

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

const TA_EPSILON: Float = 1e-14 as Float;
const MAX_PERIOD: usize = 100_000;
const CCI_SCALE: Float = 0.015 as Float;

#[inline]
fn is_ta_zero(value: Float) -> bool {
    value > -TA_EPSILON && value < TA_EPSILON
}

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

#[inline]
fn typical_price(high: Float, low: Float, close: Float) -> Float {
    (high + low + close) / 3.0 as Float
}

fn validate_observation_columns(columns: &[(&str, &[Float])]) -> Result<usize> {
    let Some(&(first_name, first_values)) = columns.first() else {
        return Ok(0);
    };
    for &(name, values) in &columns[1..] {
        validate_same_len(first_name, first_values.len(), name, values.len())?;
    }
    validate_finite_slices(columns)?;
    Ok(first_values.len())
}

fn validate_tick(columns: &[(&str, Float)]) -> Result<()> {
    for &(name, value) in columns {
        validate_finite_slices(&[(name, &[value])])?;
    }
    Ok(())
}

// Balance of Power ---------------------------------------------------------

/// Borrowed aligned OHLC input for [`BOP`] Batch Computation.
#[derive(Debug, Clone, Copy)]
pub struct BOPInput<'a> {
    /// Open price series.
    pub open: &'a [Float],
    /// High price series.
    pub high: &'a [Float],
    /// Low price series.
    pub low: &'a [Float],
    /// Close price series.
    pub close: &'a [Float],
}

/// One OHLC tick for [`BOP`] Streaming Computation.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct BOPTick {
    /// Open price.
    pub open: Float,
    /// High price.
    pub high: Float,
    /// Low price.
    pub low: Float,
    /// Close price.
    pub close: Float,
}

fn validate_bop_input(input: BOPInput<'_>) -> Result<usize> {
    validate_observation_columns(&[
        ("open", input.open),
        ("high", input.high),
        ("low", input.low),
        ("close", input.close),
    ])
}

#[inline]
fn bop_value(open: Float, high: Float, low: Float, close: Float) -> Float {
    let range = high - low;
    if range < TA_EPSILON {
        0.0 as Float
    } else {
        (close - open) / range
    }
}

fn bop_kernel(input: BOPInput<'_>, output: &mut [Float]) -> OutputRange {
    for (index, value) in output.iter_mut().enumerate() {
        *value = bop_value(
            input.open[index],
            input.high[index],
            input.low[index],
            input.close[index],
        );
    }
    if output.is_empty() {
        OutputRange::empty()
    } else {
        OutputRange::new(0, output.len())
    }
}

/// Computes Balance of Power into caller-owned Compact Output storage.
#[allow(non_snake_case)]
pub fn BOP(
    open: &[Float],
    high: &[Float],
    low: &[Float],
    close: &[Float],
    out_real: &mut [Float],
) -> Result<OutputRange> {
    let input = BOPInput {
        open,
        high,
        low,
        close,
    };
    let len = validate_bop_input(input)?;
    validate_output_len("BOP", out_real.len(), len)?;
    Ok(bop_kernel(input, &mut out_real[..len]))
}

/// Immutable Balance of Power Indicator Configuration.
#[allow(clippy::upper_case_acronyms)]
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash)]
pub struct BOPConfig;

impl BOPConfig {
    /// Creates a Balance of Power configuration.
    pub const fn new() -> Self {
        Self
    }
}

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

impl IndicatorConfig for BOPConfig {
    type Input<'a> = BOPInput<'a>;
    type Output = Vec<Float>;
    type OutputMut<'a> = &'a mut [Float];
    type BatchRunner = BOPBatchRunner;
    type Stream = BOPStream;

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

    fn compute<'a>(&self, input: Self::Input<'a>) -> Result<CompactOutput<Self::Output>> {
        let len = validate_bop_input(input)?;
        let mut values = Vec::with_capacity(len);
        values.resize(len, 0.0 as Float);
        let range = bop_kernel(input, &mut values);
        CompactOutput::new(len, range, values)
    }

    #[inline]
    fn compute_into<'a>(
        &self,
        input: Self::Input<'a>,
        output: Self::OutputMut<'a>,
    ) -> Result<OutputRange> {
        BOP(input.open, input.high, input.low, input.close, output)
    }

    #[inline]
    fn prepare_batch(&self, max_input_len: usize) -> Result<Self::BatchRunner> {
        Ok(BOPBatchRunner {
            config: *self,
            max_input_len,
        })
    }

    #[inline]
    fn stream(&self) -> Result<Self::Stream> {
        Ok(BOPStream)
    }
}

/// Reusable Prepared Batch Runner for Balance of Power.
#[derive(Debug, Clone)]
pub struct BOPBatchRunner {
    config: BOPConfig,
    max_input_len: usize,
}

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

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

    fn compute_into<'a>(
        &mut self,
        input: <BOPConfig as IndicatorConfig>::Input<'a>,
        output: <BOPConfig as IndicatorConfig>::OutputMut<'a>,
    ) -> Result<OutputRange>
    where
        BOPConfig: 'a,
    {
        let actual_input_len = input
            .open
            .len()
            .max(input.high.len())
            .max(input.low.len())
            .max(input.close.len());
        if actual_input_len > self.max_input_len {
            return Err(TalibError::prepared_capacity_exceeded(
                self.max_input_len,
                actual_input_len,
            ));
        }
        IndicatorConfig::compute_into(&self.config, input, output)
    }
}

/// Independent, stateless Streaming Computation for Balance of Power.
#[derive(Debug, Clone, Copy, Default)]
pub struct BOPStream;

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

impl StreamingComputation<BOPConfig> for BOPStream {
    type Tick = BOPTick;
    type TickOutput = Float;

    fn next(&mut self, input: Self::Tick) -> Result<Option<Self::TickOutput>> {
        validate_tick(&[
            ("open", input.open),
            ("high", input.high),
            ("low", input.low),
            ("close", input.close),
        ])?;
        Ok(Some(bop_value(
            input.open,
            input.high,
            input.low,
            input.close,
        )))
    }

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

// Commodity Channel Index -------------------------------------------------

/// Borrowed aligned open/high/low/close input for [`CCI`] Batch Computation.
#[derive(Debug, Clone, Copy)]
pub struct CCIInput<'a> {
    /// Open price series.
    pub open: &'a [Float],
    /// High price series.
    pub high: &'a [Float],
    /// Low price series.
    pub low: &'a [Float],
    /// Close price series.
    pub close: &'a [Float],
}

/// One open/high/low/close tick for [`CCI`] Streaming Computation.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct CCITick {
    /// Open price.
    pub open: Float,
    /// High price.
    pub high: Float,
    /// Low price.
    pub low: Float,
    /// Close price.
    pub close: Float,
}

fn cci_lookback(period: usize) -> Result<usize> {
    validate_bounded_period("timeperiod", period, 2)?;
    Ok(period - 1)
}

fn validate_cci_input(input: CCIInput<'_>, period: usize) -> Result<(usize, usize, usize)> {
    let lookback = cci_lookback(period)?;
    let len = validate_observation_columns(&[
        ("open", input.open),
        ("high", input.high),
        ("low", input.low),
        ("close", input.close),
    ])?;
    let count = validate_input_len(len, lookback)?;
    Ok((len, lookback, count))
}

#[inline]
fn cci_projection(last: Float, average: Float, deviation_sum: Float, period: usize) -> Float {
    let delta = last - average;
    if delta == 0.0 as Float || deviation_sum == 0.0 as Float {
        0.0 as Float
    } else {
        delta / (CCI_SCALE * (deviation_sum / period as Float))
    }
}

fn cci_kernel(
    input: CCIInput<'_>,
    period: usize,
    lookback: usize,
    count: usize,
    output: &mut [Float],
) -> OutputRange {
    if count == 0 {
        return OutputRange::empty();
    }

    for (output_index, value) in output[..count].iter_mut().enumerate() {
        let source_index = lookback + output_index;
        let mut average = 0.0 as Float;
        for physical_slot in 0..period {
            let distance = (source_index % period + period - physical_slot) % period;
            let index = source_index - distance;
            average += typical_price(input.high[index], input.low[index], input.close[index]);
        }
        average /= period as Float;

        let mut deviation_sum = 0.0 as Float;
        for physical_slot in 0..period {
            let distance = (source_index % period + period - physical_slot) % period;
            let index = source_index - distance;
            deviation_sum +=
                (typical_price(input.high[index], input.low[index], input.close[index]) - average)
                    .abs();
        }
        let last = typical_price(
            input.high[source_index],
            input.low[source_index],
            input.close[source_index],
        );
        *value = cci_projection(last, average, deviation_sum, period);
    }

    OutputRange::new(lookback, count)
}

/// Computes Commodity Channel Index into caller-owned Compact Output storage.
#[allow(non_snake_case)]
pub fn CCI(
    open: &[Float],
    high: &[Float],
    low: &[Float],
    close: &[Float],
    timeperiod: usize,
    out_real: &mut [Float],
) -> Result<OutputRange> {
    let input = CCIInput {
        open,
        high,
        low,
        close,
    };
    let (_, lookback, count) = validate_cci_input(input, timeperiod)?;
    validate_output_len("CCI", out_real.len(), count)?;
    Ok(cci_kernel(input, timeperiod, lookback, count, out_real))
}

/// Immutable Commodity Channel Index Indicator Configuration.
#[allow(clippy::upper_case_acronyms)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct CCIConfig {
    period: usize,
}

impl CCIConfig {
    /// Creates a Commodity Channel Index configuration.
    pub fn new(timeperiod: usize) -> Result<Self> {
        cci_lookback(timeperiod)?;
        Ok(Self { period: timeperiod })
    }

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

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

impl IndicatorConfig for CCIConfig {
    type Input<'a> = CCIInput<'a>;
    type Output = Vec<Float>;
    type OutputMut<'a> = &'a mut [Float];
    type BatchRunner = CCIBatchRunner;
    type Stream = CCIStream;

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

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

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

    #[inline]
    fn prepare_batch(&self, max_input_len: usize) -> Result<Self::BatchRunner> {
        Ok(CCIBatchRunner {
            config: *self,
            max_input_len,
        })
    }

    #[inline]
    fn stream(&self) -> Result<Self::Stream> {
        let mut buffer = Vec::new();
        buffer.resize(self.period, 0.0 as Float);
        Ok(CCIStream {
            period: self.period,
            buffer,
            index: 0,
            count: 0,
        })
    }
}

/// Reusable Prepared Batch Runner for Commodity Channel Index.
#[derive(Debug, Clone)]
pub struct CCIBatchRunner {
    config: CCIConfig,
    max_input_len: usize,
}

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

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

    fn compute_into<'a>(
        &mut self,
        input: <CCIConfig as IndicatorConfig>::Input<'a>,
        output: <CCIConfig as IndicatorConfig>::OutputMut<'a>,
    ) -> Result<OutputRange>
    where
        CCIConfig: 'a,
    {
        let actual_input_len = input
            .open
            .len()
            .max(input.high.len())
            .max(input.low.len())
            .max(input.close.len());
        if actual_input_len > self.max_input_len {
            return Err(TalibError::prepared_capacity_exceeded(
                self.max_input_len,
                actual_input_len,
            ));
        }
        IndicatorConfig::compute_into(&self.config, input, output)
    }
}

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

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

impl StreamingComputation<CCIConfig> for CCIStream {
    type Tick = CCITick;
    type TickOutput = Float;

    fn next(&mut self, input: Self::Tick) -> Result<Option<Self::TickOutput>> {
        validate_tick(&[
            ("open", input.open),
            ("high", input.high),
            ("low", input.low),
            ("close", input.close),
        ])?;
        let current = typical_price(input.high, input.low, input.close);

        self.buffer[self.index] = current;
        self.index = (self.index + 1) % self.period;
        if self.count < self.period {
            self.count += 1;
            if self.count < self.period {
                return Ok(None);
            }
        }

        let average = self.buffer.iter().copied().sum::<Float>() / self.period as Float;
        let mut deviation_sum = 0.0 as Float;
        for &value in &self.buffer {
            deviation_sum += (value - average).abs();
        }
        Ok(Some(cci_projection(
            current,
            average,
            deviation_sum,
            self.period,
        )))
    }

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

// Money Flow Index --------------------------------------------------------

/// Borrowed aligned open/high/low/close/volume input for [`MFI`] Batch Computation.
#[derive(Debug, Clone, Copy)]
pub struct MFIInput<'a> {
    /// Open price series.
    pub open: &'a [Float],
    /// High price series.
    pub high: &'a [Float],
    /// Low price series.
    pub low: &'a [Float],
    /// Close price series.
    pub close: &'a [Float],
    /// Volume series.
    pub volume: &'a [Float],
}

/// One open/high/low/close/volume tick for [`MFI`] Streaming Computation.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct MFITick {
    /// Open price.
    pub open: Float,
    /// High price.
    pub high: Float,
    /// Low price.
    pub low: Float,
    /// Close price.
    pub close: Float,
    /// Volume.
    pub volume: Float,
}

#[derive(Debug, Clone, Copy, Default)]
struct MoneyFlow {
    positive: Float,
    negative: Float,
}

fn mfi_lookback(period: usize) -> Result<usize> {
    validate_bounded_period("timeperiod", period, 2)?;
    Ok(period)
}

fn validate_mfi_input(input: MFIInput<'_>, period: usize) -> Result<(usize, usize, usize)> {
    let lookback = mfi_lookback(period)?;
    let len = validate_observation_columns(&[
        ("open", input.open),
        ("high", input.high),
        ("low", input.low),
        ("close", input.close),
        ("volume", input.volume),
    ])?;
    let count = validate_input_len(len, lookback)?;
    Ok((len, lookback, count))
}

#[inline]
fn directional_money_flow(current: Float, previous: Float, volume: Float) -> MoneyFlow {
    let difference = current - previous;
    if difference == 0.0 as Float {
        MoneyFlow::default()
    } else if difference < 0.0 as Float {
        MoneyFlow {
            positive: 0.0 as Float,
            negative: current * volume,
        }
    } else {
        MoneyFlow {
            positive: current * volume,
            negative: 0.0 as Float,
        }
    }
}

#[inline]
fn mfi_projection(positive: Float, negative: Float) -> Float {
    let total = positive + negative;
    if total < 1.0 as Float {
        0.0 as Float
    } else {
        100.0 as Float * (positive / total)
    }
}

#[inline]
fn money_flow_at(input: MFIInput<'_>, index: usize) -> MoneyFlow {
    let previous = typical_price(
        input.high[index - 1],
        input.low[index - 1],
        input.close[index - 1],
    );
    let current = typical_price(input.high[index], input.low[index], input.close[index]);
    directional_money_flow(current, previous, input.volume[index])
}

fn mfi_kernel(
    input: MFIInput<'_>,
    period: usize,
    lookback: usize,
    count: usize,
    output: &mut [Float],
) -> OutputRange {
    if count == 0 {
        return OutputRange::empty();
    }

    let mut positive = 0.0 as Float;
    let mut negative = 0.0 as Float;
    for source_index in 1..input.high.len() {
        if source_index > period {
            let trailing = money_flow_at(input, source_index - period);
            positive -= trailing.positive;
            negative -= trailing.negative;
        }

        let current = money_flow_at(input, source_index);
        positive += current.positive;
        negative += current.negative;

        if source_index >= lookback {
            output[source_index - lookback] = mfi_projection(positive, negative);
        }
    }

    OutputRange::new(lookback, count)
}

/// Computes Money Flow Index into caller-owned Compact Output storage.
#[allow(non_snake_case)]
pub fn MFI(
    open: &[Float],
    high: &[Float],
    low: &[Float],
    close: &[Float],
    volume: &[Float],
    timeperiod: usize,
    out_real: &mut [Float],
) -> Result<OutputRange> {
    let input = MFIInput {
        open,
        high,
        low,
        close,
        volume,
    };
    let (_, lookback, count) = validate_mfi_input(input, timeperiod)?;
    validate_output_len("MFI", out_real.len(), count)?;
    Ok(mfi_kernel(input, timeperiod, lookback, count, out_real))
}

/// Immutable Money Flow Index Indicator Configuration.
#[allow(clippy::upper_case_acronyms)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct MFIConfig {
    period: usize,
}

impl MFIConfig {
    /// Creates a Money Flow Index configuration.
    pub fn new(timeperiod: usize) -> Result<Self> {
        mfi_lookback(timeperiod)?;
        Ok(Self { period: timeperiod })
    }

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

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

impl IndicatorConfig for MFIConfig {
    type Input<'a> = MFIInput<'a>;
    type Output = Vec<Float>;
    type OutputMut<'a> = &'a mut [Float];
    type BatchRunner = MFIBatchRunner;
    type Stream = MFIStream;

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

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

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

    #[inline]
    fn prepare_batch(&self, max_input_len: usize) -> Result<Self::BatchRunner> {
        Ok(MFIBatchRunner {
            config: *self,
            max_input_len,
        })
    }

    #[inline]
    fn stream(&self) -> Result<Self::Stream> {
        let mut flows = Vec::new();
        flows.resize(self.period, MoneyFlow::default());
        Ok(MFIStream {
            period: self.period,
            flows,
            index: 0,
            count: 0,
            previous_typical: None,
            positive: 0.0 as Float,
            negative: 0.0 as Float,
        })
    }
}

/// Reusable Prepared Batch Runner for Money Flow Index.
#[derive(Debug, Clone)]
pub struct MFIBatchRunner {
    config: MFIConfig,
    max_input_len: usize,
}

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

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

    fn compute_into<'a>(
        &mut self,
        input: <MFIConfig as IndicatorConfig>::Input<'a>,
        output: <MFIConfig as IndicatorConfig>::OutputMut<'a>,
    ) -> Result<OutputRange>
    where
        MFIConfig: 'a,
    {
        let actual_input_len = input
            .open
            .len()
            .max(input.high.len())
            .max(input.low.len())
            .max(input.close.len())
            .max(input.volume.len());
        if actual_input_len > self.max_input_len {
            return Err(TalibError::prepared_capacity_exceeded(
                self.max_input_len,
                actual_input_len,
            ));
        }
        IndicatorConfig::compute_into(&self.config, input, output)
    }
}

/// Independent Streaming Computation for Money Flow Index.
#[derive(Debug, Clone)]
pub struct MFIStream {
    period: usize,
    flows: Vec<MoneyFlow>,
    index: usize,
    count: usize,
    previous_typical: Option<Float>,
    positive: Float,
    negative: Float,
}

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

impl StreamingComputation<MFIConfig> for MFIStream {
    type Tick = MFITick;
    type TickOutput = Float;

    fn next(&mut self, input: Self::Tick) -> Result<Option<Self::TickOutput>> {
        validate_tick(&[
            ("open", input.open),
            ("high", input.high),
            ("low", input.low),
            ("close", input.close),
            ("volume", input.volume),
        ])?;
        let current_typical = typical_price(input.high, input.low, input.close);
        let Some(previous_typical) = self.previous_typical else {
            self.previous_typical = Some(current_typical);
            return Ok(None);
        };
        let current = directional_money_flow(current_typical, previous_typical, input.volume);

        if self.count == self.period {
            let trailing = self.flows[self.index];
            self.positive -= trailing.positive;
            self.negative -= trailing.negative;
        } else {
            self.count += 1;
        }

        self.flows[self.index] = current;
        self.index = (self.index + 1) % self.period;
        self.positive += current.positive;
        self.negative += current.negative;
        self.previous_typical = Some(current_typical);

        if self.count < self.period {
            Ok(None)
        } else {
            Ok(Some(mfi_projection(self.positive, self.negative)))
        }
    }

    fn reset(&mut self) {
        self.flows.fill(MoneyFlow::default());
        self.index = 0;
        self.count = 0;
        self.previous_typical = None;
        self.positive = 0.0 as Float;
        self.negative = 0.0 as Float;
    }
}

// Ultimate Oscillator -----------------------------------------------------

/// Borrowed aligned high/low/close input for [`ULTOSC`] Batch Computation.
#[derive(Debug, Clone, Copy)]
pub struct ULTOSCInput<'a> {
    /// High price series.
    pub high: &'a [Float],
    /// Low price series.
    pub low: &'a [Float],
    /// Close price series.
    pub close: &'a [Float],
}

/// One high/low/close tick for [`ULTOSC`] Streaming Computation.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct ULTOSCTick {
    /// High price.
    pub high: Float,
    /// Low price.
    pub low: Float,
    /// Close price.
    pub close: Float,
}

#[derive(Debug, Clone, Copy, Default)]
struct UltimateTerm {
    buying_pressure: Float,
    true_range: Float,
}

fn sorted_ultosc_periods(period1: usize, period2: usize, period3: usize) -> Result<[usize; 3]> {
    validate_bounded_period("timeperiod1", period1, 1)?;
    validate_bounded_period("timeperiod2", period2, 1)?;
    validate_bounded_period("timeperiod3", period3, 1)?;
    let mut periods = [period1, period2, period3];
    periods.sort_unstable();
    Ok(periods)
}

fn validate_ultosc_input(
    input: ULTOSCInput<'_>,
    periods: [usize; 3],
) -> Result<(usize, usize, usize)> {
    for (index, period) in periods.iter().copied().enumerate() {
        validate_bounded_period(
            match index {
                0 => "short_period",
                1 => "medium_period",
                _ => "long_period",
            },
            period,
            1,
        )?;
    }
    let len = validate_observation_columns(&[
        ("high", input.high),
        ("low", input.low),
        ("close", input.close),
    ])?;
    let lookback = periods[2];
    let count = validate_input_len(len, lookback)?;
    Ok((len, lookback, count))
}

#[inline]
fn ultimate_term(high: Float, low: Float, close: Float, previous_close: Float) -> UltimateTerm {
    let true_low = Float::min(low, previous_close);
    let high_low = high - low;
    let high_close = (high - previous_close).abs();
    let low_close = (low - previous_close).abs();
    UltimateTerm {
        buying_pressure: close - true_low,
        true_range: Float::max(high_low, Float::max(high_close, low_close)),
    }
}

#[inline]
fn ultimate_value(buying_pressure: [Float; 3], true_range: [Float; 3]) -> Float {
    let weights = [4.0 as Float, 2.0 as Float, 1.0 as Float];
    let mut weighted = 0.0 as Float;
    for index in 0..3 {
        if !is_ta_zero(true_range[index]) {
            weighted += weights[index] * (buying_pressure[index] / true_range[index]);
        }
    }
    100.0 as Float * (weighted / 7.0 as Float)
}

#[inline]
fn ultimate_term_at(input: ULTOSCInput<'_>, index: usize) -> UltimateTerm {
    ultimate_term(
        input.high[index],
        input.low[index],
        input.close[index],
        input.close[index - 1],
    )
}

fn ultosc_kernel(
    input: ULTOSCInput<'_>,
    periods: [usize; 3],
    lookback: usize,
    count: usize,
    output: &mut [Float],
) -> OutputRange {
    if count == 0 {
        return OutputRange::empty();
    }

    let mut buying_pressure = [0.0 as Float; 3];
    let mut true_range = [0.0 as Float; 3];
    for period_index in 0..3 {
        let first = lookback + 1 - periods[period_index];
        for source_index in first..=lookback {
            let term = ultimate_term_at(input, source_index);
            buying_pressure[period_index] += term.buying_pressure;
            true_range[period_index] += term.true_range;
        }
    }
    output[0] = ultimate_value(buying_pressure, true_range);

    for source_index in lookback + 1..input.high.len() {
        let current = ultimate_term_at(input, source_index);
        for period_index in 0..3 {
            let trailing = ultimate_term_at(input, source_index - periods[period_index]);
            buying_pressure[period_index] -= trailing.buying_pressure;
            true_range[period_index] -= trailing.true_range;
            buying_pressure[period_index] += current.buying_pressure;
            true_range[period_index] += current.true_range;
        }
        output[source_index - lookback] = ultimate_value(buying_pressure, true_range);
    }

    OutputRange::new(lookback, count)
}

/// Computes Ultimate Oscillator into caller-owned Compact Output storage.
///
/// The three Periods are order-insensitive. They are sorted from shortest to
/// longest before the 4:2:1 weighting is applied, matching the Indicator
/// Definition rather than assigning weights to argument positions.
#[allow(non_snake_case)]
pub fn ULTOSC(
    high: &[Float],
    low: &[Float],
    close: &[Float],
    timeperiod1: usize,
    timeperiod2: usize,
    timeperiod3: usize,
    out_real: &mut [Float],
) -> Result<OutputRange> {
    let periods = sorted_ultosc_periods(timeperiod1, timeperiod2, timeperiod3)?;
    let input = ULTOSCInput { high, low, close };
    let (_, lookback, count) = validate_ultosc_input(input, periods)?;
    validate_output_len("ULTOSC", out_real.len(), count)?;
    Ok(ultosc_kernel(input, periods, lookback, count, out_real))
}

/// Immutable Ultimate Oscillator Indicator Configuration.
#[allow(clippy::upper_case_acronyms)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct ULTOSCConfig {
    periods: [usize; 3],
}

impl ULTOSCConfig {
    /// Creates an Ultimate Oscillator configuration.
    ///
    /// The supplied Periods are canonicalized from shortest to longest.
    pub fn new(period1: usize, period2: usize, period3: usize) -> Result<Self> {
        Ok(Self {
            periods: sorted_ultosc_periods(period1, period2, period3)?,
        })
    }

    /// Returns the canonical shortest, middle, and longest Periods.
    pub const fn periods(&self) -> [usize; 3] {
        self.periods
    }

    /// Returns the shortest configured Period.
    pub const fn short_period(&self) -> usize {
        self.periods[0]
    }

    /// Returns the middle configured Period.
    pub const fn medium_period(&self) -> usize {
        self.periods[1]
    }

    /// Returns the longest configured Period.
    pub const fn long_period(&self) -> usize {
        self.periods[2]
    }
}

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

impl IndicatorConfig for ULTOSCConfig {
    type Input<'a> = ULTOSCInput<'a>;
    type Output = Vec<Float>;
    type OutputMut<'a> = &'a mut [Float];
    type BatchRunner = ULTOSCBatchRunner;
    type Stream = ULTOSCStream;

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

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

    #[inline]
    fn compute_into<'a>(
        &self,
        input: Self::Input<'a>,
        output: Self::OutputMut<'a>,
    ) -> Result<OutputRange> {
        ULTOSC(
            input.high,
            input.low,
            input.close,
            self.periods[0],
            self.periods[1],
            self.periods[2],
            output,
        )
    }

    #[inline]
    fn prepare_batch(&self, max_input_len: usize) -> Result<Self::BatchRunner> {
        Ok(ULTOSCBatchRunner {
            config: *self,
            max_input_len,
        })
    }

    fn stream(&self) -> Result<Self::Stream> {
        let mut terms = Vec::new();
        terms.resize(self.periods[2], UltimateTerm::default());
        Ok(ULTOSCStream {
            periods: self.periods,
            terms,
            index: 0,
            count: 0,
            previous_close: None,
            buying_pressure: [0.0 as Float; 3],
            true_range: [0.0 as Float; 3],
        })
    }
}

/// Reusable Prepared Batch Runner for Ultimate Oscillator.
#[derive(Debug, Clone)]
pub struct ULTOSCBatchRunner {
    config: ULTOSCConfig,
    max_input_len: usize,
}

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

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

    fn compute_into<'a>(
        &mut self,
        input: <ULTOSCConfig as IndicatorConfig>::Input<'a>,
        output: <ULTOSCConfig as IndicatorConfig>::OutputMut<'a>,
    ) -> Result<OutputRange>
    where
        ULTOSCConfig: 'a,
    {
        let actual_input_len = input.high.len().max(input.low.len()).max(input.close.len());
        if actual_input_len > self.max_input_len {
            return Err(TalibError::prepared_capacity_exceeded(
                self.max_input_len,
                actual_input_len,
            ));
        }
        IndicatorConfig::compute_into(&self.config, input, output)
    }
}

/// Independent Streaming Computation for Ultimate Oscillator.
#[derive(Debug, Clone)]
pub struct ULTOSCStream {
    periods: [usize; 3],
    terms: Vec<UltimateTerm>,
    index: usize,
    count: usize,
    previous_close: Option<Float>,
    buying_pressure: [Float; 3],
    true_range: [Float; 3],
}

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

impl StreamingComputation<ULTOSCConfig> for ULTOSCStream {
    type Tick = ULTOSCTick;
    type TickOutput = Float;

    fn next(&mut self, input: Self::Tick) -> Result<Option<Self::TickOutput>> {
        validate_tick(&[
            ("high", input.high),
            ("low", input.low),
            ("close", input.close),
        ])?;
        let Some(previous_close) = self.previous_close else {
            self.previous_close = Some(input.close);
            return Ok(None);
        };
        let current = ultimate_term(input.high, input.low, input.close, previous_close);

        let was_full = self.count == self.terms.len();
        if was_full {
            for period_index in 0..3 {
                let period = self.periods[period_index];
                let trailing_index = (self.index + self.terms.len() - period) % self.terms.len();
                let trailing = self.terms[trailing_index];
                self.buying_pressure[period_index] -= trailing.buying_pressure;
                self.true_range[period_index] -= trailing.true_range;
            }
        }

        self.terms[self.index] = current;
        self.index = (self.index + 1) % self.terms.len();
        if !was_full {
            self.count += 1;
        }

        if was_full {
            for period_index in 0..3 {
                self.buying_pressure[period_index] += current.buying_pressure;
                self.true_range[period_index] += current.true_range;
            }
        } else if self.count == self.terms.len() {
            for period_index in 0..3 {
                let first = self.terms.len() - self.periods[period_index];
                for term in &self.terms[first..] {
                    self.buying_pressure[period_index] += term.buying_pressure;
                    self.true_range[period_index] += term.true_range;
                }
            }
        }
        self.previous_close = Some(input.close);

        if self.count < self.periods[2] {
            Ok(None)
        } else {
            Ok(Some(ultimate_value(self.buying_pressure, self.true_range)))
        }
    }

    fn reset(&mut self) {
        self.terms.fill(UltimateTerm::default());
        self.index = 0;
        self.count = 0;
        self.previous_close = None;
        self.buying_pressure = [0.0 as Float; 3];
        self.true_range = [0.0 as Float; 3];
    }
}