He escrito un código en python que creo que calcula el RSI. Escribí el código basado en lo que vi en stockcharts.com se encuentra aquí
Aquí está el código:
def getRSI(close, n=14):
"""
Computes the Relative Strength Index of a trend
:param close: closing prices
:param n: lookback period
:return: a np array containing the RSI for all periods spanning
closing price data
"""
# compute a vector for change between daily closing prices
change = np.zeros(len(close))
for i in range(1, len(close)):
change[i] = close[i-1] - close[i]
# compute a vector of gains and losses
gain = np.zeros(len(close))
loss = np.zeros(len(close))
for i in range(1, len(close)):
if change[i] >= 0:
gain[i] = change[i]
else:
loss[i] = math.fabs(change[i])
avg_gain = np.zeros(len(close))
avg_loss = np.zeros(len(close))
avg_gain[n] = np.average(gain[0:n])
avg_loss[n] = np.average(loss[0:n])
for i in range(n, len(close)):
avg_gain[i] = (avg_gain[i-1]*(n-1) + gain[i])/n
avg_loss[i] = (avg_loss[i-1]*(n-1) + loss[i])/n
RS = np.zeros(len(close))
for i in range(n, len(close)):
RS[i] = avg_gain[i]/avg_loss[i]
RSI = np.zeros(len(close))
for i in range(n, len(close)):
RSI[i] = 100 - (100/(1+RS[i]))
return RSI
Intenté calcular el RSI de COP para los precios a partir del 26 de noviembre de 2006 hasta el 26 de noviembre de 2018. Pero la curva que obtengo es completamente diferente a la que veo en Yahoo Finance o incluso en StockCharts. ¿Qué estoy haciendo mal?