rangebar 6.1.1

Non-lookahead range bar construction for cryptocurrency trading with temporal integrity guarantees
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
//! Integration tests for critical algorithm invariants
//!
//! This test suite validates the **Breach Consistency Invariant**:
//! - If `high - open >= threshold` → `close` must equal `high`
//! - If `open - low >= threshold` → `close` must equal `low`
//!
//! **Why This Matters**: Ensures non-lookahead bias and algorithmic correctness
//! across all scenarios (edge cases, large datasets, multiple thresholds).
//!
//! **Reference**: `/Users/terryli/eon/rangebar/docs/specifications/algorithm-spec.md`

use rangebar_core::{FixedPoint, RangeBar, RangeBarProcessor};

/// Breach consistency invariant validator
///
/// Validates that every completed range bar satisfies the breach consistency invariant:
/// - If high breached (high - open >= threshold), close must equal high
/// - If low breached (open - low >= threshold), close must equal low
///
/// # Arguments
///
/// * `bars` - Completed range bars to validate
/// * `threshold_decimal_bps` - Threshold in decimal bps (250 = 25bps = 0.25%)
///
/// # Returns
///
/// `Ok(())` if all bars pass invariant checks, `Err(String)` with detailed diagnostics otherwise
fn validate_breach_consistency_invariant(
    bars: &[RangeBar],
    threshold_decimal_bps: u32,
) -> Result<(), String> {
    const BASIS_POINTS_SCALE: i64 = 100_000; // v3.0.0: decimal bps

    for (i, bar) in bars.iter().enumerate() {
        // Compute thresholds from open (fixed throughout bar lifetime)
        let open_val = bar.open.0;
        let threshold_delta = (open_val * threshold_decimal_bps as i64) / BASIS_POINTS_SCALE;
        let upper_threshold = open_val + threshold_delta;
        let lower_threshold = open_val - threshold_delta;

        let high_val = bar.high.0;
        let low_val = bar.low.0;
        let close_val = bar.close.0;

        // Check if high breached threshold
        let high_breached = high_val >= upper_threshold;
        // Check if low breached threshold
        let low_breached = low_val <= lower_threshold;

        // Invariant: If high breached, close must be at high (upward breach)
        if high_breached && close_val != high_val {
            return Err(format!(
                "Breach Consistency Invariant VIOLATION at bar {}: \
                 High breached (high={} >= upper_threshold={}), \
                 but close={} != high. \
                 Bar: open={}, high={}, low={}, close={}, threshold={}bps ({}×0.1bps)",
                i,
                FixedPoint(high_val),
                FixedPoint(upper_threshold),
                FixedPoint(close_val),
                bar.open,
                bar.high,
                bar.low,
                bar.close,
                threshold_decimal_bps as f64 / 10.0,
                threshold_decimal_bps
            ));
        }

        // Invariant: If low breached, close must be at low (downward breach)
        if low_breached && close_val != low_val {
            return Err(format!(
                "Breach Consistency Invariant VIOLATION at bar {}: \
                 Low breached (low={} <= lower_threshold={}), \
                 but close={} != low. \
                 Bar: open={}, high={}, low={}, close={}, threshold={}bps ({}×0.1bps)",
                i,
                FixedPoint(low_val),
                FixedPoint(lower_threshold),
                FixedPoint(close_val),
                bar.open,
                bar.high,
                bar.low,
                bar.close,
                threshold_decimal_bps as f64 / 10.0,
                threshold_decimal_bps
            ));
        }

        // Additional sanity checks
        if high_val < open_val {
            return Err(format!(
                "Sanity check FAILED at bar {}: high < open (high={}, open={})",
                i,
                FixedPoint(high_val),
                FixedPoint(open_val)
            ));
        }

        if low_val > open_val {
            return Err(format!(
                "Sanity check FAILED at bar {}: low > open (low={}, open={})",
                i,
                FixedPoint(low_val),
                FixedPoint(open_val)
            ));
        }

        if high_val < close_val {
            return Err(format!(
                "Sanity check FAILED at bar {}: high < close (high={}, close={})",
                i,
                FixedPoint(high_val),
                FixedPoint(close_val)
            ));
        }

        if low_val > close_val {
            return Err(format!(
                "Sanity check FAILED at bar {}: low > close (low={}, close={})",
                i,
                FixedPoint(low_val),
                FixedPoint(close_val)
            ));
        }
    }

    Ok(())
}

#[cfg(test)]
mod invariant_tests {
    use super::*;
    use rangebar_core::test_utils::{AggTradeBuilder, generators, scenarios};

    /// Test breach consistency invariant on exact upward breach scenario
    #[test]
    fn test_invariant_exact_breach_upward() {
        let threshold_decimal_bps = 250; // 25bps = 0.25%
        let mut processor = RangeBarProcessor::new(threshold_decimal_bps).unwrap();

        let trades = scenarios::exact_breach_upward(threshold_decimal_bps);
        let bars = processor.process_agg_trade_records(&trades).unwrap();

        validate_breach_consistency_invariant(&bars, threshold_decimal_bps)
            .expect("Exact upward breach should satisfy invariant");
    }

    /// Test breach consistency invariant on exact downward breach scenario
    #[test]
    fn test_invariant_exact_breach_downward() {
        let threshold_decimal_bps = 250; // 25bps = 0.25%
        let mut processor = RangeBarProcessor::new(threshold_decimal_bps).unwrap();

        let trades = scenarios::exact_breach_downward(threshold_decimal_bps);
        let bars = processor.process_agg_trade_records(&trades).unwrap();

        validate_breach_consistency_invariant(&bars, threshold_decimal_bps)
            .expect("Exact downward breach should satisfy invariant");
    }

    /// Test breach consistency invariant on large price gap scenario
    #[test]
    fn test_invariant_large_gap() {
        let threshold_decimal_bps = 250; // 25bps = 0.25%
        let mut processor = RangeBarProcessor::new(threshold_decimal_bps).unwrap();

        let trades = scenarios::large_gap_sequence();
        let bars = processor.process_agg_trade_records(&trades).unwrap();

        validate_breach_consistency_invariant(&bars, threshold_decimal_bps)
            .expect("Large gap scenario should satisfy invariant");
    }

    /// Test breach consistency invariant on single breach sequence
    #[test]
    fn test_invariant_single_breach() {
        let threshold_decimal_bps = 250; // 25bps = 0.25%
        let mut processor = RangeBarProcessor::new(threshold_decimal_bps).unwrap();

        let trades = scenarios::single_breach_sequence(threshold_decimal_bps);
        let bars = processor.process_agg_trade_records(&trades).unwrap();

        validate_breach_consistency_invariant(&bars, threshold_decimal_bps)
            .expect("Single breach scenario should satisfy invariant");
    }

    /// Test breach consistency invariant on large random dataset (1M ticks)
    #[test]
    fn test_invariant_massive_realistic_dataset() {
        let threshold_decimal_bps = 250; // 25bps = 0.25%
        let mut processor = RangeBarProcessor::new(threshold_decimal_bps).unwrap();

        let trades = generators::create_massive_realistic_dataset(1_000_000);
        let bars = processor.process_agg_trade_records(&trades).unwrap();

        println!(
            "Processed {} trades into {} bars (threshold={}bps)",
            trades.len(),
            bars.len(),
            threshold_decimal_bps as f64 / 10.0
        );

        validate_breach_consistency_invariant(&bars, threshold_decimal_bps)
            .expect("Massive realistic dataset should satisfy invariant");
    }

    /// Test breach consistency invariant with multiple threshold values
    #[test]
    fn test_invariant_multiple_thresholds() {
        // Test thresholds: 0.2bps (HFT), 1bps, 10bps, 25bps, 100bps
        let thresholds = vec![2, 10, 100, 250, 1000];

        let trades = generators::create_massive_realistic_dataset(100_000);

        for threshold_decimal_bps in thresholds {
            let mut processor = RangeBarProcessor::new(threshold_decimal_bps).unwrap();
            let bars = processor.process_agg_trade_records(&trades).unwrap();

            validate_breach_consistency_invariant(&bars, threshold_decimal_bps).unwrap_or_else(
                |err| {
                    panic!(
                        "Invariant violation at threshold {}bps: {}",
                        threshold_decimal_bps as f64 / 10.0,
                        err
                    )
                },
            );

            println!(
                "✓ Threshold {}bps: {} bars generated, all satisfy invariant",
                threshold_decimal_bps as f64 / 10.0,
                bars.len()
            );
        }
    }

    /// Test breach consistency invariant on multi-day boundary dataset
    #[test]
    fn test_invariant_multi_day_boundaries() {
        let threshold_decimal_bps = 250; // 25bps = 0.25%
        let mut processor = RangeBarProcessor::new(threshold_decimal_bps).unwrap();

        let trades = generators::create_multi_day_boundary_dataset(7); // 7 days
        let bars = processor.process_agg_trade_records(&trades).unwrap();

        println!(
            "Multi-day dataset: {} trades → {} bars",
            trades.len(),
            bars.len()
        );

        validate_breach_consistency_invariant(&bars, threshold_decimal_bps)
            .expect("Multi-day boundary dataset should satisfy invariant");
    }

    /// Test breach consistency invariant on volatile market conditions
    #[test]
    fn test_invariant_volatile_conditions() {
        let threshold_decimal_bps = 250; // 25bps = 0.25%
        let mut processor = RangeBarProcessor::new(threshold_decimal_bps).unwrap();

        let base_time = 1609459200000; // 2021-01-01 00:00:00
        let trades = generators::create_volatile_day_data(base_time, 10_000);
        let bars = processor.process_agg_trade_records(&trades).unwrap();

        println!(
            "Volatile day: {} trades → {} bars",
            trades.len(),
            bars.len()
        );

        validate_breach_consistency_invariant(&bars, threshold_decimal_bps)
            .expect("Volatile market conditions should satisfy invariant");
    }

    /// Test breach consistency invariant on stable market conditions
    #[test]
    fn test_invariant_stable_conditions() {
        let threshold_decimal_bps = 250; // 25bps = 0.25%
        let mut processor = RangeBarProcessor::new(threshold_decimal_bps).unwrap();

        let base_time = 1609459200000; // 2021-01-01 00:00:00
        let trades = generators::create_stable_day_data(base_time, 10_000);
        let bars = processor.process_agg_trade_records(&trades).unwrap();

        println!("Stable day: {} trades → {} bars", trades.len(), bars.len());

        validate_breach_consistency_invariant(&bars, threshold_decimal_bps)
            .expect("Stable market conditions should satisfy invariant");
    }

    /// Test breach consistency invariant on trending market conditions
    #[test]
    fn test_invariant_trending_conditions() {
        let threshold_decimal_bps = 250; // 25bps = 0.25%
        let mut processor = RangeBarProcessor::new(threshold_decimal_bps).unwrap();

        let base_time = 1609459200000; // 2021-01-01 00:00:00
        let trades = generators::create_trending_day_data(base_time, 10_000);
        let bars = processor.process_agg_trade_records(&trades).unwrap();

        println!(
            "Trending day: {} trades → {} bars",
            trades.len(),
            bars.len()
        );

        validate_breach_consistency_invariant(&bars, threshold_decimal_bps)
            .expect("Trending market conditions should satisfy invariant");
    }

    /// Test breach consistency invariant on high-frequency data
    #[test]
    fn test_invariant_high_frequency() {
        let threshold_decimal_bps = 100; // 10bps = 0.1% (tight for HFT)
        let mut processor = RangeBarProcessor::new(threshold_decimal_bps).unwrap();

        let trades = generators::create_high_frequency_data(10); // 10ms interval
        let bars = processor.process_agg_trade_records(&trades).unwrap();

        println!(
            "High-frequency (10ms): {} trades → {} bars",
            trades.len(),
            bars.len()
        );

        validate_breach_consistency_invariant(&bars, threshold_decimal_bps)
            .expect("High-frequency data should satisfy invariant");
    }

    /// Test breach consistency invariant on low-frequency data
    #[test]
    fn test_invariant_low_frequency() {
        let threshold_decimal_bps = 250; // 25bps = 0.25%
        let mut processor = RangeBarProcessor::new(threshold_decimal_bps).unwrap();

        let trades = generators::create_low_frequency_data(60_000); // 1min interval
        let bars = processor.process_agg_trade_records(&trades).unwrap();

        println!(
            "Low-frequency (1min): {} trades → {} bars",
            trades.len(),
            bars.len()
        );

        validate_breach_consistency_invariant(&bars, threshold_decimal_bps)
            .expect("Low-frequency data should satisfy invariant");
    }

    /// Test breach consistency invariant on mixed-frequency data
    #[test]
    fn test_invariant_mixed_frequency() {
        let threshold_decimal_bps = 250; // 25bps = 0.25%
        let mut processor = RangeBarProcessor::new(threshold_decimal_bps).unwrap();

        let trades = generators::create_mixed_frequency_data();
        let bars = processor.process_agg_trade_records(&trades).unwrap();

        println!(
            "Mixed-frequency: {} trades → {} bars",
            trades.len(),
            bars.len()
        );

        validate_breach_consistency_invariant(&bars, threshold_decimal_bps)
            .expect("Mixed-frequency data should satisfy invariant");
    }

    /// Test breach consistency invariant on rapid threshold hits
    #[test]
    fn test_invariant_rapid_threshold_hits() {
        let threshold_decimal_bps = 50; // 5bps = 0.05% (tight threshold)
        let mut processor = RangeBarProcessor::new(threshold_decimal_bps).unwrap();

        let trades = generators::create_rapid_threshold_hit_data();
        let bars = processor.process_agg_trade_records(&trades).unwrap();

        println!(
            "Rapid threshold hits: {} trades → {} bars",
            trades.len(),
            bars.len()
        );

        validate_breach_consistency_invariant(&bars, threshold_decimal_bps)
            .expect("Rapid threshold hits should satisfy invariant");
    }

    /// Test breach consistency invariant on precision limit edge cases
    #[test]
    fn test_invariant_precision_limits() {
        let threshold_decimal_bps = 1; // 0.1bps = 0.001% (minimum threshold)
        let mut processor = RangeBarProcessor::new(threshold_decimal_bps).unwrap();

        let trades = generators::create_precision_limit_data();
        let bars = processor.process_agg_trade_records(&trades).unwrap();

        println!(
            "Precision limits (0.1bps): {} trades → {} bars",
            trades.len(),
            bars.len()
        );

        validate_breach_consistency_invariant(&bars, threshold_decimal_bps)
            .expect("Precision limit data should satisfy invariant");
    }

    /// Test breach consistency invariant on volume extreme edge cases
    #[test]
    fn test_invariant_volume_extremes() {
        let threshold_decimal_bps = 250; // 25bps = 0.25%
        let mut processor = RangeBarProcessor::new(threshold_decimal_bps).unwrap();

        let trades = generators::create_volume_extreme_data();
        let bars = processor.process_agg_trade_records(&trades).unwrap();

        println!(
            "Volume extremes: {} trades → {} bars",
            trades.len(),
            bars.len()
        );

        validate_breach_consistency_invariant(&bars, threshold_decimal_bps)
            .expect("Volume extreme data should satisfy invariant");
    }

    /// Test breach consistency invariant on timestamp edge cases
    #[test]
    fn test_invariant_timestamp_edges() {
        let threshold_decimal_bps = 250; // 25bps = 0.25%
        let mut processor = RangeBarProcessor::new(threshold_decimal_bps).unwrap();

        let trades = generators::create_timestamp_edge_data();
        let bars = processor.process_agg_trade_records(&trades).unwrap();

        println!(
            "Timestamp edges: {} trades → {} bars",
            trades.len(),
            bars.len()
        );

        validate_breach_consistency_invariant(&bars, threshold_decimal_bps)
            .expect("Timestamp edge data should satisfy invariant");
    }

    /// Test breach consistency invariant on floating-point stress data
    #[test]
    fn test_invariant_floating_point_stress() {
        let threshold_decimal_bps = 250; // 25bps = 0.25%
        let mut processor = RangeBarProcessor::new(threshold_decimal_bps).unwrap();

        let trades = generators::create_floating_point_stress_data();
        let bars = processor.process_agg_trade_records(&trades).unwrap();

        println!(
            "Floating-point stress: {} trades → {} bars",
            trades.len(),
            bars.len()
        );

        validate_breach_consistency_invariant(&bars, threshold_decimal_bps)
            .expect("Floating-point stress data should satisfy invariant");
    }

    /// Test breach consistency invariant on custom oscillating sequence
    #[test]
    fn test_invariant_custom_oscillation() {
        let threshold_decimal_bps = 250; // 25bps = 0.25%
        let mut processor = RangeBarProcessor::new(threshold_decimal_bps).unwrap();

        // Create oscillating sequence that repeatedly approaches but doesn't breach
        let trades = AggTradeBuilder::new()
            .with_base_price(50000.0)
            .with_base_timestamp(1609459200000)
            .add_trade(1, 1.0, 1000) // 50000.00
            .add_trade(2, 1.002, 2000) // +0.2% (no breach at 25bps threshold)
            .add_trade(3, 0.998, 3000) // -0.4% from #2
            .add_trade(4, 1.002, 4000) // +0.4% from #3
            .add_trade(5, 0.998, 5000) // -0.4% from #4
            .add_trade(6, 1.0025, 6000) // +0.25% - EXACT BREACH
            .build();

        let bars = processor.process_agg_trade_records(&trades).unwrap();

        assert_eq!(bars.len(), 1, "Should create exactly 1 bar on exact breach");

        validate_breach_consistency_invariant(&bars, threshold_decimal_bps)
            .expect("Custom oscillation should satisfy invariant");
    }

    /// Test breach consistency invariant on boundary threshold values
    #[test]
    fn test_invariant_boundary_thresholds() {
        // Test minimum and maximum valid thresholds
        let boundary_thresholds = vec![
            1,       // 0.1bps = 0.001% (minimum)
            100_000, // 10,000bps = 100% (maximum)
        ];

        let trades = generators::create_massive_realistic_dataset(10_000);

        for threshold_decimal_bps in boundary_thresholds {
            let mut processor = RangeBarProcessor::new(threshold_decimal_bps).unwrap();
            let bars = processor.process_agg_trade_records(&trades).unwrap();

            validate_breach_consistency_invariant(&bars, threshold_decimal_bps).unwrap_or_else(
                |err| {
                    panic!(
                        "Invariant violation at boundary threshold {}bps: {}",
                        threshold_decimal_bps as f64 / 10.0,
                        err
                    )
                },
            );

            println!(
                "✓ Boundary threshold {}bps: {} bars, all valid",
                threshold_decimal_bps as f64 / 10.0,
                bars.len()
            );
        }
    }
}