Possible division by zero in `AnnualReturn`



  • I'm optimizing parameters for strategy.
    Sometimes when strategy is full of fear, so perform no action. In suck situations ZeroDivisionError: float division by zero occur.

    Traceback (most recent call last):
      File "D:/Projects/trading-bot/main/lab/backtrader/morning_buy.py", line 86, in <module>
    	strategies = back_trader.run(timeframe=bt.TimeFrame.Minutes)
      File "C:\Users\Home\Anaconda3\lib\site-packages\backtrader\cerebro.py", line 794, in run
    	runstrat = self.runstrategies(iterstrat)
      File "C:\Users\Home\Anaconda3\lib\site-packages\backtrader\cerebro.py", line 927, in runstrategies
    	strat._stop()
      File "C:\Users\Home\Anaconda3\lib\site-packages\backtrader\strategy.py", line 421, in _stop
    	analyzer._stop()
      File "C:\Users\Home\Anaconda3\lib\site-packages\backtrader\analyzer.py", line 194, in _stop
    	self.stop()
      File "C:\Users\Home\Anaconda3\lib\site-packages\backtrader\analyzers\annualreturn.py", line 84, in stop
    	annualret = (value_end / value_start) - 1.0
    ZeroDivisionError: float division by zero
    

    I checked that

    b_trader.broker.setcash(1000.0)
    

    was called earlier.


  • administrators

    AnnualReturn is really a relic. It was superseded by TimeReturn which is configurable for different timeframes and can even track datas

    In any case that error seems likely to happen when there has been no year change in the data.



  • Thanks for suggestion, I replace it with:

    back_trader.addanalyzer(TimeReturn, timeframe=bt.TimeFrame.Years, _name='annualreturn')
    

    Looks loke pretty the same for SharpeRatio:

    Traceback (most recent call last):
      File "D:/Projects/trading-bot/main/lab/backtrader/morning_buy.py", line 87, in <module>
    	strategies = back_trader.run(timeframe=bt.TimeFrame.Minutes)
      File "C:\Users\Home\Anaconda3\lib\site-packages\backtrader\cerebro.py", line 794, in run
    	runstrat = self.runstrategies(iterstrat)
      File "C:\Users\Home\Anaconda3\lib\site-packages\backtrader\cerebro.py", line 927, in runstrategies
    	strat._stop()
      File "C:\Users\Home\Anaconda3\lib\site-packages\backtrader\strategy.py", line 421, in _stop
    	analyzer._stop()
      File "C:\Users\Home\Anaconda3\lib\site-packages\backtrader\analyzer.py", line 194, in _stop
    	self.stop()
      File "C:\Users\Home\Anaconda3\lib\site-packages\backtrader\analyzers\sharpe.py", line 173, in stop
    	ret_free_avg = average(ret_free)
      File "C:\Users\Home\Anaconda3\lib\site-packages\backtrader\mathsupport.py", line 38, in average
    	return math.fsum(x) / (len(x) - bessel)
    ZeroDivisionError: float division by zero

  • administrators

    That happens because the SharpeRatio is being used with less data than the timeframe with which is working.

    @Maxim-Korobov said in Possible division by zero in `AnnualReturn`:

               ret_free = [r - rate for r in returns]
               ret_free_avg = average(ret_free)
    

    The average fails because the number of returns is 0

    Change the timeframe of the analyzer to something for which returns can be generated.


Log in to reply
 

Looks like your connection to Backtrader Community was lost, please wait while we try to reconnect.