ta icon indicating copy to clipboard operation
ta copied to clipboard

FloatingPointError Division by zero

Open bshuler opened this issue 3 years ago • 2 comments

trend.py:643

I have revised this to:

    for i in range(len(self._trs)):
        try:
            dip[i] = 100 * (self._dip[i]/self._trs[i])
        except FloatingPointError as e:
            dip[i] = 0
            logger.error(e)

but I am not sure this is what you expect or intend.

We should not divide by zero

bshuler avatar Nov 23 '20 17:11 bshuler

This is for the ADX feature. Encountered the same issue, here are the logs

     # /Users/XXX/miniconda3/envs/XX/lib/python3.9/site-packages/ta/trend.py:768: RuntimeWarning: invalid value encountered in double_scalars
     #   dip[i] = 100 * (self._dip[i] / self._trs[i])
     # /Users/XXX/miniconda3/envs/XX/lib/python3.9/site-packages/ta/trend.py:772: RuntimeWarning: invalid value encountered in double_scalars
     #   din[i] = 100 * (self._din[i] / self._trs[I])

Seems like the last index (the value of self._trs[-1]) is always 0, hence the division error. Not sure if the result produced is still correct, but seems like the ADX results are all proper numeric values.

kengz avatar Dec 28 '20 14:12 kengz

same error when running code from documentation example at https://technical-analysis-library-in-python.readthedocs.io/en/latest/

code:

import pandas as pd
from ta import add_all_ta_features
from ta.utils import dropna
import json

with open("minuteDataFormatted/ABB-EQ.json") as f:
    data = json.load(f)

df = pd.DataFrame.from_dict(data["history"])

df = dropna(df)

df = add_all_ta_features(df, "open", "high", "low",
                         "close", "volume", fillna=True)

error:

RuntimeWarning: invalid value encountered in double_scalars
  dip[i] = 100 * (self._dip[i] / self._trs[i])
C:\Users\Admin\AppData\Roaming\Python\Python39\site-packages\ta\trend.py:772: RuntimeWarning: invalid value encountered in double_scalars
  din[i] = 100 * (self._din[i] / self._trs[i])

siddagra avatar Aug 08 '21 05:08 siddagra