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
//! regression_catalog.rs: Complete catalog of all Regression indicators
//!
//! This catalog contains 6 regression indicators extracted from actual implementations.
//! Organized alphabetically for easy navigation.

use crate::catalog::{
    IndicatorSignature, IndicatorCategory, ParamConstraint, ParamType, ParamValue, 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::Regression;

// ============================================================================
// Individual indicator signatures (alphabetically sorted)
// ============================================================================

/// ARIMA - AutoRegressive Integrated Moving Average
pub fn signature_arima() -> IndicatorSignature {
    IndicatorSignature::builder("ARIMA", CATEGORY)
        .name("ARIMA")
        .description("AutoRegressive Integrated Moving Average model")
        .add_constraint(
            ParamConstraint::new("p", ParamType::USize)
                .with_min(ParamValue::USize(0))
                .with_max(ParamValue::USize(16))
                .with_default(ParamValue::USize(1))
                .required()
        )
        .add_constraint(
            ParamConstraint::new("d", ParamType::USize)
                .with_min(ParamValue::USize(0))
                .with_max(ParamValue::USize(3))
                .with_default(ParamValue::USize(1))
                .required()
        )
        .add_constraint(
            ParamConstraint::new("q", ParamType::USize)
                .with_min(ParamValue::USize(0))
                .with_max(ParamValue::USize(16))
                .with_default(ParamValue::USize(1))
                .required()
        )
        .metadata("outputs", "forecast, aic, bic")
        .metadata("min_observations", "30+")
        .machine_id(BarIndicatorId::Arima)
        .role_kind(IndicatorRoleKind::Smoother)
        .output_kind(IndicatorValueKind::Single)
        // Note: "ARIMA" is already the main ID, no need for alias
        .alias("Arima")
        .alias("arima")
        .build()
}

/// ARIMAX - ARIMA with eXogenous variables
pub fn signature_arimax() -> IndicatorSignature {
    IndicatorSignature::builder("ARIMAX", CATEGORY)
        .name("ARIMAX")
        .description("ARIMA with exogenous variables")
        .add_constraint(
            ParamConstraint::new("p", ParamType::USize)
                .with_min(ParamValue::USize(0))
                .with_max(ParamValue::USize(16))
                .with_default(ParamValue::USize(1))
                .required()
        )
        .add_constraint(
            ParamConstraint::new("d", ParamType::USize)
                .with_min(ParamValue::USize(0))
                .with_max(ParamValue::USize(3))
                .with_default(ParamValue::USize(1))
                .required()
        )
        .add_constraint(
            ParamConstraint::new("q", ParamType::USize)
                .with_min(ParamValue::USize(0))
                .with_max(ParamValue::USize(16))
                .with_default(ParamValue::USize(1))
                .required()
        )
        .add_constraint(
            ParamConstraint::new("num_exog_vars", ParamType::USize)
                .with_min(ParamValue::USize(1))
                .with_max(ParamValue::USize(16))
                .with_default(ParamValue::USize(1))
                .required()
        )
        .metadata("outputs", "forecast, aic, bic, exog_coefficients")
        .metadata("min_observations", "30+")
        .machine_id(BarIndicatorId::Arimax)
        .role_kind(IndicatorRoleKind::Smoother)
        .output_kind(IndicatorValueKind::Single)
        // Note: "ARIMAX" is already the main ID, no need for alias
        .alias("Arimax")
        .alias("arimax")
        .build()
}

/// EGARCH - Exponential GARCH with asymmetric effects
pub fn signature_egarch() -> IndicatorSignature {
    IndicatorSignature::builder("EGARCH", CATEGORY)
        .name("EGARCH")
        .description("Exponential GARCH with asymmetric leverage effects")
        .add_constraint(
            ParamConstraint::new("p", ParamType::USize)
                .with_min(ParamValue::USize(1))
                .with_max(ParamValue::USize(8))
                .with_default(ParamValue::USize(1))
                .required()
        )
        .add_constraint(
            ParamConstraint::new("q", ParamType::USize)
                .with_min(ParamValue::USize(1))
                .with_max(ParamValue::USize(8))
                .with_default(ParamValue::USize(1))
                .required()
        )
        .metadata("outputs", "volatility, variance, log_likelihood, aic, bic")
        .metadata("features", "asymmetric, leverage_effect")
        .metadata("min_observations", "50+")
        .metadata("author", "Nelson (1991)")
        .machine_id(BarIndicatorId::Egarch)
        .role_kind(IndicatorRoleKind::Smoother)
        .output_kind(IndicatorValueKind::Single)
        // Note: "EGARCH" is already the main ID, no need for alias
        .alias("Egarch")
        .alias("egarch")
        .build()
}

/// GARCH - Generalized AutoRegressive Conditional Heteroskedasticity
pub fn signature_garch() -> IndicatorSignature {
    IndicatorSignature::builder("GARCH", CATEGORY)
        .name("GARCH")
        .description("Generalized AutoRegressive Conditional Heteroskedasticity model for volatility")
        .add_constraint(
            ParamConstraint::new("p", ParamType::USize)
                .with_min(ParamValue::USize(1))
                .with_max(ParamValue::USize(8))
                .with_default(ParamValue::USize(1))
                .required()
        )
        .add_constraint(
            ParamConstraint::new("q", ParamType::USize)
                .with_min(ParamValue::USize(1))
                .with_max(ParamValue::USize(8))
                .with_default(ParamValue::USize(1))
                .required()
        )
        .metadata("outputs", "volatility, variance, forecast_volatility, log_likelihood, aic, bic")
        .metadata("author", "Bollerslev (1986)")
        .metadata("min_observations", "50+")
        .machine_id(BarIndicatorId::Garch)
        .role_kind(IndicatorRoleKind::Smoother)
        .output_kind(IndicatorValueKind::Single)
        // Note: "GARCH" is already the main ID, no need for alias
        .alias("Garch")
        .alias("garch")
        .build()
}

/// Polynomial Regression - polynomial trend fitting
pub fn signature_polynomial_regression() -> IndicatorSignature {
    IndicatorSignature::builder("POLY_REG", CATEGORY)
        .name("Polynomial Regression")
        .description("Polynomial regression for nonlinear trend modeling")
        .add_constraint(
            ParamConstraint::new("degree", ParamType::USize)
                .with_min(ParamValue::USize(1))
                .with_max(ParamValue::USize(8))
                .with_default(ParamValue::USize(2))
                .required()
        )
        .metadata("outputs", "forecast, r_squared, adjusted_r_squared, mse, rmse, first_derivative, second_derivative, trend_direction")
        .metadata("trend_directions", "StrongUptrend, Uptrend, Sideways, Downtrend, StrongDowntrend")
        .metadata("min_observations", "10+")
        .machine_id(BarIndicatorId::PolyReg)
        .role_kind(IndicatorRoleKind::Smoother)
        .output_kind(IndicatorValueKind::Single)
        // Note: "POLY_REG" is already the main ID, no need for alias
        .alias("PolyReg")
        .alias("poly_reg")
        .alias("POLYNOMIALREGRESSION")
        .alias("PolynomialRegression")
        .alias("polynomialregression")
        .alias("polynomial_regression")
        .alias("POLYNOMIAL_REGRESSION")
        .alias("Polynomial_Regression")
        .build()
}

/// VAR - Vector AutoRegression
pub fn signature_var() -> IndicatorSignature {
    IndicatorSignature::builder("VAR", CATEGORY)
        .name("VAR")
        .description("Vector AutoRegression model for multivariate time series")
        .add_constraint(
            ParamConstraint::new("p", ParamType::USize)
                .with_min(ParamValue::USize(1))
                .with_max(ParamValue::USize(8))
                .with_default(ParamValue::USize(1))
                .required()
        )
        .add_constraint(
            ParamConstraint::new("n_vars", ParamType::USize)
                .with_min(ParamValue::USize(2))
                .with_max(ParamValue::USize(16))
                .with_default(ParamValue::USize(2))
                .required()
        )
        .metadata("outputs", "forecasts, residual_covariance, impulse_responses, log_likelihood, aic, bic")
        .metadata("features", "multivariate, impulse_response_analysis")
        .metadata("min_observations", "30+")
        .metadata("author", "Sims (1980)")
        .machine_id(BarIndicatorId::Var)
        .role_kind(IndicatorRoleKind::Smoother)
        .output_kind(IndicatorValueKind::Single)
        // Note: "VAR" is already the main ID, no need for alias
        .alias("Var")
        .alias("var")
        .build()
}

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

/// Static catalog of all Regression indicators
/// Base catalog with main IDs only (used for initialization)
const BASE_CATALOG: &[(&str, fn() -> IndicatorSignature)] = &[
    ("ARIMA", signature_arima as fn() -> IndicatorSignature),
    ("ARIMAX", signature_arimax as fn() -> IndicatorSignature),
    ("EGARCH", signature_egarch as fn() -> IndicatorSignature),
    ("GARCH", signature_garch as fn() -> IndicatorSignature),
    ("POLY_REG", signature_polynomial_regression as fn() -> IndicatorSignature),
    ("VAR", signature_var 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 REGRESSION_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
pub fn get_signature(id: &str) -> Option<IndicatorSignature> {
    REGRESSION_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::*;

    #[test]
    fn test_get_arima_signature() {
        let sig = get_signature("ARIMA").unwrap();
        assert_eq!(sig.id, "ARIMA");
        assert_eq!(sig.category, CATEGORY);
        assert_eq!(sig.required_params().len(), 3); // p, d, q
    }

    #[test]
    fn test_get_garch_signature() {
        let sig = get_signature("GARCH").unwrap();
        assert_eq!(sig.id, "GARCH");
        assert_eq!(sig.category, CATEGORY);
        assert_eq!(sig.required_params().len(), 2); // p, q
    }

    #[test]
    fn test_get_polynomial_signature() {
        let sig = get_signature("POLY_REG").unwrap();
        assert_eq!(sig.id, "POLY_REG");
        assert_eq!(sig.required_params().len(), 1); // degree
    }

    #[test]
    fn test_get_var_signature() {
        let sig = get_signature("VAR").unwrap();
        assert_eq!(sig.id, "VAR");
        assert_eq!(sig.required_params().len(), 2); // p, n_vars
    }

    #[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(), 6); // 6 regression indicators
    }

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

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

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

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

    #[test]
    fn test_cache_key_generation() {
        let sig = get_signature("GARCH").unwrap();
        let params = vec![
            ("p", ParamValue::USize(1)),
            ("q", ParamValue::USize(1)),
        ];
        let key = sig.cache_key(&params);
        assert!(key.contains("GARCH"));
        assert!(key.contains("1"));
    }

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

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

        // Invalid: degree = 0
        let params = vec![("degree", ParamValue::USize(0))];
        assert!(sig.validate_params(&params).is_err());

        // Invalid: degree too high
        let params = vec![("degree", ParamValue::USize(10))];
        assert!(sig.validate_params(&params).is_err());
    }

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

        // Valid params
        let params = vec![
            ("p", ParamValue::USize(2)),
            ("n_vars", ParamValue::USize(3)),
        ];
        assert!(sig.validate_params(&params).is_ok());

        // Invalid: n_vars = 1 (need at least 2 for multivariate)
        let params = vec![
            ("p", ParamValue::USize(1)),
            ("n_vars", ParamValue::USize(1)),
        ];
        assert!(sig.validate_params(&params).is_err());
    }

    #[test]
    fn test_egarch_vs_garch() {
        let garch_sig = get_signature("GARCH").unwrap();
        let egarch_sig = get_signature("EGARCH").unwrap();

        // Both should have same required params (p, q)
        assert_eq!(garch_sig.required_params().len(), 2);
        assert_eq!(egarch_sig.required_params().len(), 2);

        // But different IDs
        assert_ne!(garch_sig.id, egarch_sig.id);
    }
}