CloseTrades_After_Account_Loss_TooMuch

Download script
Author: Copyright � 2007, Tradinator
CloseTrades_After_Account_Loss_TooMuch

This script functions as an automated "emergency circuit breaker" for your trading account. Its primary purpose is to protect your capital by automatically shutting down all active trades if your losses reach a specific, pre-defined limit.

How It Works

The tool operates by constantly monitoring two main factors: your real-time account profit/loss and your current open positions. Here is the logical breakdown of its operation:

1. Setting the Safety Limit Before running the script, you define a "Cutoff Limit" (a negative value). This represents the maximum amount of money you are willing to lose in a single session. For example, if you set this limit to -1,000, the script will watch your account to see if the floating loss hits that mark.

2. Continuous Monitoring The script runs in the background, checking your account status every few seconds. On your trading screen, it provides a live display of your current Balance, Equity, and Profit/Loss, making it easy to see how close you are to your designated limit.

3. The Trigger Mechanism The moment your floating profit drops to or below your specified loss limit, the script takes over automatically:

  • Scan: It instantly scans every single open trade in your account across all symbols.
  • Execution: It identifies whether a trade is a "Buy" or a "Sell" and executes an immediate order to close it at the current market price.
  • Persistence: If a trade fails to close on the first attempt due to market volatility, the script pauses briefly and tries again to ensure the position is fully neutralized.

4. Final Confirmation Once the condition is met and all trades are successfully closed, the script logs a notification message confirming that the "Account Cutoff Limit" was reached and all positions have been cleared.

Summary

In simple terms, this script acts as an automated safety net. It removes the emotional difficulty of having to manually close trades during a bad market move, ensuring that your losses are capped at the exact dollar amount you decide before you start trading.

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

#property copyright "Copyright © 2007, Tradinator"
#property link      "tradinator.fx@gmail.com"
                                           
                                           
                                               //+---------------------------------------------------------------------+
extern double Open_Loss_To_CloseTrades=-1000;  //|The amount of money at which you want to take a loss & close ALL     |
                                               //|open trades. eg if the floating loss in your account reaches or goes |
                                               //|beyond -$1000 then ALL the open positions in your account will be    |
                                               //|closed.                                                              |      
                                               //+---------------------------------------------------------------------+
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()<= Open_Loss_To_CloseTrades)
   {
    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 Cutoff Limit Reached. All Open Trades Have Been Closed");
      return(0);
   }  
   
   Comment("Balance: ",AccountBalance(),", Account Equity: ",AccountEquity(),", Account Profit: ",AccountProfit(),
           "\nMy Account Cutoff Limit: ",Open_Loss_To_CloseTrades);
   
  return(0);
}

Comments