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/

    QQE Indicator

    Indicators/Strategies/Analyzers
    3
    6
    441
    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.
    • C
      CooleRnax last edited by

      Hello,

      I'm trying to implement QQE indicator.
      I ended up facing the error.
      How can I fix this using elegant backtrader way?

      https://www.tradingview.com/script/aCIR3UdM-Quantitative-Qualitative-Estimation-QQE/

      error:

      self.atr = abs(self.rsi_ma[-1] - self.rsi_ma[0])
      IndexError: array index out of range
      

      code:

      class QQE(bt.Indicator):
          lines = ('rsi_ma', 'fast', 'slow',)
          plotinfo = dict(subplot=False)
      
          params = dict(
              period=14,
              period_ma=5,
              qqe=4.236,
      
              period_wilders=0,
          )
      
          def __init__(self):
              self.p.period_wilders = self.p.period * 2 - 1
              self.rsi = bt.ind.RSI(self.data, period=self.p.period)
              self.rsi_ma = bt.ind.EMA(self.rsi, period=self.p.period_ma)
      
              self.atr = abs(self.rsi_ma[-1] - self.rsi_ma[0])
              self.atr_ma = bt.ind.EMA(self.atr, period=self.p.period_wilders)
      
              self.dar = bt.ind.EMA(
              self.atr_ma, period=self.p.period_wilders) * self.p.qqe
      
          def next(self):
              self.lines.rsi_ma[0] = self.rsi_ma[0]
              self.lines.fast[0] = self.rsi_ma[0] + self.dar[0]
              self.lines.slow[0] = self.rsi_ma[0] - self.dar[0]
      
      run-out 1 Reply Last reply Reply Quote 0
      • run-out
        run-out @CooleRnax last edited by

        @CooleRnax You may wish to consider changing the error line to:

        self.atr = abs(self.rsi_ma(-1) - self.rsi_ma)
        

        Your indicator runs for me using this.

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

          @run-out
          I ended up with this would be nice to see it build in

          class QQE(bt.Indicator):
              lines = ('slow', 'fast',)
              plotinfo = dict(subplot=False)
          
              params = dict(
                  period=14,
                  period_ma=5,
                  qqe=4.236,
          
                  period_wilders=0,
              )
          
              def __init__(self):
                  self.p.period_wilders = self.p.period * 2 - 1
                  rsi = bt.ind.RSI(self.data, period=self.p.period)
                  self.lines.slow = bt.ind.EMA(rsi, period=self.p.period_ma)
          
                  atr = abs(self.lines.slow(-1) - self.lines.slow)
                  atr_ma = bt.ind.EMA(atr, period=self.p.period_wilders)
          
                  dar = bt.ind.EMA(atr_ma, period=self.p.period_wilders) * self.p.qqe
          
                  self.lines.fast = QQEFast(self.lines.slow, dar)
                  super(QQE, self).__init__()
          
          
          class QQEFast(bt.Indicator):
              lines = ('fast', 'band_long', 'band_short')
              plotinfo = dict(subplot=False)
          
              def __init__(self):
                  super(QQEFast, self).__init__()
          
              def next(self):
                  band_long_new = self.data0[0] - self.data1[0]
                  band_short_new = self.data0[0] + self.data1[0]
                  # print(band_long_new)
          
                  if self.data0[-1] > self.lines.band_long[-1] \
                          and self.data0[0] > self.lines.band_long[-1]:
                      band_long = max(band_long_new, self.lines.band_long[-1])
                  else:
                      band_long = band_long_new
          
                  if self.data0[-1] < self.lines.band_short[-1] \
                          and self.data0[0] < self.lines.band_short[-1]:
                      band_short = min(band_short_new, self.lines.band_short[-1])
                  else:
                      band_short = band_short_new
          
                  self.lines.band_long[0] = band_long
                  self.lines.band_short[0] = band_short
          
                  if self.data0[0] > self.lines.band_short[-1]:
                      fast = band_long
                  elif self.data0[0] < self.lines.band_long[-1]:
                      fast = band_short
                  else:
                      fast = band_long
          
                  self.lines.fast[0] = fast
          B 1 Reply Last reply Reply Quote 0
          • B
            backtrader administrators @CooleRnax last edited by

            @CooleRnax said in QQE Indicator:

            class QQEFast(bt.Indicator):
                lines = ('fast', 'band_long', 'band_short')
                plotinfo = dict(subplot=False)
            
                def __init__(self):
                    super(QQEFast, self).__init__()
            
                def next(self):
                    band_long_new = self.data0[0] - self.data1[0]
                    band_short_new = self.data0[0] + self.data1[0]
                   if self.data0[-1] > self.lines.band_long[-1] \
                           and self.data0[0] > self.lines.band_long[-1]
            

            That's broken. There is no restriction to ensure that -1 (i.e.: at least one value before) is available in the buffer.

            You need to specifically add a minimum period during __init__ (which by the way is an empty non-needed method right now) or use the notation given by @run-out to you to let the framework know from the start what your restriction is.

            C 1 Reply Last reply Reply Quote 0
            • C
              CooleRnax @backtrader last edited by

              @backtrader
              It seems ok,
              if there is no buffer it uses else as it is designed
              https://www.tradingview.com/script/IYfA9R2k-QQE-MT4/

              1 Reply Last reply Reply Quote 0
              • B
                backtrader administrators last edited by backtrader

                If there is no buffer ... this will happen ...

                Exception was raised at line ... Index Error
                

                What the condition indicates is: if the previous data point is greater than the previous stored line value do something, if not ... do something else. But looking into the past buffer (-1) will break if there is no buffer, it won't simply jump into the else statement.

                1 Reply Last reply Reply Quote 0
                • 1 / 1
                • First post
                  Last post
                Copyright © 2016, 2017, 2018 NodeBB Forums | Contributors
                $(document).ready(function () { app.coldLoad(); }); }