Anyone able to fix this code?

 
//+------------------------------------------------------------------+
//|                                          1ClickModifyAllv03.mq4  |
//|                                     Copyright 2020, Khalid Judo  |
//|                                                              DI  |
//+------------------------------------------------------------------+
#property copyright "Copyright 2020, Khalid Judo"
#property link      "DI"
#property version   "1.00"
#property strict

//--- input parameters
extern double Buy_TP = 0;
extern double Buy_SL = 0;
extern double Sell_TP = 0;
extern double Sell_SL = 0;
extern int PosX = 90;
extern int PosY = 520;
extern int SizeX = 80;
extern int SizeY = 50;
input bool RunOnCurrentCurrencyPair = true;
input bool CloseOnlyManualTrades = false;
input bool DeletePendingOrders = false;
input int  MaxSlippage = 5;



//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- 
   
   ObjectCreate(0,"ModifyButton",OBJ_BUTTON,0,0,0);
   ObjectSetInteger(0,"ModifyButton",OBJPROP_CORNER,CORNER_RIGHT_UPPER);
   ObjectSetInteger(0,"ModifyButton",OBJPROP_XDISTANCE,PosX);
   ObjectSetInteger(0,"ModifyButton",OBJPROP_YDISTANCE,PosY);
   ObjectSetInteger(0,"ModifyButton",OBJPROP_XSIZE,SizeX);
   ObjectSetInteger(0,"ModifyButton",OBJPROP_YSIZE,SizeY);

   ObjectSetString(0,"ModifyButton",OBJPROP_TEXT,"Modify");

   ObjectSetInteger(0,"ModifyButton",OBJPROP_COLOR, White);
   ObjectSetInteger(0,"ModifyButton",OBJPROP_BGCOLOR, Green);
   ObjectSetInteger(0,"ModifyButton",OBJPROP_BORDER_COLOR,White);
   ObjectSetInteger(0,"ModifyButton",OBJPROP_BORDER_TYPE,BORDER_FLAT);
   ObjectSetInteger(0,"ModifyButton",OBJPROP_BACK,false);
   ObjectSetInteger(0,"ModifyButton",OBJPROP_HIDDEN,true);
   ObjectSetInteger(0,"ModifyButton",OBJPROP_STATE,true);
   ObjectSetInteger(0,"ModifyButton",OBJPROP_FONTSIZE,12);
   ObjectSetInteger(0,"ModifyButton",OBJPROP_ZORDER,CHARTEVENT_OBJECT_CLICK);

//---
   return(INIT_SUCCEEDED);
  }
  
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---

   ObjectDelete(0,"ModifyButton");
   
  }
  
  
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
      
  }
  
  
//+------------------------------------------------------------------+
//| ChartEvent function                                              |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
  {
//---
            
   if(sparam== "ModifyButton")
      {
      if(RunOnCurrentCurrencyPair == true && CloseOnlyManualTrades == true) CloseAllOrdersV01(DeletePendingOrders,MaxSlippage);
      else if(RunOnCurrentCurrencyPair == true && CloseOnlyManualTrades == false) CloseAllOrdersV02(DeletePendingOrders,MaxSlippage);
      //else if(RunOnCurrentCurrencyPair == false && CloseOnlyManualTrades == true) CloseAllOrdersV03(DeletePendingOrders,MaxSlippage);
      //else if(RunOnCurrentCurrencyPair == false && CloseOnlyManualTrades == false) CloseAllOrdersV04(DeletePendingOrders,MaxSlippage);
        
      ObjectSetInteger(0,"ModifyButton",OBJPROP_STATE,false);    
      }
         
//---      
  }
//+------------------------------------------------------------------+


void CloseAllOrdersV01(bool boolPendingOrders, int intMaxSlippage)
  {
   bool checkOrderClose = true;        
   int index = OrdersTotal()-1;
   
   while (index >=0 && OrderSelect (index,SELECT_BY_POS,MODE_TRADES)==true)
      {
      if (OrderType()==OP_BUY || OrderType()==OP_BUYLIMIT || OrderType()==OP_BUYSTOP)
          {
          if (OrderSymbol() == Symbol() && OrderMagicNumber() ==0 &&  Buy_TP!=0)
             {         
             checkOrderClose = OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),Buy_TP,0,Blue);            
             }
          else if (OrderSymbol() == Symbol() && OrderMagicNumber() ==0 &&  Buy_SL!=0)
             {
             checkOrderClose = OrderModify(OrderTicket(),OrderOpenPrice(),Buy_SL,OrderTakeProfit(),0,Red);            
             }
          }
      if (OrderType()==OP_SELL || OrderType()==OP_SELLLIMIT || OrderType()==OP_SELLSTOP)
          {
          if (OrderSymbol() == Symbol() && OrderMagicNumber() ==0 &&  Sell_TP!=0)
             {         
             checkOrderClose = OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),Sell_TP,0,Blue);            
             }
          else if (OrderSymbol() == Symbol() && OrderMagicNumber() ==0 &&  Sell_SL!=0)
             {
             checkOrderClose = OrderModify(OrderTicket(),OrderOpenPrice(),Sell_SL,OrderTakeProfit(),0,Red);            
             }
          }
      if(checkOrderClose == false)
         {
         int errorCode = GetLastError();
         
         if (errorCode == 1 || errorCode == 2 || errorCode == 5 || errorCode == 6 || errorCode == 64 || errorCode == 65 || errorCode == 132 || errorCode == 133 || errorCode == 139) break;
         else continue;        
         }        
      
      index--;
      }
   }      
      
      
void CloseAllOrdersV02(bool boolPendingOrders, int intMaxSlippage)
  {
   bool checkOrderClose = true;        
   int index = OrdersTotal()-1;
   
   while (index >=0 && OrderSelect (index,SELECT_BY_POS,MODE_TRADES)==true)
      {  
     if (OrderType()==OP_SELL || OrderType()==OP_SELLLIMIT || OrderType()==OP_SELLSTOP)
          {
          if (OrderSymbol() == Symbol() &&  Sell_TP!=0)
             {         
             checkOrderClose = OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),Sell_TP,0,Blue);            
             }
          if (OrderSymbol() == Symbol() &&  Sell_SL!=0)
             {
             checkOrderClose = OrderModify(OrderTicket(),OrderOpenPrice(),Sell_SL,OrderTakeProfit(),0,Red);            
             }
          }
     if(checkOrderClose == false)
         {
         int errorCode = GetLastError();
         
         if (errorCode == 1 || errorCode == 2 || errorCode == 5 || errorCode == 6 || errorCode == 64 || errorCode == 65 || errorCode == 132 || errorCode == 133 || errorCode == 139) break;
         else continue;        
         }        
     index--;
     }
     
     checkOrderClose = true;        
     index = OrdersTotal()-1;
   
       while (index >=0 && OrderSelect (index,SELECT_BY_POS,MODE_TRADES)==true)
          {
          if (OrderType()==OP_BUY || OrderType()==OP_BUYLIMIT || OrderType()==OP_BUYSTOP)
              {
              if (OrderSymbol() == Symbol() &&  Buy_TP!=0)
                 {         
                 checkOrderClose = OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),Buy_TP,0,Blue);            
                 }
              if (OrderSymbol() == Symbol() &&  Buy_SL!=0)
                 {
                 checkOrderClose = OrderModify(OrderTicket(),OrderOpenPrice(),Buy_SL,OrderTakeProfit(),0,Red);            
                 }
              }
         if(checkOrderClose == false)
             {
             int errorCode = GetLastError();
             
             if (errorCode == 1 || errorCode == 2 || errorCode == 5 || errorCode == 6 || errorCode == 64 || errorCode == 65 || errorCode == 132 || errorCode == 133 || errorCode == 139) break;
             else continue;        
             }        
         index--;
         }
   } 
   
   
//void CloseAllOrdersV03(bool boolPendingOrders, int intMaxSlippage)
//  {
//   bool checkOrderClose = true;        
//   int index = OrdersTotal()-1;
//   
//   while (index >=0 && OrderSelect (index,SELECT_BY_POS,MODE_TRADES)==true)
//      {
//      if ((OrderType()==OP_BUY || OrderType()==OP_SELL) && OrderMagicNumber() ==0)
//         {         
//         checkOrderClose = OrderClose (OrderTicket(), OrderLots(), OrderClosePrice(), intMaxSlippage, CLR_NONE);               
//         }
//         
//      else if (boolPendingOrders == true && OrderType() != OP_SELL && OrderMagicNumber() ==0)
//         {
//         checkOrderClose = OrderDelete (OrderTicket(),CLR_NONE);
//         }
//         
//      
//      if(checkOrderClose == false)
//         {
//         int errorCode = GetLastError();
//         
//         if (errorCode == 1 || errorCode == 2 || errorCode == 5 || errorCode == 6 || errorCode == 64 || errorCode == 65 || errorCode == 132 || errorCode == 133 || errorCode == 139) break;
//         else continue;        
//         }           
//                
//      
//      index--;
//      }
//   }      
      
      
//void CloseAllOrdersV04(bool boolPendingOrders, int intMaxSlippage)
//  {
//   bool checkOrderClose = true;        
//   int index = OrdersTotal()-1;
//   
//   while (index >=0 && OrderSelect (index,SELECT_BY_POS,MODE_TRADES)==true)
//      {
//      if (TP!=0)
//         {         
//         checkOrderClose = OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),TP,0,Blue);            
//         }
//      else if (SL!=0)
//         {
//         checkOrderClose = OrderModify(OrderTicket(),OrderOpenPrice(),SL,OrderTakeProfit(),0,Red);
//         }
//      else if (boolPendingOrders == true)
//         {
//         checkOrderClose = OrderDelete (OrderTicket(),CLR_NONE);
//         }
//         
//      
//      if(checkOrderClose == false)
//         {
//         int errorCode = GetLastError();
//         
//         if (errorCode == 1 || errorCode == 2 || errorCode == 5 || errorCode == 6 || errorCode == 64 || errorCode == 65 || errorCode == 132 || errorCode == 133 || errorCode == 139) break;
//         else continue;        
//         }          
//      
//      
//      index--;
//      }
//   }
 
Khalid Aldarawy:

This code has been modified from another one. I would like to fix this code and convert it to an "indicator" instead of being an "EA" ... I hope it is not hard to do ... Also 1 important thing about it, it creates a loop in my computer that freezes not only MT4 but the whole network goes down, I am not sure what creates this loop or cause this freeze but every time I use this EA it works fine the first time but then it freezes MT4 and the network if I tried to use it again.

Thanks in advance

 
Khalid Aldarawy:

This code has been modified from another one. I would like to fix this code and convert it to an "indicator" instead of being an "EA" ... I hope it is not hard to do ... Also 1 important thing about it, it creates a loop in my computer that freezes not only MT4 but the whole network goes down, I am not sure what creates this loop or cause this freeze but every time I use this EA it works fine the first time but then it freezes MT4 and the network if I tried to use it again.

Thanks in advance

Khalid Aldarawy:

This code has been modified from another one. I would like to fix this code and convert it to an "indicator" instead of being an "EA" ... I hope it is not hard to do ... Also 1 important thing about it, it creates a loop in my computer that freezes not only MT4 but the whole network goes down, I am not sure what creates this loop or cause this freeze but every time I use this EA it works fine the first time but then it freezes MT4 and the network if I tried to use it again.

Thanks in advance

I think it can not be converted to an indicator.

 
Indicators can not trade.
 
Khalid Aldarawy: Is there anything wrong with this code? it freezes MT4
   while (index >=0 && OrderSelect (index,SELECT_BY_POS,MODE_TRADES)==true){  
     ⋮
     if(checkOrderClose == false){
         ⋮
         if (errorCode …) break;
         else continue;        
         }        
     index--;
     }

On a non-fatal error you continue. The index doesn't change and you have an infinite loop. Replace your while loops with for loops.

 
   if(checkOrderClose == false)
      {
      int errorCode = GetLastError();
      if (errorCode == 1 || errorCode == 2 || errorCode == 5 || errorCode == 6 || errorCode == 64 || errorCode == 65 || errorCode == 132
            || errorCode == 133 || errorCode == 139)
         break;
      else
         continue;
      }

Also don't post code with long lines of code, break then up as above.

 
Keith Watford:

Also don't post code with long lines of code, break then up as above.

I am not quite sure if I understood :)

 
Khalid Aldarawy:

I am not quite sure if I understood :)

You see this?

         if (errorCode == 1 || errorCode == 2 || errorCode == 5 || errorCode == 6 || errorCode == 64 || errorCode == 65 || errorCode == 132 || errorCode == 133 || errorCode == 139) break;
         else continue;        

Well on my laptop screen your line of code doesn't fit the screen. This means that I have to scroll across to read the whole line. 

No problem if you keep your lines shorter.

         if (errorCode == 1 || errorCode == 2 || errorCode == 5 || errorCode == 6 || errorCode == 64 || errorCode == 65 
          || errorCode == 132 || errorCode == 133 || errorCode == 139)
         break;
         else continue;     
Often I can't be bothered to read people's code when the lines are too long. I am not the only one.
 
Keith Watford:

You see this?

Well on my laptop screen your line of code doesn't fit the screen. This means that I have to scroll across to read the whole line. 

No problem if you keep your lines shorter.

Often I can't be bothered to read people's code when the lines are too long. I am not the only one.

I got you know ... sorry about this ... I think it is a simple code ... I edited it myself ...

but it creates this freeze when I use it more than 1 time :)

I have to restart computer to regain computer stability again ... any idea what creates this issue?

Thanks in advance

 
Khalid Aldarawy:

I got you know ... sorry about this ... I think it is a simple code ... I edited it myself ...

but it creates this freeze when I use it more than 1 time :)

I have to restart computer to regain computer stability again ... any idea what creates this issue?

Thanks in advance

William has already given you the answer in post #4

 
William Roeder:

On a non-fatal error you continue. The index doesn't change and you have an infinite loop. Replace your while loops with for loops.

thank you

Reason: