Skip to content
This repository has been archived by the owner on Feb 19, 2022. It is now read-only.

Calculation RSI (wrong) #6

Open
jorgeog96 opened this issue Jun 5, 2018 · 2 comments
Open

Calculation RSI (wrong) #6

jorgeog96 opened this issue Jun 5, 2018 · 2 comments

Comments

@jorgeog96
Copy link

Hello guys, I am using: Python 3.6

Before, I implemented this RSI formula and pass df['Close'] prices and compute the result inside a numpy array:

def rsiFunc(prices, n=14):
    deltas = np.diff(prices)
    seed = deltas[:n+1]
    up = seed[seed>=0].sum()/n
    down = -seed[seed<0].sum()/n
    rs = up/down
    rsi = np.zeros_like(prices)
    rsi[:n] = 100. - 100./(1.+rs)

    for i in range(n, len(prices)):
         delta = deltas[i-1] # cause the diff is 1 shorter

        if delta>0:
            upval = delta
           downval = 0.
       else:
           upval = 0.
          downval = -delta

        up = (up*(n-1) + upval)/n
        down = (down*(n-1) + downval)/n

        rs = up/down
        rsi[i] = 100. - 100./(1.+rs)

    return rsi

Using this, I called my function:

      rsi = rsiFunc(df['Close'],14)

And the result something like this (example, 5 last elements of the array):

  [52.40554313 54.57421201 54.57421201 52.37816137 57.20738346]

But now using your RSI formula and the same df I obtain the following result using the exact same data before: (last 3 elements)

  0.398692 0.398692 0.478656

What is happening?

@deepbrook
Copy link
Collaborator

@jorgeog96 ,
Thanks for raising this issue!
Unfortunately I'm super swamped with work and currently don't have much time to look into this issue.
If there's anyone willing to look into this in the meantime, I'd be grateful. Otherwise I'm sorry to tell you that you'll have to be patient.

Cheers,
Nils

@akitxu
Copy link

akitxu commented Nov 7, 2021

@jorgeog96
Thanks for your contribution. I am trying to run your script and it returns the following error.

`import pandas
import pandas_datareader as pdr

ticker_val = "^IBEX"
nom_val = "ibex35"
start = "2011-01-20"
end = "2019-10-28"
df_val = pdr.DataReader(ticker_val, 'yahoo', start, end )

def rsiFunc(prices, n=14):
deltas = np.diff(prices)
seed = deltas[:n+1]
up = seed[seed>=0].sum()/n
down = -seed[seed<0].sum()/n
rs = up/down
rsi = np.zeros_like(prices)
rsi[:n] = 100. - 100./(1.+rs)

for i in range(n, len(prices)):
    delta = deltas[i-1] # cause the diff is 1 shorter

    if delta>0:
        upval = delta
        downval = 0.
    else:
        upval = 0.
        downval = -delta

    up = (up*(n-1) + upval)/n
    down = (down*(n-1) + downval)/n

    rs = up/down
    rsi[i] = 100. - 100./(1.+rs)

return rsi

df = df_val.copy(deep=True).reset_index()
df = rsiFunc(df_val, n=14)`


ValueError Traceback (most recent call last)
in
36
37 df = df_val.copy(deep=True).reset_index()
---> 38 df = rsiFunc(df_val, n=14)

in rsiFunc(prices, n)
20 delta = deltas[i-1] # cause the diff is 1 shorter
21
---> 22 if delta>0:
23 upval = delta
24 downval = 0.

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

What can be the cause? I will appreciate your help. Kind regards

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

No branches or pull requests

3 participants