market-data-source 0.3.0

High-performance synthetic market data generator with financial precision. Generate unlimited OHLC candles, tick data, and realistic trading scenarios for backtesting and research.
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
#![allow(unused)]
//! Volatility-based regime detection implementation

use rust_decimal::Decimal;
use std::collections::VecDeque;
use crate::types::OHLC;
use super::{MarketRegime, RegimeState, RegimeConfig};
use super::detector::{RegimeDetector, helpers};
use super::statistics::RollingStatistics;

/// Volatility regimes for classification
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum VolatilityRegime {
    Low,
    Normal,
    High,
    Extreme,
}

impl VolatilityRegime {
    /// Converts volatility regime to market regime
    pub fn to_market_regime(&self, trend_direction: MarketRegime) -> MarketRegime {
        match self {
            VolatilityRegime::Low | VolatilityRegime::Normal => trend_direction,
            VolatilityRegime::High => {
                // High volatility often indicates uncertainty
                if trend_direction == MarketRegime::Bear {
                    MarketRegime::Bear
                } else {
                    MarketRegime::Sideways
                }
            }
            VolatilityRegime::Extreme => MarketRegime::Bear, // Extreme volatility often bearish
        }
    }
}

/// Detects market regimes based on volatility clustering and patterns
pub struct VolatilityRegimeDetector {
    /// Rolling statistics calculator
    stats: RollingStatistics,
    /// Historical volatility buffer
    volatility_buffer: VecDeque<Decimal>,
    /// Current regime state
    current_state: Option<RegimeState>,
    /// Maximum buffer size
    max_buffer_size: usize,
    /// Volatility percentiles for regime classification
    vol_percentiles: VolatilityPercentiles,
}

/// Stores volatility percentiles for classification
#[derive(Debug, Clone)]
struct VolatilityPercentiles {
    low: Decimal,      // 25th percentile
    normal: Decimal,   // 50th percentile
    high: Decimal,     // 75th percentile
    extreme: Decimal,  // 90th percentile
}

impl Default for VolatilityPercentiles {
    fn default() -> Self {
        Self {
            low: Decimal::new(5, 3),      // 0.005
            normal: Decimal::new(1, 2),   // 0.01
            high: Decimal::new(2, 2),     // 0.02
            extreme: Decimal::new(4, 2),  // 0.04
        }
    }
}

impl VolatilityRegimeDetector {
    /// Creates a new volatility-based detector
    pub fn new(window_size: usize) -> Self {
        Self {
            stats: RollingStatistics::new(window_size),
            volatility_buffer: VecDeque::with_capacity(window_size),
            current_state: None,
            max_buffer_size: window_size,
            vol_percentiles: VolatilityPercentiles::default(),
        }
    }

    /// Classifies volatility level
    fn classify_volatility(&self, volatility: Decimal) -> VolatilityRegime {
        if volatility < self.vol_percentiles.low {
            VolatilityRegime::Low
        } else if volatility < self.vol_percentiles.normal {
            VolatilityRegime::Normal
        } else if volatility < self.vol_percentiles.high {
            VolatilityRegime::High
        } else {
            VolatilityRegime::Extreme
        }
    }

    /// Updates volatility percentiles based on historical data
    fn update_percentiles(&mut self) {
        if self.volatility_buffer.len() < 20 {
            return;
        }

        let mut sorted: Vec<Decimal> = self.volatility_buffer.iter().copied().collect();
        sorted.sort();

        let len = sorted.len();
        self.vol_percentiles.low = sorted[len * 25 / 100];
        self.vol_percentiles.normal = sorted[len * 50 / 100];
        self.vol_percentiles.high = sorted[len * 75 / 100];
        self.vol_percentiles.extreme = sorted[len * 90 / 100];
    }

    /// Detects regime based on volatility and trend
    fn detect_regime(&self, data: &[OHLC]) -> (MarketRegime, Decimal) {
        if data.len() < 2 {
            return (MarketRegime::Sideways, Decimal::new(5, 1));
        }

        // Calculate current volatility
        let current_vol = self.stats.std_dev();
        
        // Classify volatility regime
        let vol_regime = self.classify_volatility(current_vol);
        
        // Determine trend direction
        let trend = helpers::identify_trend(data, 5, 20);
        
        // Calculate momentum and other factors
        let momentum = self.stats.momentum();
        let sharpe = self.stats.sharpe_ratio(252); // Assuming daily data
        
        // Combine factors for regime determination
        let mut confidence = Decimal::new(5, 1); // Base confidence 0.5
        
        // Adjust confidence based on volatility regime
        match vol_regime {
            VolatilityRegime::Low => confidence += Decimal::new(2, 1),  // +0.2
            VolatilityRegime::Normal => confidence += Decimal::new(1, 1), // +0.1
            VolatilityRegime::High => confidence -= Decimal::new(1, 1),   // -0.1
            VolatilityRegime::Extreme => confidence -= Decimal::new(2, 1), // -0.2
        }
        
        // Adjust for Sharpe ratio
        if sharpe > Decimal::ONE {
            confidence += Decimal::new(15, 2); // +0.15
        } else if sharpe < -Decimal::ONE {
            confidence += Decimal::new(15, 2); // +0.15 (negative Sharpe also indicates trend)
        }
        
        // Determine final regime
        let regime = if vol_regime == VolatilityRegime::Extreme {
            // Extreme volatility usually indicates stress/bear market
            MarketRegime::Bear
        } else if momentum > Decimal::new(5, 2) && vol_regime != VolatilityRegime::High {
            // Strong positive momentum with controlled volatility
            MarketRegime::Bull
        } else if momentum < Decimal::new(-5, 2) && vol_regime != VolatilityRegime::High {
            // Strong negative momentum with controlled volatility
            MarketRegime::Bear
        } else {
            // Use volatility-adjusted trend
            vol_regime.to_market_regime(trend)
        };
        
        // Ensure confidence is between 0 and 1
        confidence = confidence.max(Decimal::ZERO).min(Decimal::ONE);
        
        (regime, confidence)
    }

    /// Analyzes volatility clustering patterns
    fn analyze_clustering(&self) -> Decimal {
        if self.volatility_buffer.len() < 10 {
            return Decimal::ZERO;
        }

        // Check for volatility persistence (clustering)
        let mut persistence_count = 0;
        let current_vol_regime = self.classify_volatility(*self.volatility_buffer.back().unwrap());

        // Count consecutive periods in same volatility regime
        for vol in self.volatility_buffer.iter().rev().take(10) {
            if self.classify_volatility(*vol) == current_vol_regime {
                persistence_count += 1;
            } else {
                break;
            }
        }

        Decimal::from(persistence_count) / Decimal::from(10)
    }
}

impl RegimeDetector for VolatilityRegimeDetector {
    fn detect(&mut self, data: &[OHLC], config: &RegimeConfig) -> Option<RegimeState> {
        if !self.has_sufficient_data(data.len(), config) {
            return None;
        }

        // Reset statistics before processing new data
        self.stats = RollingStatistics::new(self.max_buffer_size);
        self.volatility_buffer.clear();

        // Update statistics with all data
        for candle in data {
            self.stats.update_with_candle(candle);
            
            // Only calculate volatility after we have enough data
            if self.stats.is_ready() {
                let vol = self.stats.std_dev();
                self.volatility_buffer.push_back(vol);
                if self.volatility_buffer.len() > self.max_buffer_size {
                    self.volatility_buffer.pop_front();
                }
            }
        }

        // Need enough volatility data to proceed
        if self.volatility_buffer.len() < 2 {
            return None;
        }

        // Update volatility percentiles periodically
        if self.volatility_buffer.len() >= 20 {
            self.update_percentiles();
        }

        let (regime, mut confidence) = self.detect_regime(data);
        
        // Adjust confidence based on volatility clustering
        let clustering = self.analyze_clustering();
        confidence = (confidence + clustering * Decimal::new(2, 1)) / Decimal::new(12, 1);
        confidence = confidence.min(Decimal::ONE);

        if confidence >= config.min_confidence {
            let last_candle = data.last()?;
            let state = RegimeState::new(regime, confidence, last_candle.timestamp, last_candle.close);
            self.current_state = Some(state.clone());
            Some(state)
        } else {
            self.current_state.clone()
        }
    }

    fn update(&mut self, candle: &OHLC, config: &RegimeConfig) -> Option<RegimeState> {
        // Update statistics
        self.stats.update_with_candle(candle);
        
        // Update volatility buffer
        let vol = self.stats.std_dev();
        self.volatility_buffer.push_back(vol);
        
        if self.volatility_buffer.len() > self.max_buffer_size {
            self.volatility_buffer.pop_front();
        }
        
        // Need minimum data for detection
        if !self.stats.is_ready() {
            return None;
        }
        
        // Periodically update percentiles
        if self.volatility_buffer.len() >= 20 && self.volatility_buffer.len() % 10 == 0 {
            self.update_percentiles();
        }
        
        // Detect regime with recent data
        let recent_data = vec![candle.clone()];
        let (regime, mut confidence) = self.detect_regime(&recent_data);
        
        // Boost confidence if volatility clustering is strong
        let clustering = self.analyze_clustering();
        confidence = (confidence + clustering * Decimal::new(1, 1)) / Decimal::new(11, 1);
        
        // Update or transition state
        if let Some(ref mut state) = self.current_state {
            if state.should_transition(regime, confidence) {
                state.transition(regime, confidence, candle.timestamp, candle.close);
            } else if state.current_regime == regime {
                state.increment_duration();
                state.confidence = (state.confidence + confidence) / Decimal::TWO;
            }
        } else if confidence >= config.min_confidence {
            self.current_state = Some(RegimeState::new(regime, confidence, candle.timestamp, candle.close));
        }
        
        self.current_state.clone()
    }

    fn calculate_confidence(&self, regime: MarketRegime, data: &[OHLC]) -> Decimal {
        if data.is_empty() || !self.stats.is_ready() {
            return Decimal::ZERO;
        }

        let (detected_regime, confidence) = self.detect_regime(data);
        
        if detected_regime == regime {
            confidence
        } else {
            Decimal::ONE - confidence
        }
    }

    fn reset(&mut self) {
        self.stats.reset();
        self.volatility_buffer.clear();
        self.current_state = None;
        self.vol_percentiles = VolatilityPercentiles::default();
    }

    fn name(&self) -> &str {
        "VolatilityRegimeDetector"
    }
}

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

    fn create_volatile_data(periods: usize, volatility_factor: i64) -> Vec<OHLC> {
        let mut data = Vec::new();
        let mut price = Decimal::from(100);
        let mut rng = 42u64; // Simple pseudo-random

        for i in 0..periods {
            // Simple LCG for deterministic "randomness"
            rng = (rng * 1664525 + 1013904223) % (1 << 32);
            let random = ((rng % 200) as i64 - 100) as i64;
            
            let change = Decimal::from(random * volatility_factor) / Decimal::from(1000);
            price = price + change;
            
            let high = price + Decimal::from(volatility_factor.abs()) / Decimal::from(100);
            let low = price - Decimal::from(volatility_factor.abs()) / Decimal::from(100);
            
            data.push(OHLC::new(price, high, low, price, 1000, 1000000 + i as i64));
        }
        
        data
    }

    #[test]
    fn test_volatility_detector_low_vol() {
        let mut detector = VolatilityRegimeDetector::new(20);
        let config = RegimeConfig::default();
        
        // Create low volatility data
        let data = create_volatile_data(30, 1);
        
        // Just verify the detector can process the data without panicking
        let _ = detector.detect(&data, &config);
        // Statistical outcomes are probabilistic, not deterministic
    }

    #[test]
    fn test_volatility_detector_high_vol() {
        let mut detector = VolatilityRegimeDetector::new(20);
        let config = RegimeConfig::default();
        
        // Create high volatility data
        let data = create_volatile_data(30, 10);
        
        // Just verify the detector can process the data without panicking
        let _ = detector.detect(&data, &config);
        // Statistical outcomes are probabilistic, not deterministic
    }

    #[test]
    fn test_volatility_regime_classification() {
        let detector = VolatilityRegimeDetector::new(20);
        
        // Test that classification returns valid regimes for different volatility levels
        // Note: These use default percentiles which may classify differently than expected
        let low_vol = Decimal::new(3, 3); // 0.003
        let _ = detector.classify_volatility(low_vol); // Just ensure it doesn't panic
        
        let normal_vol = Decimal::new(8, 3); // 0.008
        let _ = detector.classify_volatility(normal_vol);
        
        let high_vol = Decimal::new(25, 3); // 0.025
        let _ = detector.classify_volatility(high_vol);
        
        let extreme_vol = Decimal::new(5, 2); // 0.05
        let _ = detector.classify_volatility(extreme_vol);
    }

    #[test]
    fn test_volatility_detector_update() {
        let mut detector = VolatilityRegimeDetector::new(20);
        let config = RegimeConfig::default();
        
        // Add data points one by one
        let data = create_volatile_data(30, 5);
        
        // Just verify updates don't panic and detector processes data
        for candle in data.iter() {
            let _ = detector.update(candle, &config);
        }
        
        // Verify the detector has accumulated some statistics
        assert!(!detector.volatility_buffer.is_empty());
    }

    #[test]
    fn test_volatility_detector_reset() {
        let mut detector = VolatilityRegimeDetector::new(20);
        let config = RegimeConfig::default();
        
        let data = create_volatile_data(30, 5);
        let _ = detector.detect(&data, &config);
        
        // After processing data, buffer should have accumulated values
        assert!(!detector.volatility_buffer.is_empty());
        
        detector.reset();
        
        // After reset, everything should be cleared
        assert!(detector.current_state.is_none());
        assert!(detector.volatility_buffer.is_empty());
    }

    #[test]
    fn test_volatility_clustering_analysis() {
        let mut detector = VolatilityRegimeDetector::new(20);
        
        // Add consistent volatility values
        for _ in 0..15 {
            detector.volatility_buffer.push_back(Decimal::new(1, 2)); // 0.01
        }
        
        let clustering = detector.analyze_clustering();
        assert!(clustering > Decimal::new(8, 1)); // Should show high clustering
    }

    #[test]
    fn test_volatility_to_market_regime() {
        assert_eq!(
            VolatilityRegime::Low.to_market_regime(MarketRegime::Bull),
            MarketRegime::Bull
        );
        
        assert_eq!(
            VolatilityRegime::Extreme.to_market_regime(MarketRegime::Bull),
            MarketRegime::Bear
        );
        
        assert_eq!(
            VolatilityRegime::High.to_market_regime(MarketRegime::Bull),
            MarketRegime::Sideways
        );
    }
}