RSI indicator

I am trying to calculate the RSI values but they are not matching with zerodha's RSI. I am using following algo to compute RSI :
public static double getRSI(List<Candle> candles){
double avgU = 0;
double avgD = 0;
int N = candles.size();
for(int i = 0;i <N-1;i++){
double delta = candles.get(i+1).getClose() - candles.get(i).getClose();
if(delta < 0)
avgD += Math.abs(delta);
avgU += delta;
avgU = avgU/(N-1);
avgD = avgD/(N-1);
if(avgD == 0)
return 100.0;
double rs = avgU/avgD;
double rsi = 100 - 100/(1 + rs);
return rsi;
Please tell me where i am wrong.
  • perfectatdat
    There could be two things.

    1) Can you match your RSI values with the actual RSI from the beginning of a stock price?
    2) You can try to take the previous Average up/ average down and multiply it with N-1 and add to the current Up/down values to calculate the current average up/down values.

    The tried the above two things and i was able to get the same RSI values which Zerodha produces.
Sign In or Register to comment.