Script to close all opened market orders

 

Hi all,

The following script was developed from the existing "close" script in MT4. The intention was to create a script that will close all market orders (BUY & SELL) that are open and running. However, I find that the script closes all market orders except for one. The bottom most market order is always left un-closed, after the script completes its operation. .... Have I made an error somewhere ? Can someone kindly point out the error ?

Many Thanks.


int start()
{
bool result;
double price;
int cmd,error;
//----
static int i=1;
while(i<=OrdersTotal())
{
if(OrderSelect(i-1,SELECT_BY_POS,MODE_TRADES))
{
cmd=OrderType();
//---- first order is buy or sell
if(cmd==OP_BUY || cmd==OP_SELL)
{
RefreshRates();
if(cmd==OP_BUY) price=Bid;
else price=Ask;
result=OrderClose(OrderTicket(),OrderLots(),price,3,CLR_NONE);

if (result == TRUE) {i++; continue;}


else {error=GetLastError(); Alert("LastError = ",error);}
if(error==135 || error==129)
{
RefreshRates();
continue;
}
}
else Print( "no market orders ", error);
}
}
return(0);
}

 
This will close open and pending. Modify accordingly.
//+------------------------------------------------------------------+
//| script program start function                                    |
//+------------------------------------------------------------------+
#include <stdlib.mqh>
extern bool removeFromThisChartOnly = true;

int start(){
   
   RemoveAllOrders();
   
   return(0);
}
//+------------------------------------------------------------------+




void RemoveAllOrders(){
   for(int i = OrdersTotal() - 1; i >= 0 ; i--){
      OrderSelect(i,SELECT_BY_POS);
      if(OrderSymbol() != Symbol() && removeFromThisChartOnly) continue;
      double price = MarketInfo(OrderSymbol(),MODE_ASK);
      if(OrderType() == OP_BUY) price = MarketInfo(OrderSymbol(),MODE_BID);
      if(OrderType() == OP_BUY || OrderType() == OP_SELL){
         OrderClose(OrderTicket(), OrderLots(),price,5);
      }else{
         OrderDelete(OrderTicket());
      }
      Sleep(100);
      int error = GetLastError();
      if(error > 0)
         Print("Unanticipated error: ", ErrorDescription(error));
      RefreshRates();
   }
}
EDIT: yes i know, that the price check is unneeded and can be substituted with OrderClosePrice(). I'll modify it one day :P
 
//+------------------------------------------------------------------+
//| script program start function                                    |
//+------------------------------------------------------------------+
#include <stdlib.mqh>
extern bool removeFromThisChartOnly = true;

int start(){
   
   RemoveAllOrders();
   
   return(0);
}
//+------------------------------------------------------------------+




void RemoveAllOrders(){
   for(int i = OrdersTotal() - 1; i >= 0 ; i--){
      OrderSelect(i,SELECT_BY_POS);
      if(OrderSymbol() != Symbol() && removeFromThisChartOnly) continue;
      //double price = MarketInfo(OrderSymbol(),MODE_ASK);
      //if(OrderType() == OP_BUY) price = MarketInfo(OrderSymbol(),MODE_BID);
      if(OrderType() == OP_BUY || OrderType() == OP_SELL){
         //OrderClose(OrderTicket(), OrderLots(),price,5);
         OrderClose(OrderTicket(), OrderLots(),OrderClosePrice(),5);
      }else{
         OrderDelete(OrderTicket());
      }
      Sleep(100);
      int error = GetLastError();
      if(error > 0) Print("Unanticipated error: ", ErrorDescription(error));
      RefreshRates();
   }
}
Modified. Had time :P