mirror of
https://github.com/KhizarImran/backtestingfx.git
synced 2026-07-27 20:17:44 +00:00
new: added a working example
This commit is contained in:
@@ -2,3 +2,4 @@
|
|||||||
Cargo.lock
|
Cargo.lock
|
||||||
### My claude md file
|
### My claude md file
|
||||||
CLAUDE.md
|
CLAUDE.md
|
||||||
|
*/data/
|
||||||
|
|||||||
@@ -4,3 +4,4 @@ version = "0.1.0"
|
|||||||
edition = "2024"
|
edition = "2024"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
chrono = "0.4"
|
||||||
|
|||||||
@@ -0,0 +1,27 @@
|
|||||||
|
use backtestingfx::types::Bar;
|
||||||
|
use backtestingfx::broker::Broker;
|
||||||
|
use backtestingfx::strategy::Strategy;
|
||||||
|
use backtestingfx::engine::Engine;
|
||||||
|
use backtestingfx::data::load_csv;
|
||||||
|
|
||||||
|
struct BuyEveryBar;
|
||||||
|
|
||||||
|
impl Strategy for BuyEveryBar {
|
||||||
|
fn next (&mut self, bar: &Bar, broker: &mut Broker) {
|
||||||
|
broker.close_all(bar.close, bar.timestamp); //closes any open positions
|
||||||
|
broker.buy(bar.close, 1.0, bar.timestamp); // can be more complicated with buy, sells, close position, close all etc.
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let data = load_csv("examples/data/eurusd_lse_1h.csv");
|
||||||
|
let mut engine = Engine::new(data, 10000.0);
|
||||||
|
let mut strategy = BuyEveryBar;
|
||||||
|
|
||||||
|
engine.run(&mut strategy); // main line that runs the strategy
|
||||||
|
|
||||||
|
println!("Final cash: {}", engine.broker.cash);
|
||||||
|
println!("Trades executed: {}", engine.broker.trade_history.len());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
+4
-1
@@ -1,6 +1,7 @@
|
|||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::io::{BufRead, BufReader};
|
use std::io::{BufRead, BufReader};
|
||||||
use crate::types::Bar;
|
use crate::types::Bar;
|
||||||
|
use chrono::DateTime;
|
||||||
|
|
||||||
pub fn load_csv(path: &str) -> Vec<Bar> {
|
pub fn load_csv(path: &str) -> Vec<Bar> {
|
||||||
let file = File::open(path).expect("Could not open file");
|
let file = File::open(path).expect("Could not open file");
|
||||||
@@ -11,8 +12,10 @@ pub fn load_csv(path: &str) -> Vec<Bar> {
|
|||||||
let line = line.expect("could not read line");
|
let line = line.expect("could not read line");
|
||||||
let cols: Vec<&str> = line.split(',').collect();
|
let cols: Vec<&str> = line.split(',').collect();
|
||||||
|
|
||||||
|
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 {
|
let bar = Bar {
|
||||||
timestamp: cols[0].parse().expect("Bad timestamp"),
|
timestamp: dt.timestamp(),
|
||||||
open: cols[1].parse().expect("Bad open"),
|
open: cols[1].parse().expect("Bad open"),
|
||||||
high: cols[2].parse().expect("Bad high"),
|
high: cols[2].parse().expect("Bad high"),
|
||||||
low: cols[3].parse().expect("Bad low"),
|
low: cols[3].parse().expect("Bad low"),
|
||||||
|
|||||||
Reference in New Issue
Block a user