Orders_Closer

Download script
Author: Apostol-Aristotel
Orders_Closer

This tool is designed to automate the management of active trading positions on your MetaTrader platform by identifying and closing trades once they reach a specific profit threshold. It functions as an automated "Take Profit" monitor that scans your account activity in real-time.

How it Works

The script operates by continuously reviewing all open trades in your portfolio. It specifically filters through these positions to ensure it only interacts with the ones you intend for it to manage. It uses three primary criteria to decide which trades to act upon:

  • Asset Matching: It identifies trades that are currently active on the specific currency pair or instrument you have the tool running on.
  • Identification (Magic Number): It looks for a specific "Magic Number." If you leave this as the default setting, the script will manage all trades; if you assign a specific number, it will only manage trades associated with that specific identifier.
  • Profit Threshold: The core logic checks if the current market price has moved in your favor by the amount defined in your settings (the "tp" value).

Execution Logic

Once the script identifies an active trade that meets both the identification and asset criteria, it calculates the current profit:

  1. For Buy Positions: It compares the current market price against your entry price. If the market price has risen by at least the amount you specified, the script automatically sends a command to close the position.
  2. For Sell Positions: It compares the current market price against your entry price. If the market price has dropped by at least the amount you specified, the script automatically closes the position.

Key Features

  • Precision Handling: The tool includes a "slip" setting, which allows for a small margin of movement during execution to ensure that orders are filled successfully even in fast-moving markets.
  • Automated Monitoring: Instead of requiring you to manually watch every price tick to hit your target, the script performs these calculations instantly and executes the closing trade the moment your profit goal is reached.
  • Versatility: Because it recognizes different types of orders (such as standard market orders or pending limit/stop orders), it provides a comprehensive way to lock in gains across various trading strategies.
Orders Execution
Checks for the total of open ordersIt Closes Orders by itself
15 Views
0 Downloads
0 Favorites
Orders_Closer
//+------------------------------------------------------------------+
//|                                                       Closer.mq4 |
//|                                                Apostol-Aristotel |
//|                                  http://www.apostol-aristotel.ru |
//+------------------------------------------------------------------+
#property copyright "Apostol-Aristotel"
#property link      "http://www.apostol-aristotel.ru"
extern double tp=5; // Êîëè÷åñòâî ïèïñîâ
extern int magic=0; // Åñëè magic=0, òî çàêðûâàþòñÿ âñå ñäåëêè
extern int slip=2; // Óðîâåíü ïðîñêàëüçûâàíèÿ äëÿ çàêðûòèÿ ñäåëîê

//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//----
   for (int j = 0; j < OrdersTotal(); j++){
    if (OrderSelect(j, SELECT_BY_POS)){
     if (OrderSymbol() == Symbol()&& (OrderMagicNumber()==magic || magic==0)){
      if((OrderType()==OP_BUY || OrderType()==OP_BUYSTOP || OrderType()==OP_BUYLIMIT) && Bid-OrderOpenPrice()>=tp*Point) OrderClose(OrderTicket(), OrderLots(),Bid,slip,0);
      if((OrderType()==OP_SELL || OrderType()==OP_SELLSTOP || OrderType()==OP_SELLLIMIT) && OrderOpenPrice()-Ask>=tp*Point) OrderClose(OrderTicket(), OrderLots(),Ask,slip,0);
     }
    }
   }
//----
   return(0);
  }
//+------------------------------------------------------------------+

Comments