For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

Plotly & Backtrader?



  • Has anyone been able to get Plotly working with Backtrader? I thought that the plot_mpl or mpl_plotly methods would work but I keep running into the following error. Am I just calling something incorrectly?

    Traceback (most recent call last):
      File "C:/Users/sgh31/PycharmProjects/Algorithmic Trading/AlpacaTutorial.py", line 79, in <module>
        plotly.offline.plot_mpl(result[0], filename='simple_candlestick.html')
      File "C:\Users\sgh31\PycharmProjects\Algorithmic Trading\venv\lib\site-packages\plotly\offline\offline.py", line 833, in plot_mpl
        plotly_plot = tools.mpl_to_plotly(mpl_fig, resize, strip_style, verbose)
      File "C:\Users\sgh31\PycharmProjects\Algorithmic Trading\venv\lib\site-packages\plotly\tools.py", line 470, in mpl_to_plotly
        matplotlylib.Exporter(renderer).run(fig)
      File "C:\Users\sgh31\PycharmProjects\Algorithmic Trading\venv\lib\site-packages\plotly\matplotlylib\mplexporter\exporter.py", line 45, in run
        if fig.canvas is None:
    AttributeError: 'list' object has no attribute 'canvas'
    

    For reference, my code is below. I'm just using a simple strategy as a POC to see if Plotly can work with backtrader

    # Store the figures returned by cerebro.plot() and plot them w/plotly
    result = cerebro.plot()
    plotly.offline.plot_mpl(result[0], filename='simple_candlestick.html')
    


  • Update:

    Changing it to the following seemed to work but I'm confused as to why. It looks like it was a list within a list but I can't figure out why it was nested that way with Cerebro.plot()

    # Store the figures returned by cerebro.plot() and plot them w/plotly
    result = cerebro.plot()
    plotly.offline.plot_mpl(result[0][0], filename='simple_candlestick.html')
    


  • I have been having the same issue. I've tried the fixed code above and the conversion of matplotlib graph was only partially successful - the candle chart was eliminated and the plotly results seems distorted and unclear. Is there any other alternative for plotly support? I've seen OmegaUI, not sure I need something this sophisticated.