//! Tick Bar Sampling Tests (TDD Methodology) //! //! Tests for tick-based bar sampling following Agent B3 specifications: //! - Aggregate every N ticks //! - Performance target: <50μs per bar //! - Edge cases: irregular tick timing, volume variations use chrono::{DateTime, TimeZone, Utc}; use ml::features::alternative_bars::{OHLCVBar, TickBarSampler}; use std::time::Instant; fn create_timestamp(secs: i64) -> DateTime { Utc.timestamp_opt(secs, 0).unwrap() } #[test] fn test_tick_bar_sampler_initialization() { let sampler = TickBarSampler::new(100); assert_eq!(sampler.threshold(), 100); assert_eq!(sampler.tick_count(), 0); } #[test] fn test_tick_bar_formation_exact_threshold() { let mut sampler = TickBarSampler::new(3); let ts1 = create_timestamp(1000); let ts2 = create_timestamp(1001); let ts3 = create_timestamp(1002); // Tick 1: No bar let result = sampler.update(100.0, 10.0, ts1); assert!(result.is_none()); assert_eq!(sampler.tick_count(), 1); // Tick 2: No bar let result = sampler.update(101.0, 15.0, ts2); assert!(result.is_none()); assert_eq!(sampler.tick_count(), 2); // Tick 3: Bar complete let result = sampler.update(99.0, 20.0, ts3); assert!(result.is_some()); let bar = result.unwrap(); assert_eq!(bar.timestamp, ts1); // First tick timestamp assert_eq!(bar.open, 100.0); assert_eq!(bar.high, 101.0); assert_eq!(bar.low, 99.0); assert_eq!(bar.close, 99.0); assert_eq!(bar.volume, 45.0); // 10 + 15 + 20 // Sampler should reset assert_eq!(sampler.tick_count(), 0); } #[test] fn test_tick_bar_ohlcv_calculation() { let mut sampler = TickBarSampler::new(5); let ts = create_timestamp(1000); // Sequence: 100, 105 (high), 95 (low), 102, 98 (close) sampler.update(100.0, 10.0, ts); sampler.update(105.0, 20.0, ts); sampler.update(95.0, 15.0, ts); sampler.update(102.0, 25.0, ts); let result = sampler.update(98.0, 30.0, ts); assert!(result.is_some()); let bar = result.unwrap(); assert_eq!(bar.open, 100.0); assert_eq!(bar.high, 105.0); assert_eq!(bar.low, 95.0); assert_eq!(bar.close, 98.0); assert_eq!(bar.volume, 100.0); // 10+20+15+25+30 } #[test] fn test_tick_bar_multiple_bars() { let mut sampler = TickBarSampler::new(2); let ts1 = create_timestamp(1000); let ts2 = create_timestamp(1001); let ts3 = create_timestamp(1002); // First bar: ticks 1-2 assert!(sampler.update(100.0, 10.0, ts1).is_none()); let bar1 = sampler.update(101.0, 20.0, ts1).unwrap(); assert_eq!(bar1.open, 100.0); assert_eq!(bar1.close, 101.0); assert_eq!(bar1.volume, 30.0); // Second bar: ticks 3-4 assert!(sampler.update(102.0, 15.0, ts2).is_none()); let bar2 = sampler.update(99.0, 25.0, ts3).unwrap(); assert_eq!(bar2.open, 102.0); assert_eq!(bar2.close, 99.0); assert_eq!(bar2.volume, 40.0); } #[test] fn test_tick_bar_irregular_timing() { let mut sampler = TickBarSampler::new(3); // Irregular time intervals: 1s, 10s, 100s let ts1 = create_timestamp(1000); let ts2 = create_timestamp(1001); // +1s let ts3 = create_timestamp(1011); // +10s sampler.update(100.0, 10.0, ts1); sampler.update(101.0, 20.0, ts2); let result = sampler.update(102.0, 30.0, ts3); assert!(result.is_some()); let bar = result.unwrap(); // Should use first tick timestamp regardless of gaps assert_eq!(bar.timestamp, ts1); assert_eq!(bar.open, 100.0); assert_eq!(bar.close, 102.0); } #[test] fn test_tick_bar_varying_volumes() { let mut sampler = TickBarSampler::new(4); let ts = create_timestamp(1000); // Volumes: 1, 100, 0.5, 1000 (wide range) sampler.update(100.0, 1.0, ts); sampler.update(101.0, 100.0, ts); sampler.update(99.0, 0.5, ts); let result = sampler.update(102.0, 1000.0, ts); assert!(result.is_some()); let bar = result.unwrap(); assert_eq!(bar.volume, 1101.5); // Should handle all volumes correctly } #[test] fn test_tick_bar_single_price_level() { let mut sampler = TickBarSampler::new(3); let ts = create_timestamp(1000); // All ticks at same price (edge case) sampler.update(100.0, 10.0, ts); sampler.update(100.0, 20.0, ts); let result = sampler.update(100.0, 30.0, ts); assert!(result.is_some()); let bar = result.unwrap(); // OHLC should all equal the constant price assert_eq!(bar.open, 100.0); assert_eq!(bar.high, 100.0); assert_eq!(bar.low, 100.0); assert_eq!(bar.close, 100.0); assert_eq!(bar.volume, 60.0); } #[test] fn test_tick_bar_zero_volume_ticks() { let mut sampler = TickBarSampler::new(3); let ts = create_timestamp(1000); // Some ticks with zero volume (valid in real markets) sampler.update(100.0, 10.0, ts); sampler.update(101.0, 0.0, ts); // Zero volume let result = sampler.update(99.0, 20.0, ts); assert!(result.is_some()); let bar = result.unwrap(); assert_eq!(bar.volume, 30.0); // 10 + 0 + 20 assert_eq!(bar.high, 101.0); // Zero-volume tick still affects price } #[test] fn test_tick_bar_performance_target_50us() { // Performance test: <50μs per bar (Agent B3 requirement) let mut sampler = TickBarSampler::new(100); let ts = create_timestamp(1000); let start = Instant::now(); let iterations = 1000; // Form 10 bars (100 ticks each) for i in 0..iterations { let price = 100.0 + (i as f64 * 0.1); let volume = 10.0; sampler.update(price, volume, ts); } let duration = start.elapsed(); let avg_time_per_tick = duration.as_micros() / iterations; println!("Average time per tick: {}μs", avg_time_per_tick); println!("Time per bar (100 ticks): {}μs", avg_time_per_tick * 100); // Target: <50μs per bar = <0.5μs per tick (100 ticks per bar) // We use 1μs per tick as generous allowance (100μs per bar worst case) assert!( avg_time_per_tick < 1, "Tick processing too slow: {}μs per tick (target: <1μs)", avg_time_per_tick ); } #[test] fn test_tick_bar_large_threshold() { // Test with larger threshold (e.g., 1000 ticks per bar) let mut sampler = TickBarSampler::new(1000); let ts = create_timestamp(1000); // Process 999 ticks - should not produce bar for i in 0..999 { let price = 100.0 + (i as f64 * 0.01); let result = sampler.update(price, 10.0, ts); assert!(result.is_none()); } // 1000th tick - should produce bar let result = sampler.update(109.99, 10.0, ts); assert!(result.is_some()); let bar = result.unwrap(); assert_eq!(bar.open, 100.0); assert_eq!(bar.close, 109.99); assert_eq!(bar.volume, 10000.0); // 1000 * 10.0 } #[test] fn test_tick_bar_extreme_price_movements() { let mut sampler = TickBarSampler::new(3); let ts = create_timestamp(1000); // Extreme price movements (flash crash scenario) sampler.update(100.0, 10.0, ts); sampler.update(50.0, 20.0, ts); // -50% drop let result = sampler.update(150.0, 30.0, ts); // +200% spike assert!(result.is_some()); let bar = result.unwrap(); assert_eq!(bar.open, 100.0); assert_eq!(bar.high, 150.0); assert_eq!(bar.low, 50.0); assert_eq!(bar.close, 150.0); } #[test] fn test_tick_bar_timestamp_preservation() { let mut sampler = TickBarSampler::new(2); // Each bar should use first tick's timestamp let ts1 = create_timestamp(1000); let ts2 = create_timestamp(2000); let ts3 = create_timestamp(3000); sampler.update(100.0, 10.0, ts1); let bar1 = sampler.update(101.0, 20.0, ts2).unwrap(); assert_eq!(bar1.timestamp, ts1); // First tick of bar sampler.update(102.0, 30.0, ts2); let bar2 = sampler.update(103.0, 40.0, ts3).unwrap(); assert_eq!(bar2.timestamp, ts2); // First tick of second bar } #[test] fn test_tick_bar_threshold_one() { // Edge case: threshold = 1 (every tick is a bar) let mut sampler = TickBarSampler::new(1); let ts = create_timestamp(1000); let result = sampler.update(100.0, 10.0, ts); assert!(result.is_some()); let bar = result.unwrap(); assert_eq!(bar.open, 100.0); assert_eq!(bar.high, 100.0); assert_eq!(bar.low, 100.0); assert_eq!(bar.close, 100.0); assert_eq!(bar.volume, 10.0); } #[test] fn test_tick_bar_continuous_bars() { // Test forming multiple bars in sequence without interruption let mut sampler = TickBarSampler::new(2); let ts = create_timestamp(1000); let mut bar_count = 0; for i in 0..10 { let price = 100.0 + (i as f64); let volume = 10.0 + (i as f64); if let Some(_bar) = sampler.update(price, volume, ts) { bar_count += 1; } } // 10 ticks with threshold 2 = 5 bars assert_eq!(bar_count, 5); // Should have 0 ticks remaining assert_eq!(sampler.tick_count(), 0); } #[test] #[should_panic(expected = "Threshold must be greater than 0")] fn test_tick_bar_zero_threshold_panics() { TickBarSampler::new(0); }