I am generating my own RSI calculation using exponential moving averages and comparing this with the Ta-Lib version and they produce very different results. My calculated RSI is much more profitable, but it doesn't produce an RSI value for the last day. Is smoothing a factor, or am I calculating my RSI incorrectly?
user_interval_type = 'hourly'
user_start_date = "2024-02-01"
user_end_date = "2024-02-15"
# how many days for the long term window
user_number_of_days_lt = 31
user_number_of_days_st = 10
user_currency = "SOL-USD"
# user_currency = "TSLA"
# user_currency = "MSFT"
## INTERNAL CONFIG
hourly_factor = 24
daily_factor = 1
map_xref = {'hourly': daily_factor, 'daily': daily_factor, 'minute': daily_factor}
built_in_time_interval_xref = {'daily': 'Date', 'hourly': 'Datetime', 'minute': 'Datetime'}
yahoo_daily_interval = '1d'
yahoo_hourly_interval = '1h'
yahoo_minute_interval = '5m'
yahoo_xref = {'hourly': yahoo_hourly_interval, 'daily': yahoo_daily_interval, 'minute': yahoo_minute_interval}
# lookup configurations from xref
candle_interval = map_xref[user_interval_type]
yahoo_interval = yahoo_xref[user_interval_type]
chart_time_interval = built_in_time_interval_xref[user_interval_type]
## CALCULATE MAs BASED ON CHART TYPE AND BAR INTERVAL SIZE - DAILY USE 1 BAR x NUMBER OF DAYS for MA, HOURLY USE 24 BARS x NUMBER OF DAYS FOR MA
long_term_window_size = candle_interval * user_number_of_days_lt
short_term_window_size = candle_interval * user_number_of_days_st
import yfinance as yf
# Path of the directory where the file is placed
path = '../data_modules/'
data = yf.download(
user_currency, start=user_start_date, end=user_end_date, auto_adjust=True, interval=yahoo_interval)
# Display the last 5 rows
data.head()
# Convert date to date-time format
data.index = pd.to_datetime(data.index)
# Slicing out only the Adjusted close column
data = data[['Open', 'Close']]
def RSI(data, window=10, adjust=False):
delta = data['Open'].diff(1).dropna()
loss = delta.copy()
gains = delta.copy()
gains[gains < 0] = 0
loss[loss > 0] = 0
gain_ewm = gains.ewm(com=window - 1, adjust=adjust).mean()
loss_ewm = abs(loss.ewm(com=window - 1, adjust=adjust).mean())
RS = gain_ewm / loss_ewm
RSI = 100 - 100 / (1 + RS)
return RSI
reversed_df = data.iloc[::-1]
reversed_df.tail()
print(reversed_df['Open'])
data['ta_rsi'] = ta.RSI(data['Open'], short_term_window_size)
data["RSI"] = RSI(reversed_df[["Open"]], short_term_window_size)
data[['RSI', 'ta_rsi']].tail()
Although the RSI values are different, you can see in the below subplot, they are quite similar.
