pandas-technical-indicators icon indicating copy to clipboard operation
pandas-technical-indicators copied to clipboard

Calculation RSI (wrong)

Open jorgeog96 opened this issue 7 years ago • 2 comments

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?

jorgeog96 avatar Jun 05 '18 20:06 jorgeog96

@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

deepbrook avatar Jun 07 '18 12:06 deepbrook

@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

akitxu avatar Nov 07 '21 20:11 akitxu