the following code works but does not give me the same value as on tradingview. I don't understand the problem
var rsi_gain = 0;
var rsi_loss = 0;
for (let i = 18; i <= 20; i++) {
rsi_gain += (content[i][4] > content[i - 1][4]) ? (content[i][4] - content[i - 1][4]) : 0;
rsi_loss += (content[i][4] < content[i - 1][4]) ? (content[i - 1][4] - content[i][4]) : 0;
}
// Calcul Average Gain
var AVG_gain = (rsi_gain / 3); // (Gains / Periode)
// Calcul Average Loss
var AVG_loss = (rsi_loss / 3); // (Pertes / Periode)
//RS
var RS = (AVG_gain / AVG_loss);
//RSI
var RSI = 100 - (100 / (1 + RS));
My result with these values (48979.05,48861.92,48964.83) : 53.23 Tradingview result : 62.61
image : https://www.zupimages.net/viewer.php?id=21/51/y7ik.jpeg
Thank you
The first N bars (length) are calculated by using a simple moving average, afterwards you have to apply a exponential average.
https://www.tradingview.com/pine-script-reference/v5/#fun_ta{dot}rsi
https://www.tradingview.com/pine-script-reference/v5/#fun_ta{dot}rma