From 6eefb07adde1ed97367d56700256cd558baf3663 Mon Sep 17 00:00:00 2001 From: moshferatu Date: Tue, 30 Jan 2024 10:56:02 -0800 Subject: [PATCH] Add the ability to plot backtest results --- backtest_chart.py | 28 ++++++++++++++++++++++++++++ plot.py | 8 +++++--- requirements.txt | 1 + 3 files changed, 34 insertions(+), 3 deletions(-) create mode 100644 backtest_chart.py diff --git a/backtest_chart.py b/backtest_chart.py new file mode 100644 index 0000000..d8ef8e3 --- /dev/null +++ b/backtest_chart.py @@ -0,0 +1,28 @@ +from chart import Chart +from numpy import where +from pandas import Series +from plotly.graph_objects import Bar +from typing import List + +class BacktestChart(Chart): + + def __init__( + self, + dates: Series, + profit: Series, + title: str = '' + ): + self.x = dates + self.profit = profit + self.title = title + + def traces(self) -> List[Bar]: + color = where(self.profit >= 0, 'limegreen', 'red') + return [Bar( + x = self.x, + y = self.profit, + marker = dict( + color = color, + line = dict(width = 0) + ) + )] \ No newline at end of file diff --git a/plot.py b/plot.py index 715de16..3d107c3 100644 --- a/plot.py +++ b/plot.py @@ -36,13 +36,15 @@ def figure_with_subplots(subplots: List[List[Chart]]) -> Figure: # TODO: Make this configurable. figure.update_layout( + bargap = 0, + bargroupgap = 0, paper_bgcolor = '#0f0f0f', plot_bgcolor = '#0f0f0f', font_color = '#7a7c7d', showlegend = False, - margin = dict( - pad = 15 - ) + margin = dict(pad = 15), + xaxis = dict(zerolinecolor = '#0f0f0f'), + yaxis = dict(zerolinecolor = '#0f0f0f') ) return figure diff --git a/requirements.txt b/requirements.txt index bfa2e59..7717eb3 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,3 @@ +numpy pandas plotly \ No newline at end of file