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

Best way to plot a spread as main chart?



  • Hello Backtrader Users,

    I'm trying to backtest a future spread strategy with Bollinger Bands.
    What is the best way to show only one chart with the Spread and BB?
    Currently the spread is only visible, when I enable the first data line.

    My code looks like:

    class Spread(bt.indicators.Indicator):
        _mindatas = 2
        lines = (('spread'),)
    
        def next(self):
            self.lines.spread[0] = self.data0[0] - self.data1[0]
    
    self.LONGclose = self.dnames.LONG.close
    self.dnames.LONG.plotinfo.plot=True
    self.SHORTclose = self.dnames.SHORT.close
    self.dnames.SHORT.plotinfo.plot=True
    
    
    self.spread = Spread(self.LONGclose, self.SHORTclose)
    self.spread.plotinfo.plot=True
    self.spread.plotinfo.plotmaster=None
    self.spread.plotinfo.subplot=True
    self.spread.plotinfo.plotforce=True
    
    self.bb = bt.indicators.BollingerBands(self.spread, period=self.params.bb_period, devfactor=self.params.bb_stds)
    self.bb.plotinfo.plot=True
    #self.bb.plotinfo.subplot=True
    #self.bb.plotinfo.plotforce=True
    #self.bb.plotinfo.plotmaster=self.spread
    

    Is it possible to show only the Spread Indocator with Bollinger Bands without the both data lines?

    Thanks alot for your help!

    BR
    Rolf


  • administrators

    Data is the center of the plot. It is unclear if you put the bollinger bands on the spread or on the data.

    You could in any case try to plot things using an observer, but it will require a custom observer.

    The plotting code is not a one-fits-all solution, it is simply meant as a visual aid around data and indicators and not as a fully customizable plotting engine (which is what matplotlib, the underlying engine, is)



  • Yes, I want to plot the spread and BB on the spread.
    I will take a look at the Observers.

    Thanks alot.
    Rolf


  • administrators

    It would seem that you rely on the spread an nothing else. In that case you could try this approach

    • Add a filter and set the price of the OHLC components to be that of the spread.
    • Docs - Filters

    You can with that set the Bollinger Bands to run directly on the data feed and not on the spread and things may plot nicely, given that the data is now the spread.


Log in to reply
 

});