closed_total

Download script
Author: Copyright � 2016 (Mike eXplosion)
closed_total

This script is a specialized management tool designed to automate the closing of trading positions within your MetaTrader platform. Instead of manually closing every trade one by one, this utility allows you to execute precise "bulk actions" based on specific criteria you define before running it.

Core Functionality

The primary purpose of the script is to scan all currently active trades and close them systematically. However, it does not simply close everything blindly; it uses a series of filters to ensure that only the specific trades you intend to exit are affected.

Key Filtering Features

To give you maximum control over your portfolio, the logic includes several "smart" filters:

  • Directional Control: You can choose to close only your Buy positions, only your Sell positions, or both simultaneously.
  • Asset Selection: The script can be configured to act only on a specific currency pair (for example, only closing EURUSD trades while leaving others untouched).
  • Magic Number Identification: For traders using Expert Advisors (automated bots), the script can identify trades by their unique "Magic Number." You can set it to target only a specific bot's trades or intentionally ignore trades from a specific one.
  • Profitability Filters: The tool includes sophisticated logic to separate your winners from your losers. You have the option to close only your profitable positions (to bank your gains) or only your losing positions (to cut losses), depending on your current strategy.

How the Logic Works

When you execute the script, it performs a comprehensive sweep of your open trades starting from the most recent. For every active trade found, it runs through a "checklist" of your chosen preferences:

  1. Verification: It checks if the trade matches your Symbol, Magic Number, and Profitability requirements.
  2. Execution: If a trade passes all your filters, the script automatically sends a command to the market to close that specific position at the current best available price.
  3. Reporting: The script provides feedback in the platform's log, confirming which positions were successfully closed and alerting you if any errors occur during the process.

In summary, this is a high-precision "cleanup" tool that allows you to manage your risk and portfolio layout instantly by applying specific rules to large groups of trades at once.

Orders Execution
Checks for the total of open ordersIt Closes Orders by itself
6 Views
0 Downloads
0 Favorites
closed_total
//+------------------------------------------------------------------+
//|                                                     Closed TOTAL |
//|                                Copyright © 2016 (Mike eXplosion) |
//+------------------------------------------------------------------+

#property copyright "Copyright © 2016 (Mike eXplosion)"
#property link      "https://www.mql5.com/en/users/mike_explosion"
#property version   "1.0"

#property strict

#property description "Cierre de todas las posiciones."

#property show_inputs

extern string Indicator       = "Closed TOTAL v1.0";
extern string Copyright       = "Mike eXplosion © 2016";
extern bool   Buy             =true;
extern bool   Sell            =true;
extern int    Solo_Magico     =0;
extern int    Omitir_Magico   =0;
extern bool   Solo_Simbolo    =false;
extern string Simbolo         ="EURUSD";
extern bool   Solo_Ganadoras  =false;
extern bool   Solo_Perdedoras =false;
//+------------------------------------------------------------------+
//| script program start function                                    |
//+------------------------------------------------------------------+
int start()
  {
//----
   int ticket;
   if(OrdersTotal()==0) return(0);
   for(int i=OrdersTotal()-1; i>=0; i--)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)
        {
         if(Solo_Magico>0 && OrderMagicNumber()!=Solo_Magico) continue;
         if(Omitir_Magico>0 && OrderMagicNumber()==Omitir_Magico) continue;
         if(Solo_Simbolo==true && OrderSymbol()!=Simbolo)
           {Print("Simbolo diferente"); continue;}
         if(Solo_Ganadoras==true && OrderProfit()<0) continue;
         if(Solo_Perdedoras==true && OrderProfit()>0) continue;
         if(OrderType()==0 && Buy==true)
           {

            ticket=OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),3,Red);
            if(ticket==-1) Print("Error: ",GetLastError());
            if(ticket>0) Print("Posición ",OrderTicket()," cerrada.");
           }
         if(OrderType()==1 && Sell==true)
           {

            ticket=OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),3,Red);
            if(ticket==-1) Print("Error: ",GetLastError());
            if(ticket>0) Print("Posición ",OrderTicket()," cerrada.");
           }
        }
     }
//----
   return(0);
  }
//+------------------------------------------------------------------+ 

Comments