Skip to content

Commit

Permalink
finish get_bar_from_csv
Browse files Browse the repository at this point in the history
  • Loading branch information
ryqdev committed Jun 1, 2024
1 parent 6ae8fa8 commit c55c4a2
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 6 deletions.
2 changes: 2 additions & 0 deletions data/SPY_test.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
date,open,high,low,close,Adj Close,Volume
2020-05-29,302.4599914550781,304.9599914550781,299.4700012207031,304.32000732421875,286.5516357421875,119090800
7 changes: 3 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,11 +217,10 @@ struct CSVData {

// https://docs.rs/csv/latest/csv/struct.Reader.html
pub fn get_bar_from_csv(symbol: &str) -> Result<Vec<Bar>, Box<dyn std::error::Error>> {
let mut rdr = csv::ReaderBuilder::new()
csv::ReaderBuilder::new()
.has_headers(true)
.from_reader( File::open(format!("data/{symbol}.csv"))?);

rdr.deserialize::<CSVData>().map(|line| {
.from_reader( File::open(format!("data/{symbol}.csv"))?)
.deserialize::<CSVData>().map(|line| {
let record = line?;
Ok(Bar {
date: OffsetDateTime::now_utc(),
Expand Down
5 changes: 3 additions & 2 deletions tests/csv_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ mod tests {
.filter_level(log::LevelFilter::Info)
.init();

let bars = get_bar_from_csv("SPY");
log::info!("{:?}", bars)
// from /data/SPY_test.csv
assert_eq!(302.4599914550781, get_bar_from_csv("SPY_test").unwrap()[0].open);
assert_eq!(304.32000732421875, get_bar_from_csv("SPY_test").unwrap()[0].close);
}
}

0 comments on commit c55c4a2

Please sign in to comment.