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) for fig in figs: for f in fig: f.savefig(file_path, bbox_inches='tight') return figs
def plot(self, plotter=None, numfigs=1, iplot=True, start=None, end=None, width=16, height=9, dpi=300, tight=True, use=None, **kwargs): if self._exactbars > 0: return # endif if not plotter: if self.p.oldsync: plotter = plot.Plot_OldSync(**kwargs) else: plotter = plot.Plot(**kwargs) # endif # endif figs = [] for stratlist in self.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) # endfor #plotter.show() # endfor return figs
def getFig(self, plotter=None, numfigs=1, iplot=True, start=None, end=None, width=16, height=9, dpi=300, tight=True, use=None, **kwargs): if self._exactbars > 0: return if not plotter: from backtrader import plot import matplotlib matplotlib.use('TkAgg') if self.p.oldsync: plotter = plot.Plot_OldSync(**kwargs) else: plotter = plot.Plot(**kwargs) figs = [] for stratlist in self.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) return figs
def plotToFile(self, plotter=None, numfigs=1, iplot=True, start=None, end=None, width=16, height=9, dpi=300, tight=True, use=None, path=None, **kwargs): if self._exactbars > 0: return # change to a non-interactive backend matplotlib.use('Agg') if not plotter: from backtrader import plot if self.p.oldsync: plotter = plot.Plot_OldSync(**kwargs) else: plotter = plot.Plot(**kwargs) import matplotlib.pyplot as plt figs = [] for stratlist in self.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) fig = plt.gcf() fig.set_size_inches(width, height) if path: fig.savefig(path, dpi=dpi) return figs
def process_plots(self, numfigs=1, iplot=False, start=None, end=None, width=15, height=9, dpi=350, tight=False, use=None, outdir='', **kwargs): import matplotlib.pyplot as plt plt.rcParams['figure.figsize'] = [width, height] plt.rcParams['figure.dpi'] = dpi from backtrader import plot if self.p.oldsync: plotter = plot.Plot_OldSync(**kwargs) else: plotter = plot.Plot(**kwargs) figs = [] for stratlist in self.runstrats: for si, strat in enumerate(stratlist): fig = plotter.plot(strat, figid=si * 100, numfigs=numfigs, iplot=iplot, start=start, end=end, use=use) figs.append(fig) for f in fig: f.savefig(os.path.join(outdir, 'result.png'), tight=tight) return figs
def plot(self, plotter=None, numfigs=1, iplot=True, start=None, end=None, width=16, height=9, dpi=300, tight=True, use=None, path=None, **kwargs): ''' Plots the strategies inside cerebro If ``plotter`` is None a default ``Plot`` instance is created and ``kwargs`` are passed to it during instantiation. ``numfigs`` split the plot in the indicated number of charts reducing chart density if wished ``iplot``: if ``True`` and running in a ``notebook`` the charts will be displayed inline ``use``: set it to the name of the desired matplotlib backend. It will take precedence over ``iplot`` ``start``: An index to the datetime line array of the strategy or a ``datetime.date``, ``datetime.datetime`` instance indicating the start of the plot ``end``: An index to the datetime line array of the strategy or a ``datetime.date``, ``datetime.datetime`` instance indicating the end of the plot ``width``: in inches of the saved figure ``height``: in inches of the saved figure ``dpi``: quality in dots per inches of the saved figure ``tight``: only save actual content and not the frame of the figure ''' if self._exactbars > 0: return if not plotter: from backtrader import plot if self.p.oldsync: plotter = plot.Plot_OldSync(**kwargs) else: plotter = plot.Plot(**kwargs) # pfillers = {self.datas[i]: self._plotfillers[i] # for i, x in enumerate(self._plotfillers)} # pfillers2 = {self.datas[i]: self._plotfillers2[i] # for i, x in enumerate(self._plotfillers2)} import matplotlib.pyplot as plt figs = [] for stratlist in self.runstrats: for si, strat in enumerate(stratlist): rfig = plotter.plot(strat, figid=si * 100, numfigs=numfigs, iplot=iplot, start=start, end=end, use=use) # pfillers=pfillers2) figs.append(rfig) fig = plt.gcf() plotter.show() fig.set_size_inches(width, height) if path: fig.savefig(path, dpi=dpi) return figs