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
//! levels_catalog.rs: Catalog of all Price Levels indicators
//!
//! Auto-generated catalog based on actual indicator implementations.
//! Contains IndicatorSignature definitions for 14 price level indicators.
//!
//! Note: FVG scoring (FVGREV, FVGDUR, FVGALT), LIQGAP, SWINGSTR were moved
//! to `bar_indicators::statistical_scoring::statistical_scoring_catalog`.

use crate::catalog::{
    IndicatorSignature, IndicatorCategory, ParamConstraint,
    IndicatorRoleKind,
};
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::Levels;

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

/// Classic Pivot Points - standard pivot levels for support and resistance
pub fn signature_pivot_points() -> IndicatorSignature {
    IndicatorSignature::builder("PIVOT", CATEGORY)
        .name("Classic Pivot Points")
        .description("Classic pivot levels: PP, R1-R3, S1-S3 for support/resistance")
        .add_constraint(ParamConstraint::period(1, 168, 1))
        .metadata("author", "Classic TA")
        .metadata("levels", "7 (PP, R1-R3, S1-S3)")
        .machine_id(BarIndicatorId::Pivot)
        .role_kind(IndicatorRoleKind::Level)
        .output_kind(IndicatorValueKind::Triple)
        .validated()
        // Note: "PIVOT" is already the main ID, no need for alias
        .alias("Pivot")
        .alias("pivot")
        .alias("CLASSICPIVOTPOINTS")
        .alias("ClassicPivotPoints")
        .alias("classicpivotpoints")
        .alias("classic_pivot_points")
        .alias("CLASSIC_PIVOT_POINTS")
        .alias("Classic_Pivot_Points")
        .build()
}

/// Floor Trader Pivots - floor trader pivot calculation method
pub fn signature_floor_trader_pivots() -> IndicatorSignature {
    IndicatorSignature::builder("FLOORPIVOT", CATEGORY)
        .name("Floor Trader Pivots")
        .description("Floor trader pivot calculation for intraday levels")
        .add_constraint(ParamConstraint::period(1, 168, 1))
        .metadata("author", "Floor Traders")
        .metadata("use_case", "intraday trading")
        .machine_id(BarIndicatorId::Floorpivot)
        .role_kind(IndicatorRoleKind::Level)
        .output_kind(IndicatorValueKind::Single)
        .validated()
        // Note: "FLOORPIVOT" is already the main ID, no need for alias
        .alias("Floorpivot")
        .alias("floorpivot")
        .alias("FLOORTRADERPIVOTS")
        .alias("FloorTraderPivots")
        .alias("floortraderpivots")
        .alias("floor_trader_pivots")
        .alias("FLOOR_TRADER_PIVOTS")
        .alias("Floor_Trader_Pivots")
        .build()
}

/// Camarilla Pivots - Camarilla pivot levels with 4 support/resistance levels
pub fn signature_camarilla_pivots() -> IndicatorSignature {
    IndicatorSignature::builder("CAMARILLA", CATEGORY)
        .name("Camarilla Pivots")
        .description("Camarilla pivot levels with R1-R4 and S1-S4 using 1.1 multiplier")
        .add_constraint(ParamConstraint::period(1, 168, 24))
        .metadata("author", "Nick Stott")
        .metadata("levels", "9 (PP, R1-R4, S1-S4)")
        .metadata("multiplier", "1.1")
        .machine_id(BarIndicatorId::Camarilla)
        .role_kind(IndicatorRoleKind::Level)
        .output_kind(IndicatorValueKind::Single)
        .validated()
        // Note: "CAMARILLA" is already the main ID, no need for alias
        .alias("Camarilla")
        .alias("camarilla")
        .alias("CAMARILLAPIVOTS")
        .alias("CamarillaPivots")
        .alias("camarillapivots")
        .alias("camarilla_pivots")
        .alias("CAMARILLA_PIVOTS")
        .alias("Camarilla_Pivots")
        .build()
}

/// Woodie Pivots - Woodie's pivot calculation method
pub fn signature_woodie_pivots() -> IndicatorSignature {
    IndicatorSignature::builder("WOODIE", CATEGORY)
        .name("Woodie Pivots")
        .description("Woodie's pivot calculation emphasizing current session")
        .add_constraint(ParamConstraint::period(1, 168, 1))
        .metadata("author", "Woodie")
        .metadata("emphasis", "current session")
        .machine_id(BarIndicatorId::Woodie)
        .role_kind(IndicatorRoleKind::Level)
        .output_kind(IndicatorValueKind::Single)
        .validated()
        // Note: "WOODIE" is already the main ID, no need for alias
        .alias("Woodie")
        .alias("woodie")
        .alias("WOODIEPIVOTS")
        .alias("WoodiePivots")
        .alias("woodiepivots")
        .alias("woodie_pivots")
        .alias("WOODIE_PIVOTS")
        .alias("Woodie_Pivots")
        .build()
}

/// DeMark Pivots - Tom DeMark's pivot calculation
pub fn signature_demark_pivots() -> IndicatorSignature {
    IndicatorSignature::builder("DEMARK", CATEGORY)
        .name("DeMark Pivots")
        .description("Tom DeMark's pivot calculation based on open/close relationship")
        .add_constraint(ParamConstraint::period(1, 168, 1))
        .metadata("author", "Tom DeMark")
        .metadata("feature", "open/close relationship")
        .machine_id(BarIndicatorId::Demark)
        .role_kind(IndicatorRoleKind::Level)
        .output_kind(IndicatorValueKind::Single)
        .validated()
        // Note: "DEMARK" is already the main ID, no need for alias
        .alias("Demark")
        .alias("demark")
        .alias("DEMARKPIVOTS")
        .alias("DeMarkPivots")
        .alias("demarkpivots")
        .alias("demark_pivots")
        .alias("DEMARK_PIVOTS")
        .alias("Demark_Pivots")
        .build()
}

/// Anchored VWAP - volume-weighted average price anchored to calendar periods
pub fn signature_anchored_vwap() -> IndicatorSignature {
    IndicatorSignature::builder("AVWAP", CATEGORY)
        .name("Anchored VWAP")
        .description("Volume-weighted average price anchored to monthly periods")
        .metadata("uses_volume", "true")
        .metadata("anchor", "monthly")
        .metadata("reset", "calendar-based")
        .machine_id(BarIndicatorId::Avwap)
        .role_kind(IndicatorRoleKind::Level)
        .output_kind(IndicatorValueKind::Single)
        .validated()
        // Note: "AVWAP" is already the main ID, no need for alias
        .alias("Avwap")
        .alias("avwap")
        .alias("ANCHOREDVWAP")
        .alias("AnchoredVWAP")
        .alias("anchoredvwap")
        .alias("anchored_vwap")
        .alias("ANCHORED_VWAP")
        .alias("Anchored_Vwap")
        .build()
}

/// AVWAP Multi-Anchor Reversion - reversion probability to multiple AVWAP anchors
pub fn signature_avwap_multi_anchor_reversion() -> IndicatorSignature {
    IndicatorSignature::builder("AVWAPREV", CATEGORY)
        .name("AVWAP Multi-Anchor Reversion")
        .description("Reversion probability to multiple AVWAP anchor points")
        .metadata("uses_volume", "true")
        .metadata("multi_anchor", "true")
        .machine_id(BarIndicatorId::Avwaprev)
        .role_kind(IndicatorRoleKind::Level)
        .output_kind(IndicatorValueKind::Single)
        .validated()
        // Note: "AVWAPREV" is already the main ID, no need for alias
        .alias("Avwaprev")
        .alias("avwaprev")
        .alias("AVWAPMULTIANCHORREVERSION")
        .alias("AVWAPMultiAnchorReversion")
        .alias("avwapmultianchorreversion")
        .alias("avwap_multi_anchor_reversion")
        .alias("AVWAP_MULTI_ANCHOR_REVERSION")
        .alias("Avwap_Multi_Anchor_Reversion")
        .build()
}

/// AVWAP Touch Probability - probability of touching anchored VWAP
pub fn signature_avwap_touch_probability() -> IndicatorSignature {
    IndicatorSignature::builder("AVWAPTOUCH", CATEGORY)
        .name("AVWAP Touch Probability")
        .description("Statistical probability of price touching AVWAP level")
        .metadata("uses_volume", "true")
        .metadata("statistical", "true")
        .machine_id(BarIndicatorId::Avwaptouch)
        .role_kind(IndicatorRoleKind::Level)
        .output_kind(IndicatorValueKind::Single)
        .validated()
        // Note: "AVWAPTOUCH" is already the main ID, no need for alias
        .alias("Avwaptouch")
        .alias("avwaptouch")
        .alias("AVWAPTOUCHPROBABILITY")
        .alias("AVWAPTouchProbability")
        .alias("avwaptouchprobability")
        .alias("avwap_touch_probability")
        .alias("AVWAP_TOUCH_PROBABILITY")
        .alias("Avwap_Touch_Probability")
        .build()
}

/// Break of Structure - detect BOS/CHOCH patterns
pub fn signature_break_of_structure() -> IndicatorSignature {
    IndicatorSignature::builder("BOS", CATEGORY)
        .name("Break of Structure")
        .description("Detects Break of Structure and Change of Character patterns")
        .add_constraint(ParamConstraint::period(2, 50, 10))
        .metadata("pattern", "BOS/CHOCH")
        .metadata("use_case", "structure breaks")
        .machine_id(BarIndicatorId::Bos)
        .role_kind(IndicatorRoleKind::Level)
        .output_kind(IndicatorValueKind::DoubleFlag)
        .validated()
        // Note: "BOS" is already the main ID, no need for alias
        .alias("Bos")
        .alias("bos")
        .alias("BREAKOFSTRUCTURE")
        .alias("BreakofStructure")
        .alias("breakofstructure")
        .alias("break_of_structure")
        .alias("BREAK_OF_STRUCTURE")
        .alias("Break_Of_Structure")
        .build()
}

/// FVG Detector - Fair Value Gap pattern detector
pub fn signature_fvg_detector() -> IndicatorSignature {
    IndicatorSignature::builder("FVG", CATEGORY)
        .name("Fair Value Gap Detector")
        .description("Detects Fair Value Gaps in 3-bar patterns")
        .metadata("pattern", "3-bar FVG")
        .metadata("type", "bull/bear gaps")
        .machine_id(BarIndicatorId::Fvg)
        .role_kind(IndicatorRoleKind::Level)
        .output_kind(IndicatorValueKind::DoubleFlag)
        .validated()
        // Note: "FVG" is already the main ID, no need for alias
        .alias("Fvg")
        .alias("fvg")
        .alias("FAIRVALUEGAPDETECTOR")
        .alias("FairValueGapDetector")
        .alias("fairvaluegapdetector")
        .alias("fair_value_gap_detector")
        .alias("FAIR_VALUE_GAP_DETECTOR")
        .alias("Fair_Value_Gap_Detector")
        .build()
}

/// High-Low Value Area - value area based on high/low ranges
pub fn signature_hl_value_area() -> IndicatorSignature {
    IndicatorSignature::builder("HLVA", CATEGORY)
        .name("High-Low Value Area")
        .description("Value area calculation based on high/low ranges")
        .metadata("basis", "high/low ranges")
        .machine_id(BarIndicatorId::Hlva)
        .role_kind(IndicatorRoleKind::Level)
        .output_kind(IndicatorValueKind::Single)
        .validated()
        // Note: "HLVA" is already the main ID, no need for alias
        .alias("Hlva")
        .alias("hlva")
        .alias("HIGHLOWVALUEAREA")
        .alias("HighLowValueArea")
        .alias("highlowvaluearea")
        .alias("high_low_value_area")
        .alias("HIGH_LOW_VALUE_AREA")
        .alias("High_Low_Value_Area")
        .build()
}

/// Pivot Anchored VWAP - VWAP anchored to pivot points
pub fn signature_pivot_anchored_vwap() -> IndicatorSignature {
    IndicatorSignature::builder("PIVAVWAP", CATEGORY)
        .name("Pivot Anchored VWAP")
        .description("VWAP anchored to pivot point levels")
        .metadata("uses_volume", "true")
        .metadata("anchor", "pivot points")
        .machine_id(BarIndicatorId::Pivavwap)
        .role_kind(IndicatorRoleKind::Level)
        .output_kind(IndicatorValueKind::Single)
        .validated()
        // Note: "PIVAVWAP" is already the main ID, no need for alias
        .alias("Pivavwap")
        .alias("pivavwap")
        .alias("PIVOTANCHOREDVWAP")
        .alias("PivotAnchoredVWAP")
        .alias("pivotanchoredvwap")
        .alias("pivot_anchored_vwap")
        .alias("PIVOT_ANCHORED_VWAP")
        .alias("Pivot_Anchored_Vwap")
        .build()
}

/// Rolling Midline - average of high/low over rolling window
pub fn signature_rolling_midline() -> IndicatorSignature {
    IndicatorSignature::builder("RMID", CATEGORY)
        .name("Rolling Midline")
        .description("Rolling average of (High + Low) / 2")
        .add_constraint(ParamConstraint::period(1, 200, 20))
        .metadata("formula", "(H+L)/2 average")
        .machine_id(BarIndicatorId::Rmid)
        .role_kind(IndicatorRoleKind::Level)
        .output_kind(IndicatorValueKind::Single)
        .validated()
        // Note: "RMID" is already the main ID, no need for alias
        .alias("Rmid")
        .alias("rmid")
        .alias("ROLLINGMIDLINE")
        .alias("RollingMidline")
        .alias("rollingmidline")
        .alias("rolling_midline")
        .alias("ROLLING_MIDLINE")
        .alias("Rolling_Midline")
        .build()
}

/// Rolling Quartiles - Q1, Q2 (median), Q3 over rolling window
pub fn signature_rolling_quartiles() -> IndicatorSignature {
    IndicatorSignature::builder("RQUART", CATEGORY)
        .name("Rolling Quartiles")
        .description("Rolling Q1, Q2 (median), Q3 quartiles of close prices")
        .add_constraint(ParamConstraint::period(1, 200, 20))
        .metadata("outputs", "Q1, Q2, Q3")
        .metadata("complexity", "O(N log N) per update")
        .machine_id(BarIndicatorId::Rquart)
        .role_kind(IndicatorRoleKind::Level)
        .output_kind(IndicatorValueKind::Triple)
        .validated()
        // Note: "RQUART" is already the main ID, no need for alias
        .alias("Rquart")
        .alias("rquart")
        .alias("ROLLINGQUARTILES")
        .alias("RollingQuartiles")
        .alias("rollingquartiles")
        .alias("rolling_quartiles")
        .alias("ROLLING_QUARTILES")
        .alias("Rolling_Quartiles")
        .build()
}

// ============================================================================
// Catalog HashMap
// ============================================================================

/// Static catalog of all Levels indicators
/// Base catalog with main IDs only (used for initialization)
const BASE_CATALOG: &[(&str, fn() -> IndicatorSignature)] = &[
    ("PIVOT", signature_pivot_points as fn() -> IndicatorSignature),
    ("FLOORPIVOT", signature_floor_trader_pivots as fn() -> IndicatorSignature),
    ("CAMARILLA", signature_camarilla_pivots as fn() -> IndicatorSignature),
    ("WOODIE", signature_woodie_pivots as fn() -> IndicatorSignature),
    ("DEMARK", signature_demark_pivots as fn() -> IndicatorSignature),
    ("AVWAP", signature_anchored_vwap as fn() -> IndicatorSignature),
    ("AVWAPREV", signature_avwap_multi_anchor_reversion as fn() -> IndicatorSignature),
    ("AVWAPTOUCH", signature_avwap_touch_probability as fn() -> IndicatorSignature),
    ("BOS", signature_break_of_structure as fn() -> IndicatorSignature),
    ("FVG", signature_fvg_detector as fn() -> IndicatorSignature),
    ("HLVA", signature_hl_value_area as fn() -> IndicatorSignature),
    ("PIVAVWAP", signature_pivot_anchored_vwap as fn() -> IndicatorSignature),
    ("RMID", signature_rolling_midline as fn() -> IndicatorSignature),
    ("RQUART", signature_rolling_quartiles 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 LEVELS_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::levels::levels_catalog;
///
/// let sig = levels_catalog::get_signature("PIVOT").unwrap();
/// assert_eq!(sig.id, "PIVOT");
/// ```
pub fn get_signature(id: &str) -> Option<IndicatorSignature> {
    LEVELS_CATALOG.get(id).map(|f| f())
}

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

/// Get count of indicators in this category
pub fn count() -> usize {
    BASE_CATALOG.len()
}

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

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

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

    #[test]
    fn test_get_camarilla_signature() {
        let sig = get_signature("CAMARILLA").unwrap();
        assert_eq!(sig.id, "CAMARILLA");
        assert_eq!(sig.name, "Camarilla Pivots");
    }

    #[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(), 14); // 14 level indicators (5 scoring moved to statistical_scoring)
    }

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

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

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

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

    #[test]
    fn test_bos_signature() {
        let sig = get_signature("BOS").unwrap();
        assert_eq!(sig.id, "BOS");
        assert_eq!(sig.name, "Break of Structure");
        assert_eq!(sig.required_params().len(), 1);
    }

}