Author: CodersGuru
CloseAll

This tool is designed to provide traders with a versatile and automated way to manage their open positions and pending orders within the MetaTrader 4 platform. Instead of manually closing every trade one by one, this script allows you to execute bulk actions based on specific criteria you define.

The core functionality revolves around a selection system that gives you eight different ways to clear your trading activity:

Comprehensive Management Options

  • Total Clearance: You can choose to wipe the slate clean by closing all active trades and deleting all pending orders simultaneously.
  • Specific Order Types: The logic allows you to separate "Instant" orders (active trades) from "Pending" orders (orders waiting for a price to be hit), letting you clear one category while leaving the other untouched.

Advanced Filtering Criteria To give you more granular control, the script can filter which trades are acted upon based on specific metadata or performance:

  • Identification by ID or Note: You can target specific groups of trades using a "Magic Number" (a unique identifier used by automated systems) or by searching for a specific text comment attached to the trade.
  • Profit and Loss Performance: The tool can automatically scan your account to identify only the winning trades (to lock in gains) or only the losing trades (to cut losses), closing them while leaving the rest of your portfolio active.
  • Time-Based Management: There is a specific logic included to identify "old" trades—specifically those opened on previous days—allowing you to clear out stale positions quickly.

Technical Execution When a command is triggered, the script automatically scans your entire trade history in real-time. It identifies the correct market price for each individual trade (Buy or Sell) and executes the necessary commands to either close the position at the current market rate or delete the pending request from the server. This ensures that your account is updated accurately and efficiently without requiring manual intervention for every single position.

Price Data Components
Series array that contains open time of each bar
Orders Execution
Checks for the total of open ordersIt Closes Orders by itself
15 Views
0 Downloads
0 Favorites
CloseAll
//+------------------------------------------------------------------+
//|                                                     CloseAll.mq4 |
//|                                                       CodersGuru |
//|                                            http://www.xpworx.com |
//+------------------------------------------------------------------+

#property copyright "CodersGuru"
#property link      "http://www.xpworx.com"
#property show_inputs

extern int option = 0;
//+------------------------------------------------------------------+
// Set this prameter to the type of clsoing you want:
// 0- Close all (instant and pending orders) (Default)
// 1- Close all instant orders
// 2- Close all pending orders
// 3- Close by the magic number
// 4- Close by comment
// 5- Close orders in profit
// 6- Close orders in loss
// 7- Close not today orders
//+------------------------------------------------------------------+

extern int magic_number = 0; // set it if you'll use closing option 3 - closing by magic number
extern string comment_text = ""; // set it if you'll use closing option 4 - closing by comment

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

int CloseAll()
{
   int total = OrdersTotal();
   int cnt = 0;
 
   RefreshRates();
   
   switch (option)
   {
      case 0:
            for (cnt = 0 ; cnt <= total ; cnt++)
            {
               OrderSelect(0,SELECT_BY_POS,MODE_TRADES);
                  if(OrderType()==OP_BUY)
                     OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),5,Violet);
                  if(OrderType()==OP_SELL) 
                     OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),5,Violet);
                  if(OrderType()>OP_SELL) //pending orders
                     OrderDelete(OrderTicket());
            }

         break;
      case 1:
            for (cnt = 0 ; cnt <= total ; cnt++)
            {
               OrderSelect(0,SELECT_BY_POS,MODE_TRADES);
                  if(OrderType()==OP_BUY)
                     OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),5,Violet);
                  if(OrderType()==OP_SELL) 
                     OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),5,Violet);
            }
         break;
      case 2:
            for (cnt = 0 ; cnt <= total ; cnt++)
            {
               OrderSelect(0,SELECT_BY_POS,MODE_TRADES);
                  if(OrderType()>OP_SELL)
                     OrderDelete(OrderTicket());
            }
         break;
      case 3:
         for (cnt = 0 ; cnt <= total ; cnt++)
            {
               OrderSelect(0,SELECT_BY_POS,MODE_TRADES);
               if (OrderMagicNumber() == magic_number)
               {
                  if(OrderType()==OP_BUY)
                     OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),5,Violet);
                  if(OrderType()==OP_SELL) 
                     OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),5,Violet);
                  if(OrderType()>OP_SELL)
                     OrderDelete(OrderTicket());
               }
            }         
            break;
      case 4:
         for (cnt = 0 ; cnt <= total ; cnt++)
            {
               OrderSelect(0,SELECT_BY_POS,MODE_TRADES);
               if (OrderComment() == comment_text)
               {
                  if(OrderType()==OP_BUY)
                     OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),5,Violet);
                  if(OrderType()==OP_SELL) 
                     OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),5,Violet);
                  if(OrderType()>OP_SELL)
                     OrderDelete(OrderTicket());
               }
            }         
         break;
      case 5:
         for (cnt = 0 ; cnt <= total ; cnt++)
            {
               OrderSelect(0,SELECT_BY_POS,MODE_TRADES);
               if (OrderProfit() > 0)
               {
                  if(OrderType()==OP_BUY)
                     OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),5,Violet);
                  if(OrderType()==OP_SELL) 
                     OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),5,Violet);
                  if(OrderType()>OP_SELL)
                     OrderDelete(OrderTicket());
               }
            }         
         break;
      case 6:
         for (cnt = 0 ; cnt <= total ; cnt++)
            {
               OrderSelect(0,SELECT_BY_POS,MODE_TRADES);
               if (OrderProfit() < 0)
               {
                  if(OrderType()==OP_BUY)
                     OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),5,Violet);
                  if(OrderType()==OP_SELL) 
                     OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),5,Violet);
                  if(OrderType()>OP_SELL)
                     OrderDelete(OrderTicket());
               }
            }         
         break;
      case 7:
         for (cnt = 0 ; cnt <= total ; cnt++)
            {
               OrderSelect(0,SELECT_BY_POS,MODE_TRADES);
               if (TimeDay(OrderOpenTime()) < TimeDay(CurTime()))
               {
                  if(OrderType()==OP_BUY)
                     OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),5,Violet);
                  if(OrderType()==OP_SELL) 
                     OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),5,Violet);
                  if(OrderType()>OP_SELL)
                     OrderDelete(OrderTicket());
               }
            }         
         break;
   }
}

Comments