bukosabino / ta

Technical Analysis Library using Pandas and Numpy
https://technical-analysis-library-in-python.readthedocs.io/en/latest/
MIT License
4.26k stars 875 forks source link

FloatingPointError Division by zero #207

Open bshuler opened 3 years ago

bshuler commented 3 years ago

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

kengz commented 3 years ago

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.

siddagra commented 3 years ago

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