Close_all_profitable_positions loss

Download script
Author: Copyright � 2005, Matias Romeo.
Close_all_profitable_positions loss

This script is designed as an automated management tool for the MetaTrader platform, specifically created to monitor and manage open trades based on a predefined profit threshold. It simplifies the process of exiting positions by automatically identifying and closing them when they reach a certain financial target.

How it Works

The logic follows a structured sequence to ensure every active trade is evaluated:

  • Portfolio Scanning: Every time the script runs, it performs a comprehensive sweep of all currently open trades in your account. It starts from the most recent position and works backward to ensure no orders are skipped during the scanning process.
  • Trade Identification: The tool distinguishes between different types of market positions—specifically identifying "Buy" and "Sell" orders.
  • Profit Threshold Check: For every active trade found, the script calculates the current real-time profit or loss in monetary terms (dollars). It compares this value against a user-defined target.
  • Automated Execution: If a trade's profit is equal to or less than the specific dollar amount you have set as your target, the script immediately initiates a command to close that position at the current market price. This allows for quick exits without requiring manual intervention.
  • Error Handling and Notifications: The system includes a built-in safety check. If an order fails to close for any reason (such as a connection issue or a sudden market shift), the script will trigger an on-screen alert notification. It provides the specific error code and pauses briefly before attempting to continue, ensuring you are kept informed of any technical hiccups.

Key Benefit

This tool is essentially an "auto-exit" assistant. Instead of having to monitor every individual trade manually to see if it has hit your desired dollar amount, the script handles the surveillance and execution for you, helping to maintain a disciplined approach to managing your active positions.

Orders Execution
Checks for the total of open ordersIt Closes Orders by itself
Miscellaneous
It issuies visual alerts to the screen
9 Views
0 Downloads
0 Favorites
Close_all_profitable_positions loss
/*-----------------------------+
|			       |
| Shared by www.Aptrafx.com    |
|			       |
+------------------------------*/

//+------------------------------------------------------------------+
//|                                            close-all-orders.mq4  |
//|                                  Copyright © 2005, Matias Romeo. |
//|                                       Custom Metatrader Systems. |
//+------------------------------------------------------------------+

#property copyright "Copyright © 2005, Matias Romeo."
#property link      "mailto:matiasDOTromeoATgmail.com"

extern int ProfitTarget = 1; // Profit target in dollars

int start()
{
  int total = OrdersTotal();
  for(int i=total-1;i>=0;i--)
  {
    OrderSelect(i, SELECT_BY_POS);
    int type   = OrderType();

    bool result = false;
    
    switch(type)
    {
      //Close opened long positions
      case OP_BUY       : if ( OrderProfit() <= ProfitTarget) result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, Red );
                          break;
      
      //Close opened short positions
      case OP_SELL      : if ( OrderProfit() <= ProfitTarget) result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 5, Red );
                          
    }
    
    if(result == false)
    {
      Alert("Order " , OrderTicket() , " failed to close. Error:" , GetLastError() );
      Sleep(3000);
    }  
  }
  
  return(0);
}

Comments