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



  • 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)

  • administrators

    For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/
    

  • administrators

    Thin trading, values don't change.

    Docs - Indicator Reference - RSI_Safe



  • @backtrader Thank you safediv resolved the issue


  • administrators

    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.