Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ADX Method Divide by Zero Error (ta 0.3.7) #36

Open
palisadoes opened this issue Nov 24, 2018 · 5 comments
Open

ADX Method Divide by Zero Error (ta 0.3.7) #36

palisadoes opened this issue Nov 24, 2018 · 5 comments

Comments

@palisadoes
Copy link

I've seen an issue the trs[i] value can equal zero giving this error:

/usr/local/lib/python3.6/dist-packages/ta/trend.py:170: RuntimeWarning: invalid value encountered in double_scalars
  dip[i] = 100 * (dip_mio[i]/trs[i])
/usr/local/lib/python3.6/dist-packages/ta/trend.py:174: RuntimeWarning: invalid value encountered in double_scalars
  din[i] = 100 * (din_mio[i]/trs[i])

I've checked the pandas series sent to adx, and there are no np.nan, or None values in them.

It needs to be handled in some way. The code doesn't crash, it just gives a warning.

ta version 0.3.7

@bizso09
Copy link

bizso09 commented Jan 9, 2019

it's because this line is leaving a 0 at the end

for i in range(1, len(trs)-1):
    trs[i] = trs[i-1] - (trs[i-1]/float(n)) + tr[n+i]

I haven't looked into it more, but the resulting adx has no nan

@DavidAlmasan
Copy link

Is there a fix in plan for this? Or is there a workaround that we can use until a fix is in place?

@codeninja
Copy link
Contributor

Bump for this as I'm running into the same problem using the latest master.

@codeninja
Copy link
Contributor

codeninja commented Oct 28, 2019

@bukosabino I have fixed the error with this issue. @DavidAlmasan

solution
Trend.py lines ~140 - 180

    trs = np.zeros(len(close) - (n - 1))
    trs[0] = tr.dropna()[0:n].sum()
    tr = tr.reset_index(drop=True)
    for i in range(0, len(trs)-1):
        trs[i+1] = trs[i] - (trs[i]/float(n)) + tr[n+i]

    up = high - high.shift(1)
    dn = low.shift(1) - low
    pos = abs(((up > dn) & (up > 0)) * up)
    neg = abs(((dn > up) & (dn > 0)) * dn)

    dip_mio = np.zeros(len(close) - (n - 1))
    dip_mio[0] = pos.dropna()[0:n].sum()

    pos = pos.reset_index(drop=True)
    for i in range(0, len(dip_mio)-1):
        dip_mio[i+1] = dip_mio[i] - (dip_mio[i]/float(n)) + pos[n+i]

    din_mio = np.zeros(len(close) - (n - 1))
    din_mio[0] = neg.dropna()[0:n].sum()

    neg = neg.reset_index(drop=True)
    for i in range(0, len(din_mio)-1):
        din_mio[i+1] = din_mio[i] - (din_mio[i]/float(n)) + neg[n+i]

The issue is an off by one error by which the trs variable was not having it's final value in the list set (thus remaining 0) and this resulted in a divide by 0 error when it encountered

    for i in range(len(trs)):
        dip[i] = 100 * (dip_mio[i]/trs[i])

    din = np.zeros(len(trs))
    for i in range(len(trs)):
        din[i] = 100 * (din_mio[i]/trs[i])

I will be submitting a PR for this.

@bukosabino
Copy link
Owner

Hi @codeninja ,

Thank you for your effort. I accepted your PR, but, when I test the results using your implementation, the tests didn't pass :(

Could you take a look again?

Best,
Dario

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants