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
//! Typed cache key for indicator lookups (replaces String-based keys)
//!
//! This module provides a zero-allocation, stack-allocated cache key structure
//! that replaces expensive String-based cache keys in UniversalIngridCache.
//!
//! ## Performance Benefits
//!
//! - **Zero allocations**: IndicatorKey is Copy (stack-only, no heap)
//! - **8x faster hashing**: Struct hashing (10ns) vs String hashing (80ns)
//! - **Smaller memory footprint**: 8 bytes vs 24+ bytes for String
//! - **Type safety**: Compile-time checks instead of runtime String parsing
//!
//! ## Architecture
//!
//! ```text
//! OLD: format!("SMA_20") → String (24 bytes + heap allocation)
//!//!      String hash (80ns) + HashMap lookup (30ns) = 110ns
//!
//! NEW: IndicatorKey { indicator_id: Sma, period: 20, ma_type: None, output: Main }
//!//!      Struct hash (10ns) + HashMap lookup (30ns) = 40ns
//!
//! Speedup: 110ns → 40ns = 2.75x faster per lookup
//! For 1.5M combinations × 4 indicators = 420ms → 152ms (268ms saved)
//! ```
//!
//! ## Multi-Output Indicators
//!
//! Some indicators return multiple values (e.g., Bollinger Bands: upper/middle/lower).
//! The `OutputSelector` enum allows selecting which output to cache:
//!
//! ```rust
//! use zengeld_chart_indicators::bar_indicators::bar_indicator_id::BarIndicatorId;
//! use zengeld_chart_indicators::catalog::indicator_key::{IndicatorKey, OutputSelector};
//!
//! // Bollinger Bands upper band
//! let key = IndicatorKey::with_output(BarIndicatorId::Bb, 20, None, OutputSelector::Upper);
//! assert_eq!(key.output, OutputSelector::Upper);
//!
//! // Bollinger Bands lower band
//! let key = IndicatorKey::with_output(BarIndicatorId::Bb, 20, None, OutputSelector::Lower);
//! assert_eq!(key.output, OutputSelector::Lower);
//!
//! // MACD signal line
//! let key = IndicatorKey::with_output(BarIndicatorId::Macd, 12, None, OutputSelector::Signal);
//! assert_eq!(key.output, OutputSelector::Signal);
//! ```
//!
//! ## Usage
//!
//! ```rust
//! use zengeld_chart_indicators::bar_indicators::bar_indicator_id::BarIndicatorId;
//! use zengeld_chart_indicators::bar_indicators::average::MovingAverageType;
//! use zengeld_chart_indicators::catalog::indicator_key::IndicatorKey;
//! use std::collections::HashMap;
//!
//! // Simple indicator (SMA_20)
//! let key = IndicatorKey::new(BarIndicatorId::Sma, 20, None);
//! assert_eq!(key.period, 20);
//!
//! // Indicator with MA type (ATR_RMA_14)
//! let key = IndicatorKey::new(
//!     BarIndicatorId::Atr,
//!     14,
//!     Some(MovingAverageType::RMA)
//! );
//! assert_eq!(key.ma_type, Some(MovingAverageType::RMA));
//!
//! // Use in HashMap
//! let mut cache: HashMap<IndicatorKey, i32> = HashMap::new();
//! cache.insert(key, 42);
//! assert_eq!(cache.get(&key), Some(&42));
//! ```

use crate::bar_indicators::bar_indicator_id::BarIndicatorId;
use crate::bar_indicators::average::MovingAverageType;
use crate::bar_indicators::indicator_value::IndicatorValue;
use std::hash::{Hash, Hasher};

// =============================================================================
// Output Selector for Multi-Output Indicators
// =============================================================================

/// Selects which output to extract from multi-output indicators.
///
/// Many indicators return multiple values (e.g., Bollinger Bands: upper/middle/lower,
/// MACD: line/signal/histogram, Stochastic: %K/%D). This enum specifies which
/// output to cache and use in strategies.
///
/// ## Mapping to IndicatorValue variants:
///
/// | OutputSelector | Channel3        | Macd       | Double   | Single |
/// |---------------|-----------------|------------|----------|--------|
/// | Main          | middle          | line       | first    | value  |
/// | Upper         | upper           | -          | -        | -      |
/// | Lower         | lower           | -          | -        | -      |
/// | Middle        | middle          | -          | -        | -      |
/// | Second        | -               | -          | second   | -      |
/// | Signal        | -               | signal     | -        | -      |
/// | Histogram     | -               | histogram  | -        | -      |
///
/// ## Examples
///
/// ```rust
/// use zengeld_chart_indicators::catalog::indicator_key::OutputSelector;
///
/// // Default output is Main
/// let selector = OutputSelector::default();
/// assert_eq!(selector, OutputSelector::Main);
///
/// // Short names for debugging
/// assert_eq!(OutputSelector::Upper.short_name(), "upper");
/// assert_eq!(OutputSelector::Signal.short_name(), "signal");
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
#[repr(u8)]
pub enum OutputSelector {
    /// Main/default output - calls IndicatorValue::main()
    /// - Channel3: middle
    /// - Macd: line
    /// - Double: first value
    /// - Single: the value
    #[default]
    Main = 0,

    /// Upper band (for Channel3: upper)
    Upper = 1,

    /// Lower band (for Channel3: lower)
    Lower = 2,

    /// Middle band (for Channel3: middle, same as Main for channels)
    Middle = 3,

    /// Second value (for Double: second, e.g., Stochastic %D)
    Second = 4,

    /// Signal line (for Macd: signal)
    Signal = 5,

    /// Histogram (for Macd: histogram)
    Histogram = 6,
}

impl OutputSelector {
    /// Extract the selected output from an IndicatorValue
    ///
    /// Returns f64::NAN if the requested output doesn't exist for this value type.
    #[inline]
    pub fn extract(&self, value: &IndicatorValue) -> f64 {
        match self {
            OutputSelector::Main => value.main(),
            OutputSelector::Upper => value.upper().unwrap_or(f64::NAN),
            OutputSelector::Lower => value.lower().unwrap_or(f64::NAN),
            OutputSelector::Middle => value.middle().unwrap_or(value.main()),
            OutputSelector::Second => {
                match value {
                    IndicatorValue::Double(_, second) => *second,
                    _ => f64::NAN,
                }
            }
            OutputSelector::Signal => value.macd_signal().unwrap_or(f64::NAN),
            OutputSelector::Histogram => value.macd_histogram().unwrap_or(f64::NAN),
        }
    }

    /// Get a short name for this output selector (for debugging/logging)
    pub fn short_name(&self) -> &'static str {
        match self {
            OutputSelector::Main => "main",
            OutputSelector::Upper => "upper",
            OutputSelector::Lower => "lower",
            OutputSelector::Middle => "middle",
            OutputSelector::Second => "second",
            OutputSelector::Signal => "signal",
            OutputSelector::Histogram => "hist",
        }
    }
}

/// Zero-allocation cache key for indicator lookups
///
/// ## Memory Layout (~12 bytes on 64-bit systems)
///
/// ```text
/// ┌──────────────────┬──────────┬──────────────────────┬──────────────┐
/// │  indicator_id    │  period  │      ma_type         │    output    │
/// │   (u16, 2 bytes) │ (2 bytes)│ (Option<u8>, 2 bytes)│   (u8, 1 byte)│
/// │                  │          │                      │  + padding   │
/// └──────────────────┴──────────┴──────────────────────┴──────────────┘
/// Total: ~12 bytes (vs String: 24 bytes + heap allocation)
/// ```
///
/// ## Field Usage
///
/// - `indicator_id`: BarIndicatorId enum (identifies SMA, ATR, RSI, etc.)
/// - `period`: Period parameter (e.g., 20, 50, 200)
/// - `ma_type`: Optional MovingAverageType (for ATR, Bollinger Bands, etc.)
/// - `output`: Which output to extract for multi-output indicators (default: Main)
///
/// ## Examples
///
/// ```text
/// SMA_20           → IndicatorKey { indicator_id: Sma, period: 20, ma_type: None, output: Main }
/// BB_20_upper      → IndicatorKey { indicator_id: Bb, period: 20, ma_type: None, output: Upper }
/// BB_20_lower      → IndicatorKey { indicator_id: Bb, period: 20, ma_type: None, output: Lower }
/// MACD_12_signal   → IndicatorKey { indicator_id: Macd, period: 12, ma_type: None, output: Signal }
/// STOCH_14_second  → IndicatorKey { indicator_id: Stoch, period: 14, ma_type: None, output: Second }
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct IndicatorKey {
    /// Indicator type (e.g., SMA, ATR, RSI)
    pub indicator_id: BarIndicatorId,

    /// Period parameter (e.g., 20, 50, 200)
    pub period: u16,

    /// Optional MA type (for ATR, Bollinger Bands, etc.)
    pub ma_type: Option<MovingAverageType>,

    /// Output selector for multi-output indicators (default: Main)
    pub output: OutputSelector,

    /// Hash of all extra parameters that don't fit into `period`/`ma_type` slots:
    /// secondary periods (MACD slow, MaCross slow), additional ma_types
    /// (MACD fast/slow/signal), component_configs, source (when not Close),
    /// inner_indicators (recursive). 0 when no extras.
    pub param_hash: u64,
}

impl IndicatorKey {
    /// Create a new IndicatorKey with default output (Main)
    ///
    /// ## Arguments
    /// - `indicator_id`: BarIndicatorId enum variant
    /// - `period`: Period parameter
    /// - `ma_type`: Optional MovingAverageType (for ATR, BB, etc.)
    ///
    /// ## Examples
    /// ```rust
    /// use zengeld_chart_indicators::bar_indicators::bar_indicator_id::BarIndicatorId;
    /// use zengeld_chart_indicators::bar_indicators::average::MovingAverageType;
    /// use zengeld_chart_indicators::catalog::indicator_key::IndicatorKey;
    ///
    /// let sma_key = IndicatorKey::new(BarIndicatorId::Sma, 20, None);
    /// assert_eq!(sma_key.period, 20);
    ///
    /// let atr_key = IndicatorKey::new(BarIndicatorId::Atr, 14, Some(MovingAverageType::RMA));
    /// assert_eq!(atr_key.ma_type, Some(MovingAverageType::RMA));
    /// ```
    #[inline]
    pub fn new(indicator_id: BarIndicatorId, period: u16, ma_type: Option<MovingAverageType>) -> Self {
        Self {
            indicator_id,
            period,
            ma_type,
            output: OutputSelector::Main,
            param_hash: 0,
        }
    }

    /// Create a new IndicatorKey with specific output selector
    ///
    /// Use this for multi-output indicators like Bollinger Bands, MACD, Stochastic.
    ///
    /// ## Arguments
    /// - `indicator_id`: BarIndicatorId enum variant
    /// - `period`: Period parameter
    /// - `ma_type`: Optional MovingAverageType
    /// - `output`: Which output to extract (Upper, Lower, Signal, etc.)
    ///
    /// ## Examples
    /// ```rust
    /// use zengeld_chart_indicators::bar_indicators::bar_indicator_id::BarIndicatorId;
    /// use zengeld_chart_indicators::catalog::indicator_key::{IndicatorKey, OutputSelector};
    ///
    /// let bb_upper = IndicatorKey::with_output(BarIndicatorId::Bb, 20, None, OutputSelector::Upper);
    /// assert_eq!(bb_upper.output, OutputSelector::Upper);
    ///
    /// let macd_signal = IndicatorKey::with_output(BarIndicatorId::Macd, 12, None, OutputSelector::Signal);
    /// assert_eq!(macd_signal.output, OutputSelector::Signal);
    /// ```
    #[inline]
    pub fn with_output(
        indicator_id: BarIndicatorId,
        period: u16,
        ma_type: Option<MovingAverageType>,
        output: OutputSelector,
    ) -> Self {
        Self {
            indicator_id,
            period,
            ma_type,
            output,
            param_hash: 0,
        }
    }

    /// Set `param_hash` (builder-style). Used when an indicator has extras
    /// (secondary periods, multiple ma_types, component_configs, inner_indicators)
    /// that don't fit into the typed slots.
    #[inline]
    pub fn with_param_hash(mut self, hash: u64) -> Self {
        self.param_hash = hash;
        self
    }

    /// Get the base key (same indicator/period/ma_type but with Main output)
    ///
    /// Useful for cache lookup when you need to compute the indicator once
    /// but extract multiple outputs.
    #[inline]
    pub fn base_key(&self) -> Self {
        Self {
            indicator_id: self.indicator_id,
            period: self.period,
            ma_type: self.ma_type,
            output: OutputSelector::Main,
            param_hash: self.param_hash,
        }
    }

    /// Check if this key uses the default (Main) output
    #[inline]
    pub fn is_main_output(&self) -> bool {
        self.output == OutputSelector::Main
    }

    /// Convert to legacy String key format for backward compatibility
    ///
    /// This allows gradual migration from String keys to IndicatorKey.
    /// Use only during transition period.
    ///
    /// ## Examples
    /// ```rust
    /// use zengeld_chart_indicators::bar_indicators::bar_indicator_id::BarIndicatorId;
    /// use zengeld_chart_indicators::catalog::indicator_key::{IndicatorKey, OutputSelector};
    ///
    /// let key = IndicatorKey::new(BarIndicatorId::Sma, 20, None);
    /// assert_eq!(key.to_legacy_string(), "SMA_20");
    ///
    /// let key = IndicatorKey::with_output(BarIndicatorId::Bb, 20, None, OutputSelector::Upper);
    /// assert_eq!(key.to_legacy_string(), "BB_20_upper");
    /// ```
    pub fn to_legacy_string(&self) -> String {
        let indicator_name = format!("{:?}", self.indicator_id).to_uppercase();

        let base = if let Some(ma_type) = self.ma_type {
            let ma_name = match ma_type {
                MovingAverageType::SMA => "Simple",
                MovingAverageType::EMA => "EMA",
                MovingAverageType::RMA => "Wilder",
                MovingAverageType::WMA => "WMA",
                MovingAverageType::HMA => "HMA",
                MovingAverageType::DEMA => "DEMA",
                MovingAverageType::TEMA => "TEMA",
                MovingAverageType::VWMA => "VWMA",
                MovingAverageType::TMA => "TMA",
                MovingAverageType::VWAP => "VWAP",
                MovingAverageType::AMA => "AMA",
            };
            format!("{}_{}_{}",  indicator_name, ma_name, self.period)
        } else {
            format!("{}_{}", indicator_name, self.period)
        };

        // Append output selector if not Main
        if self.output != OutputSelector::Main {
            format!("{}_{}", base, self.output.short_name())
        } else {
            base
        }
    }

    /// Parse from legacy String key format
    ///
    /// For backward compatibility during migration.
    ///
    /// ## Examples
    /// ```rust
    /// use zengeld_chart_indicators::bar_indicators::bar_indicator_id::BarIndicatorId;
    /// use zengeld_chart_indicators::catalog::indicator_key::IndicatorKey;
    ///
    /// let key = IndicatorKey::from_legacy_string("SMA_20").unwrap();
    /// assert_eq!(key.indicator_id, BarIndicatorId::Sma);
    /// assert_eq!(key.period, 20);
    /// ```
    pub fn from_legacy_string(s: &str) -> Result<Self, String> {
        let parts: Vec<&str> = s.split('_').collect();

        if parts.len() < 2 {
            return Err(format!("Invalid key format: {}", s));
        }

        // Parse indicator ID
        let indicator_str = parts[0];
        let indicator_id = Self::parse_indicator_id(indicator_str)?;

        // Check if we have MA type (3 parts: INDICATOR_MATYPE_PERIOD)
        let (ma_type, period_str) = if parts.len() >= 3 {
            let ma_str = parts[1];
            let ma_type = Some(Self::parse_ma_type(ma_str)?);
            (ma_type, parts[2])
        } else {
            (None, parts[1])
        };

        // Parse period
        let period = period_str.parse::<u16>()
            .map_err(|e| format!("Invalid period '{}': {}", period_str, e))?;

        Ok(Self::new(indicator_id, period, ma_type))
    }

    /// Parse indicator ID from string
    fn parse_indicator_id(s: &str) -> Result<BarIndicatorId, String> {
        // Convert to uppercase for case-insensitive matching
        let s_upper = s.to_uppercase();

        match s_upper.as_str() {
            "SMA" => Ok(BarIndicatorId::Sma),
            "EMA" => Ok(BarIndicatorId::Ema),
            "RMA" => Ok(BarIndicatorId::Rma),
            "WMA" => Ok(BarIndicatorId::Wma),
            "HMA" => Ok(BarIndicatorId::Hma),
            "DEMA" => Ok(BarIndicatorId::Dema),
            "TEMA" => Ok(BarIndicatorId::Tema),
            "ATR" => Ok(BarIndicatorId::Atr),
            "RSI" => Ok(BarIndicatorId::Rsi),
            "MACD" => Ok(BarIndicatorId::Macd),
            "BB" => Ok(BarIndicatorId::Bb),
            "STOCH" => Ok(BarIndicatorId::Stoch),
            "ADX" => Ok(BarIndicatorId::Adx),
            "CCI" => Ok(BarIndicatorId::Cci),
            "MFI" => Ok(BarIndicatorId::Mfi),
            _ => Err(format!("Unknown indicator: {}", s))
        }
    }

    /// Parse MA type from string
    fn parse_ma_type(s: &str) -> Result<MovingAverageType, String> {
        match s.to_uppercase().as_str() {
            "SIMPLE" | "SMA" => Ok(MovingAverageType::SMA),
            "EMA" => Ok(MovingAverageType::EMA),
            "WILDER" | "RMA" => Ok(MovingAverageType::RMA),
            "WMA" => Ok(MovingAverageType::WMA),
            "HMA" => Ok(MovingAverageType::HMA),
            "DEMA" => Ok(MovingAverageType::DEMA),
            "TEMA" => Ok(MovingAverageType::TEMA),
            "VWMA" => Ok(MovingAverageType::VWMA),
            "TMA" => Ok(MovingAverageType::TMA),
            "VWAP" => Ok(MovingAverageType::VWAP),
            "AMA" => Ok(MovingAverageType::AMA),
            _ => Err(format!("Unknown MA type: {}", s))
        }
    }
}

/// Custom Hash implementation for optimal performance
///
/// We manually implement Hash to ensure optimal hashing performance.
/// The compiler's derived Hash would work, but this gives us control
/// over the hashing strategy.
impl Hash for IndicatorKey {
    #[inline]
    fn hash<H: Hasher>(&self, state: &mut H) {
        // Hash indicator_id (as discriminant for fast hashing)
        (self.indicator_id as u16).hash(state);

        // Hash period
        self.period.hash(state);

        // Hash ma_type if present
        if let Some(ma_type) = self.ma_type {
            (ma_type as u8).hash(state);
        }

        // Hash output selector
        (self.output as u8).hash(state);

        // Hash extra-params digest (0 when indicator has no extras).
        self.param_hash.hash(state);
    }
}

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

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

    #[test]
    fn test_indicator_key_creation() {
        let key = IndicatorKey::new(BarIndicatorId::Sma, 20, None);
        assert_eq!(key.indicator_id, BarIndicatorId::Sma);
        assert_eq!(key.period, 20);
        assert_eq!(key.ma_type, None);
    }

    #[test]
    fn test_indicator_key_with_ma_type() {
        let key = IndicatorKey::new(
            BarIndicatorId::Atr,
            14,
            Some(MovingAverageType::RMA)
        );
        assert_eq!(key.indicator_id, BarIndicatorId::Atr);
        assert_eq!(key.period, 14);
        assert_eq!(key.ma_type, Some(MovingAverageType::RMA));
    }

    #[test]
    fn test_param_hash_distinguishes_secondary_params() {
        // param_hash distinguishes indicators with the same primary period but different secondary periods.
        let a = IndicatorKey::new(BarIndicatorId::Macd, 12, None).with_param_hash(0x1234);
        let b = IndicatorKey::new(BarIndicatorId::Macd, 12, None).with_param_hash(0x5678);
        let c = IndicatorKey::new(BarIndicatorId::Macd, 12, None).with_param_hash(0x1234);

        assert_ne!(a, b);
        assert_eq!(a, c);
    }

    #[test]
    fn test_indicator_key_equality() {
        let key1 = IndicatorKey::new(BarIndicatorId::Sma, 20, None);
        let key2 = IndicatorKey::new(BarIndicatorId::Sma, 20, None);
        let key3 = IndicatorKey::new(BarIndicatorId::Ema, 20, None);

        assert_eq!(key1, key2);
        assert_ne!(key1, key3);
    }

    #[test]
    fn test_indicator_key_copy() {
        let key1 = IndicatorKey::new(BarIndicatorId::Sma, 20, None);
        let key2 = key1; // Should copy, not move

        // Both should be usable
        assert_eq!(key1, key2);
    }

    #[test]
    fn test_to_legacy_string_simple() {
        let key = IndicatorKey::new(BarIndicatorId::Sma, 20, None);
        assert_eq!(key.to_legacy_string(), "SMA_20");
    }

    #[test]
    fn test_to_legacy_string_with_ma_type() {
        let key = IndicatorKey::new(
            BarIndicatorId::Atr,
            14,
            Some(MovingAverageType::RMA)
        );
        assert_eq!(key.to_legacy_string(), "ATR_Wilder_14");
    }

    #[test]
    fn test_from_legacy_string_simple() {
        let key = IndicatorKey::from_legacy_string("SMA_20").unwrap();
        assert_eq!(key.indicator_id, BarIndicatorId::Sma);
        assert_eq!(key.period, 20);
        assert_eq!(key.ma_type, None);
    }

    #[test]
    fn test_from_legacy_string_with_ma_type() {
        let key = IndicatorKey::from_legacy_string("ATR_Wilder_14").unwrap();
        assert_eq!(key.indicator_id, BarIndicatorId::Atr);
        assert_eq!(key.period, 14);
        assert_eq!(key.ma_type, Some(MovingAverageType::RMA));
    }

    #[test]
    fn test_roundtrip_conversion() {
        let original = IndicatorKey::new(
            BarIndicatorId::Atr,
            14,
            Some(MovingAverageType::RMA)
        );

        let string = original.to_legacy_string();
        let parsed = IndicatorKey::from_legacy_string(&string).unwrap();

        assert_eq!(original, parsed);
    }

    #[test]
    fn test_hash_consistency() {
        use std::collections::HashMap;

        let key = IndicatorKey::new(BarIndicatorId::Sma, 20, None);
        let mut map = HashMap::new();
        map.insert(key, 42);

        // Should be able to retrieve using the same key
        assert_eq!(map.get(&key), Some(&42));
    }

    #[test]
    fn test_memory_size() {
        use std::mem::size_of;

        // IndicatorKey should be small (8-16 bytes depending on alignment)
        let key_size = size_of::<IndicatorKey>();
        assert!(key_size <= 16, "IndicatorKey is too large: {} bytes", key_size);

        // String is much larger (24 bytes + heap allocation)
        let string_size = size_of::<String>();
        assert!(key_size < string_size,
            "IndicatorKey ({} bytes) should be smaller than String ({} bytes)",
            key_size, string_size);
    }
}