mirror of
https://github.com/KhizarImran/backtestingfx.git
synced 2026-07-27 20:17:44 +00:00
feat: column-aware CSV loading and changelog
- Replaced manual CSV parsing with csv crate, columns now looked up by name - Fixed Rust example path and contract_size - Added CHANGELOG.md for release tracking Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
+16
-19
@@ -1,31 +1,28 @@
|
||||
use std::fs::File;
|
||||
use std::io::{BufRead, BufReader};
|
||||
use crate::types::Bar;
|
||||
use chrono::DateTime;
|
||||
use csv::Reader;
|
||||
use std::fs::File;
|
||||
|
||||
pub fn load_csv(path: &str) -> Vec<Bar> {
|
||||
let file = File::open(path).expect("Could not open file");
|
||||
let reader = BufReader::new(file);
|
||||
let mut rdr = Reader::from_reader(file);
|
||||
let headers = rdr.headers().expect("could not read headers").clone();
|
||||
let col = |name: &str| headers.iter().position(|h| h == name).expect(name);
|
||||
let mut bars = Vec::new();
|
||||
|
||||
for line in reader.lines().skip(1) { //skips the header row
|
||||
let line = line.expect("could not read line");
|
||||
let cols: Vec<&str> = line.split(',').collect();
|
||||
for result in rdr.records() {
|
||||
let r = result.expect("could not read record");
|
||||
let dt = DateTime::parse_from_rfc3339(&r[0]).expect("Bad timestamp");
|
||||
|
||||
let dt = DateTime::parse_from_rfc3339(cols[0]).expect("Bad Timestamp"); // this handles the timestamp to be in 2024-01-01 00:00:00 kind of way
|
||||
|
||||
let bar = Bar {
|
||||
bars.push(Bar {
|
||||
timestamp: dt.timestamp(),
|
||||
open: cols[1].parse().expect("Bad open"),
|
||||
high: cols[2].parse().expect("Bad high"),
|
||||
low: cols[3].parse().expect("Bad low"),
|
||||
close: cols[4].parse().expect("Bad close"),
|
||||
volume: cols[5].parse().expect("Bad volume"),
|
||||
};
|
||||
|
||||
bars.push(bar); // pushes each bar from the csv to an element in Bar
|
||||
|
||||
open: r[col("open")].parse().expect("Bad open"),
|
||||
high: r[col("high")].parse().expect("Bad high"),
|
||||
low: r[col("low")].parse().expect("Bad low"),
|
||||
close: r[col("close")].parse().expect("Bad close"),
|
||||
volume: r[col("volume")].parse().expect("Bad volume"),
|
||||
});
|
||||
}
|
||||
|
||||
bars
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user