mirror of
https://github.com/KhizarImran/backtestingfx.git
synced 2026-07-27 20:17:44 +00:00
added engine rs & data rs
This commit is contained in:
+28
@@ -0,0 +1,28 @@
|
||||
use std::fs::File;
|
||||
use std::io::{BufRead, BufReader};
|
||||
use crate::types::Bar;
|
||||
|
||||
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 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();
|
||||
|
||||
let bar = Bar {
|
||||
timestamp: cols[0].parse().expect("Bad 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);
|
||||
|
||||
}
|
||||
|
||||
bars
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
use crate::types::Bar;
|
||||
use crate::broker::Broker;
|
||||
use crate::strategy::Strategy;
|
||||
|
||||
pub struct Engine {
|
||||
pub data: Vec<Bar>,
|
||||
pub broker: Broker
|
||||
}
|
||||
|
||||
impl Engine {
|
||||
pub fn new(data: Vec<Bar>, initial_cash: f64) -> Self {
|
||||
Engine {
|
||||
data,
|
||||
broker : Broker::new(initial_cash) // it takes initial cash and not Broker as Engine is responsible for broker not the user
|
||||
}
|
||||
}
|
||||
|
||||
pub fn run (&mut self, strategy: &mut dyn Strategy) { //&mut dyn allows class inheritence
|
||||
for bar in &self.data {
|
||||
strategy.next(bar, &mut self.broker);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user