eeyf 0.1.0

Eric Evans' Yahoo Finance API - A rate-limited, reliable Rust adapter for Yahoo Finance API
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
//! Data validation utilities for financial data.
//!
//! This module provides utilities for validating quote data integrity,
//! detecting anomalies, and filling missing data.

use crate::quotes::decimal::{Decimal, ZERO, ONE, from_usize};
use crate::quotes::Quote;

/// Validation errors that can occur during data validation
#[derive(Debug, Clone, PartialEq)]
pub enum ValidationError {
    /// Missing required data
    MissingData { field: String, index: usize },
    /// Invalid price (negative or zero)
    InvalidPrice { index: usize, price: Decimal },
    /// OHLC data inconsistency (e.g., high < low)
    OhlcInconsistency { index: usize, reason: String },
    /// Timestamp out of order
    TimestampOutOfOrder { index: usize },
    /// Duplicate timestamp
    DuplicateTimestamp { index: usize, timestamp: i64 },
    /// Excessive gap in data
    DataGap { start_index: usize, gap_seconds: i64 },
    /// Anomalous value detected
    Anomaly { index: usize, reason: String },
}

impl std::fmt::Display for ValidationError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            ValidationError::MissingData { field, index } => {
                write!(f, "Missing data for field '{}' at index {}", field, index)
            }
            ValidationError::InvalidPrice { index, price } => {
                write!(f, "Invalid price {} at index {}", price, index)
            }
            ValidationError::OhlcInconsistency { index, reason } => {
                write!(f, "OHLC inconsistency at index {}: {}", index, reason)
            }
            ValidationError::TimestampOutOfOrder { index } => {
                write!(f, "Timestamp out of order at index {}", index)
            }
            ValidationError::DuplicateTimestamp { index, timestamp } => {
                write!(f, "Duplicate timestamp {} at index {}", timestamp, index)
            }
            ValidationError::DataGap {
                start_index,
                gap_seconds,
            } => {
                write!(
                    f,
                    "Data gap of {} seconds starting at index {}",
                    gap_seconds, start_index
                )
            }
            ValidationError::Anomaly { index, reason } => {
                write!(f, "Anomaly detected at index {}: {}", index, reason)
            }
        }
    }
}

impl std::error::Error for ValidationError {}

/// Result of validation check
#[derive(Debug)]
pub struct ValidationResult {
    /// Whether validation passed
    pub valid: bool,
    /// List of validation errors found
    pub errors: Vec<ValidationError>,
    /// List of warnings (non-critical issues)
    pub warnings: Vec<String>,
}

impl ValidationResult {
    /// Creates a successful validation result
    pub fn success() -> Self {
        Self {
            valid: true,
            errors: Vec::new(),
            warnings: Vec::new(),
        }
    }

    /// Creates a failed validation result
    pub fn failure(errors: Vec<ValidationError>) -> Self {
        Self {
            valid: false,
            errors,
            warnings: Vec::new(),
        }
    }

    /// Adds a warning to the result
    pub fn with_warning(mut self, warning: String) -> Self {
        self.warnings.push(warning);
        self
    }
}

/// Validates quote data integrity
///
/// Checks for:
/// - Valid OHLC relationships (high >= low, open/close within high/low)
/// - Non-negative prices
/// - Timestamps in order
/// - No duplicate timestamps
///
/// # Example
/// ```
/// use eeyf::validate::validate_quotes;
/// use eeyf::quotes::Quote;
/// use rust_decimal::Decimal;
///
/// let quotes = vec![
///     Quote {
///         timestamp: 1000,
///         open: Decimal::new(100, 0),
///         high: Decimal::new(105, 0),
///         low: Decimal::new(99, 0),
///         close: Decimal::new(103, 0),
///         volume: 1000,
///         adjclose: Decimal::new(103, 0),
///     },
/// ];
///
/// let result = validate_quotes(&quotes);
/// assert!(result.valid);
/// ```
pub fn validate_quotes(quotes: &[Quote]) -> ValidationResult {
    let mut errors = Vec::new();
    let mut warnings = Vec::new();

    if quotes.is_empty() {
        warnings.push("Empty quote list".to_string());
        return ValidationResult {
            valid: true,
            errors,
            warnings,
        };
    }

    for (i, quote) in quotes.iter().enumerate() {
        // Check for valid OHLC relationships
        if quote.high < quote.low {
            errors.push(ValidationError::OhlcInconsistency {
                index: i,
                reason: format!("High ({}) < Low ({})", quote.high, quote.low),
            });
        }

        if quote.high < quote.open {
            errors.push(ValidationError::OhlcInconsistency {
                index: i,
                reason: format!("High ({}) < Open ({})", quote.high, quote.open),
            });
        }

        if quote.high < quote.close {
            errors.push(ValidationError::OhlcInconsistency {
                index: i,
                reason: format!("High ({}) < Close ({})", quote.high, quote.close),
            });
        }

        if quote.low > quote.open {
            errors.push(ValidationError::OhlcInconsistency {
                index: i,
                reason: format!("Low ({}) > Open ({})", quote.low, quote.open),
            });
        }

        if quote.low > quote.close {
            errors.push(ValidationError::OhlcInconsistency {
                index: i,
                reason: format!("Low ({}) > Close ({})", quote.low, quote.close),
            });
        }

        // Check for non-negative prices
        if quote.open <= Decimal::ZERO {
            errors.push(ValidationError::InvalidPrice {
                index: i,
                price: quote.open,
            });
        }

        if quote.high <= Decimal::ZERO {
            errors.push(ValidationError::InvalidPrice {
                index: i,
                price: quote.high,
            });
        }

        if quote.low <= Decimal::ZERO {
            errors.push(ValidationError::InvalidPrice {
                index: i,
                price: quote.low,
            });
        }

        if quote.close <= Decimal::ZERO {
            errors.push(ValidationError::InvalidPrice {
                index: i,
                price: quote.close,
            });
        }

        // Check timestamp order
        if i > 0 {
            let prev_timestamp = quotes[i - 1].timestamp;
            if quote.timestamp < prev_timestamp {
                errors.push(ValidationError::TimestampOutOfOrder { index: i });
            } else if quote.timestamp == prev_timestamp {
                errors.push(ValidationError::DuplicateTimestamp {
                    index: i,
                    timestamp: quote.timestamp,
                });
            }
        }
    }

    let valid = errors.is_empty();
    ValidationResult {
        valid,
        errors,
        warnings,
    }
}

/// Detects anomalies in quote data using statistical methods
///
/// Uses the Interquartile Range (IQR) method to detect outliers
///
/// # Example
/// ```
/// use eeyf::validate::detect_anomalies;
/// use eeyf::quotes::Quote;
/// use rust_decimal::Decimal;
///
/// let quotes = vec![
///     Quote {
///         timestamp: 1000,
///         open: Decimal::new(100, 0),
///         high: Decimal::new(105, 0),
///         low: Decimal::new(99, 0),
///         close: Decimal::new(103, 0),
///         volume: 1000,
///         adjclose: Decimal::new(103, 0),
///     },
///     // More quotes...
/// ];
///
/// let anomalies = detect_anomalies(&quotes, 3.0);
/// ```
pub fn detect_anomalies(quotes: &[Quote], threshold: f64) -> Vec<ValidationError> {
    if quotes.len() < 4 {
        return Vec::new(); // Need at least 4 points for IQR
    }

    let mut errors = Vec::new();
    let mut prices: Vec<f64> = quotes.iter().map(|q| q.close).collect();
    prices.sort_by(|a: &f64, b: &f64| a.partial_cmp(b).unwrap());

    // Calculate IQR
    let q1_idx = prices.len() / 4;
    let q3_idx = (prices.len() * 3) / 4;
    let q1 = prices[q1_idx];
    let q3 = prices[q3_idx];
    let iqr = q3 - q1;

    let lower_bound = q1 - threshold * iqr;
    let upper_bound = q3 + threshold * iqr;

    // Check each quote for anomalies
    for (i, quote) in quotes.iter().enumerate() {
        let price = quote.close;
        if price < lower_bound {
            errors.push(ValidationError::Anomaly {
                index: i,
                reason: format!(
                    "Price {} below lower bound {:.2}",
                    quote.close, lower_bound
                ),
            });
        } else if price > upper_bound {
            errors.push(ValidationError::Anomaly {
                index: i,
                reason: format!(
                    "Price {} above upper bound {:.2}",
                    quote.close, upper_bound
                ),
            });
        }

        // Check for excessive volume anomalies
        if i > 0 {
            let avg_volume = quotes.iter().map(|q| q.volume as f64).sum::<f64>()
                / quotes.len() as f64;
            if quote.volume as f64 > avg_volume * 10.0 {
                errors.push(ValidationError::Anomaly {
                    index: i,
                    reason: format!("Volume {} is 10x average volume", quote.volume),
                });
            }
        }
    }

    errors
}

/// Detects gaps in time series data
///
/// # Example
/// ```
/// use eeyf::validate::detect_gaps;
/// use eeyf::quotes::Quote;
/// use rust_decimal::Decimal;
///
/// let quotes = vec![
///     Quote {
///         timestamp: 1000,
///         open: Decimal::new(100, 0),
///         high: Decimal::new(105, 0),
///         low: Decimal::new(99, 0),
///         close: Decimal::new(103, 0),
///         volume: 1000,
///         adjclose: Decimal::new(103, 0),
///     },
///     Quote {
///         timestamp: 5000,  // Gap of 4000 seconds
///         open: Decimal::new(103, 0),
///         high: Decimal::new(107, 0),
///         low: Decimal::new(102, 0),
///         close: Decimal::new(106, 0),
///         volume: 1500,
///         adjclose: Decimal::new(106, 0),
///     },
/// ];
///
/// let gaps = detect_gaps(&quotes, 3600); // 1 hour threshold
/// assert_eq!(gaps.len(), 1);
/// ```
pub fn detect_gaps(quotes: &[Quote], max_gap_seconds: i64) -> Vec<ValidationError> {
    let mut errors = Vec::new();

    for i in 1..quotes.len() {
        let gap = quotes[i].timestamp - quotes[i - 1].timestamp;
        if gap > max_gap_seconds {
            errors.push(ValidationError::DataGap {
                start_index: i - 1,
                gap_seconds: gap,
            });
        }
    }

    errors
}

/// Fill methods for missing data
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FillMethod {
    /// Forward fill - use previous value
    Forward,
    /// Backward fill - use next value
    Backward,
    /// Linear interpolation
    Linear,
    /// Use zero
    Zero,
}

/// Fills missing data gaps by interpolating
///
/// # Example
/// ```
/// use eeyf::validate::{fill_gaps, FillMethod};
/// use eeyf::quotes::Quote;
/// use rust_decimal::Decimal;
///
/// let quotes = vec![
///     Quote {
///         timestamp: 1000,
///         open: Decimal::new(100, 0),
///         high: Decimal::new(105, 0),
///         low: Decimal::new(99, 0),
///         close: Decimal::new(103, 0),
///         volume: 1000,
///         adjclose: Decimal::new(103, 0),
///     },
///     Quote {
///         timestamp: 3000,  // Gap
///         open: Decimal::new(106, 0),
///         high: Decimal::new(110, 0),
///         low: Decimal::new(105, 0),
///         close: Decimal::new(109, 0),
///         volume: 1500,
///         adjclose: Decimal::new(109, 0),
///     },
/// ];
///
/// let filled = fill_gaps(&quotes, 600, FillMethod::Linear);
/// ```
pub fn fill_gaps(quotes: &[Quote], interval_seconds: i64, method: FillMethod) -> Vec<Quote> {
    if quotes.is_empty() {
        return Vec::new();
    }

    let mut result = Vec::new();
    result.push(quotes[0].clone());

    for i in 1..quotes.len() {
        let prev = &quotes[i - 1];
        let curr = &quotes[i];
        let gap = curr.timestamp - prev.timestamp;

        if gap > interval_seconds {
            // Fill the gap
            let num_missing = ((gap / interval_seconds) - 1) as usize;
            for j in 1..=num_missing {
                let ts = prev.timestamp + (j as i64 * interval_seconds);
                let filled_quote = match method {
                    FillMethod::Forward => Quote {
                        timestamp: ts,
                        open: prev.close,
                        high: prev.close,
                        low: prev.close,
                        close: prev.close,
                        volume: 0,
                        adjclose: prev.adjclose,
                    },
                    FillMethod::Backward => Quote {
                        timestamp: ts,
                        open: curr.open,
                        high: curr.open,
                        low: curr.open,
                        close: curr.open,
                        volume: 0,
                        adjclose: curr.adjclose,
                    },
                    FillMethod::Linear => {
                        let ratio = Decimal::from(j) / Decimal::from(num_missing + 1);
                        let interpolated = prev.close + (curr.close - prev.close) * ratio;
                        Quote {
                            timestamp: ts,
                            open: interpolated,
                            high: interpolated,
                            low: interpolated,
                            close: interpolated,
                            volume: 0,
                            adjclose: interpolated,
                        }
                    }
                    FillMethod::Zero => Quote {
                        timestamp: ts,
                        open: Decimal::ZERO,
                        high: Decimal::ZERO,
                        low: Decimal::ZERO,
                        close: Decimal::ZERO,
                        volume: 0,
                        adjclose: Decimal::ZERO,
                    },
                };
                result.push(filled_quote);
            }
        }

        result.push(curr.clone());
    }

    result
}

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

    fn create_valid_quote(timestamp: i64, close: i64) -> Quote {
        Quote {
            timestamp,
            open: Decimal::new(close, 0),
            high: Decimal::new(close + 5, 0),
            low: Decimal::new(close - 5, 0),
            close: Decimal::new(close, 0),
            volume: 1000,
            adjclose: Decimal::new(close, 0),
        }
    }

    #[test]
    fn test_validate_quotes_success() {
        let quotes = vec![create_valid_quote(1000, 100), create_valid_quote(2000, 105)];

        let result = validate_quotes(&quotes);
        assert!(result.valid);
        assert_eq!(result.errors.len(), 0);
    }

    #[test]
    fn test_validate_quotes_ohlc_inconsistency() {
        let mut quote = create_valid_quote(1000, 100);
        quote.high = Decimal::new(90, 0); // High < Low

        let result = validate_quotes(&[quote]);
        assert!(!result.valid);
        assert!(result.errors.len() > 0);
    }

    #[test]
    fn test_validate_quotes_negative_price() {
        let mut quote = create_valid_quote(1000, 100);
        quote.close = Decimal::new(-10, 0);

        let result = validate_quotes(&[quote]);
        assert!(!result.valid);
        assert!(result.errors.iter().any(|e| matches!(
            e,
            ValidationError::InvalidPrice { .. }
        )));
    }

    #[test]
    fn test_validate_quotes_out_of_order() {
        let quotes = vec![create_valid_quote(2000, 100), create_valid_quote(1000, 105)];

        let result = validate_quotes(&quotes);
        assert!(!result.valid);
        assert!(result.errors.iter().any(|e| matches!(
            e,
            ValidationError::TimestampOutOfOrder { .. }
        )));
    }

    #[test]
    fn test_validate_quotes_duplicate_timestamp() {
        let quotes = vec![create_valid_quote(1000, 100), create_valid_quote(1000, 105)];

        let result = validate_quotes(&quotes);
        assert!(!result.valid);
        assert!(result.errors.iter().any(|e| matches!(
            e,
            ValidationError::DuplicateTimestamp { .. }
        )));
    }

    #[test]
    fn test_detect_gaps() {
        let quotes = vec![create_valid_quote(1000, 100), create_valid_quote(5000, 105)];

        let gaps = detect_gaps(&quotes, 1000);
        assert_eq!(gaps.len(), 1);
    }

    #[test]
    fn test_fill_gaps_forward() {
        let quotes = vec![create_valid_quote(1000, 100), create_valid_quote(3000, 110)];

        let filled = fill_gaps(&quotes, 1000, FillMethod::Forward);
        assert_eq!(filled.len(), 3); // Original 2 + 1 filled
        assert_eq!(filled[1].timestamp, 2000);
        assert_eq!(filled[1].close, Decimal::new(100, 0)); // Forward filled
    }

    #[test]
    fn test_fill_gaps_linear() {
        let quotes = vec![create_valid_quote(1000, 100), create_valid_quote(3000, 110)];

        let filled = fill_gaps(&quotes, 1000, FillMethod::Linear);
        assert_eq!(filled.len(), 3);
        assert_eq!(filled[1].timestamp, 2000);
        assert_eq!(filled[1].close, Decimal::new(105, 0)); // Linear interpolation
    }

    #[test]
    fn test_detect_anomalies() {
        let quotes = vec![
            create_valid_quote(1000, 100),
            create_valid_quote(2000, 102),
            create_valid_quote(3000, 101),
            create_valid_quote(4000, 103),
            create_valid_quote(5000, 500), // Anomaly
        ];

        let anomalies = detect_anomalies(&quotes, 1.5);
        assert!(anomalies.len() > 0);
    }
}