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

Plot cerebro in a non-blocking way

  • There is any way to plot cerebro in a non-blocking way? Nowadays, when I run cerebro.plot() the main thred get blocked until y close the chart manually.
    Or maybe, there is any way to save to a file the cerebro.plot() chart without showing it?

  • Hello @Rubén-Briones, if you are just trying to save the chart, here's the code:

        def saveplots(cerebro, numfigs=1, iplot=True, start=None, end=None,
                 width=16, height=9, dpi=300, tight=True, use=None, file_path = '', **kwargs):
            from backtrader import plot
            if cerebro.p.oldsync:
                plotter = plot.Plot_OldSync(**kwargs)
                plotter = plot.Plot(**kwargs)
            figs = []
            for stratlist in cerebro.runstrats:
                for si, strat in enumerate(stratlist):
                    rfig = plotter.plot(strat, figid=si * 100,
                                        numfigs=numfigs, iplot=iplot,
                                        start=start, end=end, use=use)
                # this blocks code execution
            for fig in figs:
                for f in fig:
                    f.savefig(file_path, bbox_inches='tight')
            return figs
            cerebro.plot(style = 'candlestick')
        saveplots(cerebro,file_path = 'savefig.png')

Log in to reply