Add the ability to plot backtest results

This commit is contained in:
moshferatu 2024-01-30 10:56:02 -08:00
parent 186e064f03
commit 6eefb07add
3 changed files with 34 additions and 3 deletions

28
backtest_chart.py Normal file
View File

@ -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)
)
)]

View File

@ -36,13 +36,15 @@ def figure_with_subplots(subplots: List[List[Chart]]) -> Figure:
# TODO: Make this configurable. # TODO: Make this configurable.
figure.update_layout( figure.update_layout(
bargap = 0,
bargroupgap = 0,
paper_bgcolor = '#0f0f0f', paper_bgcolor = '#0f0f0f',
plot_bgcolor = '#0f0f0f', plot_bgcolor = '#0f0f0f',
font_color = '#7a7c7d', font_color = '#7a7c7d',
showlegend = False, showlegend = False,
margin = dict( margin = dict(pad = 15),
pad = 15 xaxis = dict(zerolinecolor = '#0f0f0f'),
) yaxis = dict(zerolinecolor = '#0f0f0f')
) )
return figure return figure

View File

@ -1,2 +1,3 @@
numpy
pandas pandas
plotly plotly