I tried calculating RSI based on explanation https://zerodha.com/varsity/chapter/indicators-part-1/ I took the closing price of SUZLON on daily time frame from 21-12-22 to 9-01-23. The RSI value is coming out to be 50 in calculation but on kite chart its showing 52.0. I tried with multiple scripts but same discrepancy is observed.
I think this tradingqna thread answers your question. To quote, Varsity is showing a simple average, I assume to simplify the example calculations, and Charts/Excel takes smoothed average.
Make sure to use double precision floating points while doing the calculations.
public static double CalculateRsi(IEnumerable closePrices, int interval)
{
var prices = closePrices as double[] ?? closePrices.ToArray();
double sumGain = 0;
double sumLoss = 0;
for (int i = 1; i < prices.Length; i++)
{
var difference = prices[i] - prices[i - 1];
if (difference >= 0)
{
sumGain += difference;
}
else
{
sumLoss -= difference;
}
}
var averageGain = sumGain / 14;
var averageLoss = sumLoss / 14;
var relativeStrength = averageGain / averageLoss;
double rsi = 100 - (100 / (1 + relativeStrength));
return rsi;
}
For reference ChartIQ documentation: https://documentation.chartiq.com/tutorial-Using%20and%20Customizing%20Studies%20-%20Definitions.html#rsi