Error Found while backtesting my EA

 

Hi guys,

I found this error while running a backtesting on below code. Just wondering anyone could give me a generous comment!

Thank you so much !!

void OnTick()
{
   if(zigzag_Trend()==1) Print("HH HL");
   if(zigzag_Trend()==2) Print("LH LL");
}
int zigzag_Trend()
{
   double Points = MarketInfo (Symbol(), MODE_POINT);
   int    shift, back,lasthighpos,lastlowpos;
   double val,res;
   double curlow,curhigh,lasthigh=0,lastlow=0;
   string trend = "";
   
   for(shift=Bars-ExtDepth; shift>=0; shift--)
   {
      val=Low[Lowest(NULL,0,MODE_LOW,ExtDepth,shift)];
      if(val==lastlow) val=0.0;
      else 
      { 
         lastlow=val; 
         if((Low[shift]-val)>(ExtDeviation*Point)) val=0.0;
         else
         {
            for(back=1; back<=ExtBackstep; back++)
            {
               res=ExtMapBuffer[shift+back];
               if((res!=0)&&(res>val)) ExtMapBuffer[shift+back]=0.0; 
            }
         }
      } 
      ExtMapBuffer[shift]=val;
      //--- high
      val=High[Highest(NULL,0,MODE_HIGH,ExtDepth,shift)];
      if(val==lasthigh) val=0.0;
      else 
      {
         lasthigh=val;
         if((val-High[shift])>(ExtDeviation*Point)) val=0.0;
         else
         {
            for(back=1; back<=ExtBackstep; back++)
            {
               res=ExtMapBuffer2[shift+back];
               if((res!=0)&&(res<val)) ExtMapBuffer2[shift+back]=0.0; 
            } 
         }
      }
      ExtMapBuffer2[shift]=val;
   }

   // final cutting 
   lasthigh=-1; lasthighpos=-1;
   lastlow=-1;  lastlowpos=-1;

   for(shift=Bars-ExtDepth; shift>=0; shift--)
   {
      curlow=ExtMapBuffer[shift];
      curhigh=ExtMapBuffer2[shift];
      if((curlow==0)&&(curhigh==0)) continue;
   //---
      if(curhigh!=0)
      {
         if(lasthigh>0) 
         {
            if(lasthigh<curhigh) ExtMapBuffer2[lasthighpos]=0;
            else ExtMapBuffer2[shift]=0;
         }
   //---
         if(lasthigh<curhigh || lasthigh<0)
         {
            lasthigh=curhigh;
            lasthighpos=shift;
         }
         lastlow=-1;
      }
   //----
      if(curlow!=0)
      {
         if(lastlow>0)
         {
            if(lastlow>curlow) ExtMapBuffer[lastlowpos]=0;
            else ExtMapBuffer[shift]=0;
         }
   //---
         if((curlow<lastlow)||(lastlow<0))
         {
            lastlow=curlow;
            lastlowpos=shift;
         } 
         lasthigh=-1;
      }  
   }
  
   for(shift=Bars-1; shift>=0; shift--)
   {   
      if(shift>=Bars-ExtDepth) ExtMapBuffer[shift]=0.0;
      else
      {
         res=ExtMapBuffer2[shift];
         if(res!=0.0) ExtMapBuffer[shift]=res;
      }
   }
     
        //int i=0;
        int LastZigZag, PreviousZigZag,PreviousZigZag2,PreviousZigZag3,PreviousZigZag4;
   
   int h=0;
   while ( ExtMapBuffer[h]==0 && ExtMapBuffer2[h]==0) 
         {h++;}
   LastZigZag=h;
   h++;
   while(ExtMapBuffer[h]==0 && ExtMapBuffer2[h]==0) 
         {h++;}
   PreviousZigZag=h;
   h++;
   while(ExtMapBuffer[h]==0 && ExtMapBuffer2[h]==0) 
         {h++;}
   PreviousZigZag2=h;
   h++;
   while(ExtMapBuffer[h]==0 && ExtMapBuffer2[h]==0) 
         {h++;}
   PreviousZigZag3=h;
   h++;
   while(ExtMapBuffer[h]==0 && ExtMapBuffer2[h]==0) 
         {h++;}
   PreviousZigZag4=h;
   
   topD=High[LastZigZag];
   botD=Low[LastZigZag];
   
   topC=High[PreviousZigZag];
   botC=Low[PreviousZigZag];
   
   topB=High[PreviousZigZag2];
   botB=Low[PreviousZigZag2];
   
   topA=High[PreviousZigZag3];
   botA=Low[PreviousZigZag3];
   
   topX=High[PreviousZigZag4];
   botX=Low[PreviousZigZag4];
   
   
   TimeD=Time[LastZigZag];
   TimeC=Time[PreviousZigZag];
   TimeB=Time[PreviousZigZag2];
   TimeA=Time[PreviousZigZag3];
   TimeX=Time[PreviousZigZag4];
   
   if(topD>topC)// D IS A PEAK
   {//defining fib1 as CD retracement
      TS1=TimeC;
      PS1=botC;
      TT1=TimeD;
      PT1=topD;
      if (topB<topD)
      {//defining fib2 as BC retracement
         TS2=TimeB;
         PS2=topB;
         TT2=TimeC;
         PT2=botC;
      }
      if (topB>=topD)
      {//defining fib2 as BC extension
         TS2=TimeC;
         PS2=botC;
         TT2=TimeB;
         PT2=topB;
      }
      if (botA<=botC)
      {//defining fib3 as AB retracement
         TS3=TimeA;
         PS3=botA;
         TT3=TimeB;
         PT3=topB;
      }
   }
      
   if(botD<botC)// D IS A TROUGH
   {//defining fib1 as CD retracement
      TS1=TimeC;
      PS1=topC;
      TT1=TimeD;
      PT1=botD;
      if (botB<=botD)
      {//defining fib2 as BC retracement
         TS2=TimeB;
         PS2=botB;
         TT2=TimeC;
         PT2=topC;
      }
      if (botB>botD)
      {//defining fib2 as BC extension
         TS2=TimeC;
         PS2=topC;
         TT2=TimeB;
         PT2=botB;
      }
      //defining fib3 as AB retracement
         TS3=TimeA;
         PS3=topA;
         TT3=TimeB;
         PT3=botB;
   }    
      
  // CREATE X,A,B,C,D TEXTS
   if(botD<botC)// D IS A TROUGH
   {//Alert("cas 1");
      posD=botD;
      posC=topC+11*Points;
      posB=botB;
      posA=topA+11*Points;
      posX=botX;
      XA=(topA-botX)/Points;
      AB=(topA-botB)/Points;
      BC=(topC-botB)/Points;
      CD=(topC-botD)/Points;
      
      //Comparing XABCD
      if(posC>posA && posD>posB && posB>posX)trend ="Uptrend";
      else if(posA>posC && posX>posB && posB>posD) trend ="Downtrend";
      else trend="Neutral";
   }
   
   if(topD>topC)// D IS A PEAK
   {//Alert("cas 2");
      posD=topD;
      posC=botC;
      posB=topB;
      posA=botA;
      posX=topX;
      XA=(topX-botA)/Points;
      AB=(topB-botA)/Points;
      BC=(topB-botC)/Points;
      CD=(topD-botC)/Points;
      XAAB=AB/XA;
      ABBC=BC/AB;
      BCCD=CD/BC;
      
      //Comparing XABCD
      if(posX>posB && posB>posD && posA>posC) trend ="Downtrend";
      else if(posD>posB && posB>posX && posC>posA) trend ="Uptrend";
      else trend="Neutral";
   }
   if( trend == "Uptrend" ) return(1);
   else if( trend == "Downtrend" ) return(2);
   return(0);
}