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/

    Indicator Init Error

    Indicators/Strategies/Analyzers
    2
    4
    49
    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.
    • Michael ling
      Michael ling last edited by

      Hi, I am trying to analyze a ATR difference between ta-lib and backtrader. So i create a naive code sample to dump all bt ATR value. But out of no reason, bt.ATR init seems lost its default params (p.movav = None).

      Can some help take a look?

      import backtrader as bt
      import pandas as pd
      
      
      class atr_start(bt.Strategy):
      
          def __init__(self):
              self.atr20 = bt.ind.ATR( period=20 )
      
          def next(self):
              print(f"Date: {self.data.datetime.datetime(0):%Y-%m-%d} ",
                    f"ATR20: {self.atr20.atr[0]}")
      
          def stop(self):
              atr = self.atr20.get(size=len(self.atr20))
              dt = map(bt.utils.num2date, self.data.datetime.get(size=len(self.data.datetime)))
      
              df = pd.DataFrame(list(zip(dt, atr)), columns=['datetime', 'atr20'])
              df.set_index('datetime', inplace=True)
              df.to_csv("atr20.csv")
      
      
      if __name__ == '__main__':
      
          cerebro = bt.Cerebro(stdstats=False)
      
          df = pd.read_csv('2017-2022-BNB.csv', parse_dates=True, index_col=0)
          data = bt.feeds.PandasData(dataname=df, name='BNB', timeframe=bt.TimeFrame.Days)
      
          cerebro.adddata(data)
          cerebro.addstrategy(atr_start)
      
          cerebro.run()
      
          # END
      
      
      Michael ling 1 Reply Last reply Reply Quote 0
      • Michael ling
        Michael ling @Michael ling last edited by

        @michael-ling attach error msg here:

        Traceback (most recent call last):
          File "/Volumes/Extension/Projects/backtrader/samples/atr20/atr20.py", line 34, in <module>
            cerebro.run()
          File "/Volumes/Extension/Projects/backtrader/backtrader/cerebro.py", line 1127, in run
            runstrat = self.runstrategies(iterstrat)
          File "/Volumes/Extension/Projects/backtrader/backtrader/cerebro.py", line 1217, in runstrategies
            strat = stratcls(*sargs, **skwargs)
          File "/Volumes/Extension/Projects/backtrader/backtrader/metabase.py", line 88, in __call__
            _obj, args, kwargs = cls.doinit(_obj, *args, **kwargs)
          File "/Volumes/Extension/Projects/backtrader/backtrader/metabase.py", line 78, in doinit
            _obj.__init__(*args, **kwargs)
          File "/Volumes/Extension/Projects/backtrader/samples/atr20/atr20.py", line 8, in __init__
            self.atr20 = bt.ind.ATR( period=20 )
          File "/Volumes/Extension/Projects/backtrader/backtrader/indicator.py", line 53, in __call__
            return super(MetaIndicator, cls).__call__(*args, **kwargs)
          File "/Volumes/Extension/Projects/backtrader/backtrader/metabase.py", line 88, in __call__
            _obj, args, kwargs = cls.doinit(_obj, *args, **kwargs)
          File "/Volumes/Extension/Projects/backtrader/backtrader/metabase.py", line 78, in doinit
            _obj.__init__(*args, **kwargs)
          File "/Volumes/Extension/Projects/backtrader/backtrader/indicators/atr.py", line 121, in __init__
            self.lines.atr = self.p.movav(TR(self.data), period=self.p.period)
        TypeError: 'NoneType' object is not callable
        
        Michael ling 1 Reply Last reply Reply Quote 0
        • Michael ling
          Michael ling @Michael ling last edited by

          @michael-ling sorry, i spotted a code error, this is solved. Though, I don't know how to delete/revise my post?

          Pierre Cilliers 0 1 Reply Last reply Reply Quote 0
          • Pierre Cilliers 0
            Pierre Cilliers 0 @Michael ling last edited by

            @michael-ling @michael-ling

            Just for future users arriving here. Would you please post your solution.

            I think it might be due to the fact that you are not passing in any data to your ATR indicator. Therefore it should be something like this instead:

            self.atr20 = bt.ind.ATR(self.data, period=20)
            
            1 Reply Last reply Reply Quote 0
            • 1 / 1
            • First post
              Last post
            Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors