MQL5: with For..Loop Draw Arrow Function printing only 2 elements out of 10 in Array !!!

Anil Varma  

Hi Friends

I have created a testing EA to calculate High Low value index, and draw arrow for these points on chart. However, while using for..loop it is drawing only 0 and 1 element from the array(s). Rest of the elements 2 to 9 are not drawn.

Please help me to locate the issue ...

extern int   MajorSwingSize      = 3;
extern int   PeriodsInMajorSwing = 13;
extern int   MinorSwingSize      = 1;
extern int   PeriodsInMinorSwing = 5;

       int   idxMajorSwingHigh[];
       int   idxMinorSwingHigh[];
       int   idxMajorSwingLow[];
       int   idxMinorSwingLow[];
//+----------------------------------------------------------------------------------------------------------+
//| Custom indicator initialization function
//+----------------------------------------------------------------------------------------------------------+
int OnInit()
  {
//---
   return(INIT_SUCCEEDED);
  }
//+----------------------------------------------------------------------------------------------------------+
//| Initialization Function
//+----------------------------------------------------------------------------------------------------------+
void OnTick()
{
  MqlRates MyRates[];
  ArraySetAsSeries(MyRates,true);
  int data = CopyRates(Symbol(),PERIOD_CURRENT,0,Bars(Symbol(),PERIOD_CURRENT),MyRates);

  if(Check_IsNewCandle())
  {
    int limit  = 1000;
    int countA = 0, countB = 0, countC = 0, countD = 0;
    for(int i = 1; i < limit; i++)
      {
        // Minor Swing High Logic
        if(iHighest(Symbol(),PERIOD_CURRENT,MODE_HIGH,PeriodsInMinorSwing*2,i) == i + PeriodsInMinorSwing)
          {
            if(countA < 10)
              {
                ArrayResize(idxMinorSwingHigh,countA+1);
                idxMinorSwingHigh[countA] = (int)(i+PeriodsInMinorSwing);
                countA++;
              }
          }
        // Major Swing High Logic
        if(iHighest(Symbol(),PERIOD_CURRENT,MODE_HIGH,PeriodsInMajorSwing*2,i)==i+PeriodsInMajorSwing)
          {
            if(countB < 10)
              {
                ArrayResize(idxMajorSwingHigh,countB+1);
                idxMajorSwingHigh[countB] = (int)(i+PeriodsInMajorSwing);
                countB++;
              }
          }
        // Minor Swing Low Logic
        if(iLowest(Symbol(),PERIOD_CURRENT,MODE_LOW,PeriodsInMinorSwing*2,i)==i+PeriodsInMinorSwing)
          {
            if(countC < 10)
              {
                ArrayResize(idxMinorSwingLow,countC+1);
                idxMinorSwingLow[countC] = (int)(i+PeriodsInMinorSwing);
                countC++;
              }
          }
        // Major Swing Low Logic
        if(iLowest(Symbol(),PERIOD_CURRENT,MODE_LOW,PeriodsInMajorSwing*2,i)==i+PeriodsInMajorSwing)
          {
            if(countD < 10)
              {
                ArrayResize(idxMajorSwingLow,countD+1);
                idxMajorSwingLow[countD] = (int)(i+PeriodsInMajorSwing);
                countD++;
              }
          }
      }
    // Create OBJECT ARROW for High Low Points
    for(int i = 0; i < 10; i++)
      {
        //ArrowSwing_MajorHigh(PERIOD_CURRENT,MyRates[idxMajorSwingHigh[i]].time,MyRates[idxMajorSwingHigh[i]].high);
        DrawArrow_HighLow(PERIOD_CURRENT,(string)"Major",(string)"High",MyRates[idxMajorSwingHigh[i]].time,MyRates[idxMajorSwingHigh[i]].high);
        DrawArrow_HighLow(PERIOD_CURRENT,(string)"Major",(string)"Low",MyRates[idxMajorSwingLow[i]].time,MyRates[idxMajorSwingLow[i]].low);
        DrawArrow_HighLow(PERIOD_CURRENT,(string)"Minor",(string)"High",MyRates[idxMinorSwingHigh[i]].time,MyRates[idxMinorSwingHigh[i]].high);
        DrawArrow_HighLow(PERIOD_CURRENT,(string)"Minor",(string)"Low",MyRates[idxMinorSwingLow[i]].time,MyRates[idxMinorSwingLow[i]].low);
      }    
/*
    // PRINT Statement to check array values
    for(int y = 0; y < 10; y++)
      {
        Print("MAJOR [",y,"] High [",idxMajorSwingHigh[y],"] MyRates [",MyRates[idxMajorSwingHigh[y]].high,
                          "] Low  [",idxMajorSwingLow[y],"] MyRates [",MyRates[idxMajorSwingLow[y]].low,"]");
      }
    for(int y = 0; y < 10; y++)
      {
        Print("MINOR [",y,"] High [",idxMinorSwingHigh[y],"] MyRates [",MyRates[idxMinorSwingHigh[y]].high,
                          "] Low  [",idxMinorSwingLow[y],"] MyRates [",MyRates[idxMinorSwingLow[y]].low,"]");
      }
*/
  }
}

//+----------------------------------------------------------------------------------------------------------+

//+----------------------------------------------------------------------------------------------------------+
//| Function: DrawArrow_HighLow()
//+----------------------------------------------------------------------------------------------------------+
bool DrawArrow_HighLow(const ENUM_TIMEFRAMES  chartTimeFrame,     // chart's ID
                       string                 swingType,          // Major or Minor
                       string                 priceType,          // High or Low
                       datetime               anchorTime,         // anchor point time
                       double                 anchorPrice)        // anchor point price
  {
    color             anchorColor = NULL;
    int               anchorWidth = NULL;
    string            arrowName   = NULL;
    ENUM_ARROW_ANCHOR anchorPosition = NULL;

    if(swingType == "Major")
      {
        anchorColor = clrPurple;                  anchorWidth = 3;
        if(priceType == "High")
          {
            arrowName      = "Major Swing High";  anchorPosition = ANCHOR_TOP;
          }
        else if(priceType == "Low")
          {
            arrowName      = "Major Swing Low";   anchorPosition = ANCHOR_BOTTOM;
          }
      }
    else if(swingType == "Minor")
      {
        anchorColor = clrCornflowerBlue;          anchorWidth = 1;
        if(priceType == "High")
          {
            arrowName      = "Minor Swing High";  anchorPosition = ANCHOR_TOP;
          }
        else if(priceType == "Low")
          {
            arrowName      = "Minor Swing Low";   anchorPosition = ANCHOR_BOTTOM;
          }
      }
    ResetLastError();  //--- reset the error value
//--- create an arrow
   if(!ObjectCreate(chartTimeFrame,arrowName,OBJ_ARROW,0,anchorTime,anchorPrice))
     {
      Print(__FUNCTION__,
            ": failed to create ArrowSwing_MajorHigh! Error code = ",GetLastError());
      return(false);
     }
   ObjectSetInteger(chartTimeFrame,arrowName,OBJPROP_ARROWCODE,108);     // Arrow Code
   ObjectSetInteger(chartTimeFrame,arrowName,OBJPROP_ANCHOR,anchorPosition); // Anchor Type
   ObjectSetInteger(chartTimeFrame,arrowName,OBJPROP_COLOR,anchorColor); // Arrow Color
   ObjectSetInteger(chartTimeFrame,arrowName,OBJPROP_STYLE,DRAW_ARROW);  // BorderLine Style
   ObjectSetInteger(chartTimeFrame,arrowName,OBJPROP_WIDTH,anchorWidth); // Arrow Size
   ObjectSetInteger(chartTimeFrame,arrowName,OBJPROP_BACK,false);        // display in the foreground (false) or background (true)
   ObjectSetInteger(chartTimeFrame,arrowName,OBJPROP_HIDDEN,true);       // True = hide object name in the object list   
//--- successful execution
   return(true);
  } // END Of DrawArrow_HighLow()
  
//+----------------------------------------------------------------------------------------------------------+
//| Function: ArrowSwing_MajorHigh() --> Alternate code to print only MajorHigh Arrow
//+----------------------------------------------------------------------------------------------------------+
bool ArrowSwing_MajorHigh(const ENUM_TIMEFRAMES  chartTimeFrame,     // chart's ID
                          datetime               timeMajorHigh,               // anchor point time
                          double                 priceMajorHigh)              // anchor point price
  {
    ResetLastError();  //--- reset the error value
//--- create an arrow
   if(!ObjectCreate(chartTimeFrame,"Major Swing High",OBJ_ARROW,0,timeMajorHigh,priceMajorHigh))
     {
      Print(__FUNCTION__,
            ": failed to create ArrowSwing_MajorHigh! Error code = ",GetLastError());
      return(false);
     }
   ObjectSetInteger(chartTimeFrame,"Major Swing High",OBJPROP_ARROWCODE,108);     // Arrow Code
   ObjectSetInteger(chartTimeFrame,"Major Swing High",OBJPROP_ANCHOR,ANCHOR_TOP); // Anchor Type
   ObjectSetInteger(chartTimeFrame,"Major Swing High",OBJPROP_COLOR,clrPurple);   // Arrow Color
   ObjectSetInteger(chartTimeFrame,"Major Swing High",OBJPROP_STYLE,DRAW_ARROW);  // BorderLine Style
   ObjectSetInteger(chartTimeFrame,"Major Swing High",OBJPROP_WIDTH,3);           // Arrow Size
   ObjectSetInteger(chartTimeFrame,"Major Swing High",OBJPROP_BACK,false);        // display in the foreground (false) or background (true)
   ObjectSetInteger(chartTimeFrame,"Major Swing High",OBJPROP_HIDDEN,true);       // True = hide object name in the object list   
//--- successful execution
   return(true);
  }

//+----------------------------------------------------------------------------------------------------------+
bool Check_IsNewCandle()
  {
    bool   newCandle;
    static datetime dtBarCurrent  = WRONG_VALUE;
           datetime dtBarPrevious = dtBarCurrent;
    // "SERIES_LASTBAR_DATE" Open time of the last bar of the symbol - period
    dtBarCurrent = (datetime)SeriesInfoInteger(Symbol(),PERIOD_CURRENT,SERIES_LASTBAR_DATE);  
    if(dtBarCurrent != dtBarPrevious)  newCandle = true;
    else                               newCandle = false;
    //---
  return(newCandle);
  } // END Of Check_IsNewCandle()
Reason: