Close button is not closing my pending order. Can anyone help ples.....

 
Blew script is not closing my pending order can anyone help me please.........





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

//|                                            1ClickCloseAllv03.mq4 |

//|                                Copyright 2016, Ozan Buyuksemerci |

//|                                             https://www.ozan.org |

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

#property copyright "Copyright 2016, Ozan Buyuksemerci (grandaevus)"

#property link      "https://ozan.org"

#property version   "3.00"

#property strict



//--- input parameters

input bool RunOnCurrentCurrencyPair = true;

input bool CloseOnlyManualTrades = true;

input bool DeletePendingOrders = false;

input int  MaxSlippage = 5;







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

//| Expert initialization function                                   |

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

int OnInit()

  {

//--- 

   

   ObjectCreate(0,"CloseButton",OBJ_BUTTON,0,0,0);

   ObjectSetInteger(0,"CloseButton",OBJPROP_XDISTANCE,25);

   ObjectSetInteger(0,"CloseButton",OBJPROP_YDISTANCE,100);

   ObjectSetInteger(0,"CloseButton",OBJPROP_XSIZE,100);

   ObjectSetInteger(0,"CloseButton",OBJPROP_YSIZE,50);



   ObjectSetString(0,"CloseButton",OBJPROP_TEXT,"Close All");

      

   

   ObjectSetInteger(0,"CloseButton",OBJPROP_COLOR, White);

   ObjectSetInteger(0,"CloseButton",OBJPROP_BGCOLOR, Red);

   ObjectSetInteger(0,"CloseButton",OBJPROP_BORDER_COLOR,Red);

   ObjectSetInteger(0,"CloseButton",OBJPROP_BORDER_TYPE,BORDER_FLAT);

   ObjectSetInteger(0,"CloseButton",OBJPROP_BACK,false);

   ObjectSetInteger(0,"CloseButton",OBJPROP_HIDDEN,true);

   ObjectSetInteger(0,"CloseButton",OBJPROP_STATE,false);

   ObjectSetInteger(0,"CloseButton",OBJPROP_FONTSIZE,12);



//---

   return(INIT_SUCCEEDED);

  }

  

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

//| Expert deinitialization function                                 |

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

void OnDeinit(const int reason)

  {

//---



   ObjectDelete(0,"CloseButton");

   

  }

  

  

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

//| Expert tick function                                             |

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

void OnTick()

  {

//---

      

  }

  

  

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

//| ChartEvent function                                              |

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

void OnChartEvent(const int id,

                  const long &lparam,

                  const double &dparam,

                  const string &sparam)

  {

//---

            

   if(sparam== "CloseButton")

      {

      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,"CloseButton",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 (OrderSymbol() == Symbol() && OrderMagicNumber() ==0 && (OrderType()==OP_BUY || OrderType()==OP_SELL))

         {         

         checkOrderClose = OrderClose (OrderTicket(), OrderLots(), OrderClosePrice(), intMaxSlippage, CLR_NONE);               

         }

         

      else if (boolPendingOrders == true && OrderSymbol() == Symbol() && OrderMagicNumber() ==0 && OrderType() != OP_BUY && OrderType() != OP_SELL)

         {

         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 CloseAllOrdersV02(bool boolPendingOrders, int intMaxSlippage)

  {

   bool checkOrderClose = true;        

   int index = OrdersTotal()-1;

   

   while (index >=0 && OrderSelect (index,SELECT_BY_POS,MODE_TRADES)==true)

      {

      if (OrderSymbol() == Symbol() && (OrderType()==OP_BUY || OrderType()==OP_SELL))

         {         

         checkOrderClose = OrderClose (OrderTicket(), OrderLots(), OrderClosePrice(), intMaxSlippage, CLR_NONE);               

         }

         

      else if (boolPendingOrders == true && OrderSymbol() == Symbol() && OrderType() != OP_BUY && OrderType() != OP_SELL)

         {

         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 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_BUY && 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 (OrderType()==OP_BUY || OrderType()==OP_SELL)

         {         

         checkOrderClose = OrderClose (OrderTicket(), OrderLots(), OrderClosePrice(), intMaxSlippage, CLR_NONE);               

         }

         

      else if (boolPendingOrders == true && OrderType() != OP_BUY && OrderType() != OP_SELL)

         {

         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--;

      }

   }    

script is not closing my pending order can anyone help me please.........

 
  1. When you post code please use the SRC button! Please edit your post.
              General rules and best pratices of the Forum. - General - MQL5 programming forum

  2. while (index >=0 && OrderSelect (index,SELECT_BY_POS,MODE_TRADES)==true)
    In the presence of multiple orders (one EA multiple charts, multiple EAs, manual trading,) while you are waiting for the current operation (closing, deleting, modifying) to complete, any number of other operations on other orders could have concurrently happened and changed the position indexing:
    1. For non-FIFO (US brokers,) (or the EA only opens one order per symbol,) you can simply count down in a position loop, and you won't miss orders. Get in the habit of always counting down.
                Loops and Closing or Deleting Orders - MQL4 and MetaTrader 4 - MQL4 programming forum
    2. For FIFO (US brokers,) and you (potentially) process multiple orders per symbol, you must count up and on a successful operation, reprocess all positions (set index to -1 before continuing.)
    3. and check OrderSelect in case earlier positions were deleted.
                What are Function return values ? How do I use them ? - MQL4 and MetaTrader 4 - MQL4 programming forum
                Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
    4. and if you (potentially) process multiple orders, must call RefreshRates() after server calls if you want to use the Predefined Variables (Bid/Ask) or OrderClosePrice() instead, on the next order/server call.
    You are exiting the loop on a failed select. Move the OrderSelect out of the while and into it's own if. See № 3 below.

  3. Using OrdersTotal directly and/or no Magic number filtering on your OrderSelect loop means your code is incompatible with every EA (including itself on other charts and manual trading.)
              Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 and MetaTrader 4 - MQL4 programming forum

  4. You should be able to read your code out loud and have it make sense. You would never write if( (2+2 == 4) == true) would you? if(2+2 == 4) is sufficient. So don't write if(bool == true), just use if(bool) or if(!bool). Code becomes self documenting when you use meaningful variable names, like bool isLongEnabled where as Long_Entry sounds like a trigger price or a ticket number and "if long entry" is an incomplete sentence.
Reason: