quantwave-core 0.8.0

A high-performance, Polars-native technical analysis library for Rust.
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
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
//! Native O(1) streaming patterns for Batch C
//! (CDLPIERCING, CDLSEPARATINGLINES, CDLSHOOTINGSTAR, CDLTHRUSTING, CDLKICKING, CDLKICKINGBYLENGTH, CDLHIKKAKE, CDLHIKKAKEMOD, CDLSTICKSANDWICH, CDL2CROWS)
//! Matches `talib_rs::pattern::*`.

use crate::indicators::patterns::candle_settings::*;
use crate::traits::Next;

// =============================================================================
// CDLPIERCING
// =============================================================================
#[derive(Debug, Clone)]
#[allow(non_camel_case_types)]
pub struct CDLPIERCING {
    bars_seen: usize,
    window: CandleWindow,
    body_long: RollingCandleAvg,
}

impl CDLPIERCING {
    pub fn new() -> Self {
        Self {
            bars_seen: 0,
            window: CandleWindow::new(2),
            body_long: RollingCandleAvg::new(BODY_LONG),
        }
    }
}

impl Default for CDLPIERCING {
    fn default() -> Self {
        Self::new()
    }
}

impl Next<(f64, f64, f64, f64)> for CDLPIERCING {
    type Output = f64;
    fn next(&mut self, (open, high, low, close): (f64, f64, f64, f64)) -> Self::Output {
        self.bars_seen += 1;
        self.window.push(open, high, low, close);
        self.body_long.push(open, high, low, close);

        let lookback = BODY_LONG.avg_period + 1;
        if self.bars_seen <= lookback {
            return 0.0;
        }

        let b1 = self.window.bar(1);
        let b0 = self.window.bar(0);

        let is_piercing = candle_color(b1.open, b1.close) == -1
            && real_body(b1.open, b1.close) > self.body_long.val(1)
            && candle_color(b0.open, b0.close) == 1
            && real_body(b0.open, b0.close) > self.body_long.val(0)
            && b0.open < b1.low
            && b0.close < b1.open
            && b0.close > b1.close + real_body(b1.open, b1.close) * 0.5;

        if is_piercing { 100.0 } else { 0.0 }
    }
}

// =============================================================================
// CDLSEPARATINGLINES
// =============================================================================
#[derive(Debug, Clone)]
#[allow(non_camel_case_types)]
pub struct CDLSEPARATINGLINES {
    bars_seen: usize,
    window: CandleWindow,
    shadow_vs: RollingCandleAvg,
    body_long: RollingCandleAvg,
    equal: RollingCandleAvg,
}

impl CDLSEPARATINGLINES {
    pub fn new() -> Self {
        Self {
            bars_seen: 0,
            window: CandleWindow::new(2),
            shadow_vs: RollingCandleAvg::new(SHADOW_VERY_SHORT),
            body_long: RollingCandleAvg::new(BODY_LONG),
            equal: RollingCandleAvg::new(EQUAL),
        }
    }
}

impl Default for CDLSEPARATINGLINES {
    fn default() -> Self {
        Self::new()
    }
}

impl Next<(f64, f64, f64, f64)> for CDLSEPARATINGLINES {
    type Output = f64;
    fn next(&mut self, (open, high, low, close): (f64, f64, f64, f64)) -> Self::Output {
        self.bars_seen += 1;
        self.window.push(open, high, low, close);
        self.shadow_vs.push(open, high, low, close);
        self.body_long.push(open, high, low, close);
        self.equal.push(open, high, low, close);

        let lookback = SHADOW_VERY_SHORT
            .avg_period
            .max(BODY_LONG.avg_period)
            .max(EQUAL.avg_period)
            + 1;
        if self.bars_seen <= lookback {
            return 0.0;
        }

        let b1 = self.window.bar(1);
        let b0 = self.window.bar(0);

        let color_prev = candle_color(b1.open, b1.close);
        let color_curr = candle_color(b0.open, b0.close);

        let base = color_prev != color_curr
            && (b0.open - b1.open).abs() <= self.equal.val(1)
            && real_body(b0.open, b0.close) > self.body_long.val(0);

        let bull = base
            && color_curr == 1
            && lower_shadow(b0.open, b0.low, b0.close) < self.shadow_vs.val(0);
        let bear = base
            && color_curr == -1
            && upper_shadow(b0.open, b0.high, b0.close) < self.shadow_vs.val(0);

        if bull {
            100.0
        } else if bear {
            -100.0
        } else {
            0.0
        }
    }
}

// =============================================================================
// CDLSHOOTINGSTAR
// =============================================================================
#[derive(Debug, Clone)]
#[allow(non_camel_case_types)]
pub struct CDLSHOOTINGSTAR {
    bars_seen: usize,
    window: CandleWindow,
    body_short: RollingCandleAvg,
    shadow_long: RollingCandleAvg,
    shadow_vs: RollingCandleAvg,
}

impl CDLSHOOTINGSTAR {
    pub fn new() -> Self {
        Self {
            bars_seen: 0,
            window: CandleWindow::new(2),
            body_short: RollingCandleAvg::new(BODY_SHORT),
            shadow_long: RollingCandleAvg::new(SHADOW_LONG),
            shadow_vs: RollingCandleAvg::new(SHADOW_VERY_SHORT),
        }
    }
}

impl Default for CDLSHOOTINGSTAR {
    fn default() -> Self {
        Self::new()
    }
}

impl Next<(f64, f64, f64, f64)> for CDLSHOOTINGSTAR {
    type Output = f64;
    fn next(&mut self, (open, high, low, close): (f64, f64, f64, f64)) -> Self::Output {
        self.bars_seen += 1;
        self.window.push(open, high, low, close);
        self.body_short.push(open, high, low, close);
        self.shadow_long.push(open, high, low, close);
        self.shadow_vs.push(open, high, low, close);

        // SHADOW_LONG.avg_period is 0 and cannot raise the max.
        let lookback = BODY_SHORT.avg_period.max(SHADOW_VERY_SHORT.avg_period) + 1;
        if self.bars_seen <= lookback {
            return 0.0;
        }

        let b1 = self.window.bar(1);
        let b0 = self.window.bar(0);

        let gap_up = b0.open.min(b0.close) > b1.open.max(b1.close);

        let is_shooting_star = real_body(b0.open, b0.close) < self.body_short.val(0)
            && upper_shadow(b0.open, b0.high, b0.close) > self.shadow_long.val(0)
            && lower_shadow(b0.open, b0.low, b0.close) < self.shadow_vs.val(0)
            && gap_up;

        if is_shooting_star { -100.0 } else { 0.0 }
    }
}

// =============================================================================
// CDLTHRUSTING
// =============================================================================
#[derive(Debug, Clone)]
#[allow(non_camel_case_types)]
pub struct CDLTHRUSTING {
    bars_seen: usize,
    window: CandleWindow,
    equal: RollingCandleAvg,
    body_long: RollingCandleAvg,
}

impl CDLTHRUSTING {
    pub fn new() -> Self {
        Self {
            bars_seen: 0,
            window: CandleWindow::new(2),
            equal: RollingCandleAvg::new(EQUAL),
            body_long: RollingCandleAvg::new(BODY_LONG),
        }
    }
}

impl Default for CDLTHRUSTING {
    fn default() -> Self {
        Self::new()
    }
}

impl Next<(f64, f64, f64, f64)> for CDLTHRUSTING {
    type Output = f64;
    fn next(&mut self, (open, high, low, close): (f64, f64, f64, f64)) -> Self::Output {
        self.bars_seen += 1;
        self.window.push(open, high, low, close);
        self.equal.push(open, high, low, close);
        self.body_long.push(open, high, low, close);

        let lookback = EQUAL.avg_period.max(BODY_LONG.avg_period) + 1;
        if self.bars_seen <= lookback {
            return 0.0;
        }

        let b1 = self.window.bar(1);
        let b0 = self.window.bar(0);

        let is_thrusting = candle_color(b1.open, b1.close) == -1
            && real_body(b1.open, b1.close) > self.body_long.val(1)
            && candle_color(b0.open, b0.close) == 1
            && b0.open < b1.low
            && b0.close > b1.close + self.equal.val(1)
            && b0.close <= b1.close + real_body(b1.open, b1.close) * 0.5;

        if is_thrusting { -100.0 } else { 0.0 }
    }
}

// =============================================================================
// CDLKICKING
// =============================================================================
#[derive(Debug, Clone)]
#[allow(non_camel_case_types)]
pub struct CDLKICKING {
    bars_seen: usize,
    window: CandleWindow,
    shadow_vs: RollingCandleAvg,
    body_long: RollingCandleAvg,
}

impl CDLKICKING {
    pub fn new() -> Self {
        Self {
            bars_seen: 0,
            window: CandleWindow::new(2),
            shadow_vs: RollingCandleAvg::new(SHADOW_VERY_SHORT),
            body_long: RollingCandleAvg::new(BODY_LONG),
        }
    }
}

impl Default for CDLKICKING {
    fn default() -> Self {
        Self::new()
    }
}

impl Next<(f64, f64, f64, f64)> for CDLKICKING {
    type Output = f64;
    fn next(&mut self, (open, high, low, close): (f64, f64, f64, f64)) -> Self::Output {
        self.bars_seen += 1;
        self.window.push(open, high, low, close);
        self.shadow_vs.push(open, high, low, close);
        self.body_long.push(open, high, low, close);

        let lookback = SHADOW_VERY_SHORT.avg_period.max(BODY_LONG.avg_period) + 1;
        if self.bars_seen <= lookback {
            return 0.0;
        }

        let b1 = self.window.bar(1);
        let b0 = self.window.bar(0);

        let color_prev = candle_color(b1.open, b1.close);
        let color_curr = candle_color(b0.open, b0.close);

        if color_prev != color_curr
            && real_body(b1.open, b1.close) > self.body_long.val(1)
            && upper_shadow(b1.open, b1.high, b1.close) < self.shadow_vs.val(1)
            && lower_shadow(b1.open, b1.low, b1.close) < self.shadow_vs.val(1)
            && real_body(b0.open, b0.close) > self.body_long.val(0)
            && upper_shadow(b0.open, b0.high, b0.close) < self.shadow_vs.val(0)
            && lower_shadow(b0.open, b0.low, b0.close) < self.shadow_vs.val(0)
        {
            let bull = color_prev == -1 && color_curr == 1 && b0.open > b1.open;
            let bear = color_prev == 1 && color_curr == -1 && b0.open < b1.open;
            if bull {
                100.0
            } else if bear {
                -100.0
            } else {
                0.0
            }
        } else {
            0.0
        }
    }
}

// =============================================================================
// CDLKICKINGBYLENGTH
// =============================================================================
#[derive(Debug, Clone)]
#[allow(non_camel_case_types)]
pub struct CDLKICKINGBYLENGTH {
    bars_seen: usize,
    window: CandleWindow,
    shadow_vs: RollingCandleAvg,
    body_long: RollingCandleAvg,
}

impl CDLKICKINGBYLENGTH {
    pub fn new() -> Self {
        Self {
            bars_seen: 0,
            window: CandleWindow::new(2),
            shadow_vs: RollingCandleAvg::new(SHADOW_VERY_SHORT),
            body_long: RollingCandleAvg::new(BODY_LONG),
        }
    }
}

impl Default for CDLKICKINGBYLENGTH {
    fn default() -> Self {
        Self::new()
    }
}

impl Next<(f64, f64, f64, f64)> for CDLKICKINGBYLENGTH {
    type Output = f64;
    fn next(&mut self, (open, high, low, close): (f64, f64, f64, f64)) -> Self::Output {
        self.bars_seen += 1;
        self.window.push(open, high, low, close);
        self.shadow_vs.push(open, high, low, close);
        self.body_long.push(open, high, low, close);

        let lookback = SHADOW_VERY_SHORT.avg_period.max(BODY_LONG.avg_period) + 1;
        if self.bars_seen <= lookback {
            return 0.0;
        }

        let b1 = self.window.bar(1);
        let b0 = self.window.bar(0);

        let color_prev = candle_color(b1.open, b1.close);
        let color_curr = candle_color(b0.open, b0.close);

        if color_prev != color_curr
            && real_body(b1.open, b1.close) > self.body_long.val(1)
            && upper_shadow(b1.open, b1.high, b1.close) < self.shadow_vs.val(1)
            && lower_shadow(b1.open, b1.low, b1.close) < self.shadow_vs.val(1)
            && real_body(b0.open, b0.close) > self.body_long.val(0)
            && upper_shadow(b0.open, b0.high, b0.close) < self.shadow_vs.val(0)
            && lower_shadow(b0.open, b0.low, b0.close) < self.shadow_vs.val(0)
        {
            let has_gap = (color_prev == -1 && color_curr == 1 && b0.open > b1.open)
                || (color_prev == 1 && color_curr == -1 && b0.open < b1.open);
            let curr_longer = real_body(b0.open, b0.close) >= real_body(b1.open, b1.close);
            let color = if curr_longer { color_curr } else { color_prev };

            if has_gap { (color as f64) * 100.0 } else { 0.0 }
        } else {
            0.0
        }
    }
}

// =============================================================================
// CDLHIKKAKE
// =============================================================================
#[derive(Debug, Clone)]
#[allow(non_camel_case_types)]
pub struct CDLHIKKAKE {
    bars_seen: usize,
    window: CandleWindow,
    /// Bars elapsed since the setup bar; `None` once confirmed or expired.
    pattern_age: Option<usize>,
    pattern_result: i32,
    /// High/low of the inside bar (`pattern_idx - 1` in the batch reference),
    /// captured at setup so no backwards indexing is needed while streaming.
    ref_high: f64,
    ref_low: f64,
}

impl CDLHIKKAKE {
    pub fn new() -> Self {
        Self {
            bars_seen: 0,
            window: CandleWindow::new(3),
            pattern_age: None,
            pattern_result: 0,
            ref_high: 0.0,
            ref_low: 0.0,
        }
    }

    /// Breakout confirmation against the stored inside-bar levels.
    fn confirm(&mut self, close: f64) -> f64 {
        if let Some(age) = self.pattern_age
            && (1..=3).contains(&age)
        {
            {
                if self.pattern_result > 0 && close > self.ref_high {
                    self.pattern_age = None;
                    return (self.pattern_result + 100) as f64;
                } else if self.pattern_result < 0 && close < self.ref_low {
                    self.pattern_age = None;
                    return (self.pattern_result - 100) as f64;
                }
            }
        }
        0.0
    }
}

impl Default for CDLHIKKAKE {
    fn default() -> Self {
        Self::new()
    }
}

impl Next<(f64, f64, f64, f64)> for CDLHIKKAKE {
    type Output = f64;

    fn next(&mut self, (open, high, low, close): (f64, f64, f64, f64)) -> Self::Output {
        const LOOKBACK: usize = 5;
        self.bars_seen += 1;
        self.window.push(open, high, low, close);

        if let Some(age) = self.pattern_age {
            self.pattern_age = Some(age + 1);
        }

        if self.window.len() < 3 {
            return 0.0;
        }

        let b0 = self.window.bar(0);
        let b1 = self.window.bar(1);
        let b2 = self.window.bar(2);

        // The batch reference pre-scans bars before `start`: it updates the setup
        // state but never emits and never runs confirmation during that pre-scan.
        let emitting = self.bars_seen > LOOKBACK;

        let inside = b1.high < b2.high && b1.low > b2.low;
        if inside && b0.high < b1.high && b0.low < b1.low {
            self.pattern_result = 100;
            self.pattern_age = Some(0);
            self.ref_high = b1.high;
            self.ref_low = b1.low;
            return if emitting { 100.0 } else { 0.0 };
        }
        if inside && b0.high > b1.high && b0.low > b1.low {
            self.pattern_result = -100;
            self.pattern_age = Some(0);
            self.ref_high = b1.high;
            self.ref_low = b1.low;
            return if emitting { -100.0 } else { 0.0 };
        }

        if !emitting {
            return 0.0;
        }
        self.confirm(b0.close)
    }
}

// =============================================================================
// CDLHIKKAKEMOD
// =============================================================================
#[derive(Debug, Clone)]
#[allow(non_camel_case_types)]
pub struct CDLHIKKAKEMOD {
    bars_seen: usize,
    window: CandleWindow,
    near_avg: RollingCandleAvg,
    /// Bars elapsed since the setup bar; `None` once confirmed or expired.
    pattern_age: Option<usize>,
    pattern_result: i32,
    ref_high: f64,
    ref_low: f64,
}

impl CDLHIKKAKEMOD {
    pub fn new() -> Self {
        Self {
            bars_seen: 0,
            window: CandleWindow::new(4),
            near_avg: RollingCandleAvg::new(NEAR),
            pattern_age: None,
            pattern_result: 0,
            ref_high: 0.0,
            ref_low: 0.0,
        }
    }
}

impl Default for CDLHIKKAKEMOD {
    fn default() -> Self {
        Self::new()
    }
}

impl Next<(f64, f64, f64, f64)> for CDLHIKKAKEMOD {
    type Output = f64;

    fn next(&mut self, (open, high, low, close): (f64, f64, f64, f64)) -> Self::Output {
        self.bars_seen += 1;
        self.window.push(open, high, low, close);
        self.near_avg.push(open, high, low, close);

        if let Some(age) = self.pattern_age {
            self.pattern_age = Some(age + 1);
        }

        // C TA-Lib: lookback = max(1, NEAR.avg_period) + 5
        let lookback = 1usize.max(NEAR.avg_period) + 5;
        if self.bars_seen <= lookback {
            return 0.0;
        }

        let b0 = self.window.bar(0);
        let b1 = self.window.bar(1);
        let b2 = self.window.bar(2);
        let b3 = self.window.bar(3);

        // Two nested inside bars, then a breakout.
        if b1.high < b2.high && b1.low > b2.low && b2.high < b3.high && b2.low > b3.low {
            let near_avg = self.near_avg.val(2);
            if b0.high < b1.high && b0.low < b1.low && b2.close <= b2.low + near_avg {
                self.pattern_result = 100;
                self.pattern_age = Some(0);
                self.ref_high = b1.high;
                self.ref_low = b1.low;
            } else if b0.high > b1.high && b0.low > b1.low && b2.close >= b2.high - near_avg {
                self.pattern_result = -100;
                self.pattern_age = Some(0);
                self.ref_high = b1.high;
                self.ref_low = b1.low;
            }
        }

        // Confirmation — unlike CDLHIKKAKE this can fire on the setup bar itself,
        // and the setup alone never emits.
        if let Some(age) = self.pattern_age
            && age <= 3
        {
            {
                if self.pattern_result > 0 && b0.close > self.ref_high {
                    self.pattern_age = None;
                    return (self.pattern_result + 100) as f64;
                } else if self.pattern_result < 0 && b0.close < self.ref_low {
                    self.pattern_age = None;
                    return (self.pattern_result - 100) as f64;
                }
            }
        }
        0.0
    }
}

// =============================================================================
// CDLSTICKSANDWICH
// =============================================================================
#[derive(Debug, Clone)]
#[allow(non_camel_case_types)]
pub struct CDLSTICKSANDWICH {
    bars_seen: usize,
    window: CandleWindow,
    equal: RollingCandleAvg,
}

impl CDLSTICKSANDWICH {
    pub fn new() -> Self {
        Self {
            bars_seen: 0,
            window: CandleWindow::new(3),
            equal: RollingCandleAvg::new(EQUAL),
        }
    }
}

impl Default for CDLSTICKSANDWICH {
    fn default() -> Self {
        Self::new()
    }
}

impl Next<(f64, f64, f64, f64)> for CDLSTICKSANDWICH {
    type Output = f64;
    fn next(&mut self, (open, high, low, close): (f64, f64, f64, f64)) -> Self::Output {
        self.bars_seen += 1;
        self.window.push(open, high, low, close);
        self.equal.push(open, high, low, close);

        let lookback = EQUAL.avg_period + 2;
        if self.bars_seen <= lookback {
            return 0.0;
        }

        let b2 = self.window.bar(2);
        let b1 = self.window.bar(1);
        let b0 = self.window.bar(0);

        let is_sandwich = candle_color(b2.open, b2.close) == -1
            && candle_color(b1.open, b1.close) == 1
            && candle_color(b0.open, b0.close) == -1
            && b1.low > b2.close
            && (b0.close - b2.close).abs() <= self.equal.val(2);

        if is_sandwich { 100.0 } else { 0.0 }
    }
}

// =============================================================================
// CDL2CROWS
// =============================================================================
#[derive(Debug, Clone)]
#[allow(non_camel_case_types)]
pub struct CDL2CROWS {
    bars_seen: usize,
    window: CandleWindow,
    body_long: RollingCandleAvg,
}

impl CDL2CROWS {
    pub fn new() -> Self {
        Self {
            bars_seen: 0,
            window: CandleWindow::new(3),
            body_long: RollingCandleAvg::new(BODY_LONG),
        }
    }
}

impl Default for CDL2CROWS {
    fn default() -> Self {
        Self::new()
    }
}

impl Next<(f64, f64, f64, f64)> for CDL2CROWS {
    type Output = f64;
    fn next(&mut self, (open, high, low, close): (f64, f64, f64, f64)) -> Self::Output {
        self.bars_seen += 1;
        self.window.push(open, high, low, close);
        self.body_long.push(open, high, low, close);

        let lookback = BODY_LONG.avg_period + 2;
        if self.bars_seen <= lookback {
            return 0.0;
        }

        let b2 = self.window.bar(2);
        let b1 = self.window.bar(1);
        let b0 = self.window.bar(0);

        let is_2crows = candle_color(b2.open, b2.close) == 1
            && real_body(b2.open, b2.close) > self.body_long.val(2)
            && candle_color(b1.open, b1.close) == -1
            && b1.open.min(b1.close) > b2.open.max(b2.close) // real body gap up
            && candle_color(b0.open, b0.close) == -1
            && b0.open < b1.open && b0.open > b1.close
            && b0.close > b2.open && b0.close < b2.close;

        if is_2crows { -100.0 } else { 0.0 }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::indicators::patterns::fixtures as fx;
    use crate::test_pattern_parity;

    test_pattern_parity!(
        test_cdlpiercing,
        CDLPIERCING,
        talib_rs::pattern::cdl_piercing,
        fx::piercing
    );
    test_pattern_parity!(
        test_cdlseparatinglines,
        CDLSEPARATINGLINES,
        talib_rs::pattern::cdl_separatinglines
    );
    test_pattern_parity!(
        test_cdlshootingstar,
        CDLSHOOTINGSTAR,
        talib_rs::pattern::cdl_shootingstar,
        fx::shootingstar
    );
    test_pattern_parity!(
        test_cdlthrusting,
        CDLTHRUSTING,
        talib_rs::pattern::cdl_thrusting,
        fx::thrusting
    );
    test_pattern_parity!(
        test_cdlkicking,
        CDLKICKING,
        talib_rs::pattern::cdl_kicking,
        fx::kicking
    );
    test_pattern_parity!(
        test_cdlkickingbylength,
        CDLKICKINGBYLENGTH,
        talib_rs::pattern::cdl_kickingbylength,
        fx::kickingbylength
    );
    test_pattern_parity!(test_cdlhikkake, CDLHIKKAKE, talib_rs::pattern::cdl_hikkake);
    // The talib-rs oracle's NEAR rolling sum for this pattern is arithmetically
    // incoherent, so exact parity is impossible; the native implementation is the
    // correct one. Evidence (all reproducible on the harness' 200k walk):
    //
    //  * talib-rs seeds `near_sum` over bars [lookback-3-P, lookback-4] (the window
    //    for bar `lookback-3`) but consumes it as the average for bar `i-2`
    //    = `lookback-2`, then updates it with `+= cr(i-2) - cr(i-2-P)`. Seed and
    //    update disagree, so the window is never repaired. Simulating the index set
    //    (P = NEAR.avg_period = 5, lookback = 10) gives, at i = 1510,
    //    {2, 1503, 1504, 1505, 1506, 1507} — SIX terms still divided by five, and
    //    bar 2 is pinned in the sum forever. The correct window is {1503..1507}.
    //    Every other averaged pattern in talib-rs (e.g. cdl_homingpigeon) uses the
    //    contiguous "P bars strictly before the target bar" convention that
    //    `RollingCandleAvg` implements, which is why the other 58 patterns match.
    //  * An independent from-scratch model of C TA-Lib semantics (contiguous NEAR
    //    window ending at bar i-3, used for bar i-2) reproduces the native output
    //    EXACTLY — 0 mismatches over 200,000 bars — and differs from the oracle at
    //    4 bars. A one-bar-shifted variant matches neither. So native == C TA-Lib.
    //  * talib-rs also drops C's `else` between the setup block and the
    //    confirmation block, i.e. it lets a setup confirm on its own bar. That is
    //    provably a no-op here: a bullish setup requires high[i] < high[i-1] and
    //    confirmation requires close[i] > high[i-1] >= high[i] >= close[i]
    //    (contradiction); the bearish case is symmetric. Native keeps the same
    //    shape and is unaffected.
    //
    // Measured divergence: 4 bars / 200,000 on the deterministic walk, and never
    // more than 1 per run over 40,000 random walks of length <= 200. Bound of 8
    // gives 2x headroom while staying well below the oracle's 20 non-zero signals,
    // so silencing or over-firing the pattern still fails the test.
    test_pattern_parity!(
        test_cdlhikkakemod,
        CDLHIKKAKEMOD,
        talib_rs::pattern::cdl_hikkakemod,
        fx::hikkakemod,
        oracle_exempt = "talib-rs's cdl_hikkakemod NEAR rolling sum is non-contiguous: it \
                         seeds the window one bar early and then updates it with a rule that \
                         does not match the seed, leaving 6 terms divided by 5 with one \
                         ancient bar pinned in the sum forever. An independent model of C \
                         TA-Lib semantics matches the native implementation exactly (0/200,000) \
                         and the oracle at only 4 bars.",
        max_mismatches = 8,
        reference_mismatches = 4
    );
    test_pattern_parity!(
        test_cdlsticksandwich,
        CDLSTICKSANDWICH,
        talib_rs::pattern::cdl_sticksandwich,
        fx::sticksandwich
    );
    test_pattern_parity!(
        test_cdl2crows,
        CDL2CROWS,
        talib_rs::pattern::cdl_2crows,
        fx::two_crows
    );
}