Navigation

    Backtrader Community

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    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

    Indicators/Strategies/Analyzers
    2
    5
    1296
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • M
      mothee last edited by mothee

      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__()
      
      
      
      1 Reply Last reply Reply Quote 0
      • M
        mothee last edited by

        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?

        1 Reply Last reply Reply Quote 0
        • B
          backtrader administrators last edited by

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

          M 1 Reply Last reply Reply Quote 0
          • M
            mothee @backtrader last edited by

            @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()
            
            1 Reply Last reply Reply Quote 0
            • B
              backtrader administrators last edited by

              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.

              1 Reply Last reply Reply Quote 0
              • 1 / 1
              • First post
                Last post
              Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors