Backtrader Community

    • 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/

    AttributeError: 'Lines_LineSeries_LineIterator_DataAccessor_Indicat' object has no attribute '_addobserver'

    General Code/Help
    1
    1
    176
    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.
    • U
      utkarsh last edited by

      I am facing this error in the following code:
      AttributeError: 'Lines_LineSeries_LineIterator_DataAccessor_Indicat' object has no attribute '_addobserver'

      import pandas as pd
      import yfinance as yf
      from datetime import date
      import backtrader as bt
      
      
      cerebro=bt.Cerebro()
      data = bt.feeds.PandasData(dataname=yf.download('MSFT', date(2020,1,1), date.today()))
      class DonchianChannels(bt.Indicator):
          '''
          Params Note:
            - ``lookback`` (default: -1)
              If `-1`, the bars to consider will start 1 bar in the past and the
              current high/low may break through the channel.
              If `0`, the current prices will be considered for the Donchian
              Channel. This means that the price will **NEVER** break through the
              upper/lower channel bands.
          '''
      
          alias = ('DCH', 'DonchianChannel',)
      
          lines = ('dcm', 'dch', 'dcl',)  # dc middle, dc high, dc low
          params = dict(
              period=20,
              lookback=-1,  # consider current bar or not
          )
      
          plotinfo = dict(subplot=False)  # plot along with data
          plotlines = dict(
              dcm=dict(ls='--'),  # dashed line
              dch=dict(_samecolor=True),  # use same color as prev line (dcm)
              dcl=dict(_samecolor=True),  # use same color as prev line (dch)
          )
      
          def __init__(self):
              self.dataclose = self.datas[0].close
              hi, lo = self.data.high, self.data.low
              if self.p.lookback:  # move backwards as needed
                  hi, lo = hi(self.p.lookback), lo(self.p.lookback)
      
              self.l.dch = bt.ind.Highest(hi, period=self.p.period)
              self.l.dcl = bt.ind.Lowest(lo, period=self.p.period)
              self.l.dcm = (self.l.dch + self.l.dcl) / 2.0  # avg of the above
          def next(self):
              if self.dataclose[0] > self.l.dch[0]:
                  self.sell()
              elif self.dataclose[0] < self.l.dcl[0]:
                  self.buy()
      cerebro.adddata(data)
      cerebro.addstrategy(DonchianChannels)
      print('Starting Portfolio Value: %.2f' % cerebro.broker.getvalue())
      
      cerebro.run()
      print('Final Portfolio Value: %.2f' % cerebro.broker.getvalue())
      1 Reply Last reply Reply Quote 0
      • 1 / 1
      • First post
        Last post
      Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors