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
//! average_catalog.rs: Catalog of all Average/Moving Average indicators
//!
//! Auto-generated catalog based on actual indicator implementations.
//! Contains IndicatorSignature definitions for 22 moving average indicators.
//!
//! Note: Legacy OHLCV-specific MA indicators have been REMOVED.
//! For OHLCV field selection, use MovingAverageWithField wrapper:
//!   MovingAverageWithField::new(MovingAverageType::SMA, 20, OhlcvField::High)

use crate::catalog::{
    IndicatorSignature, IndicatorCategory, ParamConstraint, ParamType, ParamValue, SourceType,
    IndicatorRoleKind,
};
use crate::bar_indicators::average::moving_average::MovingAverageType;
use crate::bar_indicators::indicator_value::IndicatorValueKind;
use super::super::bar_indicator_id::BarIndicatorId;

use once_cell::sync::Lazy;
use std::collections::HashMap;

/// Category for all indicators in this module
pub const CATEGORY: IndicatorCategory = IndicatorCategory::Average;

// ============================================================================
// Individual indicator signatures
// ============================================================================

/// Simple Moving Average - arithmetic mean over N periods
pub fn signature_sma() -> IndicatorSignature {
    IndicatorSignature::builder("SMA", CATEGORY)
        .name("Simple Moving Average")
        .description("Arithmetic mean of prices over N periods")
        .add_constraint(ParamConstraint::period(2, 200, 20))
        .metadata("complexity", "O(1) with ring buffer")
        .metadata("author", "Classic TA")
        .machine_id(BarIndicatorId::Sma)
        .role_kind(IndicatorRoleKind::Smoother)
        .output_kind(IndicatorValueKind::Single)
        // Note: "SMA" is already the main ID, no need for alias
        .alias("Sma")
        .alias("sma")
        .alias("SIMPLEMOVINGAVERAGE")
        .alias("SimpleMovingAverage")
        .alias("simplemovingaverage")
        .alias("simple_moving_average")
        .alias("SIMPLE_MOVING_AVERAGE")
        .alias("Simple_Moving_Average")
        .build()
}

/// Exponential Moving Average - weighted MA giving more weight to recent prices
pub fn signature_ema() -> IndicatorSignature {
    IndicatorSignature::builder("EMA", CATEGORY)
        .name("Exponential Moving Average")
        .description("Weighted moving average with exponential decay")
        .add_constraint(ParamConstraint::period(2, 200, 12))
        .metadata("complexity", "O(1)")
        .metadata("author", "Classic TA")
        .machine_id(BarIndicatorId::Ema)
        .role_kind(IndicatorRoleKind::Smoother)
        .output_kind(IndicatorValueKind::Single)
        // Note: "EMA" is already the main ID, no need for alias
        .alias("Ema")
        .alias("ema")
        .alias("EXPONENTIALMOVINGAVERAGE")
        .alias("ExponentialMovingAverage")
        .alias("exponentialmovingaverage")
        .alias("exponential_moving_average")
        .alias("EXPONENTIAL_MOVING_AVERAGE")
        .alias("Exponential_Moving_Average")
        .build()
}

/// Weighted Moving Average - linear weights from 1 to N
pub fn signature_wma() -> IndicatorSignature {
    IndicatorSignature::builder("WMA", CATEGORY)
        .name("Weighted Moving Average")
        .description("Moving average with linear weights")
        .add_constraint(ParamConstraint::period(2, 256, 20))
        .metadata("max_period", "256")
        .machine_id(BarIndicatorId::Wma)  // Canonical name for optimized WMA
        .role_kind(IndicatorRoleKind::Smoother)
        .output_kind(IndicatorValueKind::Single)
        // Note: "WMA" is already the main ID, no need for alias
        .alias("Wma")
        .alias("wma")
        .alias("WEIGHTEDMOVINGAVERAGE")
        .alias("WeightedMovingAverage")
        .alias("weightedmovingaverage")
        .alias("weighted_moving_average")
        .alias("WEIGHTED_MOVING_AVERAGE")
        .alias("Weighted_Moving_Average")
        .build()
}

/// Hull Moving Average - fast and smooth MA by Alan Hull
pub fn signature_hma() -> IndicatorSignature {
    IndicatorSignature::builder("HMA", CATEGORY)
        .name("Hull Moving Average")
        .description("Fast and smooth moving average using WMAs")
        .add_constraint(ParamConstraint::period(2, 200, 16))
        .metadata("author", "Alan Hull")
        .metadata("formula", "WMA(2*WMA(n/2) - WMA(n), sqrt(n))")
        .machine_id(BarIndicatorId::Hma)  // Canonical name for optimized HMA
        .role_kind(IndicatorRoleKind::Smoother)
        .output_kind(IndicatorValueKind::Single)
        // Note: "HMA" is already the main ID, no need for alias
        .alias("Hma")
        .alias("hma")
        .alias("HULLMOVINGAVERAGE")
        .alias("HullMovingAverage")
        .alias("hullmovingaverage")
        .alias("hull_moving_average")
        .alias("HULL_MOVING_AVERAGE")
        .alias("Hull_Moving_Average")
        .build()
}

/// Double Exponential Moving Average
pub fn signature_dema() -> IndicatorSignature {
    IndicatorSignature::builder("DEMA", CATEGORY)
        .name("Double Exponential Moving Average")
        .description("Double smoothed EMA for reduced lag")
        .add_constraint(ParamConstraint::period(2, 200, 20))
        .metadata("formula", "2*EMA - EMA(EMA)")
        .machine_id(BarIndicatorId::Dema)
        .role_kind(IndicatorRoleKind::Smoother)
        .output_kind(IndicatorValueKind::Single)
        // Note: "DEMA" is already the main ID, no need for alias
        .alias("Dema")
        .alias("dema")
        .alias("DOUBLEEXPONENTIALMOVINGAVERAGE")
        .alias("DoubleExponentialMovingAverage")
        .alias("doubleexponentialmovingaverage")
        .alias("double_exponential_moving_average")
        .alias("DOUBLE_EXPONENTIAL_MOVING_AVERAGE")
        .alias("Double_Exponential_Moving_Average")
        .build()
}

/// Triple Exponential Moving Average
pub fn signature_tema() -> IndicatorSignature {
    IndicatorSignature::builder("TEMA", CATEGORY)
        .name("Triple Exponential Moving Average")
        .description("Triple smoothed EMA for minimal lag")
        .add_constraint(ParamConstraint::period(2, 200, 20))
        .metadata("formula", "3*EMA - 3*EMA(EMA) + EMA(EMA(EMA))")
        .machine_id(BarIndicatorId::Tema)
        .role_kind(IndicatorRoleKind::Smoother)
        .output_kind(IndicatorValueKind::Single)
        // Note: "TEMA" is already the main ID, no need for alias
        .alias("Tema")
        .alias("tema")
        .alias("TRIPLEEXPONENTIALMOVINGAVERAGE")
        .alias("TripleExponentialMovingAverage")
        .alias("tripleexponentialmovingaverage")
        .alias("triple_exponential_moving_average")
        .alias("TRIPLE_EXPONENTIAL_MOVING_AVERAGE")
        .alias("Triple_Exponential_Moving_Average")
        .build()
}

/// Running Moving Average (Wilder's smoothing)
pub fn signature_rma() -> IndicatorSignature {
    IndicatorSignature::builder("RMA", CATEGORY)
        .name("Running Moving Average")
        .description("Wilder's smoothing method used in RSI")
        .add_constraint(ParamConstraint::period(2, 200, 14))
        .metadata("author", "J. Welles Wilder")
        .metadata("aka", "Wilder's Moving Average")
        .machine_id(BarIndicatorId::Rma)
        .role_kind(IndicatorRoleKind::Smoother)
        .output_kind(IndicatorValueKind::Single)
        // Note: "RMA" is already the main ID, no need for alias
        .alias("Rma")
        .alias("rma")
        .alias("RUNNINGMOVINGAVERAGE")
        .alias("RunningMovingAverage")
        .alias("runningmovingaverage")
        .alias("running_moving_average")
        .alias("RUNNING_MOVING_AVERAGE")
        .alias("Running_Moving_Average")
        .build()
}

/// Volume Weighted Average Price
pub fn signature_vwap() -> IndicatorSignature {
    IndicatorSignature::builder("VWAP", CATEGORY)
        .name("Volume Weighted Average Price")
        .description("Average price weighted by volume")
        .add_constraint(ParamConstraint::period(2, 200, 20))
        .metadata("uses_volume", "true")
        .metadata("price_type", "typical (HLC/3)")
        .source_type(SourceType::PriceAndVolume)
        .machine_id(BarIndicatorId::Vwap)
        .role_kind(IndicatorRoleKind::Smoother)
        .output_kind(IndicatorValueKind::Single)
        // Note: "VWAP" is already the main ID, no need for alias
        .alias("Vwap")
        .alias("vwap")
        .alias("VOLUMEWEIGHTEDAVERAGEPRICE")
        .alias("VolumeWeightedAveragePrice")
        .alias("volumeweightedaverageprice")
        .alias("volume_weighted_average_price")
        .alias("VOLUME_WEIGHTED_AVERAGE_PRICE")
        .alias("Volume_Weighted_Average_Price")
        .build()
}

/// Triangular Moving Average
pub fn signature_tma() -> IndicatorSignature {
    IndicatorSignature::builder("TMA", CATEGORY)
        .name("Triangular Moving Average")
        .description("Double-smoothed simple moving average")
        .add_constraint(ParamConstraint::period(2, 200, 20))
        .metadata("formula", "SMA of SMA")
        .machine_id(BarIndicatorId::Tma)
        .role_kind(IndicatorRoleKind::Smoother)
        .output_kind(IndicatorValueKind::Single)
        // Note: "TMA" is already the main ID, no need for alias
        .alias("Tma")
        .alias("tma")
        .alias("TRIANGULARMOVINGAVERAGE")
        .alias("TriangularMovingAverage")
        .alias("triangularmovingaverage")
        .alias("triangular_moving_average")
        .alias("TRIANGULAR_MOVING_AVERAGE")
        .alias("Triangular_Moving_Average")
        .build()
}

/// Adaptive Moving Average (Kaufman's)
pub fn signature_ama() -> IndicatorSignature {
    IndicatorSignature::builder("AMA", CATEGORY)
        .name("Adaptive Moving Average")
        .description("Kaufman's Adaptive Moving Average based on efficiency ratio")
        .add_constraint(
            ParamConstraint::new("period_efficiency_ratio", ParamType::USize)
                .with_min(ParamValue::USize(2))
                .with_max(ParamValue::USize(200))
                .with_default(ParamValue::USize(10))
                .required()
        )
        .add_constraint(
            ParamConstraint::new("fast_period", ParamType::USize)
                .with_min(ParamValue::USize(2))
                .with_max(ParamValue::USize(50))
                .with_default(ParamValue::USize(2))
                .required()
        )
        .add_constraint(
            ParamConstraint::new("slow_period", ParamType::USize)
                .with_min(ParamValue::USize(2))
                .with_max(ParamValue::USize(100))
                .with_default(ParamValue::USize(30))
                .required()
        )
        .metadata("author", "Perry Kaufman")
        .metadata("complexity", "Uses Efficiency Ratio")
        .machine_id(BarIndicatorId::Ama)  // Canonical name for optimized AMA
        .role_kind(IndicatorRoleKind::Smoother)
        .output_kind(IndicatorValueKind::Single)
        // Note: "AMA" is already the main ID, no need for alias
        .alias("Ama")
        .alias("ama")
        .alias("ADAPTIVEMOVINGAVERAGE")
        .alias("AdaptiveMovingAverage")
        .alias("adaptivemovingaverage")
        .alias("adaptive_moving_average")
        .alias("ADAPTIVE_MOVING_AVERAGE")
        .alias("Adaptive_Moving_Average")
        .build()
}

/// Fractal Adaptive Moving Average
pub fn signature_frama() -> IndicatorSignature {
    IndicatorSignature::builder("AV_FRAMA", CATEGORY)
        .name("Fractal Adaptive Moving Average")
        .description("Adaptive MA based on fractal dimension")
        .add_constraint(ParamConstraint::period(2, 200, 20))
        .metadata("author", "John Ehlers")
        .machine_id(BarIndicatorId::AvFrama)
        .role_kind(IndicatorRoleKind::Smoother)
        .output_kind(IndicatorValueKind::Single)
        // Note: "AV_FRAMA" is already the main ID, no need for alias
        .alias("AvFrama")
        .alias("av_frama")
        .alias("FRACTALADAPTIVEMOVINGAVERAGE")
        .alias("FractalAdaptiveMovingAverage")
        .alias("fractaladaptivemovingaverage")
        .alias("fractal_adaptive_moving_average")
        .alias("FRACTAL_ADAPTIVE_MOVING_AVERAGE")
        .alias("Fractal_Adaptive_Moving_Average")
        .build()
}

/// Fractal Adaptive Moving Average (Advanced version)
pub fn signature_frama_advanced() -> IndicatorSignature {
    IndicatorSignature::builder("FRAMAADV", CATEGORY)
        .name("Fractal Adaptive Moving Average Advanced")
        .description("Enhanced FRAMA with additional smoothing")
        .add_constraint(ParamConstraint::period(2, 200, 20))
        .metadata("author", "John Ehlers")
        .metadata("variant", "advanced")
        .machine_id(BarIndicatorId::Framaadv)
        .role_kind(IndicatorRoleKind::Smoother)
        .output_kind(IndicatorValueKind::Single)
        // Note: "FRAMAADV" is already the main ID, no need for alias
        .alias("Framaadv")
        .alias("framaadv")
        .alias("FRACTALADAPTIVEMOVINGAVERAGEADVANCED")
        .alias("FractalAdaptiveMovingAverageAdvanced")
        .alias("fractaladaptivemovingaverageadvanced")
        .alias("fractal_adaptive_moving_average_advanced")
        .alias("FRACTAL_ADAPTIVE_MOVING_AVERAGE_ADVANCED")
        .alias("Fractal_Adaptive_Moving_Average_Advanced")
        .build()
}

/// Linear Regression Moving Average
pub fn signature_lr() -> IndicatorSignature {
    IndicatorSignature::builder("LR", CATEGORY)
        .name("Linear Regression")
        .description("Linear regression line over N periods")
        .add_constraint(ParamConstraint::period(2, 200, 20))
        .metadata("aka", "Linear Regression Line")
        .machine_id(BarIndicatorId::Lr)
        .role_kind(IndicatorRoleKind::Smoother)
        .output_kind(IndicatorValueKind::Single)
        // Note: "LR" is already the main ID, no need for alias
        .alias("Lr")
        .alias("lr")
        .alias("LINEARREGRESSION")
        .alias("LinearRegression")
        .alias("linearregression")
        .alias("linear_regression")
        .alias("LINEAR_REGRESSION")
        .alias("Linear_Regression")
        .build()
}

/// Ehlers Fractal Adaptive Moving Average
pub fn signature_ehlers_fractal_adaptive_ma() -> IndicatorSignature {
    IndicatorSignature::builder("EHLERSFA", CATEGORY)
        .name("Ehlers Fractal Adaptive MA")
        .description("Ehlers' implementation of fractal adaptive smoothing")
        .add_constraint(ParamConstraint::period(2, 200, 20))
        .metadata("author", "John Ehlers")
        .machine_id(BarIndicatorId::Ehlersfa)
        .role_kind(IndicatorRoleKind::Smoother)
        .output_kind(IndicatorValueKind::Single)
        // Note: "EHLERSFA" is already the main ID, no need for alias
        .alias("Ehlersfa")
        .alias("ehlersfa")
        .alias("EHLERSFRACTALADAPTIVEMA")
        .alias("EhlersFractalAdaptiveMA")
        .alias("ehlersfractaladaptivema")
        .alias("ehlers_fractal_adaptive_ma")
        .alias("EHLERS_FRACTAL_ADAPTIVE_MA")
        .alias("Ehlers_Fractal_Adaptive_Ma")
        .build()
}

/// Ehlers Zero Lag EMA
pub fn signature_ehlers_zero_lag_ema() -> IndicatorSignature {
    IndicatorSignature::builder("EHLERSZ", CATEGORY)
        .name("Ehlers Zero Lag EMA")
        .description("Zero-lag exponential moving average by Ehlers")
        .add_constraint(ParamConstraint::period(2, 200, 20))
        .metadata("author", "John Ehlers")
        .metadata("feature", "reduced lag")
        .machine_id(BarIndicatorId::Ehlersz)
        .role_kind(IndicatorRoleKind::Smoother)
        .output_kind(IndicatorValueKind::Single)
        // Note: "EHLERSZ" is already the main ID, no need for alias
        .alias("Ehlersz")
        .alias("ehlersz")
        .alias("EHLERSZEROLAGEMA")
        .alias("EhlersZeroLagEMA")
        .alias("ehlerszerolagema")
        .alias("ehlers_zero_lag_ema")
        .alias("EHLERS_ZERO_LAG_EMA")
        .alias("Ehlers_Zero_Lag_Ema")
        .build()
}

/// Arnaud Legoux Moving Average
pub fn signature_alma() -> IndicatorSignature {
    IndicatorSignature::builder("ALMA", CATEGORY)
        .name("Arnaud Legoux Moving Average")
        .description("Gaussian-weighted moving average")
        .add_constraint(ParamConstraint::period(2, 200, 9))
        .add_constraint(
            ParamConstraint::new("offset", ParamType::F64)
                .with_min(ParamValue::F64(0.0))
                .with_max(ParamValue::F64(1.0))
                .with_default(ParamValue::F64(0.85))
        )
        .add_constraint(
            ParamConstraint::new("sigma", ParamType::F64)
                .with_min(ParamValue::F64(1.0))
                .with_max(ParamValue::F64(10.0))
                .with_default(ParamValue::F64(6.0))
        )
        .metadata("author", "Arnaud Legoux")
        .machine_id(BarIndicatorId::Alma)
        .role_kind(IndicatorRoleKind::Smoother)
        .output_kind(IndicatorValueKind::Single)
        // Note: "ALMA" is already the main ID, no need for alias
        .alias("Alma")
        .alias("alma")
        .alias("ARNAUDLEGOUXMOVINGAVERAGE")
        .alias("ArnaudLegouxMovingAverage")
        .alias("arnaudlegouxmovingaverage")
        .alias("arnaud_legoux_moving_average")
        .alias("ARNAUD_LEGOUX_MOVING_AVERAGE")
        .alias("Arnaud_Legoux_Moving_Average")
        .build()
}

/// Jurik Moving Average
pub fn signature_jurik_ma() -> IndicatorSignature {
    IndicatorSignature::builder("JMA", CATEGORY)
        .name("Jurik Moving Average")
        .description("Low-lag adaptive moving average")
        .add_constraint(ParamConstraint::period(2, 200, 7))
        .add_constraint(
            ParamConstraint::new("phase", ParamType::F64)
                .with_min(ParamValue::F64(-100.0))
                .with_max(ParamValue::F64(100.0))
                .with_default(ParamValue::F64(0.0))
        )
        .metadata("author", "Mark Jurik")
        .machine_id(BarIndicatorId::Jma)
        .role_kind(IndicatorRoleKind::Smoother)
        .output_kind(IndicatorValueKind::Single)
        // Note: "JMA" is already the main ID, no need for alias
        .alias("Jma")
        .alias("jma")
        .alias("JURIKMOVINGAVERAGE")
        .alias("JurikMovingAverage")
        .alias("jurikmovingaverage")
        .alias("jurik_moving_average")
        .alias("JURIK_MOVING_AVERAGE")
        .alias("Jurik_Moving_Average")
        .build()
}

/// McGinley Dynamic
pub fn signature_mcginley_dynamic() -> IndicatorSignature {
    IndicatorSignature::builder("MCGINLEY", CATEGORY)
        .name("McGinley Dynamic")
        .description("Automatically adjusting moving average")
        .add_constraint(ParamConstraint::period(2, 200, 20))
        .metadata("author", "John McGinley")
        .metadata("feature", "auto-adjusting")
        .machine_id(BarIndicatorId::Mcginley)
        .role_kind(IndicatorRoleKind::Smoother)
        .output_kind(IndicatorValueKind::Single)
        // Note: "MCGINLEY" is already the main ID, no need for alias
        .alias("Mcginley")
        .alias("mcginley")
        .alias("MCGINLEYDYNAMIC")
        .alias("McGinleyDynamic")
        .alias("mcginleydynamic")
        .alias("mcginley_dynamic")
        .alias("MCGINLEY_DYNAMIC")
        .alias("Mcginley_Dynamic")
        .build()
}

/// T3 Moving Average (Tillson)
pub fn signature_t3() -> IndicatorSignature {
    IndicatorSignature::builder("T3", CATEGORY)
        .name("T3 Moving Average")
        .description("Tillson's T3 - smooth and responsive")
        .add_constraint(ParamConstraint::period(2, 200, 5))
        .add_constraint(
            ParamConstraint::new("volume_factor", ParamType::F64)
                .with_min(ParamValue::F64(0.0))
                .with_max(ParamValue::F64(1.0))
                .with_default(ParamValue::F64(0.7))
        )
        .metadata("author", "Tim Tillson")
        .machine_id(BarIndicatorId::T3)
        .role_kind(IndicatorRoleKind::Smoother)
        .output_kind(IndicatorValueKind::Single)
        // Note: "T3" is already the main ID, no need for alias
        .alias("t3")
        .alias("T3MOVINGAVERAGE")
        .alias("T3MovingAverage")
        .alias("t3movingaverage")
        .alias("t3_moving_average")
        .alias("T3_MOVING_AVERAGE")
        .alias("T3_Moving_Average")
        .build()
}

/// Triple Moving Average
pub fn signature_trima() -> IndicatorSignature {
    IndicatorSignature::builder("TRIMA", CATEGORY)
        .name("Triple Moving Average")
        .description("Triangular/Triple smoothed moving average")
        .add_constraint(ParamConstraint::period(2, 200, 20))
        .machine_id(BarIndicatorId::Trima)
        .role_kind(IndicatorRoleKind::Smoother)
        .output_kind(IndicatorValueKind::Single)
        // Note: "TRIMA" is already the main ID, no need for alias
        .alias("Trima")
        .alias("trima")
        .alias("TRIPLEMOVINGAVERAGE")
        .alias("TripleMovingAverage")
        .alias("triplemovingaverage")
        .alias("triple_moving_average")
        .alias("TRIPLE_MOVING_AVERAGE")
        .alias("Triple_Moving_Average")
        .build()
}

/// Variable Index Dynamic Average
pub fn signature_vidya() -> IndicatorSignature {
    IndicatorSignature::builder("AV_VIDYA", CATEGORY)
        .name("Variable Index Dynamic Average")
        .description("Volatility-adjusted moving average")
        .add_constraint(ParamConstraint::period(2, 200, 20))
        .add_constraint(ParamConstraint::ma_type(MovingAverageType::SMA))
        .metadata("author", "Tushar Chande")
        .metadata("feature", "volatility-adaptive")
        .metadata("note", "VIDYA uses CMO for volatility and smooths with configurable MA type")
        .machine_id(BarIndicatorId::AvVidya)
        .role_kind(IndicatorRoleKind::Smoother)
        .output_kind(IndicatorValueKind::Single)
        // Note: "AV_VIDYA" is already the main ID, no need for alias
        .alias("AvVidya")
        .alias("av_vidya")
        .alias("VARIABLEINDEXDYNAMICAVERAGE")
        .alias("VariableIndexDynamicAverage")
        .alias("variableindexdynamicaverage")
        .alias("variable_index_dynamic_average")
        .alias("VARIABLE_INDEX_DYNAMIC_AVERAGE")
        .alias("Variable_Index_Dynamic_Average")
        .build()
}

/// Volume Weighted Moving Average
pub fn signature_vwma() -> IndicatorSignature {
    IndicatorSignature::builder("VWMA", CATEGORY)
        .name("Volume Weighted Moving Average")
        .description("SMA weighted by volume")
        .add_constraint(ParamConstraint::period(2, 200, 20))
        .metadata("uses_volume", "true")
        .source_type(SourceType::PriceAndVolume)
        .machine_id(BarIndicatorId::Vwma)
        .role_kind(IndicatorRoleKind::Smoother)
        .output_kind(IndicatorValueKind::Single)
        // Note: "VWMA" is already the main ID, no need for alias
        .alias("Vwma")
        .alias("vwma")
        .alias("VOLUMEWEIGHTEDMOVINGAVERAGE")
        .alias("VolumeWeightedMovingAverage")
        .alias("volumeweightedmovingaverage")
        .alias("volume_weighted_moving_average")
        .alias("VOLUME_WEIGHTED_MOVING_AVERAGE")
        .alias("Volume_Weighted_Moving_Average")
        .build()
}

// ============================================================================
// Catalog HashMap with Auto-Generated Aliases
// ============================================================================

/// Base catalog with main IDs only (used for initialization)
const BASE_CATALOG: &[(&str, fn() -> IndicatorSignature)] = &[
    ("SMA", signature_sma as fn() -> IndicatorSignature),
    ("EMA", signature_ema as fn() -> IndicatorSignature),
    ("WMA", signature_wma as fn() -> IndicatorSignature),
    ("HMA", signature_hma as fn() -> IndicatorSignature),
    ("DEMA", signature_dema as fn() -> IndicatorSignature),
    ("TEMA", signature_tema as fn() -> IndicatorSignature),
    ("RMA", signature_rma as fn() -> IndicatorSignature),
    ("VWAP", signature_vwap as fn() -> IndicatorSignature),
    ("TMA", signature_tma as fn() -> IndicatorSignature),
    ("AMA", signature_ama as fn() -> IndicatorSignature),
    ("AV_FRAMA", signature_frama as fn() -> IndicatorSignature),
    ("FRAMAADV", signature_frama_advanced as fn() -> IndicatorSignature),
    ("LR", signature_lr as fn() -> IndicatorSignature),
    ("EHLERSFA", signature_ehlers_fractal_adaptive_ma as fn() -> IndicatorSignature),
    ("EHLERSZ", signature_ehlers_zero_lag_ema as fn() -> IndicatorSignature),
    ("ALMA", signature_alma as fn() -> IndicatorSignature),
    ("JMA", signature_jurik_ma as fn() -> IndicatorSignature),
    ("MCGINLEY", signature_mcginley_dynamic as fn() -> IndicatorSignature),
    ("T3", signature_t3 as fn() -> IndicatorSignature),
    ("TRIMA", signature_trima as fn() -> IndicatorSignature),
    ("AV_VIDYA", signature_vidya as fn() -> IndicatorSignature),
    ("VWMA", signature_vwma as fn() -> IndicatorSignature),
];

/// Expanded catalog with all aliases auto-generated from signatures
/// This allows O(1) lookup by any alias without manual maintenance
pub static AVERAGE_CATALOG: Lazy<HashMap<String, fn() -> IndicatorSignature>> = Lazy::new(|| {
    let mut m = HashMap::new();

    for &(main_id, func) in BASE_CATALOG {
        // Call function once to get signature with aliases
        let sig = func();

        // Insert main ID
        m.insert(main_id.to_string(), func);

        // Auto-insert all aliases from signature
        for alias in &sig.aliases {
            m.insert(alias.clone(), func);
        }
    }

    m
});

// ============================================================================
// Public API
// ============================================================================

/// Get indicator signature by ID
///
/// ## Example
/// ```rust
/// use zengeld_chart_indicators::bar_indicators::average::average_catalog;
///
/// let sig = average_catalog::get_signature("SMA").unwrap();
/// assert_eq!(sig.id, "SMA");
/// ```
pub fn get_signature(id: &str) -> Option<IndicatorSignature> {
    AVERAGE_CATALOG.get(id).map(|f| f())
}

/// Get all indicator IDs in this category
/// Returns only main IDs, not aliases
pub fn all_indicator_ids() -> Vec<&'static str> {
    BASE_CATALOG.iter().map(|(id, _)| *id).collect()
}

/// Get count of indicators in this category
/// Returns count of unique indicators, not including aliases
pub fn count() -> usize {
    BASE_CATALOG.len()
}

// ============================================================================
// Tests
// ============================================================================

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

    #[test]
    fn test_get_sma_signature() {
        let sig = get_signature("SMA").unwrap();
        assert_eq!(sig.id, "SMA");
        assert_eq!(sig.category, CATEGORY);
        assert_eq!(sig.required_params().len(), 1);
    }

    #[test]
    fn test_get_ema_signature() {
        let sig = get_signature("EMA").unwrap();
        assert_eq!(sig.id, "EMA");
        assert_eq!(sig.name, "Exponential Moving Average");
    }

    #[test]
    fn test_get_ama_signature() {
        let sig = get_signature("AMA").unwrap();
        assert_eq!(sig.id, "AMA");
        // AMA has 3 required parameters
        assert_eq!(sig.required_params().len(), 3);
    }

    #[test]
    fn test_all_signatures_valid() {
        for id in all_indicator_ids() {
            let sig = get_signature(id).unwrap();
            assert_eq!(sig.id, id);
            assert_eq!(sig.category, CATEGORY);
        }
    }

    #[test]
    fn test_count() {
        assert_eq!(count(), 22); // 22 average indicators (3 LEGACY removed)
    }

    #[test]
    fn test_simple_ma_validation() {
        let sig = get_signature("SMA").unwrap();

        // Valid params
        let params = vec![("period", ParamValue::USize(20))];
        assert!(sig.validate_params(&params).is_ok());

        // Invalid: out of range
        let params = vec![("period", ParamValue::USize(1))];
        assert!(sig.validate_params(&params).is_err());
    }

    #[test]
    fn test_cache_key_generation() {
        let sig = get_signature("SMA").unwrap();
        let params = vec![("period", ParamValue::USize(20))];
        let key = sig.cache_key(&params);
        assert_eq!(key, "SMA_20");
    }

    #[test]
    fn test_ama_cache_key() {
        let sig = get_signature("AMA").unwrap();
        let params = vec![
            ("period_efficiency_ratio", ParamValue::USize(10)),
            ("fast_period", ParamValue::USize(2)),
            ("slow_period", ParamValue::USize(30)),
        ];
        let key = sig.cache_key(&params);
        // Keys are sorted alphabetically
        assert!(key.contains("AMA"));
        assert!(key.contains("10"));
        assert!(key.contains("2"));
        assert!(key.contains("30"));
    }
}