CloseAllProfit1
Download scriptThis script is designed as an automated utility for MetaTrader 4 to manage and clear out open trading positions based on a specific profit milestone. Its primary function is to act as a "cleanup" tool that automatically closes all active trades once the account reaches a designated financial goal.
Core Logic Overview
The script operates using a straightforward conditional check. Instead of executing trades, it monitors your current account status and acts only when a specific requirement is met.
1. The Profit Threshold Check Before doing anything else, the script looks at your total realized profit. It compares this amount against a pre-defined target (the "Profit" variable). If your current profit is lower than this target, the script does nothing and stops immediately. This ensures that the automation only triggers once your account has reached your desired success level.
2. Identifying Open Positions If the profit target has been met, the script scans through every open position currently active in your trading account. It systematically looks at each trade, one by one, to identify whether you are currently in a "Buy" or a "Sell" position.
3. Automated Execution of Closure Once the script identifies an open position, it immediately sends a command to close that trade at the current market price. It handles both buying and selling types equally, ensuring that every active trade is liquidated.
4. Error Handling and Persistence The logic includes a safety mechanism to handle rapid market fluctuations. If the market moves so quickly that a trade cannot be closed on the first attempt (due to a "price changed" error), the script is designed to refresh its data and immediately try again. This ensures that the closure of positions is completed successfully despite volatile price movements.
Summary for the User
Essentially, this tool serves as an automated "Take Profit" trigger for your entire account. Once you reach your stated profit goal, it sweeps the market by closing all remaining open trades to secure your gains and leave your account clear.
//+------------------------------------------------------------------+
//| Çàêðûòü ïî îáùåìó ïðîôèòó.mq4 |
//| Krokus |
//| http://www.liteforex.net |
//+------------------------------------------------------------------+
#property copyright "Krokus"
#property link "http://www.liteforex.net"
#property show_inputs
extern double Profit=20;
extern int Slipage=3;
//+------------------------------------------------------------------+
//| script program start function |
//+------------------------------------------------------------------+
int start()
{
//----
if(AccountProfit()<Profit) return(0);
int t=OrdersTotal();
for(int i=t-1;i>=0;i--)
{
if(!OrderSelect(i,SELECT_BY_POS))continue;
if(OrderType()==OP_SELL )
{
OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),Slipage,Red);
if(GetLastError()==135) {RefreshRates();i++;}
continue;
}
if( OrderType()==OP_BUY )
{
OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),Slipage,Red);
if(GetLastError()==135) {RefreshRates();i++;}
continue;
}
}
//----
return(0);
}
//+------------------------------------------------------------------+
Comments