Rectangle size and first bar data are not correct

 

Hi all,

I have tried to make an EA that draw a rectangle every 4  bars (can be x bars) and calculate the range of the 4 bars, but It seems to miss with the high or low of the first bar.

Any clue how to fix it?

input string Start_Time;
input int Risk_Size;

void OnTick()
  {
   int x;
   static int lastBar = -1;
   static int FirstBar = -1;
   static bool Start = false;
   static bool Once = false;
   static int Rectangle_Count=1;
   int HighestCandle;
   int LowestCandle;
   static int count_first_bars = 4;
   double High[];
   double Low[];
   string myTime;
   
   myTime = GetTime();
   //Comment(myTime);
   
   if (Once==false) {
      ObjectsDeleteAll(0);
      Once = true;
   }
   
   if (myTime==Start_Time) {
     Start=true;
     FirstBar=Bars(Symbol(),Period());
      }
   if (Start==true) { 
    if (lastBar != Bars(Symbol(),Period()))
     {
      lastBar = Bars(Symbol(),Period());
      if ((FirstBar - Bars(Symbol(),Period())) % 4 == 0)
      {   
         MqlRates PriceInformation[];
         ArraySetAsSeries(High,true);
         ArraySetAsSeries(Low,true);
         CopyHigh(_Symbol,_Period,0,4,High);
         CopyLow(_Symbol,_Period,0,4,Low);
         HighestCandle= ArrayMaximum(High,0,4);
         LowestCandle= ArrayMinimum(Low,0,4);
         
         ArraySetAsSeries(PriceInformation,true);
         int Data=CopyRates(Symbol(),Period(),0,Bars(Symbol(),Period()),PriceInformation);  
         string rectangleName = "Rectangle_" + IntegerToString(Rectangle_Count);
         Comment("The time now: ",myTime,", High:",PriceInformation[HighestCandle].high,", Low: ",PriceInformation[LowestCandle].low,", Deff:",PriceInformation[HighestCandle].high-PriceInformation[LowestCandle].low,", Lot size:",Risk_Size/((PriceInformation[HighestCandle].high-PriceInformation[LowestCandle].low)*100));
         ObjectCreate(
            _Symbol,
            rectangleName,
            OBJ_RECTANGLE,
            0,
            PriceInformation[4].time,
            PriceInformation[HighestCandle].high,
            PriceInformation[1].time,
            PriceInformation[LowestCandle].low
          );
         ObjectSetInteger(0,"Rectangle",OBJPROP_COLOR,clrBlanchedAlmond);
      } else {
         Rectangle_Count=Rectangle_Count+1;
      }
    
   }
  }
 }
 
 string GetTime()
 {
   string Time;
   
   Time = TimeToString(TimeLocal(),TIME_MINUTES);
   
   return Time; 
 }