Hi,
I know this is a bit old or not needed (due to TA-Lib) now, but I think the RSI calculation in the code given at the top is not correct. For anybody who's still interested...
The correct answer to the example in the comment should be:
[ 70.02141328 69.57446809 69.37901499 80.26607539 73.39901478
59.90338164 62.61261261 60. 48.47775176 53.87840671
48.95238095 43.86281588 37.6744186 32.20910624 32.66331658
38.0794702 31.70391061 25.066313 30.17902813]
And the modified RSI function to produce above result is:
def rsi(prices, period=14):
num_prices = len(prices)
if num_prices < period:
# show error message
raise SystemExit('Error: num_prices < period')
# this could be named gains/losses to save time/memory in the future
changes = prices[1:] - prices[:-1]
#num_changes = len(changes)
rsi_range = num_prices - period
rsis = np.zeros(rsi_range)
gains = np.array(changes)
# assign 0 to all negative values
masked_gains = gains < 0
gains[masked_gains] = 0
losses = np.array(changes)
# assign 0 to all positive values
masked_losses = losses > 0
losses[masked_losses] = 0
for idx in range(0, rsi_range):
avg_gain = np.mean(gains[idx:(idx+period)])
avg_loss = np.mean(losses[idx:(idx+period)])
if avg_loss == 0:
rsis[idx] = 100
else:
rs = avg_gain / (-avg_loss)
rsis[idx] = 100 - (100 / (1 + rs))
return rsis