Terminator_MQ4
Download scriptOverview
The Terminator script is a specialized utility designed for MetaTrader 4 users who need an automated way to clear their trading environment quickly. Its primary function is to identify, close, and remove all active trades and pending orders across all currency symbols on your account simultaneously. It serves as a "panic button" or a systematic cleanup tool to ensure that no open positions remain.
Core Logic and Functionality
The script operates based on three specific conditions (parameters) that determine when it should initiate its cleanup process:
1. Immediate Execution (CloseNow) If you set this value to anything other than zero, the script is instructed to begin its work immediately. This overrides other settings, allowing for a manual, instant sweep of all open trades and pending orders.
2. Profit Target Management (CloseAtEquity) The script monitors your account's "Floating Profit" (the difference between your current Equity and your Account Balance). You can set a specific numerical threshold; once your floating profit exceeds this amount, the script will automatically trigger the closure of all positions. This is useful for securing profits systematically as soon as a certain milestone is reached.
3. Time-Based Execution (CloseAtHour) The script can be programmed to act at a specific time of day. If you set the hour to a specific time (e.g., 20 for 8:00 PM), the script will initiate its cleanup process when that hour is reached. If the value is set to 24 or higher, this time-based trigger is essentially deactivated.
Operational Behavior
Once one of the conditions above is met, the script performs a comprehensive sweep:
- Active Trades: It identifies all open "Buy" and "Sell" positions and attempts to close them at the current market price.
- Pending Orders: It identifies any "Limit" or "Stop" orders (orders waiting to be triggered) and deletes them from the system entirely.
Safety Features
To prevent the script from running indefinitely or causing confusion, it includes a notification system. Once the script successfully identifies that there are zero remaining trades or orders left on the account, it will trigger an alert message asking you to remove the expert from your chart. This confirms that the cleanup is complete and the script's job is finished.
/*-----------------------------+
| |
| Shared by www.Aptrafx.com |
| |
+------------------------------*/
//+------------------------------------------------------------------+
//| Terminator_MQ4.mq4 |
//| Copyright © 2006, 9/4/2006 David W Honeywell |
//| transport.david@gmail.com |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2006, 9/4/2006 David W Honeywell"
#property link "transport.david@gmail.com"
//---------------------------------------------------------------------------
// When This Runs It Closes And Deletes Every Trade And Order On All Symbols ,
// Will Not Stop Untill There Are No Current Trades Or Orders .
//---------------------------------------------------------------------------
#property copyright "Copyright © 2006, 9/4/2006 David W or Renee A Honeywell"
#property link "DavidHoneywell800@msn.com RenHon@msn.com"
extern int CloseNow = 0; // Any value other than 0 == CloseMAll 10 and trades will start closing and or cancelling after the price has moved .
//----------------------------------------------------------------------------------------------
// Re-Set CloseAtEquity value AFTER you have attached this expert to the Chart(s) .
// Use the Properties Window
extern int CloseAtEquity = 500000000; // ( (Equity - Balance) > CloseProfit ) == CloseMAll True
// Use the Properties Window
// Re-Set CloseAtEquity value AFTER you have attached this expert to the Chart(s) .
//----------------------------------------------------------------------------------------------
extern int CloseAtHour = 24; // 24 or greater and the CloseAtHour is "disabled" .
int i;
double CloseMAll = 0;
double prevtime = 0;
int start()
{
if ( (Hour()!=CloseAtHour) && (CloseNow==0) && ((AccountEquity()-AccountBalance()) < CloseAtEquity) ) { CloseMAll = -10; }
if ( (Hour()==CloseAtHour) || (CloseNow!=0) || ((AccountEquity()-AccountBalance()) > CloseAtEquity) ) { CloseMAll = 10; }
if (CloseMAll > 0)
{
for(i = OrdersTotal()-1; i >=0 ; i--)
{ OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
if ( (OrderType() == OP_BUY) || (OrderType() == OP_SELL) )
{
OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),0,White);
CloseMAll = 10;
}
if ( (OrderType() == OP_BUYSTOP) || (OrderType() == OP_SELLSTOP) || (OrderType() == OP_BUYLIMIT) || (OrderType() == OP_SELLLIMIT) )
{
OrderDelete( OrderTicket() );
CloseMAll = 10;
}
}
}
if ( (prevtime != Time[0]) && (OrdersTotal() <= 0) && (CloseMAll >= 0) )
{
Alert("Remove Expert");
prevtime = Time[0];
CloseMAll = -10;
}
return(0);
}
Comments