mylittleindicators 0.1.8

Multi-stream financial indicators library — 556 bar indicators + 21 event primitives across 35 categories. Consumes 27 stream kinds from digdigdig3 exchange connectors: OHLCV bars, ticks, orderbook (snapshot/delta/L3), funding/predicted funding/funding settlement, mark price, index price, open interest, liquidations, ticker, agg trades, long/short ratio, option greeks, volatility index, historical volatility, basis (derived), composite index, settlement events, block trades, insurance fund, risk limit, market warning, and three kline-family variants. Live-verified on 12 exchanges (89% pass-rate on a 150s BTC slice).
Documentation
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
//! Synthetic data generators for indicator testing
//!
//! Different indicators require different types of price action to produce
//! meaningful (non-zero) values. This module provides specialized data generators
//! for each category of indicator.

/// A single OHLCV bar with timestamp
#[derive(Debug, Clone, Copy)]
pub struct Bar {
    pub open: f64,
    pub high: f64,
    pub low: f64,
    pub close: f64,
    pub volume: f64,
    pub timestamp: i64,
}

impl Bar {
    pub fn new(open: f64, high: f64, low: f64, close: f64, volume: f64, timestamp: i64) -> Self {
        Self { open, high, low, close, volume, timestamp }
    }
}

/// Type of synthetic data to generate
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum DataType {
    /// Smooth sinusoidal - default for most indicators
    Smooth,
    /// Contains candle patterns (hammers, dojis, engulfings, etc.)
    CandlePatterns,
    /// Contains price gaps (FVG, liquidity gaps)
    Gaps,
    /// Trending with divergences (price up, momentum down or vice versa)
    Divergence,
    /// High volatility with jumps and breakouts
    VolatilityBreakout,
    /// Volatility clustering (ARCH effects)
    VolatilityClustering,
    /// Structural breaks in the data
    StructuralBreaks,
    /// Range compression followed by expansion
    Squeeze,
    /// With specific timestamps for calendar indicators
    Calendar,
    /// Correlated returns between price and volume
    Correlated,
    /// Strong trend for market structure detection
    StrongTrend,
    /// Choppy/ranging market
    Ranging,
    /// Sweep + reversion patterns (SweepRev)
    SweepReversion,
    /// Clear ZigZag swings with defined highs/lows
    ZigZagSwings,
    /// Tick-level data simulation
    TickData,
}

/// Generate synthetic bar data for testing
pub fn generate_bars(data_type: DataType, count: usize, start_ts: i64) -> Vec<Bar> {
    match data_type {
        DataType::Smooth => generate_smooth(count, start_ts),
        DataType::CandlePatterns => generate_candle_patterns(count, start_ts),
        DataType::Gaps => generate_gaps(count, start_ts),
        DataType::Divergence => generate_divergence(count, start_ts),
        DataType::VolatilityBreakout => generate_volatility_breakout(count, start_ts),
        DataType::VolatilityClustering => generate_volatility_clustering(count, start_ts),
        DataType::StructuralBreaks => generate_structural_breaks(count, start_ts),
        DataType::Squeeze => generate_squeeze(count, start_ts),
        DataType::Calendar => generate_calendar(count, start_ts),
        DataType::Correlated => generate_correlated(count, start_ts),
        DataType::StrongTrend => generate_strong_trend(count, start_ts),
        DataType::Ranging => generate_ranging(count, start_ts),
        DataType::SweepReversion => generate_sweep_reversion(count, start_ts),
        DataType::ZigZagSwings => generate_zigzag_swings(count, start_ts),
        DataType::TickData => generate_tick_data(count, start_ts),
    }
}

/// Smooth sinusoidal data (default)
fn generate_smooth(count: usize, start_ts: i64) -> Vec<Bar> {
    (0..count)
        .map(|i| {
            let base = 100.0 + (i as f64 * 0.1).sin() * 10.0;
            let open = base;
            let close = base + (i as f64 * 0.15).cos() * 1.0;
            // Ensure high >= max(open, close) and low <= min(open, close)
            let high = open.max(close) + 0.5 + (i as f64 * 0.3).sin().abs() * 1.0;
            let low = open.min(close) - 0.5 - (i as f64 * 0.2).cos().abs() * 1.0;
            let volume = 1000.0 + (i as f64 * 50.0);
            let timestamp = start_ts + (i as i64 * 3600);
            Bar::new(open, high, low, close, volume, timestamp)
        })
        .collect()
}

/// Generate data with specific candle patterns
/// Each pattern is designed to match the detection criteria of its indicator:
/// - Hammer: body_to_range >= 0.1 (10%), lower_shadow >= 2x body, upper_shadow <= 0.5x body
/// - ShootingStar: same but reversed
/// - Doji: body_to_range < 0.1 (10%)
/// - Engulfing: second candle engulfs first, opposite colors, size_ratio >= 1.2
fn generate_candle_patterns(count: usize, start_ts: i64) -> Vec<Bar> {
    let mut bars = Vec::with_capacity(count);
    let mut price = 100.0;

    for i in 0..count {
        let timestamp = start_ts + (i as i64 * 3600);
        let volume = 1000.0 + (i as f64 * 10.0);

        // Create patterns at specific intervals, ensuring proper setups
        let bar = match i % 40 {
            // Hammer: body at top (>60% of range), long lower shadow (>=2x body), minimal upper shadow
            // Body=1.0, Range=6.0, body_to_range=16.7% > 10% ✓
            // lower_shadow=5.0, lower_shadow/body=5.0 >= 2.0 ✓
            // upper_shadow=0.0, upper_shadow/body=0.0 <= 0.5 ✓
            5 => {
                let low = price - 5.0;
                let open = price;
                let close = price + 1.0;
                let high = price + 1.0; // No upper shadow
                Bar::new(open, high, low, close, volume, timestamp)
            }
            // Shooting Star: body at bottom, long upper shadow (>=2x body), minimal lower shadow
            // Body=1.0, Range=6.0, body_to_range=16.7% > 10% ✓
            10 => {
                let high = price + 5.0;
                let open = price;
                let close = price - 1.0;
                let low = price - 1.0; // No lower shadow
                Bar::new(open, high, low, close, volume, timestamp)
            }
            // Doji: body_to_range < 10%
            // Body=0.1, Range=4.0, body_to_range=2.5% < 10% ✓
            15 => {
                let open = price;
                let close = price + 0.1;
                let high = price + 2.0;
                let low = price - 2.0;
                Bar::new(open, high, low, close, volume, timestamp)
            }
            // Setup for Bullish Engulfing (previous bearish bar)
            // This needs to be smaller so next bar can engulf it
            19 => {
                let open = price + 1.0;
                let close = price; // Bearish: close < open
                let high = price + 1.2;
                let low = price - 0.2;
                Bar::new(open, high, low, close, volume, timestamp)
            }
            // Bullish Engulfing: bullish candle engulfs previous bearish
            // Body must be >1.2x previous body and fully engulf it
            20 => {
                let open = price - 0.5;  // Below previous close
                let close = price + 1.8; // Above previous open (engulfs)
                let high = close + 0.2;
                let low = open - 0.2;
                price = close;
                Bar::new(open, high, low, close, volume * 1.5, timestamp)
            }
            // Setup for Bearish Engulfing (previous bullish bar)
            24 => {
                let open = price;
                let close = price + 1.0; // Bullish: close > open
                let high = price + 1.2;
                let low = price - 0.2;
                Bar::new(open, high, low, close, volume, timestamp)
            }
            // Bearish Engulfing: bearish candle engulfs previous bullish
            25 => {
                let prev_open = price;
                let prev_close = price + 1.0;
                let open = prev_close + 0.5;  // Above previous close
                let close = prev_open - 0.8;  // Below previous open (engulfs)
                let high = open + 0.2;
                let low = close - 0.2;
                price = close;
                Bar::new(open, high, low, close, volume * 1.5, timestamp)
            }
            // Marubozu: full body, no shadows (open=low or high, close=high or low)
            30 => {
                let direction = if i % 80 < 40 { 1.0 } else { -1.0 };
                let open = price;
                let close = price + direction * 3.0;
                let high = open.max(close);
                let low = open.min(close);
                price = close;
                Bar::new(open, high, low, close, volume, timestamp)
            }
            // Morning Star: 3-bar pattern (bearish -> small star -> bullish)
            // star_high < first body low, third close > first midpoint
            // Bar 1: Bearish candle
            26 => {
                let open = price + 3.0;
                let close = price;
                let high = open + 0.5;
                let low = close - 0.5;
                Bar::new(open, high, low, close, volume, timestamp)
            }
            // Bar 2: Small star (doji-like, gapped down)
            27 => {
                let star_open = price - 2.0;  // Gap down below previous close
                let star_close = star_open + 0.2;  // Small body
                let star_high = star_open + 0.5;  // Must be < previous body low (price - 0.5)
                let star_low = star_open - 0.5;
                Bar::new(star_open, star_high, star_low, star_close, volume * 0.5, timestamp)
            }
            // Bar 3: Bullish candle closing above first midpoint
            28 => {
                let third_open = price - 1.5;
                let third_close = price + 2.5;  // Above first midpoint (price + 1.5)
                let third_high = third_close + 0.3;
                let third_low = third_open - 0.3;
                price = third_close;
                Bar::new(third_open, third_high, third_low, third_close, volume * 1.5, timestamp)
            }

            // Evening Star: 3-bar pattern (bullish -> small star -> bearish)
            // Bar 1: Bullish candle
            29 => {
                let open = price;
                let close = price + 3.0;
                let high = close + 0.5;
                let low = open - 0.5;
                Bar::new(open, high, low, close, volume, timestamp)
            }
            // Bar 3: Bearish candle closing below first midpoint
            31 => {
                let third_open = price + 4.5;
                let third_close = price + 0.5;  // Below first midpoint (price + 1.5)
                let third_high = third_open + 0.3;
                let third_low = third_close - 0.3;
                price = third_close;
                Bar::new(third_open, third_high, third_low, third_close, volume * 1.5, timestamp)
            }

            // Three White Soldiers: 3 consecutive strong bullish candles
            32 => {
                let open = price;
                let close = price + 2.5;
                let high = close + 0.2;
                let low = open - 0.2;
                price = close;
                Bar::new(open, high, low, close, volume * 1.2, timestamp)
            }
            33 => {
                let open = price - 0.3;  // Opens within previous body
                let close = price + 2.3;  // Closes higher than previous
                let high = close + 0.2;
                let low = open - 0.2;
                price = close;
                Bar::new(open, high, low, close, volume * 1.3, timestamp)
            }
            34 => {
                let open = price - 0.3;
                let close = price + 2.3;
                let high = close + 0.2;
                let low = open - 0.2;
                price = close;
                Bar::new(open, high, low, close, volume * 1.4, timestamp)
            }

            // Three Black Crows: 3 consecutive strong bearish candles
            35 => {
                let open = price;
                let close = price - 2.5;
                let high = open + 0.2;
                let low = close - 0.2;
                price = close;
                Bar::new(open, high, low, close, volume * 1.2, timestamp)
            }
            36 => {
                let open = price + 0.3;  // Opens within previous body
                let close = price - 2.3;  // Closes lower than previous
                let high = open + 0.2;
                let low = close - 0.2;
                price = close;
                Bar::new(open, high, low, close, volume * 1.3, timestamp)
            }
            37 => {
                let open = price + 0.3;
                let close = price - 2.3;
                let high = open + 0.2;
                let low = close - 0.2;
                price = close;
                Bar::new(open, high, low, close, volume * 1.4, timestamp)
            }

            // Piercing Pattern: bearish candle followed by bullish closing above midpoint
            // Bar 1: Bearish
            38 => {
                let open = price + 3.0;
                let close = price;
                let high = open + 0.3;
                let low = close - 0.3;
                Bar::new(open, high, low, close, volume, timestamp)
            }
            // Bar 2: Bullish opening below previous low, closing above midpoint
            39 => {
                let prev_mid = (price + 3.0 + price) / 2.0;  // ~1.5 above current
                let open = price - 0.5;  // Below previous low
                let close = prev_mid + 0.5;  // Above midpoint
                let high = close + 0.2;
                let low = open - 0.2;
                price = close;
                Bar::new(open, high, low, close, volume * 1.3, timestamp)
            }

            // Normal trending bars
            _ => {
                let change = (i as f64 * 0.1).sin() * 1.5;
                price += change;
                let open = price - change;
                let close = price;
                let high = open.max(close) + 0.5;
                let low = open.min(close) - 0.5;
                Bar::new(open, high, low, close, volume, timestamp)
            }
        };

        bars.push(bar);
    }

    bars
}

/// Generate data with Fair Value Gaps (FVG)
/// FVG is a 3-bar pattern where middle bar's range doesn't overlap with surrounding bars:
/// - Bullish FVG: low[1] > high[0] AND low[1] > high[2]
/// - Bearish FVG: high[1] < low[0] AND high[1] < low[2]
fn generate_gaps(count: usize, start_ts: i64) -> Vec<Bar> {
    let mut bars = Vec::with_capacity(count);
    let mut price = 100.0;

    for i in 0..count {
        let timestamp = start_ts + (i as i64 * 3600);
        let volume = 1000.0 + (i as f64 * 10.0);

        // Create FVG triplets at specific positions
        let bar = match i % 20 {
            // Bullish FVG setup: Bar 0 (anchor bar before gap)
            5 => {
                let open = price;
                let close = price + 1.0;
                let high = price + 1.5;  // High = 101.5
                let low = price - 0.5;
                Bar::new(open, high, low, close, volume, timestamp)
            }
            // Bullish FVG: Bar 1 (gap bar) - low must be > previous high
            6 => {
                // Previous high was ~101.5, so low must be > 101.5
                let low = price + 3.0;  // Low = 103.0 > 101.5 ✓
                let open = low + 1.0;
                let close = low + 2.0;
                let high = low + 3.0;
                price = close;
                Bar::new(open, high, low, close, volume * 2.0, timestamp)
            }
            // Bullish FVG: Bar 2 - high must be < bar 1's low
            7 => {
                // Bar 1's low was ~103.0, so high must be < 103.0
                let prev_gap_low = price - 2.0; // Approximate bar 1's low
                let high = prev_gap_low - 0.5;  // High < gap low ✓
                let open = high - 1.0;
                let close = high - 0.5;
                let low = high - 2.0;
                price = close;
                Bar::new(open, high, low, close, volume, timestamp)
            }
            // Bearish FVG setup: Bar 0 (anchor bar before gap)
            12 => {
                let open = price;
                let close = price - 1.0;
                let high = price + 0.5;
                let low = price - 1.5;  // Low = ~98.5
                Bar::new(open, high, low, close, volume, timestamp)
            }
            // Bearish FVG: Bar 1 (gap bar) - high must be < previous low
            13 => {
                // Previous low was ~98.5, so high must be < 98.5
                let high = price - 3.0;  // High = ~97 < 98.5 ✓
                let open = high - 1.0;
                let close = high - 2.0;
                let low = high - 3.0;
                price = close;
                Bar::new(open, high, low, close, volume * 2.0, timestamp)
            }
            // Bearish FVG: Bar 2 - low must be > bar 1's high
            14 => {
                // Bar 1's high was ~97, so low must be > 97
                let prev_gap_high = price + 2.0; // Approximate bar 1's high
                let low = prev_gap_high + 0.5;  // Low > gap high ✓
                let open = low + 0.5;
                let close = low + 1.0;
                let high = low + 2.0;
                price = close;
                Bar::new(open, high, low, close, volume, timestamp)
            }
            // Normal bars
            _ => {
                let change = (i as f64 * 0.05).sin() * 1.0;
                price += change;
                let open = price - change;
                let close = price;
                let high = open.max(close) + 0.8;
                let low = open.min(close) - 0.8;
                Bar::new(open, high, low, close, volume, timestamp)
            }
        };

        bars.push(bar);
    }

    bars
}

/// Generate data with divergences (price trending one way, structure suggesting reversal)
fn generate_divergence(count: usize, start_ts: i64) -> Vec<Bar> {
    let mut bars = Vec::with_capacity(count);
    let mut price = 100.0;

    // Create a bullish divergence scenario:
    // Price makes lower lows, but momentum/RSI makes higher lows
    // This means: steeper drops followed by smaller drops

    for i in 0..count {
        let timestamp = start_ts + (i as i64 * 3600);

        let phase = i / 50; // Change phase every 50 bars
        let within_phase = i % 50;

        let (change, vol_mult) = match phase % 4 {
            // Phase 0: Sharp drop
            0 => {
                let drop = -0.5 - (within_phase as f64 * 0.02);
                (drop, 1.5)
            }
            // Phase 1: Weak recovery (divergence setup)
            1 => {
                let rise = 0.3 + (within_phase as f64 * 0.01);
                (rise, 0.8)
            }
            // Phase 2: Smaller drop (higher low in momentum)
            2 => {
                let drop = -0.3 - (within_phase as f64 * 0.005);
                (drop, 1.2)
            }
            // Phase 3: Strong recovery (divergence confirmed)
            3 => {
                let rise = 0.8 + (within_phase as f64 * 0.03);
                (rise, 2.0)
            }
            _ => (0.0, 1.0),
        };

        price += change;
        let open = price - change;
        let close = price;
        let volatility = 1.0 + change.abs() * 0.5;
        let high = open.max(close) + volatility;
        let low = open.min(close) - volatility;
        let volume = 1000.0 * vol_mult;

        bars.push(Bar::new(open, high, low, close, volume, timestamp));
    }

    bars
}

/// Generate data with volatility breakouts
fn generate_volatility_breakout(count: usize, start_ts: i64) -> Vec<Bar> {
    let mut bars = Vec::with_capacity(count);
    let mut price = 100.0;

    for i in 0..count {
        let timestamp = start_ts + (i as i64 * 3600);

        // Alternate between quiet periods and breakouts
        let cycle = i / 30;
        let within_cycle = i % 30;

        let (volatility, trend, vol_mult) = if within_cycle < 20 {
            // Quiet period - low volatility
            (0.5, (within_cycle as f64 * 0.02).sin() * 0.1, 0.5)
        } else {
            // Breakout - high volatility
            let breakout_strength = (within_cycle - 20) as f64 * 0.5;
            let direction = if cycle % 2 == 0 { 1.0 } else { -1.0 };
            (3.0 + breakout_strength, direction * 1.5, 3.0)
        };

        price += trend;
        let open = price;
        let close = price + trend;
        let high = open.max(close) + volatility;
        let low = open.min(close) - volatility;
        let volume = 1000.0 * vol_mult;

        bars.push(Bar::new(open, high, low, close, volume, timestamp));
    }

    bars
}

/// Generate data with volatility clustering (ARCH effects)
fn generate_volatility_clustering(count: usize, start_ts: i64) -> Vec<Bar> {
    let mut bars = Vec::with_capacity(count);
    let mut price = 100.0_f64;
    let mut current_vol: f64 = 1.0;

    for i in 0..count {
        let timestamp = start_ts + (i as i64 * 3600);

        // GARCH-like: volatility depends on previous volatility
        // High vol -> likely high vol, low vol -> likely low vol
        let vol_shock = if i % 40 == 0 {
            // Occasional volatility shock
            3.0
        } else {
            0.0
        };

        // Mean reversion in volatility
        current_vol = 0.95 * current_vol + 0.05 * 1.0 + vol_shock;
        current_vol = current_vol.clamp(0.3, 5.0);

        let return_val = (i as f64 * 0.1).sin() * current_vol * 0.5;
        price *= 1.0 + return_val / 100.0;

        let open = price / (1.0 + return_val / 100.0);
        let close = price;
        let range = current_vol * 2.0;
        let high = open.max(close) + range * 0.6;
        let low = open.min(close) - range * 0.4;
        let volume = 1000.0 * (1.0 + current_vol * 0.5);

        bars.push(Bar::new(open, high, low, close, volume, timestamp));
    }

    bars
}

/// Generate data with structural breaks
fn generate_structural_breaks(count: usize, start_ts: i64) -> Vec<Bar> {
    let mut bars = Vec::with_capacity(count);
    let mut price = 100.0;
    let mut trend = 0.1;

    for i in 0..count {
        let timestamp = start_ts + (i as i64 * 3600);

        // Structural break every ~100 bars
        if i % 100 == 50 {
            trend = -trend * 1.5; // Reverse and strengthen trend
            price += trend * 10.0; // Jump
        }

        price += trend;
        let noise = (i as f64 * 0.2).sin() * 0.5;
        let open = price - trend;
        let close = price + noise;
        let high = open.max(close) + 1.0;
        let low = open.min(close) - 1.0;
        let volume = 1000.0;

        bars.push(Bar::new(open, high, low, close, volume, timestamp));
    }

    bars
}

/// Generate squeeze pattern (compression -> expansion)
fn generate_squeeze(count: usize, start_ts: i64) -> Vec<Bar> {
    let mut bars = Vec::with_capacity(count);
    let mut price = 100.0;

    for i in 0..count {
        let timestamp = start_ts + (i as i64 * 3600);

        let cycle = i / 40;
        let within_cycle = i % 40;

        // First 30 bars: compression (decreasing range)
        // Last 10 bars: expansion (increasing range)
        let range = if within_cycle < 30 {
            let compression = (30 - within_cycle) as f64 / 30.0;
            0.5 + compression * 2.0
        } else {
            let expansion = (within_cycle - 30) as f64;
            0.5 + expansion * 1.5
        };

        // After squeeze, breakout in alternating direction
        let trend = if within_cycle >= 30 {
            let direction = if cycle % 2 == 0 { 1.0 } else { -1.0 };
            direction * (within_cycle - 30) as f64 * 0.5
        } else {
            (within_cycle as f64 * 0.05).sin() * 0.2
        };

        price += trend;
        let open = price - trend * 0.5;
        let close = price;
        let high = open.max(close) + range;
        let low = open.min(close) - range;
        let volume = 1000.0 * (1.0 + range * 0.3);

        bars.push(Bar::new(open, high, low, close, volume, timestamp));
    }

    bars
}

/// Generate calendar-aware data (specific days/times)
fn generate_calendar(count: usize, _start_ts: i64) -> Vec<Bar> {
    let mut bars = Vec::with_capacity(count);
    let mut price = 100.0;

    // Start on Monday 00:00 UTC (adjust start_ts to be Monday)
    // 1704067200 is Monday, January 1, 2024 00:00:00 UTC
    let monday_start = 1704067200_i64;

    for i in 0..count {
        // Each bar is 1 hour
        let timestamp = monday_start + (i as i64 * 3600);

        // Calculate day of week (0 = Monday)
        let hours_from_start = i as i64;
        let day_of_week = (hours_from_start / 24) % 7;
        let hour_of_day = hours_from_start % 24;

        // Higher volatility during market open hours
        let session_vol = if (13..=20).contains(&hour_of_day) {
            2.0 // US session
        } else if (7..=15).contains(&hour_of_day) {
            1.5 // EU session
        } else if (0..=8).contains(&hour_of_day) {
            1.2 // Asia session
        } else {
            0.8 // Off-hours
        };

        // Monday open often has gaps
        let gap = if day_of_week == 0 && hour_of_day == 0 && i > 0 {
            (i as f64 * 0.1).sin() * 3.0
        } else {
            0.0
        };

        // End of month effect (every ~720 bars assuming 1 hour per bar)
        let eom_vol = if i % 720 > 690 { 1.5 } else { 1.0 };

        price += gap + (i as f64 * 0.05).sin() * session_vol * 0.5;
        let volatility = session_vol * eom_vol;
        let open = price - gap;
        let close = price;
        let high = open.max(close) + volatility;
        let low = open.min(close) - volatility;
        let volume = 1000.0 * session_vol;

        bars.push(Bar::new(open, high, low, close, volume, timestamp));
    }

    bars
}

/// Generate correlated price-volume data
fn generate_correlated(count: usize, start_ts: i64) -> Vec<Bar> {
    let mut bars = Vec::with_capacity(count);
    let mut price = 100.0;
    let mut prev_change = 0.0;

    for i in 0..count {
        let timestamp = start_ts + (i as i64 * 3600);

        // Create strong AUTOCORRELATION: today's return similar to yesterday's
        // This is key for mutual information (lag-1 dependency)
        let momentum = prev_change * 0.7; // 70% carryover from previous bar
        let phase = (i / 50) % 3;
        let base_trend = match phase {
            0 => 1.5,   // Uptrend period
            1 => -1.2,  // Downtrend period
            _ => 0.3,   // Consolidation
        };

        let noise = (i as f64 * 0.2).sin() * 0.3;
        let price_change = momentum + base_trend * 0.3 + noise;
        prev_change = price_change;

        // Volume correlates with absolute price change (for TE)
        let volume_factor = 1.0 + price_change.abs() * 1.5;

        price = (price + price_change).max(30.0);
        let open = price - price_change * 0.7;
        let close = price;
        let range = price_change.abs().max(0.5);
        let high = open.max(close) + range * 0.3;
        let low = open.min(close) - range * 0.3;
        let volume = 500.0 + 1000.0 * volume_factor;

        bars.push(Bar::new(open, high, low, close, volume, timestamp));
    }

    bars
}

/// Generate strong trending data
fn generate_strong_trend(count: usize, start_ts: i64) -> Vec<Bar> {
    let mut bars = Vec::with_capacity(count);
    let mut price = 100.0_f64;

    for i in 0..count {
        let timestamp = start_ts + (i as i64 * 3600);

        // Alternate between uptrend and downtrend
        let phase = i / 100;
        let trend: f64 = if phase % 2 == 0 { 0.5 } else { -0.5 };

        // Add pullbacks
        let pullback = if i % 20 < 5 { -trend * 0.3 } else { 0.0 };

        price += trend + pullback;
        let open = price - trend;
        let close = price;

        // Higher highs/lows in trend direction
        let (high, low) = if trend > 0.0 {
            (close + 1.5, open - 0.5)
        } else {
            (open + 0.5, close - 1.5)
        };

        let volume = 1000.0 * (1.0 + trend.abs());

        bars.push(Bar::new(open, high, low, close, volume, timestamp));
    }

    bars
}

/// Generate ranging/choppy data with sharp spikes for XOR divergence detection
/// XOR needs: short RSI(7) in extreme while long RSI(21) stays neutral
/// This requires 5-7 consecutive bars of strong movement after a stable period
fn generate_ranging(count: usize, start_ts: i64) -> Vec<Bar> {
    let mut bars = Vec::with_capacity(count);
    let mut price = 100.0;

    for i in 0..count {
        let timestamp = start_ts + (i as i64 * 3600);

        // Create pattern: 25 stable bars, then 7 extreme bars, then 8 reversal bars
        let cycle_phase = i % 40;
        let change = if (25..=31).contains(&cycle_phase) {
            // Sharp 7-bar spike up - pushes short RSI(7) above 70
            // Long RSI(21) needs more bars to reach extreme
            5.0
        } else if (32..=39).contains(&cycle_phase) {
            // Sharp reversal down - pushes short RSI(7) below 30
            -6.0
        } else {
            // Stable oscillation keeps long RSI near 50
            (i as f64 * 0.15).sin() * 0.3
        };

        price = (price + change).clamp(50.0, 150.0);

        let open = price - change * 0.4;
        let close = price;
        let high = open.max(close) + 0.3;
        let low = open.min(close) - 0.3;
        let volume = 1000.0;

        bars.push(Bar::new(open, high, low, close, volume, timestamp));
    }

    bars
}

/// Generate sweep + reversion patterns
/// SweepRev needs:
/// - Price to exceed previous high/low (sweep)
/// - Then close back within the range (reversion)
/// lookback_period is typically 40, so we need 40+ bar ranges
fn generate_sweep_reversion(count: usize, start_ts: i64) -> Vec<Bar> {
    let mut bars = Vec::with_capacity(count);
    let mut price = 100.0;
    let mut range_high = price + 5.0;
    let mut range_low = price - 5.0;

    for i in 0..count {
        let timestamp = start_ts + (i as i64 * 3600);
        let volume = 1000.0 + (i as f64 * 10.0);

        let cycle = i % 60;

        let bar = match cycle {
            // First 40 bars: establish range
            0..=39 => {
                let oscillation = (cycle as f64 * 0.15).sin() * 3.0;
                price = 100.0 + oscillation;
                let open = price - oscillation * 0.3;
                let close = price;
                let high = open.max(close) + 0.5;
                let low = open.min(close) - 0.5;

                // Track range
                if high > range_high { range_high = high; }
                if low < range_low { range_low = low; }

                Bar::new(open, high, low, close, volume, timestamp)
            }
            // Bar 40-42: Bearish sweep (high exceeds range high, closes in lower quartile)
            40..=42 => {
                let sweep_high = range_high + 2.0 + (cycle - 40) as f64;  // Exceeds range
                let close = range_low + (range_high - range_low) * 0.2;   // Closes in lower 20%
                let open = sweep_high - 1.0;
                let low = close - 0.5;
                price = close;
                Bar::new(open, sweep_high, low, close, volume * 2.0, timestamp)
            }
            // Bar 43-45: Continuation down
            43..=45 => {
                let change = -1.5;
                price += change;
                let open = price - change;
                let close = price;
                let high = open.max(close) + 0.3;
                let low = open.min(close) - 0.3;
                Bar::new(open, high, low, close, volume, timestamp)
            }
            // Bar 46-48: Bullish sweep (low exceeds range low, closes in upper quartile)
            46..=48 => {
                let sweep_low = range_low - 2.0 - (cycle - 46) as f64;  // Exceeds range
                let close = range_low + (range_high - range_low) * 0.8;  // Closes in upper 20%
                let open = sweep_low + 1.0;
                let high = close + 0.5;
                price = close;
                Bar::new(open, high, sweep_low, close, volume * 2.0, timestamp)
            }
            // Rest: reset and oscillate
            _ => {
                let oscillation = ((cycle - 49) as f64 * 0.2).sin() * 2.0;
                price = 100.0 + oscillation;
                range_high = price + 5.0;
                range_low = price - 5.0;
                let open = price - oscillation * 0.3;
                let close = price;
                let high = open.max(close) + 0.5;
                let low = open.min(close) - 0.5;
                Bar::new(open, high, low, close, volume, timestamp)
            }
        };

        bars.push(bar);
    }

    bars
}

/// Generate clear ZigZag swings with defined pivots
/// ZigZagCandle needs N-bar swing highs/lows where center > all N neighbors on both sides
/// We create clear peaks every 10 bars with 5-bar approach and 5-bar decline
fn generate_zigzag_swings(count: usize, start_ts: i64) -> Vec<Bar> {
    let mut bars: Vec<Bar> = Vec::with_capacity(count);

    for i in 0..count {
        let timestamp = start_ts + (i as i64 * 3600);
        let volume = 1000.0;

        // Create alternating peaks and troughs every 10 bars
        // Pattern: 5 bars rising to peak, 5 bars falling to trough
        let cycle = i % 20;
        let base = 100.0;

        let close = match cycle {
            // Rising to peak at bar 5
            0 => base,
            1 => base + 2.0,
            2 => base + 4.0,
            3 => base + 6.0,
            4 => base + 8.0,
            5 => base + 10.0,  // PEAK - higher than 4 neighbors on each side
            6 => base + 8.0,
            7 => base + 6.0,
            8 => base + 4.0,
            9 => base + 2.0,
            // Falling to trough at bar 15
            10 => base,
            11 => base - 2.0,
            12 => base - 4.0,
            13 => base - 6.0,
            14 => base - 8.0,
            15 => base - 10.0,  // TROUGH - lower than 4 neighbors on each side
            16 => base - 8.0,
            17 => base - 6.0,
            18 => base - 4.0,
            19 => base - 2.0,
            _ => base,
        };

        let prev_close = if i > 0 { bars[i - 1].close } else { close };
        let open = prev_close;
        let high = open.max(close) + 0.5;
        let low = open.min(close) - 0.5;

        bars.push(Bar::new(open, high, low, close, volume, timestamp));
    }

    bars
}

/// Generate tick-level data simulation
/// Each bar represents aggregated tick activity with varying tick counts
/// Volume = number of ticks, and we create patterns in tick distribution
fn generate_tick_data(count: usize, start_ts: i64) -> Vec<Bar> {
    let mut bars = Vec::with_capacity(count);
    let mut price = 100.0;

    for i in 0..count {
        let timestamp = start_ts + (i as i64 * 60);  // 1-minute bars for ticks
        let hour = (i / 60) % 24;

        // Tick count varies by "session"
        let base_ticks = match hour {
            9..=11 => 500.0,   // Morning session - high activity
            12..=13 => 200.0,  // Lunch - low activity
            14..=16 => 600.0,  // Afternoon - highest
            _ => 100.0,        // Off-hours - minimal
        };

        // Add randomness using deterministic pattern
        let tick_noise = 1.0 + (i as f64 * 0.7).sin() * 0.5;
        let tick_count = base_ticks * tick_noise;

        // Price moves proportional to tick imbalance
        let buy_ratio = 0.5 + (i as f64 * 0.1).sin() * 0.2;
        let imbalance = buy_ratio - 0.5;  // -0.2 to +0.2
        let price_change = imbalance * tick_count.sqrt() * 0.01;

        price *= 1.0 + price_change;
        price = price.clamp(80.0, 120.0);

        let open = price / (1.0 + price_change);
        let close = price;
        let volatility = tick_count.sqrt() * 0.02;
        let high = open.max(close) + volatility;
        let low = open.min(close) - volatility;

        // Volume = tick count (this is what TickVolumeAnalyzer expects)
        bars.push(Bar::new(open, high, low, close, tick_count, timestamp));
    }

    bars
}

/// Get recommended data type for an indicator category
pub fn recommended_data_type(indicator_name: &str) -> DataType {
    let name_lower = indicator_name.to_lowercase();

    // Specific indicators that need exact matching (check before generic patterns)
    // TickVolume needs TickData, not Calendar
    if name_lower == "tickvolume" || name_lower == "tick_volume" || name_lower == "tickvol" {
        return DataType::TickData;
    }

    // Candle patterns
    if name_lower.contains("hammer") || name_lower.contains("doji") ||
       name_lower.contains("engulf") || name_lower.contains("harami") ||
       name_lower.contains("star") || name_lower.contains("marubozu") ||
       name_lower.contains("tweezer") || name_lower.contains("piercing") ||
       name_lower.contains("cloud") || name_lower.contains("soldiers") ||
       name_lower.contains("crows") || name_lower.contains("candle") ||
       name_lower.contains("pattern")
    {
        return DataType::CandlePatterns;
    }

    // Gaps/FVG
    if name_lower.contains("fvg") || name_lower.contains("gap") ||
       name_lower.contains("liq")
    {
        return DataType::Gaps;
    }

    // Divergence
    if name_lower.contains("div") {
        return DataType::Divergence;
    }

    // Volatility
    if name_lower.contains("breakout") || name_lower.contains("vb") ||
       name_lower.contains("jump") || name_lower.contains("rbv")
    {
        return DataType::VolatilityBreakout;
    }

    // ARCH/GARCH
    if name_lower.contains("arch") || name_lower.contains("garch") {
        return DataType::VolatilityClustering;
    }

    // Structural breaks
    if name_lower.contains("cusum") || name_lower.contains("break") ||
       name_lower.contains("bp")
    {
        return DataType::StructuralBreaks;
    }

    // Squeeze
    if name_lower.contains("squeeze") || name_lower.contains("compress") ||
       name_lower.contains("rcb") || name_lower.contains("wave")
    {
        return DataType::Squeeze;
    }

    // Calendar
    if name_lower.contains("month") || name_lower.contains("qtr") ||
       name_lower.contains("calendar") || name_lower.contains("tenc") ||
       name_lower.contains("som") || name_lower.contains("soq") ||
       name_lower.contains("tick")
    {
        return DataType::Calendar;
    }

    // Entropy/correlation
    if name_lower.contains("entropy") || name_lower.contains("mutual") ||
       name_lower.contains("transfer") || name_lower.contains("mi") ||
       name_lower.contains("te") || name_lower.contains("xmil")
    {
        return DataType::Correlated;
    }

    // Sweep reversion
    if name_lower.contains("sweep") {
        return DataType::SweepReversion;
    }

    // ZigZag patterns
    if name_lower.contains("zigzag") {
        return DataType::ZigZagSwings;
    }

    // Tick volume
    if name_lower.contains("tickvol") || name_lower.contains("tick_vol") {
        return DataType::TickData;
    }

    // Market structure
    if name_lower.contains("cipher") || name_lower.contains("mrf") ||
       name_lower.contains("fractal") || name_lower.contains("ichimoku") ||
       name_lower.contains("bos") || name_lower.contains("avwap")
    {
        return DataType::StrongTrend;
    }

    // Logic gates - need varied input
    if name_lower.contains("logic") || name_lower.contains("thresh") ||
       name_lower.contains("hyst")
    {
        return DataType::Ranging;
    }

    // STFT/spectral
    if name_lower.contains("stft") || name_lower.contains("spectral") ||
       name_lower.contains("fft")
    {
        return DataType::VolatilityClustering;
    }

    // Default
    DataType::Smooth
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_generate_all_types() {
        let types = [
            DataType::Smooth,
            DataType::CandlePatterns,
            DataType::Gaps,
            DataType::Divergence,
            DataType::VolatilityBreakout,
            DataType::VolatilityClustering,
            DataType::StructuralBreaks,
            DataType::Squeeze,
            DataType::Calendar,
            DataType::Correlated,
            DataType::StrongTrend,
            DataType::Ranging,
            DataType::SweepReversion,
            DataType::ZigZagSwings,
            DataType::TickData,
        ];

        for data_type in types {
            let bars = generate_bars(data_type, 100, 1704067200);
            assert_eq!(bars.len(), 100, "{:?} should generate 100 bars", data_type);

            for (i, bar) in bars.iter().enumerate() {
                assert!(bar.high >= bar.low, "{:?} bar {} high >= low", data_type, i);
                assert!(bar.high >= bar.open, "{:?} bar {} high >= open", data_type, i);
                assert!(bar.high >= bar.close, "{:?} bar {} high >= close", data_type, i);
                assert!(bar.low <= bar.open, "{:?} bar {} low <= open", data_type, i);
                assert!(bar.low <= bar.close, "{:?} bar {} low <= close", data_type, i);
                assert!(bar.volume > 0.0, "{:?} bar {} volume > 0", data_type, i);
            }
        }
    }

    #[test]
    fn test_candle_patterns_has_patterns() {
        let bars = generate_bars(DataType::CandlePatterns, 100, 1704067200);

        // Check for hammer pattern (long lower shadow)
        let has_hammer = bars.iter().any(|b| {
            let body = (b.close - b.open).abs();
            let lower_shadow = b.open.min(b.close) - b.low;
            lower_shadow > body * 2.0
        });
        assert!(has_hammer, "Should have hammer-like patterns");

        // Check for doji (small body)
        let has_doji = bars.iter().any(|b| {
            let body = (b.close - b.open).abs();
            let range = b.high - b.low;
            body < range * 0.1
        });
        assert!(has_doji, "Should have doji-like patterns");
    }

    #[test]
    fn test_gaps_has_gaps() {
        let bars = generate_bars(DataType::Gaps, 100, 1704067200);

        // Check for gaps between consecutive bars
        let mut gap_count = 0;
        for i in 1..bars.len() {
            let prev = &bars[i - 1];
            let curr = &bars[i];

            // Gap up: current low > previous high
            if curr.low > prev.high {
                gap_count += 1;
            }
            // Gap down: current high < previous low
            if curr.high < prev.low {
                gap_count += 1;
            }
        }

        assert!(gap_count >= 3, "Should have at least 3 gaps, found {}", gap_count);
    }

    #[test]
    fn test_recommended_data_type() {
        assert_eq!(recommended_data_type("Hammer"), DataType::CandlePatterns);
        assert_eq!(recommended_data_type("FvgDetector"), DataType::Gaps);
        assert_eq!(recommended_data_type("RsiDiv"), DataType::Divergence);
        assert_eq!(recommended_data_type("VolatilityBreakout"), DataType::VolatilityBreakout);
        assert_eq!(recommended_data_type("ArchLm"), DataType::VolatilityClustering);
        assert_eq!(recommended_data_type("BpCusum"), DataType::StructuralBreaks);
        assert_eq!(recommended_data_type("Rcb"), DataType::Squeeze);
        assert_eq!(recommended_data_type("MonthTurn"), DataType::Calendar);
        assert_eq!(recommended_data_type("MutualInformation"), DataType::Correlated);
        assert_eq!(recommended_data_type("Sma"), DataType::Smooth);
        // New types
        assert_eq!(recommended_data_type("SweepRev"), DataType::SweepReversion);
        assert_eq!(recommended_data_type("TickVolume"), DataType::TickData);
    }

    #[test]
    fn test_sweep_reversion_has_sweeps() {
        let bars = generate_bars(DataType::SweepReversion, 200, 1704067200);

        // Check for sweep pattern: high exceeds range then closes low
        let mut found_sweep = false;
        for i in 41..bars.len() {
            let bar = &bars[i];
            // Sweep: large range where high is far from close
            let range = bar.high - bar.low;
            let close_position = (bar.close - bar.low) / range;
            if range > 5.0 && close_position < 0.3 {
                found_sweep = true;
                break;
            }
        }
        assert!(found_sweep, "Should have sweep patterns");
    }

    #[test]
    fn test_zigzag_has_swings() {
        let bars = generate_bars(DataType::ZigZagSwings, 100, 1704067200);

        // Check for clear swing high/low points
        let mut swing_highs = 0;
        let mut swing_lows = 0;

        for i in 20..bars.len() - 20 {
            // Local max
            if (0..20).all(|j| bars[i].high >= bars[i - j - 1].high) &&
               (0..20).all(|j| bars[i].high >= bars[i + j + 1].high)
            {
                swing_highs += 1;
            }
            // Local min
            if (0..20).all(|j| bars[i].low <= bars[i - j - 1].low) &&
               (0..20).all(|j| bars[i].low <= bars[i + j + 1].low)
            {
                swing_lows += 1;
            }
        }

        assert!(swing_highs >= 1 || swing_lows >= 1, "Should have swing points");
    }

    #[test]
    fn test_tick_data_has_varying_volume() {
        let bars = generate_bars(DataType::TickData, 1440, 1704067200);  // 24 hours of minute data

        // Check that volume varies significantly (session effects)
        let volumes: Vec<f64> = bars.iter().map(|b| b.volume).collect();
        let max_vol = volumes.iter().cloned().fold(0.0, f64::max);
        let min_vol = volumes.iter().cloned().fold(f64::MAX, f64::min);

        assert!(max_vol / min_vol > 3.0, "Volume should vary by at least 3x across sessions");
    }
}