| 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(); |
| |
| |
| 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(); |
| |
| |
| 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 |
| } |
|
|