iRSIOnArray and rsi are not the same MT4

 

I coded an indicator and realized that I'm not getting the same results from both methods used for calculating the RSI
can any one give me a hint why they are different? how to fix it?

I searched the whole forum and found not a single clue!

//+------------------------------------------------------------------+
//|                                                          RSI.mq4 |
//|                                  Copyright 2023, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2023, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#property indicator_separate_window
#property indicator_minimum 0
#property indicator_maximum 100
#property indicator_buffers 6
#property indicator_plots   6
//--- plot RSIH1
#property indicator_label1  "High1"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrRed
#property indicator_style1  STYLE_DASH
#property indicator_width1  1
//--- plot RSIL1
#property indicator_label2  "Low1"
#property indicator_type2   DRAW_LINE
#property indicator_color2  clrRed
#property indicator_style2  STYLE_DASHDOT
#property indicator_width2  1
//--- plot RSIC1
#property indicator_label3  "Close1"
#property indicator_type3   DRAW_LINE
#property indicator_color3  clrRed
#property indicator_style3  STYLE_SOLID
#property indicator_width3  2
//--- plot RSIH2
#property indicator_label4  "High2"
#property indicator_type4   DRAW_LINE
#property indicator_color4  clrBlue
#property indicator_style4  STYLE_DASH
#property indicator_width4  1
//--- plot RSIL2
#property indicator_label5  "Low2"
#property indicator_type5   DRAW_LINE
#property indicator_color5  clrBlue
#property indicator_style5  STYLE_DASHDOT
#property indicator_width5  1
//--- plot RSIC2
#property indicator_label6  "Close2"
#property indicator_type6   DRAW_LINE
#property indicator_color6  clrBlue 
#property indicator_style6  STYLE_SOLID
#property indicator_width6  2


#property indicator_level1     30.0
#property indicator_level2     70.0
#property indicator_levelcolor clrSilver
#property indicator_levelstyle STYLE_DOT

//--- input parameters
input int      RSIPeriod=14;
//--- indicator buffers
double         RSIH1Buffer[];
double         RSIL1Buffer[];
double         RSIC1Buffer[];
double         RSIH2Buffer[];
double         RSIL2Buffer[];
double         RSIC2Buffer[];

double    _high1,_low1,_close1,_high2,_low2,_close2;
int _start;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
  _start =0;
//--- indicator buffers mapping
   SetIndexBuffer(0,RSIH1Buffer);
   SetIndexBuffer(1,RSIL1Buffer);
   SetIndexBuffer(2,RSIC1Buffer);
   SetIndexBuffer(3,RSIH2Buffer);
   SetIndexBuffer(4,RSIL2Buffer);
   SetIndexBuffer(5,RSIC2Buffer);

   IndicatorDigits(_Digits);
 //---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
//---
   double _h5[];
   double _c5[];
   double _l5[];
   ArrayResize(_h5,1500);
   ArrayResize(_c5,1500);
   ArrayResize(_l5,1500);
   
   if(_start==0 || NewBar())
   {
   _start=1;
     for(int j=1499; j>=0; j--)
        {
         _h5[j]=iHigh(_Symbol,5,j);
         _l5[j]=iLow(_Symbol,5,j);
         _c5[j]=iClose(_Symbol,5,j);      
        }
   
   }
   
      
   for(int i=1400; i>=0; i--)
     {

            _high1 =iRSIOnArray(_h5,0,RSIPeriod,i); 
            _low1  =iRSIOnArray(_l5,0,RSIPeriod,i);
            _close1=iRSIOnArray(_c5,0,RSIPeriod,i); 

            _high2 =iRSI(_Symbol,5,RSIPeriod,PRICE_HIGH ,i); 
            _low2  =iRSI(_Symbol,5,RSIPeriod,PRICE_LOW  ,i); 
            _close2=iRSI(_Symbol,5,RSIPeriod,PRICE_CLOSE,i);
            
            

         RSIH1Buffer[i]=_high1;
         RSIL1Buffer[i]=_low1;
         RSIC1Buffer[i]=_close1;
         
         RSIH2Buffer[i]=_high2;
         RSIL2Buffer[i]=_low2;
         RSIC2Buffer[i]=_close2;

      }
    
//--- return value of prev_calculated for next call
   return(rates_total);
  }

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
bool NewBar()
  {
   static datetime lastbar;
   datetime curbar = Time[0];
   if(lastbar!=curbar)
     {
      lastbar=curbar;
      return (true);
     }
   else
     {
      return(false);
     }
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
Discover new MetaTrader 5 opportunities with MQL5 community and services
Discover new MetaTrader 5 opportunities with MQL5 community and services
  • 2023.11.20
  • www.mql5.com
MQL5: language of trade strategies built-in the MetaTrader 5 Trading Platform, allows writing your own trading robots, technical indicators, scripts and libraries of functions
 

I found the the solution myself!

I should have used ArraySetAsSeries.


   

   ArraySetAsSeries(_h5, true); 

   ArraySetAsSeries(_c5, true) ;

   ArraySetAsSeries(_l5, true) ;
 
hhr_rahimi #: I should have used ArraySetAsSeries.

Before populating the array(s).