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/

    ZeroDivisionError: float division by zero on RSI

    Indicators/Strategies/Analyzers
    2
    5
    3043
    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.
    • M
      Mike last edited by

      I'm receiving following error on 15m time frame, on other timeframes strategy works well

      Starting Portfolio Value: 100000.00
      Traceback (most recent call last):
      File "/home/bs/SOFT/ALGO/pierwszy.py", line 189, in <module>
      cerebro.run()
      File "/home/bs/.local/lib/python3.6/site-packages/backtrader/cerebro.py", line 1127, in run
      runstrat = self.runstrategies(iterstrat)
      File "/home/bs/.local/lib/python3.6/site-packages/backtrader/cerebro.py", line 1293, in runstrategies
      self._runonce(runstrats)
      File "/home/bs/.local/lib/python3.6/site-packages/backtrader/cerebro.py", line 1652, in _runonce
      strat._once()
      File "/home/bs/.local/lib/python3.6/site-packages/backtrader/lineiterator.py", line 292, in _once
      indicator._once()
      File "/home/bs/.local/lib/python3.6/site-packages/backtrader/lineiterator.py", line 292, in _once
      indicator._once()
      File "/home/bs/.local/lib/python3.6/site-packages/backtrader/linebuffer.py", line 632, in _once
      self.once(self._minperiod, self.buflen())
      File "/home/bs/.local/lib/python3.6/site-packages/backtrader/linebuffer.py", line 756, in once
      self._once_op(start, end)
      File "/home/bs/.local/lib/python3.6/site-packages/backtrader/linebuffer.py", line 773, in _once_op
      dst[i] = op(srca[i], srcb[i])
      ZeroDivisionError: float division by zero

      Process finished with exit code 1

      The strategy I'm using, if I change period to 21 everything is working fine

      class fs(bt.Strategy):

      def __init__(self):
          self.rsi = bt.indicators.RSI_SMA(self.data.close, period=14)
      
      def next(self):
          if not self.position:
              if self.rsi < 30:
                  self.buy(size=1)
          else:
              if self.rsi > 70:
                  self.sell(size=1)
      
      1 Reply Last reply Reply Quote 0
      • B
        backtrader administrators last edited by

        For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/
        
        1 Reply Last reply Reply Quote 0
        • B
          backtrader administrators last edited by

          Thin trading, values don't change.

          Docs - Indicator Reference - RSI_Safe

          M 1 Reply Last reply Reply Quote 0
          • M
            Mike @backtrader last edited by

            @backtrader Thank you safediv resolved the issue

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

              It's not a perfect solution. Because the original definition of the RSI doesn't have provisions for the case in which a DivisionByZero happens. This only happens when the prices don't change over time, which was possibly not contemplated by the author, back then.

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