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 Williams R



  • Hi, I'm having this issue when using Williams R, I saw on other posts that for RSI for example there is a param safediv, but its not the case with Williams R.

    I was trying to debug and the error presents when I have same values with different timestamps.
    What would be the solution for this? Should I use another indicator? This data comes from BitMEX.

    name: ohlcv
    time close high low open symbol trades volume


    1526590500000000000 8210.5 8210.5 8210.5 8210.5 XBTUSD 0 304917
    1526590560000000000 8210.5 8210.5 8210.5 8210.5 XBTUSD 0 304917
    1526590620000000000 8210.5 8210.5 8210.5 8210.5 XBTUSD 0 304917
    1526590680000000000 8210.5 8210.5 8210.5 8210.5 XBTUSD 0 304917
    1526590740000000000 8210.5 8210.5 8210.5 8210.5 XBTUSD 0 304917
    1526590800000000000 8210.5 8210.5 8210.5 8210.5 XBTUSD 0 304917
    1526590860000000000 8210.5 8210.5 8210.5 8210.5 XBTUSD 0 304917
    1526590920000000000 8210.5 8210.5 8210.5 8210.5 XBTUSD 0 304917
    1526590980000000000 8210.5 8210.5 8210.5 8210.5 XBTUSD 0 304917
    1526591040000000000 8210.5 8210.5 8210.5 8210.5 XBTUSD 0 304917
    1526591100000000000 8210.5 8210.5 8210.5 8210.5 XBTUSD 0 304917
    1526591160000000000 8210.5 8210.5 8210.5 8210.5 XBTUSD 0 304917
    1526591220000000000 8210.5 8210.5 8210.5 8210.5 XBTUSD 0 304917
    1526591280000000000 8210.5 8210.5 8210.5 8210.5 XBTUSD 0 304917
    1526591340000000000 8210.5 8210.5 8210.5 8210.5 XBTUSD 0 304917
    1526591400000000000 8210.5 8210.5 8210.5 8210.5 XBTUSD 0 304917
    1526591460000000000 8210.5 8210.5 8210.5 8210.5 XBTUSD 0 304917
    1526591520000000000 8210.5 8210.5 8210.5 8210.5 XBTUSD 0 304917
    1526591580000000000 8210.5 8210.5 8210.5 8210.5 XBTUSD 0 304917
    1526591640000000000 8210.5 8210.5 8210.5 8210.5 XBTUSD 0 304917

    File "c:\program files (x86)\microsoft visual studio\shared\python36_64\lib\site-packages\backtrader\linebuffer.py", line 773, in _once_op
    dst[i] = op(srca[i], srcb[i])
    File "c:\program files (x86)\microsoft visual studio\shared\python36_64\lib\site-packages\backtrader\linebuffer.py", line 756, in once
    self._once_op(start, end)
    File "c:\program files (x86)\microsoft visual studio\shared\python36_64\lib\site-packages\backtrader\lineroot.py", line 165, in oncestart
    self.once(start, end)
    File "c:\program files (x86)\microsoft visual studio\shared\python36_64\lib\site-packages\backtrader\linebuffer.py", line 631, in _once
    self.oncestart(self._minperiod - 1, self._minperiod)
    File "c:\program files (x86)\microsoft visual studio\shared\python36_64\lib\site-packages\backtrader\lineiterator.py", line 292, in _once
    indicator._once()
    File "c:\program files (x86)\microsoft visual studio\shared\python36_64\lib\site-packages\backtrader\lineiterator.py", line 292, in _once
    indicator._once()
    File "c:\program files (x86)\microsoft visual studio\shared\python36_64\lib\site-packages\backtrader\cerebro.py", line 1652, in _runonce
    strat._once()
    File "c:\program files (x86)\microsoft visual studio\shared\python36_64\lib\site-packages\backtrader\cerebro.py", line 1293, in runstrategies
    self._runonce(runstrats)
    File "c:\program files (x86)\microsoft visual studio\shared\python36_64\lib\site-packages\backtrader\cerebro.py", line 1127, in run
    runstrat = self.runstrategies(iterstrat)
    File "c:\workspace\algo-trading\strategy\momentum_strategy.py", line 145, in <module>
    cerebro.run()
    File "c:\program files (x86)\microsoft visual studio\2017\enterprise\common7\ide\extensions\microsoft\python\core\packages\ptvsd\pydevd_pydev_imps_pydev_execfile.py", line 25, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
    File "c:\program files (x86)\microsoft visual studio\2017\enterprise\common7\ide\extensions\microsoft\python\core\packages\ptvsd\pydevd\pydevd.py", line 1035, in run
    pydev_imports.execfile(file, globals, locals) # execute the script
    File "c:\program files (x86)\microsoft visual studio\2017\enterprise\common7\ide\extensions\microsoft\python\core\packages\ptvsd\pydevd\pydevd.py", line 1628, in main
    globals = debugger.run(setup['file'], None, None, is_module)
    File "c:\program files (x86)\microsoft visual studio\2017\enterprise\common7\ide\extensions\microsoft\python\core\packages\ptvsd_main.py", line 99, in _run
    except SystemExit as ex:
    File "c:\program files (x86)\microsoft visual studio\2017\enterprise\common7\ide\extensions\microsoft\python\core\packages\ptvsd_main.py", line 47, in run_file
    run(argv, addr, **kwargs)
    File "c:\program files (x86)\microsoft visual studio\2017\enterprise\common7\ide\extensions\microsoft\python\core\packages\ptvsd\debugger.py", line 36, in debug
    run(address, filename, *args, **kwargs)
    File "c:\program files (x86)\microsoft visual studio\2017\enterprise\common7\ide\extensions\microsoft\python\core\ptvsd_launcher.py", line 111, in <module>
    vspd.debug(filename, port_num, debug_id, debug_options, run_as)


  • administrators

    @lucas-condé said in ZeroDivisionError: float division by zero on Williams R:

    I saw on other posts that for RSI for example there is a param safediv, but its not the case with Williams R.

    Because some people found the problem before with an indicator known to work better than WilliamsR

    The real problem being the application of those indicators to scenarios where the variability of the prices is NULL for the considered period, i.e.: the trading is so thin that there is actuallly no trading and no price change. And the lack of variability creates a division by zero.

    And the designers/creators of the indicators were working in markets where things happen. Try to answer what has to happen when there is no variability during the entire considered period

    • The indicator goes to 100
    • The indicator goes to 0
    • The indicator goes to 50
    • The indicator remains fixed at some other value ... (maybe the last value before the Exception)

    You choose.

    What would be the solution for this?

    At some point in time the indicator will be patched to avoid raising the exception. But this is not the solution to the actual problem, which is the application of an non-appropriate indicator to a data set.

    @lucas-condé said in ZeroDivisionError: float division by zero on Williams R:

    Should I use another indicator?

    That's for you to decide, obviously.


  • administrators

    In any case notice that the definition of the WilliamsR indicator was actually not original and is just the inverse of the fast line of the indicator (%k)

    See one reference which describes it: https://stockcharts.com/school/doku.php?id=chart_school:technical_indicators:williams_r

    Which implies that (as mentioned in the article) you can use the %k of the Fast Stochastic instead of the %R of WilliamsR



  • Thank you for your feedback, I will switch to another indicator.