Skip to content

Commit

Permalink
add csv mode
Browse files Browse the repository at this point in the history
  • Loading branch information
ryqdev committed Jun 1, 2024
1 parent 50b936e commit 5e856a3
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 30 deletions.
33 changes: 33 additions & 0 deletions src/csv/csv.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use std::fs::File;
use time::OffsetDateTime;
use crate::{Bar};


#[derive(Debug, serde::Deserialize, PartialEq)]
struct CSVData {
date: String,
open: f64,
high: f64,
low: f64,
close: f64
}

// 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>> {
csv::ReaderBuilder::new()
.has_headers(true)
.from_reader( File::open(format!("data/{symbol}.csv"))?)
.deserialize::<CSVData>().map(|line| {
let record = line?;
Ok(Bar {
date: OffsetDateTime::now_utc(),
open: record.open,
high: record.high,
low: record.low,
close: record.close,
volume: 0.0,
wap: 0.0,
count: 0,
})
}).collect()
}
1 change: 1 addition & 0 deletions src/csv/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub(crate) mod csv;
32 changes: 2 additions & 30 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pub mod broker;
mod analyzer;
mod visualization;
pub mod strategy;
mod csv;

use std::fs::File;

Expand Down Expand Up @@ -132,7 +133,7 @@ impl Golden for BackTestGolden {

fn set_data_feed(&mut self, symbol: &str) -> &mut dyn Golden{
log::info!("set data feed for {}", symbol);
self.data = get_bar_from_csv(symbol).unwrap();
self.data = csv::csv::get_bar_from_csv(symbol).unwrap();
self
}
fn set_broker(&mut self, cash: f64) -> &mut dyn Golden {
Expand Down Expand Up @@ -195,34 +196,5 @@ pub struct Bar {
pub count: i32,
}

#[derive(Debug, serde::Deserialize, PartialEq)]
struct CSVData {
date: String,
open: f64,
high: f64,
low: f64,
close: f64
}

// 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>> {
csv::ReaderBuilder::new()
.has_headers(true)
.from_reader( File::open(format!("data/{symbol}.csv"))?)
.deserialize::<CSVData>().map(|line| {
let record = line?;
Ok(Bar {
date: OffsetDateTime::now_utc(),
open: record.open,
high: record.high,
low: record.low,
close: record.close,
volume: 0.0,
wap: 0.0,
count: 0,
})
}).collect()
}

// fn connect_and_fetch_market_data() -> impl Iterator<Item = Bar>{
//

0 comments on commit 5e856a3

Please sign in to comment.