Backtrader Community

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    1. Home
    2. monke
    For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/
    M
    • Profile
    • Following 0
    • Followers 0
    • Topics 1
    • Posts 1
    • Best 0
    • Controversial 0
    • Groups 0

    monke

    @monke

    0
    Reputation
    1
    Profile views
    1
    Posts
    0
    Followers
    0
    Following
    Joined Last Online

    monke Unfollow Follow

    Latest posts made by monke

    • Custom Indicator unable to find line attributes in the passed datafeed; 'Lines_LineSeries_LineIterator_DataAccessor_Indicat' object has no attribute

      I created a custom indicator which I intend to use on multiple data feeds. In the init method of my strategy, I created a line object 'spread' for the feeds (it will be used in the indicator). However, the line attribute is not being found when I use it in my indicator
      'Lines_LineSeries_LineIterator_DataAccessor_Indicat' object has no attribute 'spread'

      Here is my code:

      class em_fit_converter(bt.Indicator):
          lines = ('A','B','C','D','G','H','k')
          params = (('period', 50),)
          W = 50 #trailing window used for em_fit in everyday calculation
          max_iter = 1000
          
          
          def next(self):
              n = len(self.datas) - 1
              self.lines.A[0], self.lines.B[0], self.lines.C[0],
              self.lines.D[0], self.lines.G[0], self.lines.H[0],
              self.lines.k[0] = em_fit(self.spread.get(size = self.p.period), self.market_rets.get(size = self.p.period), 
                                       init_params, num_iter = max_iter)
      
      class testStrategy(bt.Strategy):
          
          
          def __init__(self):
              
              n = len(self.datas) - 1                           #will be used to point to log market returns (last data feed)
              
              for i,d in enumerate(self.datas[0:n]):                 #data order in self.datas same as that of stocks listed in the pairs array
                  if i%2 == 0:                                  #for even indices in self.datas aka first stock in every pair
                      self.spread = self.datas[i].close - self.datas[i+1].close
                      self.market_rets = self.datas[n].close
                      self.lines.values = em_fit_converter(self.datas[i])
                      self.theta = self.values.lines.B/(1 - self.values.lines.A)
                      self.true_spread = self.spread - self.values.lines.D * self.datas[n].close
                      self.cum_sum_true_spread = sum(self.true_spread.get(size = 5))
                      
          def next(self):
              
              for i,d in enumerate(self.datas[0:n]):
                  if i%2 == 0:
                      if self.cum_sum_spread < self.theta:
                          self.buy(data = d, size = 10)
                          self.sell(data = self.datas[i+1], size = 10)
                          
                      if self.cum_sum_spread > self.theta:
                          self.sell(data = d, size = 10)
                          self.buy(data = self.datas[i+1], size = 10)
      

      Here is how im adding the indicator, strategy etc. to cerebro (I have not included the code that adds the data feeds here)

      cerebro.addstrategy(testStrategy)
      cerebro.addindicator(em_fit_converter)
      cerebro.addanalyzer(bt.analyzers.SharpeRatio, _name = 'sharpe')
      test = cerebro.run(runonce = False)
      

      This is the error:
      AttributeError: 'Lines_LineSeries_LineIterator_DataAccessor_Indicat' object has no attribute 'spread'

      Any help is appreciated. Thanks in advance!

      posted in Indicators/Strategies/Analyzers indicator custom ind
      M
      monke