File size: 2,127 Bytes
59da845
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
use std::str::FromStr;

#[derive(Debug, Clone)]
pub struct Ohlcv {
    pub time: String,
    pub open: f64,
    pub high: f64,
    pub low: f64,
    pub close: f64,
    pub tick_volume: u64,
    pub spread: u32,
}

#[derive(Debug, Clone)]
pub struct Tick {
    pub time: String,
    pub bid: f64,
    pub ask: f64,
    pub volume: u64,
}

pub fn parse_ohlcv_csv(csv_data: &str) -> Vec<Ohlcv> {
    let mut results = Vec::new();
    let mut lines = csv_data.lines();
    
    // Skip header
    lines.next();
    
    for line in lines {
        if line.trim().is_empty() {
            continue;
        }
        let parts: Vec<&str> = line.split(',').collect();
        if parts.len() >= 7 {
            if let (Ok(o), Ok(h), Ok(l), Ok(c), Ok(tv), Ok(s)) = (
                f64::from_str(parts[1]),
                f64::from_str(parts[2]),
                f64::from_str(parts[3]),
                f64::from_str(parts[4]),
                u64::from_str(parts[5]),
                u32::from_str(parts[6]),
            ) {
                results.push(Ohlcv {
                    time: parts[0].to_string(),
                    open: o,
                    high: h,
                    low: l,
                    close: c,
                    tick_volume: tv,
                    spread: s,
                });
            }
        }
    }
    results
}

pub fn parse_tick_csv(csv_data: &str) -> Vec<Tick> {
    let mut results = Vec::new();
    let mut lines = csv_data.lines();
    
    // Skip header
    lines.next();
    
    for line in lines {
        if line.trim().is_empty() {
            continue;
        }
        let parts: Vec<&str> = line.split(',').collect();
        if parts.len() >= 4 {
            if let (Ok(b), Ok(a), Ok(v)) = (
                f64::from_str(parts[1]),
                f64::from_str(parts[2]),
                u64::from_str(parts[3]),
            ) {
                results.push(Tick {
                    time: parts[0].to_string(),
                    bid: b,
                    ask: a,
                    volume: v,
                });
            }
        }
    }
    results
}