-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
-
Hi guys, is there a way to override 'plot' method to make the scalable height of the chart or at least to set some height?
For instance, right now it plots the chart only on a half of the page height in the browser, so I'd like to make it bigger in height.
I tried a few approaches, but no luck, e.g. this one:
from backtesting import Backtest, Strategy
from backtesting.test import SMA, GOOG # Example data
from bokeh.layouts import gridplot
from bokeh.plotting import show
from bokeh.models import DatetimeTickFormatter
class CustomBacktest(Backtest):
def plot(self, **kwargs):
# Call the original plot method
fig = super().plot(**kwargs)
# Apply full-height adjustments to all figures
for f in fig.select(dict(type="Figure")):
f.sizing_mode = "stretch_both" # Adjust each figure for full resizing
f.plot_height = 800 # Ensure height is large enough to fill the screen
if hasattr(f.xaxis[0], "formatter"):
f.xaxis[0].formatter = DatetimeTickFormatter(days="%d %b", months="%m/%Y")
# Apply sizing_mode to the overall layout
fig.sizing_mode = "stretch_both"
# Show the adjusted figure
show(fig, browser=None if kwargs.get("open_browser", True) else 'none')
return fig
# Define a simple strategy
class SmaCross(Strategy):
def init(self):
self.sma1 = self.I(SMA, self.data.Close, 10)
self.sma2 = self.I(SMA, self.data.Close, 20)
def next(self):
if self.sma1[-1] > self.sma2[-1]:
self.buy()
elif self.sma1[-1] < self.sma2[-1]:
self.sell()
# Run the backtest with the customized plot
bt = CustomBacktest(GOOG, SmaCross, cash=10_000, commission=0.002)
stats = bt.run()
bt.plot()
Beta Was this translation helpful? Give feedback.
All reactions
I've been playing around with this for a while, because ideally the plot would be fullscreen.
This works for me, but the result (the panes not retaining designated aspect) is an issue I didn't find how to overcome.
bt = Backtest(...) fig = bt.plot(open_browser=False) fig.sizing_mode = 'stretch_both' from bokeh.io import show show(fig)
If anybody finds a way to make them look pretty, please open a PR.
Replies: 3 comments 1 reply
-
Referring to the documentation of Backtesting.py, it states that 'The height is currently non-adjustable.' Therefore, you likely won't be able to achieve your desired outcome using the built-in bt.plot(). Alternatively, you might consider exporting the necessary data to Excel to create the chart.
Beta Was this translation helpful? Give feedback.
All reactions
-
Referring to the documentation of Backtesting.py, it states that 'The height is currently non-adjustable.' Therefore, you likely won't be able to achieve your desired outcome using the built-in bt.plot(). Alternatively, you might consider exporting the necessary data to Excel to create the chart.
Well, thank you, I saw and read this in the documentation before posting the question here. But still think there is a way to override the library method.
I tried a few approaches and they didn't work.
So, maybe someone knows why this is impossible (to change the chart height).
Beta Was this translation helpful? Give feedback.
All reactions
-
I've been playing around with this for a while, because ideally the plot would be fullscreen.
This works for me, but the result (the panes not retaining designated aspect) is an issue I didn't find how to overcome.
bt = Backtest(...) fig = bt.plot(open_browser=False) fig.sizing_mode = 'stretch_both' from bokeh.io import show show(fig)
If anybody finds a way to make them look pretty, please open a PR.
Beta Was this translation helpful? Give feedback.
All reactions
-
I've attempted to solve this by making the individual chart components flexible. PR: #1302
Beta Was this translation helpful? Give feedback.