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

Why a simple user defined indicator failed to find the self.data.high



  • It failed when I test if with error 'uiltins.AttributeError: 'Lines_LineSeries_LineSeriesStub' object has no attribute 'high'', seem the self.data.high not exist.

    #!/usr/bin/env python
    # -*- coding: utf-8; py-indent-offset:4 -*-
    from __future__ import (absolute_import, division, print_function,
                            unicode_literals)
    
    from backtrader.indicators import Indicator
    
    class RangeInd(Indicator):
        '''
        Measures the range of prices in a period
    
        Formula:
          - rng = (data.high - data.low) 
    
        '''
        alias = ('RNG',)
    
        lines = ('rng',)
    
         
    
        def __init__(self):
            self.lines.rng =  self.data.high - self.data.low
            super(RangeInd, self).__init__()
    
    
    


  • after I add a parameter params = (('period', 20),) in the class RangeInd, the test run successfully. Why must I add a params into the class?


  • administrators

    There is no need to add params. But you don't even show how you run things ... no way to know what was broken.



  • @backtrader
    The main test fragement as below:

    data = bt.feeds.PandasData(dataname = data_df,
                                       timeframe = timeframe,
                                       #fromdate=datetime.datetime.strptime(self.dateInterval[0], '%Y%m%d'),
                                       #todate=datetime.datetime.strptime(self.dateInterval[1], '%Y%m%d'),
                                       datetime = EXT_Bar_Date,
                                       open = EXT_Bar_Open,
                                       high = EXT_Bar_High,
                                       low = EXT_Bar_Low,
                                       close = EXT_Bar_Close,
                                       volume = EXT_Bar_Volume,
                                       openinterest=-1)
            
            cerebro = bt.Cerebro(stdstats=False)
            # Add the Data Feed to Cerebro
            cerebro.adddata(data)
    
            kwargs_i['plotname'] = '_'.join([plot_name, kwargs_i_name]) #u can replace by a const string here.
            cerebro.addindicator(func_ind, **kwargs_i)
            cerebro.run()
    

  • administrators

    See, I do understand that your code is yours, but that is far away from being any test script.

    Furthermore, it is for sure not part of any test script because the indentation is wrong which would immediately generate a SyntaxError.

    The easiest way, also for you, to reproduce the error and really narrow it down to the lack of a params definition is to define a very simple script which actually reproduces the error. See for example this in your script

    cerebro.addindicator(func_ind, **kwargs_i)
    

    What's func_ind? Without ruling out a problem in backtrader itself, you may be passing the wrong thing to addindicator. Just a long shot.