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
//! RoleKind taxonomy — semantic classification of indicator roles.
//!
//! Used to validate that an indicator is appropriate for a given
//! `OperatorClass` slot (e.g., only `Smoother` indicators make sense
//! as the left operand of a `Cross` event with MA semantics).

use crate::BarIndicatorId;

/// Semantic role a bar indicator plays in a strategy.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum RoleKind {
    /// Moving average / smoother (SMA, EMA, HMA, Kalman, etc.).
    Smoother,
    /// Bounded oscillator [0..100] or [-100..100] (RSI, Stoch, CCI, etc.).
    OscillatorBounded,
    /// Unbounded oscillator (MACD, ROC, CMO, TSI, etc.).
    OscillatorUnbounded,
    /// Price channel (Bollinger Bands, Keltner, Donchian, etc.).
    Channel,
    /// Volatility measure (ATR, NATR, Chop, RV, etc.).
    Volatility,
    /// Trend strength indicator (ADX, VHF, HURST, etc.).
    TrendStrength,
    /// Volume flow indicator (OBV, CMF, MFI, etc.).
    VolumeFlow,
    /// Pivot / swing detector (Zigzag, Pivot levels, etc.).
    PivotIndicator,
    /// Candlestick pattern detector.
    PatternDetector,
    /// Regime / logic filter (MRF, Hyst, LogicAnd, etc.).
    RegimeFilter,
    /// Trend-stop / trailing-stop indicator (SuperTrend, SSL, etc.).
    ///
    /// Outputs a single value positioned above or below price.
    /// The "side" flip (stop crosses price) is the primary event.
    TrendStop,
    /// Normalized scalar measurement: probability, density, tanh-strength, EMA magnitude.
    ///
    /// Output is a `Single(f64)` in [0,1] or [-1,1]. Not events, not price levels.
    /// Examples: FVG reversion probability, liquidity gap density, swing strength score.
    StatisticalScoring,
    /// Unknown / uncategorised — treated as general-purpose.
    Other,
}

/// Map a `BarIndicatorId` to its canonical `RoleKind`.
///
/// The mapping is a best-effort classification. Indicators that do not
/// clearly fit a category are mapped to `RoleKind::Other`.
pub fn role_kind_for(id: BarIndicatorId) -> RoleKind {
    match id {
        // ── Smoothers ───────────────────────────────────────────────────────
        BarIndicatorId::Sma
        | BarIndicatorId::Ema
        | BarIndicatorId::Wma
        | BarIndicatorId::Hma
        | BarIndicatorId::Rma
        | BarIndicatorId::Dema
        | BarIndicatorId::Tema
        | BarIndicatorId::Tma
        | BarIndicatorId::Trima
        | BarIndicatorId::T3
        | BarIndicatorId::Alma
        | BarIndicatorId::Ama
        | BarIndicatorId::Jma
        | BarIndicatorId::Mcginley
        | BarIndicatorId::Lr
        | BarIndicatorId::Vwap
        | BarIndicatorId::Vwma
        | BarIndicatorId::SessionVwap
        | BarIndicatorId::AvFrama
        | BarIndicatorId::AvVidya
        | BarIndicatorId::Ehlersfa
        | BarIndicatorId::Ehlersz
        | BarIndicatorId::Framaadv
        | BarIndicatorId::Adaptivema
        | BarIndicatorId::Frama
        | BarIndicatorId::Kama
        | BarIndicatorId::Mama
        | BarIndicatorId::Vidya
        | BarIndicatorId::Abgfilter
        | BarIndicatorId::Ekf
        | BarIndicatorId::Kalman
        | BarIndicatorId::Kslope
        | BarIndicatorId::Kslopez
        | BarIndicatorId::Particle
        | BarIndicatorId::Rts
        | BarIndicatorId::Zlsma
        | BarIndicatorId::FootprintPoc => RoleKind::Smoother,

        // ── Trend Stops ───────────────────────────────────────────────────────
        BarIndicatorId::Supertrend
        | BarIndicatorId::Ssl
        | BarIndicatorId::GannHilo => RoleKind::TrendStop,

        // ── Bounded Oscillators ──────────────────────────────────────────────
        BarIndicatorId::Rsi
        | BarIndicatorId::StochRsi
        | BarIndicatorId::Stoch
        | BarIndicatorId::Stochkd
        | BarIndicatorId::AdaptiveStoch
        | BarIndicatorId::Cci
        | BarIndicatorId::WilliamsR
        | BarIndicatorId::Mfi
        | BarIndicatorId::IftRsi
        | BarIndicatorId::Demarker
        | BarIndicatorId::ConnorsRsi
        | BarIndicatorId::Imi
        | BarIndicatorId::Kdj
        | BarIndicatorId::Psl
        | BarIndicatorId::Qqe
        | BarIndicatorId::Uo
        | BarIndicatorId::UoSmooth
        | BarIndicatorId::Rsx
        | BarIndicatorId::Rsioma
        | BarIndicatorId::AtrRsi
        | BarIndicatorId::Vwrsi
        | BarIndicatorId::Smi
        | BarIndicatorId::Pzo => RoleKind::OscillatorBounded,

        // ── Unbounded Oscillators ────────────────────────────────────────────
        BarIndicatorId::Macd
        | BarIndicatorId::MacdHist
        | BarIndicatorId::MacdHistZ
        | BarIndicatorId::MacdSignal
        | BarIndicatorId::Roc
        | BarIndicatorId::RocPct
        | BarIndicatorId::Cmo
        | BarIndicatorId::Tsi
        | BarIndicatorId::Ewmac
        | BarIndicatorId::EwmacRobust
        | BarIndicatorId::Ppo
        | BarIndicatorId::PpoSignal
        | BarIndicatorId::Apo
        | BarIndicatorId::Coppock
        | BarIndicatorId::Bop
        | BarIndicatorId::Kst
        | BarIndicatorId::Kvo
        | BarIndicatorId::Trix
        | BarIndicatorId::Stc
        | BarIndicatorId::Pfe
        | BarIndicatorId::Pmo
        | BarIndicatorId::Bias
        | BarIndicatorId::Dpo
        | BarIndicatorId::DpoPct
        | BarIndicatorId::Rvgi
        | BarIndicatorId::MoFisher
        | BarIndicatorId::Dss
        | BarIndicatorId::Gator
        | BarIndicatorId::Cog
        | BarIndicatorId::Didi => RoleKind::OscillatorUnbounded,

        // ── Channels ────────────────────────────────────────────────────────
        BarIndicatorId::Bb
        | BarIndicatorId::Kc
        | BarIndicatorId::Dc
        | BarIndicatorId::Atrchan
        | BarIndicatorId::Envelope
        | BarIndicatorId::Regchan
        | BarIndicatorId::Starc
        | BarIndicatorId::Adaptivebb
        | BarIndicatorId::Vwapchan
        | BarIndicatorId::Projbands
        | BarIndicatorId::Theilsenchan
        | BarIndicatorId::Stddevchan
        | BarIndicatorId::Medchan
        | BarIndicatorId::Adaptivechan
        | BarIndicatorId::Darvas
        | BarIndicatorId::Dpobands
        | BarIndicatorId::Trimabands
        | BarIndicatorId::Qrchan
        | BarIndicatorId::Pivotchan
        | BarIndicatorId::Pricechan
        | BarIndicatorId::Percentilech
        | BarIndicatorId::Ichimoku
        | BarIndicatorId::Ichimokupos
        | BarIndicatorId::Ichimokuthick => RoleKind::Channel,

        // ── Volatility ───────────────────────────────────────────────────────
        BarIndicatorId::Atr
        | BarIndicatorId::Natr
        | BarIndicatorId::Atrp
        | BarIndicatorId::Atrbw
        | BarIndicatorId::Atrc
        | BarIndicatorId::Atrpt
        | BarIndicatorId::Atrz
        | BarIndicatorId::Tr
        | BarIndicatorId::Rvi
        | BarIndicatorId::Rv
        | BarIndicatorId::Rvz
        | BarIndicatorId::Chop
        | BarIndicatorId::Ui
        | BarIndicatorId::Vov
        | BarIndicatorId::Vovp
        | BarIndicatorId::Vovpt
        | BarIndicatorId::Avr
        | BarIndicatorId::Bpv
        | BarIndicatorId::C2cvp
        | BarIndicatorId::Cv
        | BarIndicatorId::Dvr
        | BarIndicatorId::Har
        | BarIndicatorId::Hvc2c
        | BarIndicatorId::Kp
        | BarIndicatorId::Nr
        | BarIndicatorId::Pgry
        | BarIndicatorId::Rbvj
        | BarIndicatorId::Rcb
        | BarIndicatorId::Rp
        | BarIndicatorId::Rq
        | BarIndicatorId::Sqmom
        | BarIndicatorId::Vbd
        | BarIndicatorId::Vbexp
        | BarIndicatorId::VoDc
        | BarIndicatorId::VoKc
        | BarIndicatorId::VoMi
        | BarIndicatorId::VoVr
        | BarIndicatorId::Wvf
        | BarIndicatorId::Fuzzy
        | BarIndicatorId::Abb => RoleKind::Volatility,

        // ── Trend Strength ────────────────────────────────────────────────────
        BarIndicatorId::Adx
        | BarIndicatorId::AdxSlope
        | BarIndicatorId::Vhf
        | BarIndicatorId::VhfMa
        | BarIndicatorId::TrEr
        | BarIndicatorId::Tii
        | BarIndicatorId::Ravi
        | BarIndicatorId::Sdl
        | BarIndicatorId::Apen
        | BarIndicatorId::Sampen
        | BarIndicatorId::Gapo
        | BarIndicatorId::Rwi
        | BarIndicatorId::EmaSlope
        | BarIndicatorId::KamaSlope
        | BarIndicatorId::LrSlope => RoleKind::TrendStrength,

        // ── Volume Flow ───────────────────────────────────────────────────────
        BarIndicatorId::Obv
        | BarIndicatorId::Cmf
        | BarIndicatorId::Vfi
        | BarIndicatorId::Pvt
        | BarIndicatorId::Vpt
        | BarIndicatorId::Ad
        | BarIndicatorId::Cho
        | BarIndicatorId::Eom
        | BarIndicatorId::Vzo
        | BarIndicatorId::NviPvi
        | BarIndicatorId::Poc
        | BarIndicatorId::Pvo
        | BarIndicatorId::Rvol
        | BarIndicatorId::Vdelta
        | BarIndicatorId::Cvd
        | BarIndicatorId::Vo
        | BarIndicatorId::Vpin
        | BarIndicatorId::Vroc
        | BarIndicatorId::Vz
        | BarIndicatorId::Vprofile
        | BarIndicatorId::Rvp
        | BarIndicatorId::Di
        | BarIndicatorId::Tmf
        | BarIndicatorId::Wad
        | BarIndicatorId::Asi
        | BarIndicatorId::Fi
        | BarIndicatorId::Ii
        | BarIndicatorId::Iip
        | BarIndicatorId::Iir
        | BarIndicatorId::MoObv
        | BarIndicatorId::FootprintChart
        | BarIndicatorId::FootprintImbalance
        | BarIndicatorId::UptickDowntickVolume => RoleKind::VolumeFlow,

        BarIndicatorId::TradeFlowImbalance => RoleKind::OscillatorBounded,

        // ── Pivot Indicators ──────────────────────────────────────────────────
        BarIndicatorId::Pivot
        | BarIndicatorId::Floorpivot
        | BarIndicatorId::Camarilla
        | BarIndicatorId::Demark
        | BarIndicatorId::Woodie
        | BarIndicatorId::Rmid
        | BarIndicatorId::Rquart
        | BarIndicatorId::Pivavwap
        | BarIndicatorId::Avwap
        | BarIndicatorId::Avwaprev
        | BarIndicatorId::Avwaptouch
        | BarIndicatorId::Hlva => RoleKind::PivotIndicator,

        // ── Statistical Scoring ───────────────────────────────────────────────
        BarIndicatorId::Fvgrev
        | BarIndicatorId::Fvgdur
        | BarIndicatorId::Fvgalt
        | BarIndicatorId::Liqgap
        | BarIndicatorId::Swingstr
        | BarIndicatorId::SwingAge
        | BarIndicatorId::Kcomp
        | BarIndicatorId::Kscr => RoleKind::StatisticalScoring,

        // ── Pattern Detectors ─────────────────────────────────────────────────
        BarIndicatorId::Bos
        | BarIndicatorId::Fvg
        | BarIndicatorId::Candleanatomy
        | BarIndicatorId::Heikinashi
        | BarIndicatorId::Wickspike => RoleKind::PatternDetector,

        // ── Regime Filters ────────────────────────────────────────────────────
        BarIndicatorId::Mrf
        | BarIndicatorId::Hyst
        | BarIndicatorId::Logicand
        | BarIndicatorId::Logicor
        | BarIndicatorId::Logicxor
        | BarIndicatorId::Wcomp
        | BarIndicatorId::Logicsign
        | BarIndicatorId::HaTrend
        | BarIndicatorId::Kregime => RoleKind::RegimeFilter,

        // ── Book / L2 derived prices ──────────────────────────────────────────
        BarIndicatorId::BookMicroprice => RoleKind::Smoother,
        BarIndicatorId::LiquiditySweep => RoleKind::OscillatorUnbounded,
        BarIndicatorId::BookPressure => RoleKind::OscillatorUnbounded,
        BarIndicatorId::SpreadDistribution => RoleKind::OscillatorBounded,
        BarIndicatorId::OrderBookVelocity => RoleKind::Other,
        // ── Book delta indicators ─────────────────────────────────────────────
        BarIndicatorId::IcebergDetector => RoleKind::PatternDetector,
        BarIndicatorId::LevelReplenishRate => RoleKind::OscillatorUnbounded,
        BarIndicatorId::BookChurnRate => RoleKind::OscillatorUnbounded,
        // ── Funding / OI indicators ───────────────────────────────────────────
        BarIndicatorId::FundingMomentum => RoleKind::OscillatorUnbounded,
        BarIndicatorId::FundingZScore => RoleKind::OscillatorUnbounded,
        BarIndicatorId::OiChangeRate => RoleKind::OscillatorUnbounded,

        // ── Open Interest indicators ──────────────────────────────────────────
        BarIndicatorId::OiZScore => RoleKind::OscillatorBounded,
        BarIndicatorId::OiMomentum => RoleKind::OscillatorUnbounded,
        BarIndicatorId::OiPercentile => RoleKind::OscillatorBounded,
        BarIndicatorId::LongSqueezeDetector => RoleKind::PatternDetector,
        BarIndicatorId::OiPriceCorrelation => RoleKind::OscillatorBounded,

        // ── Ticker / 24h stats indicators ─────────────────────────────────────
        BarIndicatorId::Volume24hMomentum => RoleKind::OscillatorUnbounded,
        BarIndicatorId::HighLowRangeRatio => RoleKind::OscillatorUnbounded,
        BarIndicatorId::PriceChange24hZScore => RoleKind::OscillatorBounded,

        // ── Liquidation indicators ─────────────────────────────────────────────
        BarIndicatorId::LiquidationRate => RoleKind::OscillatorUnbounded,
        BarIndicatorId::LiquidationVolumeImbalance => RoleKind::PatternDetector,
        BarIndicatorId::LiquidationCascade => RoleKind::PatternDetector,
        BarIndicatorId::LiquidationVolumeVelocity => RoleKind::OscillatorUnbounded,
        BarIndicatorId::StopHuntDetector => RoleKind::PatternDetector,
        BarIndicatorId::LiquidationClusterDetector => RoleKind::PatternDetector,
        BarIndicatorId::LiquidationCooldown => RoleKind::OscillatorUnbounded,

        // ── Sentiment indicators ──────────────────────────────────────────────
        BarIndicatorId::LongShortRatioMomentum => RoleKind::OscillatorBounded,
        BarIndicatorId::LongShortExtremeDetector => RoleKind::PatternDetector,
        BarIndicatorId::RatioVsPriceDivergence => RoleKind::PatternDetector,
        BarIndicatorId::AggTradeFlowImbalance => RoleKind::OscillatorBounded,
        BarIndicatorId::AggTradeSizeDistribution => RoleKind::OscillatorUnbounded,

        // ── Index/Basis indicators ────────────────────────────────────────────
        BarIndicatorId::PriceVsIndexSpread => RoleKind::OscillatorUnbounded,
        BarIndicatorId::IndexComponentDrift => RoleKind::OscillatorBounded,
        BarIndicatorId::IndexCorrelationBreakdown => RoleKind::OscillatorBounded,
        BarIndicatorId::BasisMomentum => RoleKind::OscillatorUnbounded,
        BarIndicatorId::BasisExtreme => RoleKind::PatternDetector,
        BarIndicatorId::BasisZScore => RoleKind::OscillatorBounded,

        // ── Volatility advanced indicators ────────────────────────────────────
        BarIndicatorId::HvMomentum => RoleKind::OscillatorUnbounded,
        BarIndicatorId::HvSpike => RoleKind::PatternDetector,
        BarIndicatorId::VolIdxSpike => RoleKind::PatternDetector,
        BarIndicatorId::VolIdxMomentum => RoleKind::OscillatorUnbounded,

        // ── Greeks indicators ─────────────────────────────────────────────────
        BarIndicatorId::DeltaExposureFlow => RoleKind::OscillatorUnbounded,
        BarIndicatorId::GammaSqueezeDetector => RoleKind::PatternDetector,
        BarIndicatorId::IvSkew => RoleKind::OscillatorUnbounded,

        // ── Greeks advanced indicators ────────────────────────────────────────
        BarIndicatorId::CharmTracker => RoleKind::OscillatorUnbounded,
        BarIndicatorId::VegaExposureFlow => RoleKind::OscillatorUnbounded,
        BarIndicatorId::ThetaDecayTracker => RoleKind::OscillatorUnbounded,
        BarIndicatorId::PinRiskDetector => RoleKind::PatternDetector,

        // ── Stress indicators ─────────────────────────────────────────────────
        BarIndicatorId::FundDepletionRate => RoleKind::OscillatorUnbounded,
        BarIndicatorId::FundStressDetector => RoleKind::PatternDetector,
        BarIndicatorId::InsuranceFundMomentum => RoleKind::OscillatorUnbounded,
        BarIndicatorId::SettlementApproachSignal => RoleKind::OscillatorBounded,
        BarIndicatorId::SettlementPriceMomentum => RoleKind::OscillatorUnbounded,
        BarIndicatorId::SettlementVsMarkSpread => RoleKind::OscillatorUnbounded,

        // ── Microstructure indicators ─────────────────────────────────────────
        BarIndicatorId::BlockTradeFlow => RoleKind::OscillatorUnbounded,
        BarIndicatorId::BlockTradeImpact => RoleKind::OscillatorUnbounded,
        BarIndicatorId::L3OrderRate => RoleKind::OscillatorUnbounded,
        BarIndicatorId::L3LargeOrderTracker => RoleKind::PatternDetector,
        BarIndicatorId::L3CancelRatio => RoleKind::OscillatorBounded,
        BarIndicatorId::BlockTradeSizeAnomaly => RoleKind::OscillatorBounded,
        BarIndicatorId::QuoteStuffingDetector => RoleKind::PatternDetector,

        // ── Risk indicators ───────────────────────────────────────────────────
        BarIndicatorId::LeverageReductionWarning => RoleKind::PatternDetector,
        BarIndicatorId::MmrTracker => RoleKind::OscillatorBounded,
        BarIndicatorId::RiskLimitProximity => RoleKind::OscillatorBounded,

        // ── Funding indicators ────────────────────────────────────────────────
        BarIndicatorId::FundingDrift => RoleKind::OscillatorUnbounded,
        BarIndicatorId::FundingTimeDecay => RoleKind::OscillatorBounded,
        BarIndicatorId::PredictedFundingExtreme => RoleKind::PatternDetector,
        BarIndicatorId::SettledFundingMomentum => RoleKind::OscillatorUnbounded,
        BarIndicatorId::FundingSettlementImpact => RoleKind::OscillatorUnbounded,

        // ── Funding advanced indicators ───────────────────────────────────────
        BarIndicatorId::AnnualizedFundingRate => RoleKind::OscillatorUnbounded,
        BarIndicatorId::FundingDirectionShift => RoleKind::PatternDetector,
        BarIndicatorId::FundingExtremeAlert => RoleKind::PatternDetector,

        // ── MarkPrice advanced indicators ─────────────────────────────────────
        BarIndicatorId::MarkPriceMomentum => RoleKind::OscillatorUnbounded,
        BarIndicatorId::MarkPriceVolatility => RoleKind::OscillatorUnbounded,
        BarIndicatorId::MarkPriceGapDetector => RoleKind::PatternDetector,

        // ── Misc indicators ───────────────────────────────────────────────────
        BarIndicatorId::AuctionLiquidityScore => RoleKind::OscillatorUnbounded,
        BarIndicatorId::AuctionPriceDeviation => RoleKind::OscillatorUnbounded,
        BarIndicatorId::AuctionImbalance => RoleKind::OscillatorUnbounded,
        BarIndicatorId::WarningRate => RoleKind::OscillatorUnbounded,
        BarIndicatorId::WarningFrequencyFilter => RoleKind::PatternDetector,

        // ── Ticker advanced indicators ────────────────────────────────────────
        BarIndicatorId::TickerSpreadRatio => RoleKind::OscillatorBounded,
        BarIndicatorId::Volume24hZScore => RoleKind::OscillatorBounded,

        // ── Microstructure extra indicators ──────────────────────────────────
        BarIndicatorId::L3SpooferScore => RoleKind::OscillatorBounded,
        BarIndicatorId::QuoteLifecycleTracker => RoleKind::OscillatorUnbounded,

        // ── Cross-stream composite indicators ─────────────────────────────────
        BarIndicatorId::FundingOiPressure => RoleKind::OscillatorUnbounded,
        BarIndicatorId::IvHvSpread => RoleKind::OscillatorUnbounded,
        BarIndicatorId::SqueezeProbability => RoleKind::OscillatorBounded,
        BarIndicatorId::FundingSentimentAlignment => RoleKind::PatternDetector,
        BarIndicatorId::VolRegimeEntry => RoleKind::PatternDetector,
        BarIndicatorId::BlockTradeVolumeRatio => RoleKind::OscillatorBounded,
        BarIndicatorId::CapitulationDetector => RoleKind::PatternDetector,
        BarIndicatorId::IndexTrackingError => RoleKind::OscillatorUnbounded,

        // ── Book Advanced indicators ──────────────────────────────────────────
        BarIndicatorId::BidAskAsymmetry => RoleKind::OscillatorBounded,
        BarIndicatorId::BidAskBounceRate => RoleKind::OscillatorUnbounded,
        BarIndicatorId::MidPriceVelocity => RoleKind::OscillatorUnbounded,
        BarIndicatorId::BestLevelVolatility => RoleKind::OscillatorUnbounded,
        BarIndicatorId::LayerConcentration => RoleKind::OscillatorBounded,
        BarIndicatorId::PriceLevelDensity => RoleKind::OscillatorUnbounded,

        // ── Tick Advanced indicators ──────────────────────────────────────────
        BarIndicatorId::VwapDeviation => RoleKind::OscillatorUnbounded,
        BarIndicatorId::TradeRunDetector => RoleKind::PatternDetector,
        BarIndicatorId::SizeWeightedDirectionalMomentum => RoleKind::OscillatorBounded,
        BarIndicatorId::TickFrequencyAnomaly => RoleKind::OscillatorUnbounded,
        BarIndicatorId::AggressorBurstDetector => RoleKind::PatternDetector,
        BarIndicatorId::LargeTickMomentum => RoleKind::OscillatorBounded,
        BarIndicatorId::ValueAreaTracker => RoleKind::PivotIndicator,
        BarIndicatorId::VolumeImbalanceZone => RoleKind::PatternDetector,

        // ── Category C composite + adaptive + cross-asset indicators ─────────
        BarIndicatorId::MarketStressComposite => RoleKind::OscillatorBounded,
        BarIndicatorId::RiskOffDetector => RoleKind::PatternDetector,
        BarIndicatorId::SentimentComposite => RoleKind::OscillatorBounded,
        BarIndicatorId::CompoundSqueezeProbability => RoleKind::OscillatorBounded,
        BarIndicatorId::TpoSessionBalance => RoleKind::PivotIndicator,
        BarIndicatorId::CompositeWeightDrift => RoleKind::OscillatorBounded,
        BarIndicatorId::AdaptiveWindowSelector => RoleKind::OscillatorUnbounded,
        BarIndicatorId::AdaptiveThreshold => RoleKind::OscillatorUnbounded,

        // ── Everything else ───────────────────────────────────────────────────
        _ => RoleKind::Other,
    }
}

impl From<RoleKind> for crate::catalog::data::indicator_signature::IndicatorRoleKind {
    /// Map the finer AST `RoleKind` (13 variants) onto the coarser catalog
    /// `IndicatorRoleKind` (12 variants) the codegen pools filter on.
    ///
    /// The catalog enum rolls several AST roles into coarser buckets:
    /// `VolumeFlow→Volume`, `PivotIndicator→Level`, `StatisticalScoring→Statistical`,
    /// and both `TrendStrength` + `RegimeFilter` → `Regime` (catalog has no
    /// separate regime-filter bucket). Single source: AST classification flows
    /// into catalog filtering through this one conversion.
    fn from(r: RoleKind) -> Self {
        use crate::catalog::data::indicator_signature::IndicatorRoleKind as Ik;
        match r {
            RoleKind::Smoother => Ik::Smoother,
            RoleKind::OscillatorBounded => Ik::OscillatorBounded,
            RoleKind::OscillatorUnbounded => Ik::OscillatorUnbounded,
            RoleKind::Channel => Ik::Channel,
            RoleKind::Volatility => Ik::Volatility,
            RoleKind::TrendStrength => Ik::Regime,
            RoleKind::VolumeFlow => Ik::Volume,
            RoleKind::PivotIndicator => Ik::Level,
            RoleKind::PatternDetector => Ik::Pattern,
            RoleKind::RegimeFilter => Ik::Regime,
            RoleKind::TrendStop => Ik::TrendStop,
            RoleKind::StatisticalScoring => Ik::Statistical,
            RoleKind::Other => Ik::Other,
        }
    }
}

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

    #[test]
    fn sma_is_smoother() {
        assert_eq!(role_kind_for(BarIndicatorId::Sma), RoleKind::Smoother);
    }

    #[test]
    fn rsi_is_oscillator_bounded() {
        assert_eq!(role_kind_for(BarIndicatorId::Rsi), RoleKind::OscillatorBounded);
    }

    #[test]
    fn bb_is_channel() {
        assert_eq!(role_kind_for(BarIndicatorId::Bb), RoleKind::Channel);
    }

    #[test]
    fn atr_is_volatility() {
        assert_eq!(role_kind_for(BarIndicatorId::Atr), RoleKind::Volatility);
    }

    #[test]
    fn adx_is_trend_strength() {
        assert_eq!(role_kind_for(BarIndicatorId::Adx), RoleKind::TrendStrength);
    }

    #[test]
    fn obv_is_volume_flow() {
        assert_eq!(role_kind_for(BarIndicatorId::Obv), RoleKind::VolumeFlow);
    }

    #[test]
    fn wickspike_is_pattern_detector() {
        assert_eq!(role_kind_for(BarIndicatorId::Wickspike), RoleKind::PatternDetector);
    }

    #[test]
    fn mrf_is_regime_filter() {
        assert_eq!(role_kind_for(BarIndicatorId::Mrf), RoleKind::RegimeFilter);
    }

    #[test]
    fn supertrend_is_trend_stop() {
        assert_eq!(role_kind_for(BarIndicatorId::Supertrend), RoleKind::TrendStop);
    }

    #[test]
    fn ssl_is_trend_stop() {
        assert_eq!(role_kind_for(BarIndicatorId::Ssl), RoleKind::TrendStop);
    }

    #[test]
    fn gann_hilo_is_trend_stop() {
        assert_eq!(role_kind_for(BarIndicatorId::GannHilo), RoleKind::TrendStop);
    }

    #[test]
    fn didi_is_oscillator_unbounded() {
        assert_eq!(role_kind_for(BarIndicatorId::Didi), RoleKind::OscillatorUnbounded);
    }

    #[test]
    fn hatrend_is_regime_filter() {
        assert_eq!(role_kind_for(BarIndicatorId::HaTrend), RoleKind::RegimeFilter);
    }

    #[test]
    fn kcomp_is_statistical_scoring() {
        assert_eq!(role_kind_for(BarIndicatorId::Kcomp), RoleKind::StatisticalScoring);
    }

    #[test]
    fn kregime_is_regime_filter() {
        assert_eq!(role_kind_for(BarIndicatorId::Kregime), RoleKind::RegimeFilter);
    }

    #[test]
    fn kscr_is_statistical_scoring() {
        assert_eq!(role_kind_for(BarIndicatorId::Kscr), RoleKind::StatisticalScoring);
    }
}