The Index Counter Inside The Loop - please help !

 

Hello! I'm having trouble with a simple index counter in loop. I have tried every way to get it to work. * I have moved this function out of the OnTick section and called it as a separate function from there. * I have tried to include the moments of data array changes based on indexes when the index gets a new element using same- and separate loops. * I have tried a bool variable as an additional condition that would provide additional confirmation * I have even made a static timestamp[] array and indexed the data there to compare. In other words, I've been trying every way for weeks and I'm absolutely at a loss because I can't get anything to work unless I can actually read data even in such simple functions. Some more competent people please help me, I'm really in trouble... I put a simple example code where I try to get the consecutiveHigh and consecutiveLow counters to work, but they give random answers.

dear friends, I hope for your help


double peakHigh[MAX_ELEMENTS] = {0};
double peakLow[MAX_ELEMENTS] = {0};
datetime timepeakhigh[MAX_ELEMENTS] = {0};
datetime timepeaklow[MAX_ELEMENTS]= {0};

void OnTick()
{
    static datetime previousTime = 0;
    datetime currentTime = iTime(_Symbol, PERIOD_CURRENT, 0);
    if (previousTime != currentTime)
    {
int indexPeakHigh = 0;
int indexPeakLow = 0;
int indextimepeakhigh = 0;
int indextimepeaklow = 0;
int consecutiveHigh =0;
int consecutiveLow =0;

MqlRates rates[];
ArraySetAsSeries(rates, true);
int copiedRates = CopyRates(_Symbol, _Period, 0, CYCLESTOTAL, rates);

double prevhigh = rates[0].high;
double prevlow = rates[0].low;
double curhigh;
double curlow;
 
for (int i = 1; i < copiedRates - 2; i++) 
{
    
    curhigh = rates[i].high;
    curlow = rates[i].low;
    
    if (curhigh > prevhigh && curhigh > rates[i + 1].high )
    {
       if (indexPeakHigh < MAX_ELEMENTS)
       {
            consecutiveHigh ++;   // <---THE PROBLEMATIC COUNTER
            peakHigh[indexPeakHigh] = curhigh;
            indexPeakHigh++;
            datetime ObjHighTime = iTime(_Symbol, _Period, i);
            double ObjHighPrice = curhigh;
            string ObjHighName = "ObjHighName" + IntegerToString(indexPeakHigh);
            ObjectCreate(0, ObjHighName, OBJ_ARROW, 0, ObjHighTime, ObjHighPrice + 0.00001);
            ObjectSetInteger(0, ObjHighName, OBJPROP_ARROWCODE, 156);
            ObjectSetInteger(0, ObjHighName, OBJPROP_COLOR, clrRed);
            timepeakhigh[indextimepeakhigh] = ObjHighTime;
            indextimepeakhigh++;
            consecutiveLow =0;   // <---THE PROBLEMATIC COUNTER
       } 
    
    }
    prevhigh = curhigh;
    
    if (curlow < prevlow && curlow < rates[i + 1].low )
    {   
       
        if (indexPeakLow < MAX_ELEMENTS)
        {
            consecutiveLow ++;   // <--- THE PROBLEMATIC COUNTER !!
            peakLow[indexPeakLow] = curlow;
            indexPeakLow++;
            datetime ObjLowTime = iTime(_Symbol, _Period, i);
            double ObjLowPrice = curlow;
            string ObjLowName = "ObjLowName" + IntegerToString(indexPeakLow);
            ObjectCreate(0, ObjLowName, OBJ_ARROW, 0, ObjLowTime, ObjLowPrice);
            ObjectSetInteger(0, ObjLowName, OBJPROP_ARROWCODE, 156);
            ObjectSetInteger(0, ObjLowName, OBJPROP_COLOR, clrBlue);
            timepeaklow[indextimepeaklow] = ObjLowTime;
            indextimepeaklow++;
            consecutiveHigh =0;  // <--- THE PROBLEMATIC COUNTER 
        }   
     
     }       
     prevlow = curlow;
  }
  previousTime = currentTime;
}