CloseTrades_After_Account_Profit_Reached

Download script
Author: Copyright � 2007, Tradinator
CloseTrades_After_Account_Profit_Reached

This automated script is designed to act as a safety net and an efficiency tool for your trading account. Its primary purpose is to monitor your real-time financial performance and automatically exit all active positions once a specific profit milestone is met.

How the Logic Works

The Profit Goal At the core of this script is a customizable target value. You define exactly how much money you wish to gain in profit. The script constantly watches your account's "floating" profit—the current gains or losses from all currently open trades that haven't been finalized yet.

Continuous Monitoring The tool runs in the background, constantly checking your live account status. While it is running, it displays a helpful on-screen summary for you, showing your current balance, your total equity, and your current floating profit compared against your pre-set target.

Automatic Execution The moment your total floating profit meets or exceeds your designated dollar amount, the script takes immediate action. It scans every single open trade in your account simultaneously. Regardless of whether a trade is a "Buy" or a "Sell," it sends an instruction to close them instantly at the current market price.

Reliability Measures The logic includes a built-in "retry" mechanism. If the market is moving too fast and a trade fails to close on the first attempt due to minor price fluctuations, the script will pause briefly and try again until the trade is successfully finalized. Once all positions are closed, it notifies you with an on-screen message confirming that the target was reached and your trades have been secured.

Summary for the User

Think of this as an "Automatic Take Profit" for your entire account. Instead of having to manually watch every single trade and click "Close" one by one when you reach your goal, this script handles the heavy lifting for you. It ensures that as soon as your account hits your desired profit level, everything is locked in and closed automatically.

Orders Execution
Checks for the total of open ordersIt Closes Orders by itself
18 Views
0 Downloads
0 Favorites
CloseTrades_After_Account_Profit_Reached
//+------------------------------------------------------------------+
//|                    CloseTrades_After_Account_Profit_Reached.mq4  |
//|                                     Copyright © 2007, Tradinator |
//|                                          tradinator.fx@gmail.com |
//+------------------------------------------------------------------+

#property copyright "Copyright © 2007, Tradinator"
#property link      "tradinator.fx@gmail.com"


                                       
extern double My_Money_Profit_Target=300;     //The amount of money profit at which you want to close ALL open trades.
                                          
int Slippage=5;
int i;

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//---- 
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//---- 
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+


int start()
{
if (AccountProfit()>= My_Money_Profit_Target)
   {
    for(i=OrdersTotal()-1;i>=0;i--)
       {
       OrderSelect(i, SELECT_BY_POS);
       int type   = OrderType();
               
       bool result = false;
              
       switch(type)
          {
          //Close opened long positions
          case OP_BUY  : result = OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),Slippage,Pink);
                         break;
               
          //Close opened short positions
          case OP_SELL : result = OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),Slippage,Pink);
                          
          }
          
       if(result == false)
          {
            Sleep(3000);
          }  
       }
      Print ("Account Profit Reached. All Open Trades Have Been Closed");
      return(0);
   }  
   
   Comment("Balance: ",AccountBalance(),", Account Equity: ",AccountEquity(),", Account Profit: ",AccountProfit(),
           "\nMy Account Profit Target: ",My_Money_Profit_Target);
   
  return(0);
}

Comments