Skip to content

Commit

Permalink
clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
ryqdev committed May 28, 2024
1 parent 29b0f23 commit 03528cc
Show file tree
Hide file tree
Showing 9 changed files with 22 additions and 29 deletions.
4 changes: 0 additions & 4 deletions src/err.rs
Original file line number Diff line number Diff line change
@@ -1,4 +0,0 @@

pub enum Error {
Anyhow(anyhow::Error),
}
3 changes: 1 addition & 2 deletions src/green/broker/backtest.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::green::strategy::hold::Order;
use super::Broker;
use crate::green::green::Order;

#[derive(Default, Clone, Debug)]
pub struct BackTestBroker{
Expand Down
2 changes: 1 addition & 1 deletion src/green/broker/live.rs
Original file line number Diff line number Diff line change
@@ -1 +1 @@
fn live_trading(){}
// fn live_trading(){}
1 change: 0 additions & 1 deletion src/green/feeds/yahoo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ type HistoricalData = (String, Vec<f64>, usize);

impl YahooFinanceData{
fn fetch_csv_data(symbol: &str) -> anyhow::Result<Vec<Vec<f64>>> {
// Date,Open,High,Low,Close,Adj Close,Volume
let file = File::open(format!("data/{symbol}.csv"))?;

let mut reader = csv::ReaderBuilder::new()
Expand Down
20 changes: 13 additions & 7 deletions src/green/green.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,22 @@ use std::collections::hash_set::SymmetricDifference;
use std::collections::VecDeque;
use std::default::Default;
use std::fs::File;

use ibapi::orders::{order_builder, OrderNotification};

use crate::cmds::backtest::BackTestCommand;
use crate::err::Error;
use crate::green::{
feeds::BaseData,
strategy::Strategy,
strategy::{
Strategy,
hold::{SimpleStrategy}
},
broker::Broker,
broker::backtest::BackTestBroker,
analyzer::Analyzer
analyzer::Analyzer,
visualization
};
use crate::green::strategy::hold::{SimpleStrategy};
use crate::green::visualization;

#[derive(Debug, Default, Copy, Clone)]
pub enum Action {
Expand Down Expand Up @@ -61,12 +65,14 @@ impl Green {
let close_price = bar.last().unwrap();
match order.action {
Action::Buy => {
log::info!("Buy: {:?}", order);
self.broker.cash.push(cash - order.size * close_price);
self.broker.position.push(position + order.size);
self.broker.net_assets.push(self.broker.cash.last().unwrap() + self.broker.position.last().unwrap() * close_price);
self.broker.order.push(order)
}
Action::Sell => {
log::info!("Sell: {:?}", order);
self.broker.cash.push(cash + order.size * close_price);
self.broker.position.push(position - order.size);
self.broker.net_assets.push(self.broker.cash.last().unwrap() + self.broker.position.last().unwrap() * close_price);
Expand Down Expand Up @@ -135,9 +141,9 @@ impl GreenBuilder{
self.strategy = strategy;
self
}
pub fn add_analyzer(&mut self, analyzer: Box<dyn Analyzer>) -> &GreenBuilder {
self
}
// pub fn add_analyzer(&mut self, analyzer: Box<dyn Analyzer>) -> &GreenBuilder {
// self
// }
pub fn build(&self) -> Box<Green> {
Box::new(Green{
data: self.data.clone(),
Expand Down
5 changes: 1 addition & 4 deletions src/green/strategy/hold.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::green::broker::backtest::BackTestBroker;
use crate::green::green::{Action, Green, Order};
use crate::green::green::{Action, Order};
use crate::green::strategy::Strategy;


Expand All @@ -11,13 +10,11 @@ impl Strategy for SimpleStrategy {
let open_price = data[0];
let close_price = data[3];
if close_price > open_price {
log::info!("buy");
Order{
action: Action::Buy,
size: 1.0
}
} else {
log::info!("sell");
Order{
action: Action::Sell,
size: 1.0
Expand Down
3 changes: 2 additions & 1 deletion src/green/strategy/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
pub mod hold;

use crate::green::broker::Broker;
use crate::green::green::Order;

pub trait Strategy {
fn next(&mut self, _: &Vec<f64>);
fn next(&mut self, _: &Vec<f64>) -> Order;
}
11 changes: 3 additions & 8 deletions src/green/visualization/candle.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
use std::{
fs::File
};
use eframe::emath::Vec2;
use egui::{Stroke, Color32};
use egui_plot::{Plot, BoxPlot, BoxElem, BoxSpread, Legend, Line};

use egui::{Stroke, Color32, DragValue, Event};
use egui_plot::{Plot, BoxPlot, BoxElem, BoxSpread, Legend, PlotPoints, Line};

use crate::green::strategy::hold::Order;
use crate::green::green::Order;

#[derive(Default)]
pub struct App {
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ fn init_log() {
"{}:{} {} [{}] - {}",
record.file().unwrap_or("unknown_file"),
record.line().unwrap_or(0),
chrono::Local::now().format("%Y-%m-%dT%H:%M:%S").to_owned().blue(),
chrono::Local::now().format("%Y-%m-%dT%H:%M:%S"),
record.level(),
record.args()
)
Expand Down

0 comments on commit 03528cc

Please sign in to comment.