Skip to content
Home » How to use Backtrader plots in Python Notebooks

How to use Backtrader plots in Python Notebooks

The recent crypto bull market got me interested in chartist approaches to trading strategies. I’ve always wanted to use Deepnote‘s notebook solution, so I decided to combine my newfound hobby of algorithmic trading with my new tool craving. I started using Backtrader in Deepnote, but that produced some plotting issues.

Basically, when you use the plot method on a Cerebro object in a Notebook, some unexpected things will present themselves.

If you use plot, without or without the iplot argument set to True, you’ll run into this error:

'NoneType' object has no attribute '_send_event'

Furthermore, IPython will render the Javascript object simply as the following text lines.

<IPython.core.display.Javascript object>
[[<Figure size 432x288 with 10 Axes>]]

What you can do to produce an interpretable visualization in your notebook is the following:

%matplotlib inline
cerebro = bt.Cerebro()

cerebro.adddata(bt.feeds.PandasData(dataname = df))

plt.rcParams['figure.figsize'] = [15, 12]
plt.rcParams.update({'font.size': 12}) 
cerebro.plot(iplot = False)

This should produce something readable, yet not interactive, like this (cropped).

Great success!

Say thanks, ask questions or give feedback

Technologies get updated, syntax changes and honestly… I make mistakes too. If something is incorrect, incomplete or doesn’t work, let me know in the comments below and help thousands of visitors.

2 thoughts on “How to use Backtrader plots in Python Notebooks”

Leave a Reply

Your email address will not be published. Required fields are marked *