closed_loss

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

This tool is designed as an automated management script for the MetaTrader trading platform. Its primary purpose is to scan your active trading portfolio and automatically identify specific positions that meet your predefined criteria—specifically focusing on closing out losing trades to manage risk effectively.

Core Functionality

The script performs a comprehensive sweep of all currently open orders in your account. Instead of closing everything at once, it applies a series of "filters" to ensure that only the trades you intend to manage are affected. Once it identifies a trade that matches your specific rules, it executes an immediate market closure.

Key Filtering Options

To give users precise control, the script operates based on the following logic:

  • Directional Control: You can choose whether the script should act on "Buy" orders, "Sell" orders, or both simultaneously.
  • Profitability Filters: The tool can be set to target only losing trades (to cut losses), only winning trades (to lock in profits), or a combination of both.
  • Asset Specificity: You have the option to limit the script's actions to a specific currency pair (e.g., EURUSD) or allow it to scan all symbols in your account.
  • Magic Number Identification: For traders who use automated Expert Advisors (EAs), the script can distinguish between trades opened by different bots. You can instruct the script to only manage trades with a specific "Magic Number" or, conversely, to ignore certain ones entirely.

How it Works in Practice

When executed, the script works backward through your open positions. For every position it finds, it asks a series of "Yes/No" questions based on your settings:

  1. Does this trade belong to the Magic Number I want to manage?
  2. Is this on the correct currency pair?
  3. Is this a winning or losing trade based on my selection?

If the trade answers "Yes" to all your requirements, the script automatically closes that position and provides a notification in the journal confirming whether the action was successful or if an error occurred. This allows for rapid portfolio cleaning without the need for manual intervention.

Orders Execution
Checks for the total of open ordersIt Closes Orders by itself
6 Views
0 Downloads
0 Favorites
closed_loss
//+------------------------------------------------------------------+
//|                                                      Closed LOSS |
//|                                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 perdedoras."

#property show_inputs

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