I am new to pandas. What is the best way to calculate the relative strength part in the RSI indicator in pandas? So far I got the following:
from pylab impor
You can also use the following. If statements will ensure the first RSI value is calculated differently (and properly) from the rest of the values. In the end, all NaN values will be replaced with blanks.
This assumes you have already imported pandas and your dataframe is df. The only additional data required is a column of Close prices which is labeled as Close. You can reference this column as df.Close, however, sometimes you may have multiple word with space separators as a column header (which requires df['word1 word2'] format). As a consistent practice I always use the df['Close'] format.
import numpy as np
# Calculate change in closing prices day over day
df['Delta'] = df['Close'].diff(periods=1, axis=0)
# Calculate if difference in close is Gain
conditions = [df['Delta'] <= 0, df['Delta'] > 0]
choices = [0, df['Delta']]
df['ClGain'] = np.select(conditions, choices)
# Calculate if difference in close is Loss
conditions = [df['Delta'] >= 0, df['Delta'] < 0]
choices = [0, -df['Delta']]
df['ClLoss'] = np.select(conditions, choices)
# Determine periods to calculate RSI over
rsi_n = 9
# Calculate Avg Gain over n periods
conditions = [df.index < rsi_n, df.index == rsi_n, df.index > rsi_n]
choices = ["", df['ClGain'].rolling(rsi_n).mean(), ((df['AvgGain'].shift(1) * (rsi_n - 1)) + df['ClGain']) / rsi_n]
df['AvgGain'] = np.select(conditions, choices)
# Calculate Avg Loss over n periods
conditions = [df.index < rsi_n, df.index == rsi_n, df.index > rsi_n]
choices = ["", df['ClLoss'].rolling(rsi_n).mean(), ((df['AvgLoss'].shift(1) * (rsi_n - 1)) + df['ClLoss']) / rsi_n]
df['AvgLoss'] = np.select(conditions, choices)
# Calculate RSI
df['RSI'] = 100-(100 / (1 + (df['AvgGain'] / df['AvgLoss'])))
# Replace NaN cells with blanks
df = df.replace(np.nan, "", regex=True)
# (OPTIONAL) Remove columns used to create RSI
del df['Delta']
del df['ClGain']
del df['ClLoss']
del df['AvgGain']
del df['AvgLoss']