CloseAll

Скрипт для удаления всех ордеров по символу. С небольшой фичей - можно указать чтобы наилучший рыночный ордер не удалялся.

CloseAll.mq4
//+------------------------------------------------------------------+
//|                                                     CloseAll.mq4 |
//|                                                Maxim A.Kuznetsov |
//|                                                      luxtrade.tk |
//+------------------------------------------------------------------+
#property copyright "Maxim A.Kuznetsov"
#property link      "luxtrade.tk"
#property version   "1.00"
#property strict
 
#include <nonstd.mqh>
 
input bool EXCEPT_BEST=true;
input int SLIPPAGE=10;
 
int excepted_buy=-1;
int excepted_sell=-1;
double distance_buy=DBL_MAX;
double distance_sell=DBL_MAX;
 
void OnStart()
{
   if (EXCEPT_BEST) DetectExcepted();
   CloseAll();      
}
 
void DetectExcepted() 
{
   int type;
   double price;
   for(int pos=OrdersTotal()-1;pos>=0;pos--) {
      if (!OrderSelect(pos,SELECT_BY_POS,MODE_TRADES)) continue;
      if (OrderSymbol()!=_Symbol) continue;
      if (OrderCloseTime()!=0) continue;
      type=OrderType();
      if (type!=OP_BUY && type!=OP_SELL) continue;
      price=OrderOpenPrice();
      if (type==OP_BUY && (excepted_buy==-1 || distance_buy<Ask-price)) {
         distance_buy=Ask-price;
         excepted_buy=OrderTicket();
      } else if (type==OP_SELL && (excepted_sell==-1 || distance_sell<price-Bid)) {
         distance_sell=price-Bid;
         excepted_sell=OrderTicket();
      }
   }
}
void CloseAll() 
{
   int type;
   int omit=0;
   int att=3;
   int count=0;
   bool ok;
   do {
      omit=0;
      RefreshRates();
      for(int pos=OrdersTotal()-1;pos>=0;pos--) {
         if (!OrderSelect(pos,SELECT_BY_POS,MODE_TRADES)) continue;
         if (OrderSymbol()!=_Symbol) continue;
         if (OrderCloseTime()!=0) continue;
         type=OrderType();
         if (excepted_buy!=-1 && OrderTicket()==excepted_buy) continue;
         if (excepted_sell!=-1 && OrderTicket()==excepted_sell) continue;
         if (type==OP_BUY) {
            ok=OrderClose(OrderTicket(),OrderLots(),Bid,SLIPPAGE);
         } else if (type==OP_SELL) {
            ok=OrderClose(OrderTicket(),OrderLots(),Ask,SLIPPAGE);
         } else {
            ok=OrderDelete(OrderTicket());
         }
         if (!ok) {
            omit++;
         } else {
            count++;
         }
      }
      att--;   
   } while(omit>0 && att>0);
   Alert(StringFormat("CloseAll %s: %d orders cancelled",_Symbol,count));
}