Author: Copyright � 2016 (Mike eXplosion)
closed_win

This tool is designed for traders using the MetaTrader platform who need a way to quickly and systematically manage their active open positions. Its primary purpose is to automatically identify and close specific trades based on a set of pre-defined rules, helping you clear out your portfolio instantly without having to click on every individual trade manually.

How the Logic Works

When you run this script, it scans all your currently open trading positions and evaluates them against several filters that you can customize:

  • Trade Type Selection: You can choose whether the script should act on "Buy" orders, "Sell" orders, or both simultaneously.
  • Profitability Filtering: The script can be set to only close winning trades, only close losing trades, or ignore those filters entirely to close everything that meets your other criteria.
  • Specific Asset Filtering: If you only want to manage a specific currency pair or asset (for example, just EURUSD), you can enable the "Symbol Only" mode. This prevents the script from affecting any other assets in your account.
  • Magic Number Management: For traders who use automated robots (Expert Advisors), the script supports "Magic Numbers." You can tell the script to only close trades opened by a specific robot, or conversely, tell it to ignore a specific robot's trades while closing everything else.

The Execution Process

Once the script identifies a trade that matches every single one of your requirements (e.g., it is a Buy order, it belongs to the EURUSD symbol, and it is currently in profit), it executes a closing command.

The script works backward through your list of open trades to ensure it processes everything correctly. When a trade is successfully closed, the tool provides a notification in the system log to confirm exactly which position was finalized and if any errors occurred during the process.

Orders Execution
Checks for the total of open ordersIt Closes Orders by itself
10 Views
0 Downloads
0 Favorites
closed_win
//+------------------------------------------------------------------+
//|                                                       Closed WIN |
//|                                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 posiciones ganadoras."

#property show_inputs

extern string Indicator       = "Closed WIN 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  =true;
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