Navigation

    Backtrader Community

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

    Plot cerebro in a non-blocking way

    General Discussion
    2
    2
    133
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Rubén Briones
      Rubén Briones last edited by

      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?
      Thanks!

      1 Reply Last reply Reply Quote 0
      • Robin Dhillon
        Robin Dhillon last edited by

        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)
                else:
                    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)
                        figs.append(rfig)
        
                    # this blocks code execution
                    # plotter.show()
        
                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')
        
        1 Reply Last reply Reply Quote 1
        • 1 / 1
        • First post
          Last post
        Copyright © 2016, 2017, 2018 NodeBB Forums | Contributors
        $(document).ready(function () { app.coldLoad(); }); }