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

Indicator won't plot when it is derived from data with plot=False



  • Hello Guys,

    I have added an example code below to illustrate the problem, but basically once I set a data feed plot=False, I am not able to plot an indicator based on that data. Some tries (below) would cause runtime errors.

    Explanation for the example code shown below:
    Create two datas, data0 (oracle) and data1(yahoo), set plot=False on data1.
    Create a strategy that just creates two simple moving averages.
    That is it.

    What happens:
    1-If I remove the plot=False from data1, then sma1 plots as expected, but I don't want to plot data1, just its sma. The example appears to be silly, but it is just an example, in my application data1 is breadth and doesn't make sense plot it, just clobbers the picture.
    2-If I add the call to bt.LinePlotterIndicator(self.sma1, name='sma1',subplot=True) then I get the following runtime error:
    AttributeError: 'Lines_LineSeries_LineIterator_DataAccessor_Indicat' object has no attribute 'owner'
    3-If I add the self.sma1.plotinfo.plotforce=True then I get the following runtime error:
    AttributeError: 'NoneType' object has no attribute 'addindicator'

    I don't know what else to try. Any help would be appreciated!
    Thanks!

    Example code:

    import datetime  # For datetime objects
    import backtrader as bt
    
    # Create a Stratey
    class TestStrategy(bt.Strategy):
      def __init__(self):
        # 1 days MovingAverageSimple indicator on data0
        self.sma0 = bt.indicators.SimpleMovingAverage(
            self.data0,period=1,subplot=True)
        # 2 days MovingAverageSimple indicator on data1
        self.sma1 = bt.indicators.SimpleMovingAverage(
            self.data1,period=2,plot=True,subplot=True)
    
        # Following line runtime error:
        # AttributeError: 'Lines_LineSeries_LineIterator_DataAccessor_Indicat' object has no attribute 'owner'
        # bt.LinePlotterIndicator(self.sma1, name='sma1',subplot=True)
    
        # Following line runtime error:
        # AttributeError: 'NoneType' object has no attribute 'addindicator'
        # self.sma1.plotinfo.plotforce=True
    
    if __name__ == '__main__':
      cerebro = bt.Cerebro()
      cerebro.addstrategy(TestStrategy)
      # Create the 1st data
      data0 = bt.feeds.YahooFinanceCSVData(
        dataname='bt_src/backtrader-master/datas/orcl-2014.txt',
        fromdate=datetime.datetime(2014, 3, 1),
        todate=datetime.datetime(2014, 3, 30)
        )
      # Create the 2nd data
      data1 = bt.feeds.YahooFinanceCSVData(
        dataname='bt_src/backtrader-master/datas/yhoo-2014.txt',
        fromdate=datetime.datetime(2014, 3, 1),
        todate=datetime.datetime(2014, 3, 30),
        plot=False
        )
      cerebro.adddata(data0)
      cerebro.adddata(data1)
      # Run over everything
      cerebro.run()
      cerebro.plot(style='candle')
    


  • The indicators are sorted by datas when plotting with default options so should be no surprise that if no data shown, than no corresponding indicators shown as well. Try to use plotmaster parameter (Docs - Plotting - Object-wide plotting options) to show your indicator on the other data. Maybe this will help.



  • Hi ab_trade,

    Thanks for your prompt response. I will show what I understood from your answer. It actually printed the indicator, with the other indicator, but I am not sure if this is what you meant:
    So I modified the following line (added the plotmaster=self.sma0):
    From:

        # 2 days MovingAverageSimple indicator on data1
        self.sma1 = bt.indicators.SimpleMovingAverage(
            self.data1,period=2,plot=True,subplot=True)
    
    

    To:

        # 2 days MovingAverageSimple indicator on data1
        self.sma1 = bt.indicators.SimpleMovingAverage(
            self.data1,period=2,plot=True,subplot=True,plotmaster=self.sma0)
    
    

    And then it shows sma1 in the same window as sma0. Is this what you suggested?
    Any other workaround comes to mind?

    Screenshot from 2020-05-04 14-28-41.png



  • I might be wrong, but you may not be able to show it independently with the data turned off. Data feed is the only really independent plot, others are connected to it or connected to something else.

    As an idea - you may try to make separate indicator applied to data feed you show which will be calculated based on data feed you don't show. Or rework data feed you don't show into indicator, since it looks like it is not a data feed really, but some type of indicator.


Log in to reply
 

});