backtesting.py icon indicating copy to clipboard operation
backtesting.py copied to clipboard

_stats.py:122: RuntimeWarning: divide by zero encountered in double_scalars

Open mablue opened this issue 3 years ago • 10 comments

Expected Behavior

/home/mab/Documents/RESEARCH/GARS/.env/lib/python3.9/site-packages/backtesting/_stats.py:122: RuntimeWarning: divide by zero encountered in double_scalars
  s.loc['Sortino Ratio'] = np.clip((annualized_return - risk_free_rate) / (np.sqrt(np.mean(day_returns.clip(-np.inf, 0)**2)) * np.sqrt(annual_trading_days)), 0, np.inf)  # noqa: E501

Actual Behavior

Steps to Reproduce

  1. Runnig a code that have 6 sma and time periods changes every time.
        n0 = 10
        n1 = 20
        n2 = 30
        n3 = 40
        n4 = 50
        n5 = 60

        def init(self):
            close = self.data.Close
            self.sma0 = self.I(SMA, close, round(self.n0))
            self.sma1 = self.I(SMA, close, round(self.n1))
            self.sma2 = self.I(SMA, close, round(self.n2))
            self.sma3 = self.I(SMA, close, round(self.n3))
            self.sma4 = self.I(SMA, close, round(self.n4))
            self.sma5 = self.I(SMA, close, round(self.n5))

Additional info

  • Backtesting version: 0.?.?

mablue avatar Jan 27 '22 14:01 mablue

same issue; any fix?

sharmaakhil100 avatar Oct 18 '22 21:10 sharmaakhil100

I have the same warning

samueltg92 avatar Oct 20 '22 20:10 samueltg92

Me too.I've same issue and I posted the issue on the github but they still ignoring about that

KnudY9 avatar Nov 26 '22 11:11 KnudY9

C:\Users\hoge\AppData\Local\Programs\Python\Python36\lib\site-packages\backtesting_stats.py:122: RuntimeWarning: divide by zero encountered in double_scalars s.loc['Sortino Ratio'] = np.clip((annualized_return - risk_free_rate) / (np.sqrt(np.mean(day_returns.clip(-np.inf, 0)**2)) * np.sqrt(annual_trading_days)), 0, np.inf) # noqa: E501

KnudY9 avatar Nov 26 '22 11:11 KnudY9

That's my error code. Thanks in advance

KnudY9 avatar Nov 26 '22 11:11 KnudY9

I am getting the same error on v.0.3.3. You can use: np.seterr(divide='ignore') This won't fix the problem, but it will turn off the error warning. In addition, as I understand it, this only affects the final result of the "'Sortino Ratio" so if you dont use this indicator, then its not so critical

Lanimel avatar Jan 03 '23 16:01 Lanimel

this only affects the final result of the "'Sortino Ratio" so if you dont use this indicator, then its not so critical

Thanks buddy

KnudY9 avatar Jan 03 '23 19:01 KnudY9

Thanks!

sharmaakhil100 avatar Jan 04 '23 07:01 sharmaakhil100