This code locks up / freezes up the EA (not responding)

 
Hi all, 

In this code I am count back bars for the MACD true indicator (iCustom) etc etc. 
I use these bar count to initialize i in parts of the code. This locks up the EA and Metatrader stops responding. 

How to diagnose this or perhaps someone can explain why ? 
Thanks

See code below with highlighted comments that crashes Metatrader. 

//+------------------------------------------------------------------+
//|                                                depthcharger1.mq4 |
//|                        Copyright 2020, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2020, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

//Some inputs
extern double    TakeProfit=20.0; //optional use later
extern double    Lots=0.1;
extern double    StopLoss=10.0; //optional use later
extern int       MagicNumber=123486;

//+++++ user can change retrace and/or extension:
extern double fibo_retrace=0.500; // C retracement level desired
extern double fibo_extension=0.618;  // D extention level desired

double val1,val2,histo,histo_1,A,B,C,D;
int histoupcount,histodowncount;
bool traded = false;

int ticket,total,result;

//++++ These are adjusted for 5 digit brokers.

int     pips2points;    // slippage  3 pips    3=points    30=points
double  pips2dbl;       // Stoploss 15 pips    0.0015      0.00150
//int     Digits.pips;    // DoubleToStr(dbl/pips2dbl, Digits.pips)

    // OrderSend(... Slippage.Pips * pips2points, Bid - StopLossPips * pips2dbl
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
    tiles();
    //histo_up_count();
    //histo_down_count();
    //Print ("histoupcount ", histoupcount);
    //Print ("histodowncount ", histodowncount);
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   ObjectsDeleteAll(0,OBJ_RECTANGLE_LABEL);
   ObjectsDeleteAll(0,OBJ_LABEL);

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
    val1=iFractals(NULL, 0, MODE_UPPER,3);
    val2=iFractals(NULL, 0, MODE_LOWER,3);
    histo  = iCustom(NULL,0,"MACD True",2,1);
    total = OrdersTotal();
    signal();
    histo_up_count();
    histo_down_count();
    Print ("histoupcount ", histoupcount);
    Print ("histodowncount ", histodowncount);
    
    
  
    
  }
//+------------------------------------------------------------------+

void BUY()
  {
   for (int i = OrdersTotal() - 1; i >= 0; i--)
     {
      ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3*pips2points,Bid-StopLoss*pips2dbl,Bid+TakeProfit*pips2dbl,"depthcharger1",MagicNumber,0,Green);
             if(ticket<0) Alert("OrderSend Failed: ", GetLastError()); 
      // return;
     }
  }
  
   
void SELL()
  {
   for (int i = OrdersTotal() - 1; i >= 0; i--)
     {
      ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3*pips2points,Ask-StopLoss*pips2dbl,Ask+TakeProfit*pips2dbl,"depthcharger1",MagicNumber,0,Red);
             if(ticket<0) Alert("OrderSend Failed: ", GetLastError()); 
      // return;
     }
  }
  

void exit()
  {
   for (int i = OrdersTotal() - 1; i >= 0; i--)
     {
      ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3*pips2points,Ask-StopLoss*pips2dbl,Ask+TakeProfit*pips2dbl,"depthcharger1",MagicNumber,0,Red);
             if(ticket<0) Alert("OrderSend Failed: ", GetLastError()); 
      // return;
     }
  }
  

void signal()
   {
   if(histo()==true) ObjectSet("B4",OBJPROP_BGCOLOR,clrLime);
   else ObjectSet("B4",OBJPROP_BGCOLOR,clrBlack);
   if(histo()==false) ObjectSet("B5",OBJPROP_BGCOLOR,clrRed);
   else ObjectSet("B5",OBJPROP_BGCOLOR,clrBlack);
   
   if(A_high()==true) ObjectSet("B6",OBJPROP_BGCOLOR,clrRed);
   else ObjectSet("B6",OBJPROP_BGCOLOR,clrBlack);
   if(A_low()==true) ObjectSet("B3",OBJPROP_BGCOLOR,clrLime);
   else ObjectSet("B3",OBJPROP_BGCOLOR,clrBlack);
   
   if(B_high()==true) ObjectSet("B2",OBJPROP_BGCOLOR,clrLime);
   else ObjectSet("B2",OBJPROP_BGCOLOR,clrBlack);
   if(B_low()==true) ObjectSet("B7",OBJPROP_BGCOLOR,clrRed);
   else ObjectSet("B7",OBJPROP_BGCOLOR,clrBlack);
   
      
   }

void histo_up_count()
   {
   for (int i=0; i < 50; i++)
      {
      histo_1 = iCustom(NULL,0,"MACD True",2,i);
      if (histo_1 > 0)
         {
         //Print(i);
         histoupcount = i;
         }
     
      else return;
              
      }   
      return;
    }

void histo_down_count()
   {
   for (int i=0; i < 50; i++)
      {
      histo_1 = iCustom(NULL,0,"MACD True",2,i);
      if (histo_1 < 0)
         {
         //Print(i);
         histodowncount = i;
         }
     
      else return;        
      
      }   
      return;
    }
   

        
bool histo()
   {
   if(histo > 0) return(true);
   else return(false);
   }

bool A_high()
   {
      { 
      if(val1==0 && histo()==false)
         {
         for (int i=0; val1==0; i++)
            {
            val1=iFractals(NULL, 0, MODE_UPPER, i);
            A = val1;
            if(A!=0)
               {
               Print(A, " A high Located");
               //Print("A bars ",i);
               //Print(A);
               return(true);
               }
             }
            }
        }
    return(false);                 
   }
   
bool A_low()
   {
   if(val2==0 && histo()==true)
      {
      for (int i=0; val2==0; i++)
         {
          val2=iFractals(NULL, 0, MODE_LOWER, i);
          A = val2;
          if(A!=0)
          {
           Print(A, " A Low Located");
           //Print(A);
           return(true);
           }
          }
         }
               
    return(false);                 
   }


bool B_high()
   {
      { 
      if(val1==0 && histo()==true)
         {
         for (int i=0; val1==0; i++) //I want this instead (int i=histoupcount; val1==0; i--) but crashes MetaTrader
            {
            val1=iFractals(NULL, 0, MODE_UPPER, i);
            B = val1;
            if(B!=0)
               {
               Print(B, " B high Located");
               //Print(A);
               return(true);
               }
             }
            }
        }
    return(false);                 
   }


   bool B_low()
   {
      { 
      if(val2==0 && histo()==false)
         {
         for (int i=0; val2==0; i++) //I want this instead (int i=histodowncount: val1 ==0; i--) but crashes MetaTrader
            {
            val2=iFractals(NULL, 0, MODE_LOWER, i);
            B = val2;
            if(B!=0)
               {
               Print(B, " B low Located");
               //Print(A);
               return(true);
               }
             }
            }
        }
    return(false);                 
   }



int tiles()
   {
    
   int chart_ID=0;


//----------------------B9

   string name9="B9";
   if(!ObjectCreate(0,name9,OBJ_RECTANGLE_LABEL,0,0,0))
     {
      Print(__FUNCTION__,
            ": failed to create the button! Error code = ",GetLastError());
      return(false);
     }
//--- set button coordinates 
   ObjectSet(name9,OBJPROP_XDISTANCE,0);
   ObjectSet(name9,OBJPROP_YDISTANCE,300);
   ObjectSet(name9,OBJPROP_YSIZE,250);
   ObjectSet(name9,OBJPROP_XSIZE,140);  
   ObjectSet(name9,OBJPROP_BGCOLOR,clrDimGray);
   ObjectSet(name9,OBJPROP_BACK,false);
//--- successful execution 


//----------------------B10
   string name10="B10";
   if(!ObjectCreate(0,name10,OBJ_LABEL,0,0,0))
     {
      Print(__FUNCTION__,
            ": failed to create the button! Error code = ",GetLastError());
      return(false);
     }
//--- set button coordinates 
   ObjectSet(name10,OBJPROP_XDISTANCE,15);
   ObjectSet(name10,OBJPROP_YDISTANCE,370);  
   //ObjectSet(name10,OBJPROP_BGCOLOR,clrLime);
   ObjectSetText(name10,"BUY",12,"Arial",clrLime);
   
   

//----------------------B11
   string name11="B11";
   if(!ObjectCreate(0,name11,OBJ_LABEL,0,0,0))
     {
      Print(__FUNCTION__,
            ": failed to create the button! Error code = ",GetLastError());
      return(false);
     }
//--- set button coordinates 
   ObjectSet(name11,OBJPROP_XDISTANCE,10);
   ObjectSet(name11,OBJPROP_YDISTANCE,450);  
   //ObjectSet(name10,OBJPROP_BGCOLOR,clrLime);
   ObjectSetText(name11,"SELL",12,"Arial",clrRed);
   //ObjectSet(chart_ID,name10,OBJPROP_FONT,Arial);


//-----------------------B12
   string name12="B12";
   if(!ObjectCreate(0,name12,OBJ_LABEL,0,0,0))
     {
      Print(__FUNCTION__,
            ": failed to create the button! Error code = ",GetLastError());
      return(false);
     }
     
   ObjectSet(name12,OBJPROP_XDISTANCE,15);
   ObjectSet(name12,OBJPROP_YDISTANCE,320);  
   ObjectSetText(name12,CharToStr(217),25,"Wingdings",clrLime);
   
//-----------------------B13
   string name13="B13";
   if(!ObjectCreate(0,name13,OBJ_LABEL,0,0,0))
     {
      Print(__FUNCTION__,
            ": failed to create the button! Error code = ",GetLastError());
      return(false);
     }
     
   ObjectSet(name13,OBJPROP_XDISTANCE,15);
   ObjectSet(name13,OBJPROP_YDISTANCE,470);  
   ObjectSetText(name13,CharToStr(218),25,"Wingdings",clrRed);
     

//----------------------B1
   string name1="B1";
   if(!ObjectCreate(0,name1,OBJ_RECTANGLE_LABEL,0,0,0))
     {
      Print(__FUNCTION__,
            ": failed to create the button! Error code = ",GetLastError());
      return(false);
     }

//--- set text and coordinates   
   ObjectSet(name1,OBJPROP_XDISTANCE,90);
   ObjectSet(name1,OBJPROP_YDISTANCE,330);
   ObjectSet(name1,OBJPROP_YSIZE,15);
   ObjectSet(name1,OBJPROP_XSIZE,40);  
   ObjectSet(name1,OBJPROP_BGCOLOR,clrBlack);
   
//--- successful execution 

//-----------------------B2
   string name2="B2";
   if(!ObjectCreate(0,name2,OBJ_RECTANGLE_LABEL,0,0,0))
     {
      Print(__FUNCTION__,
            ": failed to create the button! Error code = ",GetLastError());
      return(false);
     }
//--- set text and coordinates 
   ObjectSet(name2,OBJPROP_XDISTANCE,90);
   ObjectSet(name2,OBJPROP_YDISTANCE,350);
   ObjectSet(name2,OBJPROP_YSIZE,15);
   ObjectSet(name2,OBJPROP_XSIZE,40);  
   ObjectSet(name2,OBJPROP_BGCOLOR,clrBlack);
//--- successful execution 

//-------------------------B3
   string name3="B3";
   if(!ObjectCreate(0,name3,OBJ_RECTANGLE_LABEL,0,0,0))
     {
      Print(__FUNCTION__,
            ": failed to create the button! Error code = ",GetLastError());
      return(false);
     }
//--- set button coordinates 
   ObjectSet(name3,OBJPROP_XDISTANCE,90);
   ObjectSet(name3,OBJPROP_YDISTANCE,370);
   ObjectSet(name3,OBJPROP_YSIZE,15);
   ObjectSet(name3,OBJPROP_XSIZE,40);  
   ObjectSet(name3,OBJPROP_BGCOLOR,clrBlack);
   //return(true);

//-------------------------B4  
   string name4="B4";
   if(!ObjectCreate(0,name4,OBJ_RECTANGLE_LABEL,0,0,0))
     {
      Print(__FUNCTION__,
            ": failed to create the button! Error code = ",GetLastError());
      return(false);
     }
//--- set button coordinates 
   ObjectSet(name4,OBJPROP_XDISTANCE,90);
   ObjectSet(name4,OBJPROP_YDISTANCE,390);
   ObjectSet(name4,OBJPROP_YSIZE,15);
   ObjectSet(name4,OBJPROP_XSIZE,40);  
   ObjectSet(name4,OBJPROP_BGCOLOR,clrBlack);
//--- successful execution 

//-------------------------B5
   string name5="B5";
   if(!ObjectCreate(0,name5,OBJ_RECTANGLE_LABEL,0,0,0))
     {
      Print(__FUNCTION__,
            ": failed to create the button! Error code = ",GetLastError());
      return(false);
     }
//--- set button coordinates 
   ObjectSet(name5,OBJPROP_XDISTANCE,90);
   ObjectSet(name5,OBJPROP_YDISTANCE,450);
   ObjectSet(name5,OBJPROP_YSIZE,15);
   ObjectSet(name5,OBJPROP_XSIZE,40);  
   ObjectSet(name5,OBJPROP_BGCOLOR,clrBlack);
//--- successful execution 

//-------------------------B6 
   string name6="B6";
   if(!ObjectCreate(0,name6,OBJ_RECTANGLE_LABEL,0,0,0))
     {
      Print(__FUNCTION__,
            ": failed to create the button! Error code = ",GetLastError());
      return(false);
     }
//--- set button coordinates 
   ObjectSet(name6,OBJPROP_XDISTANCE,90);
   ObjectSet(name6,OBJPROP_YDISTANCE,470);
   ObjectSet(name6,OBJPROP_YSIZE,15);
   ObjectSet(name6,OBJPROP_XSIZE,40);  
   ObjectSet(name6,OBJPROP_BGCOLOR,clrBlack);
//--- successful execution 

//-------------------------B7  
   string name7="B7";
   if(!ObjectCreate(0,name7,OBJ_RECTANGLE_LABEL,0,0,0))
     {
      Print(__FUNCTION__,
            ": failed to create the button! Error code = ",GetLastError());
      return(false);
     }
//--- set button coordinates 
   ObjectSet(name7,OBJPROP_XDISTANCE,90);
   ObjectSet(name7,OBJPROP_YDISTANCE,490);
   ObjectSet(name7,OBJPROP_YSIZE,15);
   ObjectSet(name7,OBJPROP_XSIZE,40);  
   ObjectSet(name7,OBJPROP_BGCOLOR,clrBlack);
//--- successful execution 

//-------------------------B8 
   string name8="B8";
   if(!ObjectCreate(0,name8,OBJ_RECTANGLE_LABEL,0,0,0))
     {
      Print(__FUNCTION__,
            ": failed to create the button! Error code = ",GetLastError());
      return(false);
     }
//--- set button coordinates 
   ObjectSet(name8,OBJPROP_XDISTANCE,90);
   ObjectSet(name8,OBJPROP_YDISTANCE,510);
   ObjectSet(name8,OBJPROP_YSIZE,15);
   ObjectSet(name8,OBJPROP_XSIZE,40);  
   ObjectSet(name8,OBJPROP_BGCOLOR,clrBlack);
//--- successful execution 

   return(true);



  }
    

Please advise
Thanks
 
I ran the indicated code, worked fine.
#property strict
void OnStart()
  {
  double B,val1=0;
         for (int i=0; val1==0; i++) //I want this instead (int i=histoupcount; val1==; i--) but crashes MetaTrader
            {
            val1=iFractals(NULL, 0, MODE_UPPER, i);
            B = val1;
            if(B!=0)
               {
               Print(B, " B high Located ",i); // testscr USDSGD,Daily: 1.4005 B high Located 6

               //Print(A);
               return;
               }
            }

Use the debugger or print out your variables, including _LastError and prices and find out why. Do you really expect us to debug your code for you?
          Code debugging - Developing programs - MetaEditor Help
          Error Handling and Logging in MQL5 - MQL5 Articles (2015)
          Tracing, Debugging and Structural Analysis of Source Code - MQL5 Articles (2011)
          Introduction to MQL5: How to write simple Expert Advisor and Custom Indicator - MQL5 Articles (2010)

 
Thanks


I do not get any debugging output or errors found in the debugger. 
Switching time frames makes MetaTrader (not responding)

I do not understand the crash with no errors or debug info. 
Reason: