finance-query 3.0.0

A Rust library for querying financial data
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
use std::collections::HashMap;

use crate::backtesting::error::Result;
use crate::backtesting::strategy::Strategy;
use crate::indicators::{self, Indicator};
use crate::models::chart::Candle;

use super::BacktestEngine;

/// Returns true if the indicator needs high/low price series.
#[inline]
fn needs_high_low(indicator: &Indicator) -> bool {
    matches!(
        indicator,
        Indicator::Atr(_)
            | Indicator::Supertrend { .. }
            | Indicator::DonchianChannels(_)
            | Indicator::Cci(_)
            | Indicator::WilliamsR(_)
            | Indicator::Adx(_)
            | Indicator::Mfi(_)
            | Indicator::Cmf(_)
            | Indicator::Stochastic { .. }
            | Indicator::Aroon(_)
            | Indicator::Ichimoku { .. }
            | Indicator::ParabolicSar { .. }
            | Indicator::KeltnerChannels { .. }
            | Indicator::TrueRange
            | Indicator::ChoppinessIndex(_)
            | Indicator::Vwap
            | Indicator::ChaikinOscillator
            | Indicator::AccumulationDistribution
            | Indicator::BalanceOfPower(_)
            | Indicator::BullBearPower(_)
            | Indicator::ElderRay(_)
            | Indicator::AwesomeOscillator { .. }
            | Indicator::PivotPointsStandard
            | Indicator::PivotPointsFibonacci
            | Indicator::HeikinAshi
            | Indicator::ZigZag(_)
            | Indicator::FibonacciRetracement(_)
    )
}

/// Returns true if the indicator needs the volume series.
#[inline]
fn needs_volumes(indicator: &Indicator) -> bool {
    matches!(
        indicator,
        Indicator::Obv
            | Indicator::Mfi(_)
            | Indicator::Cmf(_)
            | Indicator::Vwma(_)
            | Indicator::Vwap
            | Indicator::ChaikinOscillator
            | Indicator::AccumulationDistribution
    )
}

/// Compute a single indicator and return all resulting (key, values) pairs.
fn compute_one(
    closes: &[f64],
    highs: &[f64],
    lows: &[f64],
    volumes: &[f64],
    opens: &[f64],
    name: String,
    indicator: Indicator,
) -> Result<Vec<(String, Vec<Option<f64>>)>> {
    let mut out = Vec::with_capacity(5);
    match indicator {
        Indicator::Sma(period) => {
            out.push((name, indicators::sma(closes, period)));
        }
        Indicator::Ema(period) => {
            out.push((name, indicators::ema(closes, period)));
        }
        Indicator::Rsi(period) => {
            out.push((name, indicators::rsi(closes, period)?));
        }
        Indicator::Macd { fast, slow, signal } => {
            let m = indicators::macd(closes, fast, slow, signal)?;
            out.push((format!("macd_line_{fast}_{slow}_{signal}"), m.macd_line));
            out.push((format!("macd_signal_{fast}_{slow}_{signal}"), m.signal_line));
            out.push((
                format!("macd_histogram_{fast}_{slow}_{signal}"),
                m.histogram,
            ));
        }
        Indicator::Bollinger { period, std_dev } => {
            let bb = indicators::bollinger_bands(closes, period, std_dev)?;
            out.push((format!("bollinger_upper_{period}_{std_dev}"), bb.upper));
            out.push((format!("bollinger_middle_{period}_{std_dev}"), bb.middle));
            out.push((format!("bollinger_lower_{period}_{std_dev}"), bb.lower));
        }
        Indicator::Atr(period) => {
            out.push((name, indicators::atr(highs, lows, closes, period)?));
        }
        Indicator::Supertrend { period, multiplier } => {
            let st = indicators::supertrend(highs, lows, closes, period, multiplier)?;
            out.push((format!("supertrend_value_{period}_{multiplier}"), st.value));
            let uptrend: Vec<Option<f64>> = st
                .is_uptrend
                .into_iter()
                .map(|v| v.map(|b| if b { 1.0 } else { 0.0 }))
                .collect();
            out.push((format!("supertrend_uptrend_{period}_{multiplier}"), uptrend));
        }
        Indicator::DonchianChannels(period) => {
            let dc = indicators::donchian_channels(highs, lows, period)?;
            out.push((format!("donchian_upper_{period}"), dc.upper));
            out.push((format!("donchian_middle_{period}"), dc.middle));
            out.push((format!("donchian_lower_{period}"), dc.lower));
        }
        Indicator::Wma(period) => {
            out.push((name, indicators::wma(closes, period)?));
        }
        Indicator::Dema(period) => {
            out.push((name, indicators::dema(closes, period)?));
        }
        Indicator::Tema(period) => {
            out.push((name, indicators::tema(closes, period)?));
        }
        Indicator::Hma(period) => {
            out.push((name, indicators::hma(closes, period)?));
        }
        Indicator::Obv => {
            out.push((name, indicators::obv(closes, volumes)?));
        }
        Indicator::Momentum(period) => {
            out.push((name, indicators::momentum(closes, period)?));
        }
        Indicator::Roc(period) => {
            out.push((name, indicators::roc(closes, period)?));
        }
        Indicator::Cci(period) => {
            out.push((name, indicators::cci(highs, lows, closes, period)?));
        }
        Indicator::WilliamsR(period) => {
            out.push((name, indicators::williams_r(highs, lows, closes, period)?));
        }
        Indicator::Adx(period) => {
            out.push((name, indicators::adx(highs, lows, closes, period)?));
        }
        Indicator::Mfi(period) => {
            out.push((name, indicators::mfi(highs, lows, closes, volumes, period)?));
        }
        Indicator::Cmf(period) => {
            out.push((name, indicators::cmf(highs, lows, closes, volumes, period)?));
        }
        Indicator::Cmo(period) => {
            out.push((name, indicators::cmo(closes, period)?));
        }
        Indicator::Vwma(period) => {
            out.push((name, indicators::vwma(closes, volumes, period)?));
        }
        Indicator::Alma {
            period,
            offset,
            sigma,
        } => {
            out.push((name, indicators::alma(closes, period, offset, sigma)?));
        }
        Indicator::McginleyDynamic(period) => {
            out.push((name, indicators::mcginley_dynamic(closes, period)?));
        }
        Indicator::Stochastic {
            k_period,
            k_slow,
            d_period,
        } => {
            let s = indicators::stochastic(highs, lows, closes, k_period, k_slow, d_period)?;
            out.push((format!("stochastic_k_{k_period}_{k_slow}_{d_period}"), s.k));
            out.push((format!("stochastic_d_{k_period}_{k_slow}_{d_period}"), s.d));
        }
        Indicator::StochasticRsi {
            rsi_period,
            stoch_period,
            k_period,
            d_period,
        } => {
            let s =
                indicators::stochastic_rsi(closes, rsi_period, stoch_period, k_period, d_period)?;
            out.push((
                format!("stoch_rsi_k_{rsi_period}_{stoch_period}_{k_period}_{d_period}"),
                s.k,
            ));
            out.push((
                format!("stoch_rsi_d_{rsi_period}_{stoch_period}_{k_period}_{d_period}"),
                s.d,
            ));
        }
        Indicator::AwesomeOscillator { fast, slow } => {
            out.push((
                name,
                indicators::awesome_oscillator(highs, lows, fast, slow)?,
            ));
        }
        Indicator::CoppockCurve {
            wma_period,
            long_roc,
            short_roc,
        } => {
            out.push((
                name,
                indicators::coppock_curve(closes, long_roc, short_roc, wma_period)?,
            ));
        }
        Indicator::Aroon(period) => {
            let a = indicators::aroon(highs, lows, period)?;
            out.push((format!("aroon_up_{period}"), a.aroon_up));
            out.push((format!("aroon_down_{period}"), a.aroon_down));
        }
        Indicator::Ichimoku {
            conversion,
            base,
            lagging,
            displacement,
        } => {
            let ich =
                indicators::ichimoku(highs, lows, closes, conversion, base, lagging, displacement)?;
            out.push((
                format!("ichimoku_conversion_{conversion}_{base}_{lagging}_{displacement}"),
                ich.conversion_line,
            ));
            out.push((
                format!("ichimoku_base_{conversion}_{base}_{lagging}_{displacement}"),
                ich.base_line,
            ));
            out.push((
                format!("ichimoku_leading_a_{conversion}_{base}_{lagging}_{displacement}"),
                ich.leading_span_a,
            ));
            out.push((
                format!("ichimoku_leading_b_{conversion}_{base}_{lagging}_{displacement}"),
                ich.leading_span_b,
            ));
            out.push((
                format!("ichimoku_lagging_{conversion}_{base}_{lagging}_{displacement}"),
                ich.lagging_span,
            ));
        }
        Indicator::ParabolicSar { step, max } => {
            out.push((
                name,
                indicators::parabolic_sar(highs, lows, closes, step, max)?,
            ));
        }
        Indicator::KeltnerChannels {
            period,
            multiplier,
            atr_period,
        } => {
            let kc =
                indicators::keltner_channels(highs, lows, closes, period, atr_period, multiplier)?;
            out.push((
                format!("keltner_upper_{period}_{multiplier}_{atr_period}"),
                kc.upper,
            ));
            out.push((
                format!("keltner_middle_{period}_{multiplier}_{atr_period}"),
                kc.middle,
            ));
            out.push((
                format!("keltner_lower_{period}_{multiplier}_{atr_period}"),
                kc.lower,
            ));
        }
        Indicator::TrueRange => {
            out.push((name, indicators::true_range(highs, lows, closes)?));
        }
        Indicator::ChoppinessIndex(period) => {
            out.push((
                name,
                indicators::choppiness_index(highs, lows, closes, period)?,
            ));
        }
        Indicator::Vwap => {
            out.push((name, indicators::vwap(highs, lows, closes, volumes)?));
        }
        Indicator::ChaikinOscillator => {
            out.push((
                name,
                indicators::chaikin_oscillator(highs, lows, closes, volumes)?,
            ));
        }
        Indicator::AccumulationDistribution => {
            out.push((
                name,
                indicators::accumulation_distribution(highs, lows, closes, volumes)?,
            ));
        }
        Indicator::BalanceOfPower(period) => {
            out.push((
                name,
                indicators::balance_of_power(opens, highs, lows, closes, period)?,
            ));
        }
        Indicator::BullBearPower(period) => {
            let bbp = indicators::bull_bear_power(highs, lows, closes, period)?;
            out.push((format!("bull_power_{period}"), bbp.bull_power));
            out.push((format!("bear_power_{period}"), bbp.bear_power));
        }
        Indicator::ElderRay(period) => {
            let er = indicators::elder_ray(highs, lows, closes, period)?;
            out.push((format!("elder_bull_{period}"), er.bull_power));
            out.push((format!("elder_bear_{period}"), er.bear_power));
        }
        Indicator::PivotPointsStandard => {
            let pv = indicators::pivot_points(highs, lows, closes)?;
            push_pivot_series(&mut out, "pivot", &pv);
        }
        Indicator::PivotPointsFibonacci => {
            let pv = indicators::fibonacci_pivot_points(highs, lows, closes)?;
            push_pivot_series(&mut out, "fib_pivot", &pv);
        }
        Indicator::HeikinAshi => {
            let ha = indicators::heikin_ashi_raw(opens, highs, lows, closes)?;
            out.push((
                "ha_open".to_string(),
                ha.open.into_iter().map(Some).collect(),
            ));
            out.push((
                "ha_high".to_string(),
                ha.high.into_iter().map(Some).collect(),
            ));
            out.push(("ha_low".to_string(), ha.low.into_iter().map(Some).collect()));
            out.push((
                "ha_close".to_string(),
                ha.close.into_iter().map(Some).collect(),
            ));
        }
        Indicator::ZigZag(deviation_pct) => {
            let pivots = indicators::zigzag(highs, lows, deviation_pct)?;
            let mut series = vec![None; closes.len()];
            for p in &pivots {
                if let Some(slot) = series.get_mut(p.index) {
                    *slot = Some(p.price);
                }
            }
            out.push((name, series));
        }
        Indicator::FibonacciRetracement(period) => {
            let levels = indicators::fibonacci_retracement(highs, lows, period)?;
            out.push((
                "fib_swing_high".to_string(),
                levels.iter().map(|o| o.map(|l| l.swing_high)).collect(),
            ));
            out.push((
                "fib_swing_low".to_string(),
                levels.iter().map(|o| o.map(|l| l.swing_low)).collect(),
            ));
            out.push((
                "fib_23_6".to_string(),
                levels.iter().map(|o| o.map(|l| l.level_23_6)).collect(),
            ));
            out.push((
                "fib_38_2".to_string(),
                levels.iter().map(|o| o.map(|l| l.level_38_2)).collect(),
            ));
            out.push((
                "fib_50".to_string(),
                levels.iter().map(|o| o.map(|l| l.level_50)).collect(),
            ));
            out.push((
                "fib_61_8".to_string(),
                levels.iter().map(|o| o.map(|l| l.level_61_8)).collect(),
            ));
            out.push((
                "fib_78_6".to_string(),
                levels.iter().map(|o| o.map(|l| l.level_78_6)).collect(),
            ));
        }
    }
    Ok(out)
}

/// Flatten a [`indicators::PivotPoints`] series into named scalar series
/// (`{prefix}`, `{prefix}_r1`, ... `{prefix}_s3`) for the backtesting DSL,
/// which only operates on flat `Vec<Option<f64>>` time series.
fn push_pivot_series(
    out: &mut Vec<(String, Vec<Option<f64>>)>,
    prefix: &str,
    pv: &[Option<indicators::PivotPoints>],
) {
    out.push((
        prefix.to_string(),
        pv.iter().map(|o| o.map(|p| p.pivot)).collect(),
    ));
    out.push((
        format!("{prefix}_r1"),
        pv.iter().map(|o| o.map(|p| p.r1)).collect(),
    ));
    out.push((
        format!("{prefix}_r2"),
        pv.iter().map(|o| o.map(|p| p.r2)).collect(),
    ));
    out.push((
        format!("{prefix}_r3"),
        pv.iter().map(|o| o.map(|p| p.r3)).collect(),
    ));
    out.push((
        format!("{prefix}_s1"),
        pv.iter().map(|o| o.map(|p| p.s1)).collect(),
    ));
    out.push((
        format!("{prefix}_s2"),
        pv.iter().map(|o| o.map(|p| p.s2)).collect(),
    ));
    out.push((
        format!("{prefix}_s3"),
        pv.iter().map(|o| o.map(|p| p.s3)).collect(),
    ));
}

/// Pre-compute a set of indicators on the given candles.
///
/// Accepts a list of `(key, Indicator)` pairs as returned by
/// [`Condition::required_indicators`] and returns a map from key to
/// the computed `Vec<Option<f64>>`. Multi-output indicators (MACD,
/// Bollinger, Supertrend, etc.) insert additional derived keys,
/// ignoring the supplied key for those variants.
///
/// When 4 or more indicators are requested, computation runs in parallel
/// using rayon's thread pool.
pub(crate) fn compute_for_candles(
    candles: &[Candle],
    required: Vec<(String, Indicator)>,
) -> Result<HashMap<String, Vec<Option<f64>>>> {
    if required.is_empty() {
        return Ok(HashMap::new());
    }

    // Two requests for the same `Indicator` recompute it, and multi-output
    // variants then overwrite each other's derived keys. Compute each distinct
    // indicator once and record the names that were dropped: single-output
    // variants key their result by the caller's name, so a dropped name still
    // has to resolve — a custom `IndicatorRef` may share an `Indicator` with a
    // built-in one while using its own key.
    let (required, aliases) = {
        let mut uniq: Vec<(String, Indicator)> = Vec::with_capacity(required.len());
        let mut aliases: Vec<(String, String)> = Vec::new();
        for (name, ind) in required {
            match uniq.iter().find(|(_, u)| *u == ind) {
                Some((kept, _)) if *kept != name => aliases.push((name, kept.clone())),
                Some(_) => {}
                None => uniq.push((name, ind)),
            }
        }
        (uniq, aliases)
    };

    let use_hl = required.iter().any(|(_, i)| needs_high_low(i));
    let use_vol = required.iter().any(|(_, i)| needs_volumes(i));
    let use_open = required
        .iter()
        .any(|(_, i)| matches!(i, Indicator::BalanceOfPower(_) | Indicator::HeikinAshi));

    // Extract price series upfront (single pass each, cache-friendly).
    let closes: Vec<f64> = candles.iter().map(|c| c.close).collect();
    let (highs, lows): (Vec<f64>, Vec<f64>) = if use_hl {
        candles.iter().map(|c| (c.high, c.low)).unzip()
    } else {
        (vec![], vec![])
    };
    let volumes: Vec<f64> = if use_vol {
        candles.iter().map(|c| c.volume as f64).collect()
    } else {
        vec![]
    };
    let opens: Vec<f64> = if use_open {
        candles.iter().map(|c| c.open).collect()
    } else {
        vec![]
    };

    type IndPairs = Vec<(String, Vec<Option<f64>>)>;

    // Parallelise only when both the indicator count and candle count are large
    // enough that rayon task-dispatch overhead is outweighed by the savings.
    // Empirically: ≥4 indicators AND ≥1 000 candles.
    let groups: Result<Vec<IndPairs>> = if required.len() >= 4 && candles.len() >= 1_000 {
        use rayon::prelude::*;
        required
            .into_par_iter()
            .map(|(name, ind)| compute_one(&closes, &highs, &lows, &volumes, &opens, name, ind))
            .collect()
    } else {
        required
            .into_iter()
            .map(|(name, ind)| compute_one(&closes, &highs, &lows, &volumes, &opens, name, ind))
            .collect()
    };

    let groups = groups?;
    let capacity: usize = groups.iter().map(|v| v.len()).sum();
    let mut result = HashMap::with_capacity(capacity + aliases.len());
    for group in groups {
        for (k, v) in group {
            result.insert(k, v);
        }
    }
    // A multi-output indicator ignores the caller's key, so its kept name is
    // absent here and the alias is correctly a no-op.
    for (dropped, kept) in aliases {
        if let Some(values) = result.get(&kept).cloned() {
            result.entry(dropped).or_insert(values);
        }
    }
    Ok(result)
}

impl BacktestEngine {
    /// Pre-compute all indicators required by the strategy
    pub(crate) fn compute_indicators<S: Strategy>(
        &self,
        candles: &[Candle],
        strategy: &S,
    ) -> Result<HashMap<String, Vec<Option<f64>>>> {
        compute_for_candles(candles, strategy.required_indicators())
    }

    /// Pre-compute stretched HTF indicator arrays for all `HtfCondition`s in the strategy.
    ///
    /// For each unique `(interval, utc_offset_secs)` pair:
    /// - Resample the full candle history to the HTF interval
    /// - Compute required indicators on the resampled candles
    /// - Build a mapping from base timeframe to HTF indices
    /// - Stretch each HTF indicator value to base timeframe length
    /// - Store the stretched array in the result map under the `htf_key`
    pub(super) fn compute_htf_indicators<S: Strategy>(
        &self,
        candles: &[Candle],
        strategy: &S,
    ) -> Result<HashMap<String, Vec<Option<f64>>>> {
        use std::collections::HashSet;

        use crate::backtesting::condition::HtfIndicatorSpec;
        use crate::backtesting::resample::{base_to_htf_index, resample};
        use crate::constants::Interval;

        let specs = strategy.htf_requirements();
        if specs.is_empty() {
            return Ok(HashMap::new());
        }

        let mut result = HashMap::new();

        // Group specs by (interval, utc_offset_secs) — one resample per unique pair.
        let mut by_interval: HashMap<(Interval, i64), Vec<HtfIndicatorSpec>> = HashMap::new();
        for spec in specs {
            by_interval
                .entry((spec.interval, spec.utc_offset_secs))
                .or_default()
                .push(spec);
        }

        for ((interval, utc_offset_secs), specs) in by_interval {
            let htf_candles = resample(candles, interval, utc_offset_secs);
            if htf_candles.is_empty() {
                continue;
            }

            // De-duplicate indicators by base_key to avoid recomputing MACD/Bollinger
            // etc. when multiple output keys (line, signal, histogram) are requested.
            let mut required: Vec<(String, crate::indicators::Indicator)> = Vec::new();
            let mut seen_base_keys: HashSet<&str> = HashSet::new();
            for spec in &specs {
                if seen_base_keys.insert(&spec.base_key) {
                    required.push((spec.base_key.clone(), spec.indicator));
                }
            }

            let htf_values = compute_for_candles(&htf_candles, required)?;
            let mapping = base_to_htf_index(candles, &htf_candles);

            for spec in &specs {
                if let Some(htf_vec) = htf_values.get(&spec.base_key) {
                    let stretched: Vec<Option<f64>> = mapping
                        .iter()
                        .map(|htf_idx| htf_idx.and_then(|i| htf_vec.get(i).copied().flatten()))
                        .collect();
                    result.insert(spec.htf_key.clone(), stretched);
                }
            }
        }

        Ok(result)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::backtesting::engine::fixtures::make_candles;

    #[test]
    fn distinct_keys_for_one_indicator_both_resolve() {
        // A custom `IndicatorRef` may pick its own key while requesting the same
        // `Indicator` as a built-in ref. Deduping the computation must not drop
        // the second key, or the condition reading it silently never fires.
        let candles = make_candles(
            &(0..300)
                .map(|i| 100.0 + (i % 7) as f64)
                .collect::<Vec<f64>>(),
        );
        let map = compute_for_candles(
            &candles,
            vec![
                ("sma_20".to_string(), Indicator::Sma(20)),
                ("my_ma".to_string(), Indicator::Sma(20)),
            ],
        )
        .unwrap();

        assert!(map.contains_key("sma_20"));
        assert!(map.contains_key("my_ma"), "aliased key was dropped");
        assert_eq!(map.get("sma_20"), map.get("my_ma"));
    }

    #[test]
    fn duplicate_indicator_requests_produce_identical_map() {
        let prices: Vec<f64> = (0..500)
            .map(|i| 100.0 + (i as f64 * 0.1).sin() * 5.0)
            .collect();
        let candles = make_candles(&prices);

        let one = compute_for_candles(
            &candles,
            vec![(
                "bb_u".to_string(),
                Indicator::Bollinger {
                    period: 20,
                    std_dev: 2.0,
                },
            )],
        )
        .unwrap();
        let two = compute_for_candles(
            &candles,
            vec![
                (
                    "bb_u".to_string(),
                    Indicator::Bollinger {
                        period: 20,
                        std_dev: 2.0,
                    },
                ),
                (
                    "bb_l".to_string(),
                    Indicator::Bollinger {
                        period: 20,
                        std_dev: 2.0,
                    },
                ),
            ],
        )
        .unwrap();

        for (k, v) in &one {
            assert_eq!(two.get(k), Some(v), "key {k} changed when requested twice");
        }
    }
}