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

Crossover indicator doesn't plot as expected



  • Hi guys, I find some weird things when plotting Crossover indicator.
    sma_crossover indicator shows up properly, but adx_crossover indicator doesn't show up as expected. What's going on here?

    import datetime
    
    import backtrader as bt
    
    
    class SMACross(bt.Strategy):
    
        params = (
            ('pfast', 10),
            ('pslow', 30),
        )
        def __init__(self):
            self.sma_fast = bt.talib.SMA(self.datas[0].close, timeperiod=self.p.pfast)
            self.sma_slow = bt.talib.SMA(self.datas[0].close, timeperiod=self.p.pslow)
            self.sma_crossover = bt.ind.CrossOver(self.sma_fast, self.sma_slow)
    
            self.adx = bt.talib.ADX(self.datas[0].high, self.datas[0].low, self.datas[0].close)
            self.adx_fast = bt.talib.SMA(self.adx, timeperiod=self.p.pfast)
            self.adx_slow = bt.talib.SMA(self.adx, timeperiod=self.p.pslow)
            self.adx_crossover = bt.ind.CrossOver(
                self.adx_fast, self.adx_slow, plot=True, subplot=True)
            
        def next(self):
            pass
    
    
    if __name__ == '__main__':
        cerebro = bt.Cerebro()
        cerebro.addstrategy(SMACross)
        datapath = 'datas/orcl-1995-2014.txt'
    
        # Create a Data Feed
        data = bt.feeds.YahooFinanceCSVData(
            dataname=datapath,
            # Do not pass values before this date
            fromdate=datetime.datetime(2000, 1, 1),
            # Do not pass values before this date
            todate=datetime.datetime(2000, 12, 31),
            # Do not pass values after this date
            reverse=False)
            
        cerebro.adddata(data)
        cerebro.run()
        cerebro.plot()
    

  • administrators

    @xyshell said in Crossover indicator doesn't plot as expected:

    but adx_crossover indicator doesn't show up as expected.

    Understanding how something doesn't show up as expected without knowing what's expected and what's actually happening is a challenging problem. A real conundrum.

    @xyshell said in Crossover indicator doesn't plot as expected:

    What's going on here?

    A picture would probably let us experience what's actually going on.



  • Figure_0.png
    Sorry about the confusion. This is what I got from cerebro.plot() and I want the adx_crossover indicator plotted in the bottom.



  • @backtrader I've provided the picture below, thanks!


  • administrators

    @xyshell said in Crossover indicator doesn't plot as expected:

        self.adx_crossover = bt.ind.CrossOver(
            self.adx_fast, self.adx_slow, plot=True, subplot=True)
    

    Indicators are plotted subordinated to its master. The crossover happens on two subindicators of an indicator, so the data ceased to be the master a couple of levels before the current point.

    Use plotmaster=self.data

    See: Docs - Plotting - https://www.backtrader.com/docu/plotting/plotting/



  • @backtrader said in Crossover indicator doesn't plot as expected:

    plotmaster=self.data

    Thanks for reply, according to your suggestion, I added plotmaster=self.data. But the adx_crossover still don't show up at the bottom.

    import datetime
    
    import backtrader as bt
    
    
    class SMACross(bt.Strategy):
    
        params = (
            ('pfast', 10),
            ('pslow', 30),
        )
        def __init__(self):
            self.sma_fast = bt.talib.SMA(self.datas[0].close, timeperiod=self.p.pfast)
            self.sma_slow = bt.talib.SMA(self.datas[0].close, timeperiod=self.p.pslow)
            self.sma_crossover = bt.ind.CrossOver(self.sma_fast, self.sma_slow)
    
            self.adx = bt.talib.ADX(self.datas[0].high, self.datas[0].low, self.datas[0].close)
            self.adx_fast = bt.talib.SMA(self.adx, timeperiod=self.p.pfast)
            self.adx_slow = bt.talib.SMA(self.adx, timeperiod=self.p.pslow)
            self.adx_crossover = bt.ind.CrossOver(
                self.adx_fast, self.adx_slow, plotmaster=self.data, plot=True, subplot=True)
    
        def next(self):
            if not self.position:
                if self.sma_crossover > 0:
                    self.buy()
            elif self.position:
                if self.sma_crossover < 0:
                    self.sell()
    
    if __name__ == '__main__':
        cerebro = bt.Cerebro()
        cerebro.addstrategy(SMACross)
        datapath = 'datas/orcl-1995-2014.txt'
    
        # Create a Data Feed
        data = bt.feeds.YahooFinanceCSVData(
            dataname=datapath,
            # Do not pass values before this date
            fromdate=datetime.datetime(2000, 1, 1),
            # Do not pass values before this date
            todate=datetime.datetime(2000, 12, 31),
            # Do not pass values after this date
            reverse=False)
            
        cerebro.adddata(data)
        cerebro.run()
        cerebro.plot()
    

    Figure_0.png



  • Any further suggestions?



  • @xyshell indicators.png

    Create a new custom indicator which has combines the two lines: adx_fast and adx_slow. then add subplot=True when initiating that indicator.



  • @hghhgghdf-dfdf

    works like a charm !!! Thank you sir



  • import datetime
    
    import backtrader as bt
    
    
    class ADX(bt.Indicator):
        lines = ('adx', 'fast', 'slow')
        params = (
            ('pfast', 10),
            ('pslow', 30)
        )
    
        def __init__(self):
            self.lines.adx = bt.talib.ADX(
                self.data.high, self.data.low, self.data.close)
            self.lines.fast = bt.ind.SMA(self.lines.adx, period=self.p.pfast)
            self.lines.slow = bt.ind.SMA(self.lines.adx, period=self.p.pslow)
    
    
    class SMACross(bt.Strategy):
    
        params = (
            ('pfast', 10),
            ('pslow', 30),
        )
    
        def __init__(self):
            self.sma_fast = bt.talib.SMA(
                self.datas[0].close, timeperiod=self.p.pfast)
            self.sma_slow = bt.talib.SMA(
                self.datas[0].close, timeperiod=self.p.pslow)
            self.sma_crossover = bt.ind.CrossOver(self.sma_fast, self.sma_slow)
    
            self.adx = ADX(self.datas[0])
            self.adx_crossover = bt.ind.CrossOver(
                self.adx.fast, self.adx.slow, subplot=True)
    
        def next(self):
            if not self.position:
                if self.sma_crossover > 0:
                    self.buy()
            elif self.position:
                if self.sma_crossover < 0:
                    self.sell()
    
    
    if __name__ == '__main__':
        cerebro = bt.Cerebro()
        cerebro.addstrategy(SMACross)
        datapath = 'datas/orcl-1995-2014.txt'
    
        # Create a Data Feed
        data = bt.feeds.YahooFinanceCSVData(
            dataname=datapath,
            # Do not pass values before this date
            fromdate=datetime.datetime(2000, 1, 1),
            # Do not pass values before this date
            todate=datetime.datetime(2000, 12, 31),
            # Do not pass values after this date
            reverse=False)
    
        cerebro.adddata(data)
        cerebro.run()
        cerebro.plot()
    

    Screen Shot 2020-03-22 at 11.49.33 AM.png


Log in to reply
 

});