Code for rsi

Imran
Dear traders.

I would like to share the function to calculate rsi...
on the same discussion, I would be adding other indicators also.


def rsi(df, period):
df['pnl'] = df['close'].diff()
df.loc[df['pnl'] >= 0, 'gain'] = df['pnl']
df['gain'].fillna(value=0, inplace=True)
df.loc[df['pnl'] < 0, 'loss'] = df['pnl'].abs()
df['loss'].fillna(value=0, inplace=True)

df['average_gain'] = df['gain'].ewm(com = period-1, min_periods=period).mean()
df['average_loss'] = df['loss'].ewm(com = period-1, min_periods=period).mean()
df['rs'] = df['average_gain'] / df['average_loss']
df['rsi'] = 100 - (100/(1+df['rs']))
return df

Sign In or Register to comment.