//+------------------------------------------------------------------+
//|                                                     closeAll.mq4 |
//|                        Copyright 2018, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Sywa"
#property version   "1.00"
#property strict

 
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//---
   int totalorder = OrdersTotal();
   int order_ticket[];
   ArrayResize(order_ticket, totalorder);
   bool useGlobal = true;
   int confirmGlobal =  MessageBox("Close Global ?", "Confirmation",MB_YESNO);
   string symbolstr = "for "+ Symbol() +"?";
   if (confirmGlobal == IDYES) 
   {
      useGlobal = true;
      symbolstr = "for All" +"?";;
   }
   else useGlobal = false;
   
   
   int confirm =  MessageBox("Are you sure Close Profit "+ symbolstr  , "Confirmation",MB_YESNO);
   
   if (confirm == IDYES)
   {  
      // Get All Ticket
      for (int i = 0; i< totalorder; i++)
      {
         bool is_ok = OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
         order_ticket[i]= OrderTicket();
      }
      
      // Close All Order
      for (int i = 0 ; i < totalorder; i++)
      {
         bool getselect = OrderSelect(order_ticket[i], SELECT_BY_TICKET, MODE_TRADES);
      
         double order_lot = OrderLots();
         if ((!useGlobal && OrderSymbol() == Symbol()) || useGlobal)
         {  
            if (OrderProfit() >= 0)
            {
               if (OrderType() == OP_BUY) 
               { 
                    int closeBuy = OrderClose(order_ticket[i], order_lot, MarketInfo(OrderSymbol(), MODE_BID), 3, Red);               
                    Sleep(200); 
               } 
               else if (OrderType() == OP_SELL) 
               {
                  int closeSell = OrderClose(order_ticket[i], order_lot, MarketInfo(OrderSymbol(), MODE_ASK), 3,Red);         
                  Sleep(200);
               }
            }
         }       
      }
   }
}
//+------------------------------------------------------------------+
