RSI Code

I am trying to use the below code to calculate RSI. I am getting the error:

module 'pandas' has no attribute 'stats'

Code:

def RSI(series, period):

 delta = series.diff().dropna()

 u = delta * 0

 d = u.copy()

 u[delta > 0] = delta[delta > 0]

 d[delta < 0] = -delta[delta < 0]

 u[u.index[period-1]] = np.mean( u[:period] ) #first value is sum of avg gains

 u = u.drop(u.index[:(period-1)])

 d[d.index[period-1]] = np.mean( d[:period] ) #first value is sum of avg losses

 d = d.drop(d.index[:(period-1)])

 rs = pd.stats.moments.ewma(u, com=period-1, adjust=False) / <br />
 pd.stats.moments.ewma(d, com=period-1, adjust=False)

 return 100 - 100 / (1 + rs)

df['RSI'] = RSI(df['Close'], 14)



Let me know the correct code for RSI calculation.

Hello Ashish,



pd.stats.moments.ewma is now deprecated as it was available in pandas 0.15.0.



You can use this function instead:  https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.ewm.html  Have a look at the examples given below and replace the rs ewma statements with them. 



For example, the first one:

pd.stats.moments.ewma(u, com=period-1, adjust=False)

can be replaced by:

u.ewm(com=period-1, adjust=False) 



Do get back if you face issues.