Skip to content

Commit

Permalink
build backtest
Browse files Browse the repository at this point in the history
  • Loading branch information
ryqdev committed May 21, 2024
1 parent c7614e1 commit cda8a9c
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 27 deletions.
24 changes: 9 additions & 15 deletions src/cmds/backtest.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
use std::{
fs,
process::exit
};
use super::Command;
use anyhow::{Result};
use clap::{Arg, ArgMatches, Command as ClapCommand};
use serde_derive::{Deserialize, Serialize};
use clickhouse::{ Client, Row};
use async_trait::async_trait;
use time::Date;
use crate::green::{
Green, feeds, strategy,
Green, feeds, strategy, broker
};
use crate::strategy::hold::BuyAndHold;

Expand Down Expand Up @@ -43,16 +38,15 @@ impl Command for BackTestCommand {

async fn backtest(symbol: &str) -> Result<()> {
log::info!("Backtesting {}...", symbol);
let green = Green::new()?;

// green.add_strategy(BuyAndHold);
let green = Green::new()
.add_data_feed(feeds::yahoo::YahooFinanceData{
csv_file_path: "data/SPY.csv".to_string(),
start_date: "1993-01-29".to_string(),
end_date: "2024-05-17".to_string()
})
.add_strategy(BuyAndHold{})
.build();

// let date_feed: Box<dyn BaseData> = feeds::yahoo::YahooFinanceData{
// csv_file_path: "".to_string(),
// start_date: "".to_string(),
// end_date: "".to_string()
// };
// green.add_data_feed();
green.run();
green.plot();
Ok(())
Expand Down
13 changes: 13 additions & 0 deletions src/green/broker/backtest.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use super::Broker;

pub struct BackTestBroker{
pub(crate) cash: f64,
pub(crate) net_assets: f64,
}

impl Broker for BackTestBroker {
fn set_cash(&mut self, cash: f64) {
self.cash = cash
}

}
7 changes: 5 additions & 2 deletions src/green/broker/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
pub trait Broker{}
pub trait Broker{
fn set_cash(&mut self, cash: f64);
}

pub mod paper;
pub mod live;
pub mod live;
pub mod backtest;
53 changes: 43 additions & 10 deletions src/green/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,38 @@ pub mod broker;
mod analyzer;
mod visualization;

use std::default::Default;
use crate::err::Error;
use crate::green::{
feeds::BaseData,
strategy::Strategy,
broker::Broker,
broker::backtest::BackTestBroker,
analyzer::Analyzer
};
use crate::green::feeds::yahoo::YahooFinanceData;
use crate::strategy::hold::BuyAndHold;


pub struct Green {
data: String,
data: Box<YahooFinanceData>,
strategy: BuyAndHold,
}

pub struct GreenBuilder {
data: Box<YahooFinanceData>,
strategy: BuyAndHold,
}

impl Green {
pub fn new() -> anyhow::Result<Self> {
Ok(Green{
data: "foobar".to_string(),
})
pub fn new() -> GreenBuilder {
GreenBuilder{
..Default::default()
}
}
pub fn run(&self) {
log::info!("running...")
}
pub fn add_data_feed(&self, data: Box<dyn BaseData>){}
pub fn add_broker(&self,broker: Box<dyn Broker>){}
pub fn add_strategy(&self, strategy: Box<dyn Strategy>){}
pub fn add_analyzer(&self, analyzer: Box<dyn Analyzer>){}
pub fn run(&self) {}
pub fn plot(&self) {
log::info!("Ploting...");

Expand All @@ -38,4 +47,28 @@ impl Green {
Box::new(|cc| Box::new(visualization::candle::App::default())),
).expect("Plotting error");
}
}


impl GreenBuilder{
pub fn add_data_feed(&mut self, data: YahooFinanceData) -> &mut GreenBuilder{
self.data = Box::from(data);
self
}
// pub fn add_broker(&mut self,broker: BackTestBroker) -> &mut GreenBuilder {
// self.broker = broker;
// }
pub fn add_strategy(&mut self, strategy: BuyAndHold) -> &mut GreenBuilder{
self.strategy = strategy;
self
}
pub fn add_analyzer(&self, analyzer: Box<dyn Analyzer>) -> &GreenBuilder {
self
}
pub fn build(self) -> Green {
Green{
data: self.data,
strategy: self.strategy,
}
}
}

0 comments on commit cda8a9c

Please sign in to comment.