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 as a param for another indicator is NaN

    General Code/Help
    2
    6
    125
    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

      class UpperLines(bt.Indicator):
          lines = ('upperlines',)
          params = dict(atr_factor=1.5)
          res_smooth = 14
          res_period = 10
          
          def __init__(self):
              hsma = bt.talib.SMA(self.data.high, timeperiod=self.res_smooth)
              res = bt.talib.MAX(hsma, timeperiod=self.res_period)
              atr = bt.talib.ATR(self.data.high, self.data.low, self.data.close) * self.params.atr_factor
              self.lines.upperlines = res
              # TODO: figure out why this line doesn't like 'res'
              # self.lines.upperlines = res + self.atr
      

      With my current understanding, an indicator is a "line" which then can be played with. So I then would pass hsma into a MAX function. So my question is: how can I get the values from bt.talib.SMA to pass into the MAX function, which I believe is responsible for the NaN result of res?

      run-out 1 Reply Last reply Reply Quote 0
      • run-out
        run-out @LeggoMaEggo last edited by

        @leggomaeggo FYI I ran your code and it's working fine. You should check your talib installation. https://www.backtrader.com/docu/talib/talib/

        RunBacktest.com

        LeggoMaEggo 2 Replies Last reply Reply Quote 1
        • LeggoMaEggo
          LeggoMaEggo @run-out last edited by

          @run-out That is very odd. Talib works since bt.talib.SMA is able to return values that isn't NaN and is set and plotted correctly if I just do `self.lines.upperlines = hsma. Perhaps calling the indicator class is wrong?

          class BaseStrategy(bt.Strategy):
              def __init__(self):
                  super().__init__()
                  self.ures = UpperLines()
          
          class LinesStrategy(BaseStrategy):
              params = (
                  ('maperiod', 15),
              )
              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
                  super().__init__()
                  self.dataclose = self.datas[0].close
          
              def next(self):
                  super().next()
                  # Simply log the closing price of the series from the reference
                  self.log('Close, %.2f' % self.dataclose[0])
                  print(self.ures[0])
                  
          
          1 Reply Last reply Reply Quote 0
          • LeggoMaEggo
            LeggoMaEggo @run-out last edited by

            @run-out Still having a bit of trouble figuring this out. Still NaN for res. Maybe the Strategy implementation/inheritance is wrong as seen above?

            run-out 1 Reply Last reply Reply Quote 0
            • run-out
              run-out @LeggoMaEggo last edited by

              @leggomaeggo
              If your code was running fine on my machine, then the code is good. You have a data or setup with talib problem. Try running the same code I did on a different dataset, something quit different.

              RunBacktest.com

              LeggoMaEggo 1 Reply Last reply Reply Quote 1
              • LeggoMaEggo
                LeggoMaEggo @run-out last edited by

                @run-out Ah, yup. It's a data-feed problem. Using a different data-feed, everything works as expected. Will look into as to why the data-feed isn't working as expected. Thank you!

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