Skip to content

Commit

Permalink
#79 reformatted files and removed stuff done twice
Browse files Browse the repository at this point in the history
  • Loading branch information
TristanFecteau committed Feb 3, 2022
1 parent e90e3b8 commit 2a04b44
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 39 deletions.
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
aiohttp
python-dotenv
python-dotenv
streamlit
30 changes: 8 additions & 22 deletions src/bullets/runner.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import csv
import os
from datetime import datetime, timedelta
from bullets.portfolio.transaction import Status
from bullets.strategy import Strategy
Expand All @@ -11,12 +10,11 @@
import os.path as osp
import json


class Runner:
def __init__(self, strategy: Strategy, logdir: str = None):
def __init__(self, strategy: Strategy):
self.strategy = strategy
self.logdir = logdir
self.holidays = None
self.stats = {}

def start(self):
"""
Expand All @@ -35,8 +33,7 @@ def start(self):
self.strategy.on_finish()
logger.info("=========== Backtest complete ===========")
self._save_backtest_log()
if not self.logdir is None:
self._save_final_stats()
self._open_viewer_app()

def _get_moments(self, resolution: Resolution, start_time: datetime, end_time: datetime):
moments = []
Expand Down Expand Up @@ -105,19 +102,8 @@ def _update_final_timestamp(self):
final_timestamp = transaction.timestamp
self.strategy.update_time(final_timestamp)

def _save_final_stats(self):
self.stats['profit'] = self.strategy.portfolio.cash_balance - self.strategy.starting_balance
self.stats['final_balance'] = self.strategy.portfolio.cash_balance

LOG_REPO = "../log" #TODO : put in env file
print(os.getcwd())
try:
os.mkdir(osp.join(LOG_REPO, self.logdir))
except OSError as error:
print(error)

#TODO : add a temporary csv format save so the report can be handled with excel as well
with open(osp.join(LOG_REPO,self.logdir,'strategy_report.json'), 'w') as fp:
json.dump(self.stats, fp, indent=4)
print("Log file successfully saved under {}".format(osp.join(LOG_REPO, self.logdir)))
return 0
def _open_viewer_app(self):
# TODO : Open the viewer app and make it visualise the data stored in the files
# (see _save_stats_to_csv & _save_transactions_to_cvs)
#
output_folder = self.strategy.output_folder
1 change: 1 addition & 0 deletions src/bullets/viewer/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from bullets.viewer import *
3 changes: 2 additions & 1 deletion src/bullets/viewer/graphs/basic_charts.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import plotly.graph_objects as go


def candle_chart(df):
fig = go.Figure(data=[go.Candlestick(x=df['Date'],
open=df['Open'],
high=df['High'],
low=df['Low'],
close=df['Close'])])
return fig
return fig
1 change: 0 additions & 1 deletion src/bullets/viewer/processing/__init__.py

This file was deleted.

16 changes: 9 additions & 7 deletions src/bullets/viewer/processing/dataloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,23 @@
from glob import glob
import pandas as pd


def list_log_files(path_to_log):
print(glob(path_to_log + "/*", recursive = True))
print(glob(path_to_log + "/*", recursive=True))
log_dict = {}
paths_to_directories = glob(path_to_log + "/*", recursive = True)
paths_to_directories = glob(path_to_log + "/*", recursive=True)
for path in paths_to_directories:
if osp.isfile(osp.join(path,'strategy_log.json')):
if osp.isfile(osp.join(path, 'strategy_log.json')):
print(osp.basename(path))
log_dict[osp.basename(path)] = path
elif len(glob(osp.join(path) + "/*", recursive = True))!=0 :
elif len(glob(osp.join(path) + "/*", recursive=True)) != 0:
log_dict.update(list_log_files(path))
else :
else:
return {}
print(log_dict)
return log_dict


def load_strategy(path_to_log):
data = pd.read_csv(osp.join(path_to_log,"stock_1.csv"))
return data
data = pd.read_csv(osp.join(path_to_log, "stock_1.csv"))
return data
11 changes: 4 additions & 7 deletions src/bullets/viewer/viewer_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@
from processing.dataloader import *
from graphs.basic_charts import *

#CONSTANTS TO FETCH FROM CONFIG FILE
# CONSTANTS TO FETCH FROM CONFIG FILE
PATH_TO_LOG = "../../../log/"


if 'loaded' not in st.session_state.keys() or st.session_state["loaded"]==False:
if 'loaded' not in st.session_state.keys() or st.session_state["loaded"] == False:
file_dict = list_log_files(PATH_TO_LOG)
st.session_state['file_dict'] = file_dict
st.session_state['loaded'] = False
Expand All @@ -15,8 +14,7 @@
selected_file = st.selectbox("Select a log file", file_dict.keys())
is_selected = st.button("Load log file")


if is_selected :
if is_selected:

st.markdown("# Analysing {}".format(selected_file))
st.markdown("---")
Expand All @@ -30,6 +28,5 @@

st.sidebar.markdown("Nice sidebar")


st.dataframe(data)
st.plotly_chart(candle_chart(data))
st.plotly_chart(candle_chart(data))

0 comments on commit 2a04b44

Please sign in to comment.