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/

    Indicator NaN in Strategy class but not in Indicator class

    General Code/Help
    2
    2
    124
    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.
    • LeggoMaEggo
      LeggoMaEggo last edited by

      I am missing something such that the indicator line isn't being read properly in the strategy, but is read properly in the indicator itself?

      class SlopeCalc(bt.Indicator):
          lines = ('slopescalcs',)
          params = dict(typeToCalc='volatility', period=5)
      
          def __init__(self):
              if self.params.typeToCalc == 'volatility':
                  self.lines.slopecalc = bt.talib.VAR(self.data.close, timeperiod=self.params.period, nbdev=1.0)
          
          def next(self):
              print("in ind", self.lines.slopecalc[0])
      
      
      class LaneStrategy(bt.Strategy):
      
          def log(self, txt, dt=None):
              ''' Logging function for this strategy'''
              dt = dt or self.datas[0].datetime.date(0)
              print('%s, %s' % (dt.isoformat(), txt))
      
          def __init__(self):
              # Keep a reference to the "close" line in the data[0] dataseries
              self.dataclose = self.datas[0].close
      
              self.slopevolat = SlopeCalc()
      
      
          def next(self):
              print("in strat",self.slopevolat[0])
      
      

      Printing yields:

      in ind 6.162160000044992e-07
      in strat nan
      in ind 5.590959997192613e-07
      in strat nan
      in ind 3.006904000013577e-06
      in strat nan
      in ind 8.901303999886423e-06
      in strat nan
      in ind 1.0513023999569882e-05
      in strat nan
      in ind 6.540775999708259e-06
      in strat nan
      in ind 4.6533039999019365e-06
      in strat nan
      in ind 1.3609624000210374e-05
      in strat nan
      in ind 2.923282400013605e-05
      in strat nan
      in ind 2.4948944000069417e-05
      in strat nan
      in ind 1.0268935999757645e-05
      .....
      
      run-out 1 Reply Last reply Reply Quote 0
      • run-out
        run-out @LeggoMaEggo last edited by

        @leggomaeggo said in Indicator NaN in Strategy class but not in Indicator class:

        lines = ('slopescalcs',)

        Missing an s

        if self.params.typeToCalc == 'volatility':
                    self.lines.slopecalc = bt.talib.VAR(self.data.close, timeperiod=self.params.period, nbdev=1.0)
            
        

        RunBacktest.com

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