RSI computing shows incorrect results

 

Hello friends,


I am trying to write a Expert Advisor for trading but I would like to use the Relative Strenght Index for my trades.

This is the code i wrote based on the formula on the internet and also the indicator. It should work but it doesn't match the results found on Tradingview and also Metatrader 4 with the same window settings.

I don't know why can someone help me out please?


//Relative Strength Index
float RSI(int Length)
  {

   float RSI, AvgIncrease, AvgDecline;
   float SumIncrease = 0;
   float SumDecline = 0;

   for(int i = 1; i <= Length; i++)
     {
      if(Close[i] > Close[i-1])
        {
         SumIncrease += Close[i] - Close[i-1];
        }
      else
        {
         SumDecline -= Close[i] - Close[i-1];
        }
     }

   AvgIncrease = SumIncrease / Length;
   AvgDecline = SumDecline / Length;

   RSI = 100 - 100 / (1 + AvgIncrease/AvgDecline);
   return RSI;

  }
 
Did you check the RSI's code that exists in MT4 "RSI.mq4"?
 
Amine Abed #:
Did you check the RSI's code that exists in MT4 "RSI.mq4"?

Hi friend.

This should be it. Thank you friendly friend.

//Relative Strength Index
float RSI(int Length)
  {
   
   float RSI = iRSI(NULL, 0, Length, PRICE_CLOSE , 0);
   return RSI;

  }

 
Magus I #: This should be it.

Please edit your post and use the CODE button (Alt-S)! (For large amounts of code, attach it.)
          General rules and best pratices of the Forum. - General - MQL5 programming forum (2019)
          Messages Editor