TakeAllProfit
Download scriptThis script is designed as a simplified management tool for active trading accounts. Its primary function is to automatically scan your open positions and close any trade that has reached a positive profit status.
How the Logic Works
1. Automated Market Scanning When the script is executed, it begins by reviewing every single "Buy" or "Sell" order currently active in your trading account. It works systematically from the most recent trades back to the oldest ones to ensure all active positions are accounted for.
2. Real-Time Profit Calculation For every trade found, the script performs a real-time calculation of your current standing. It doesn't just look at the raw price movement; it calculates the "Net Profit" by combining:
- The current market price (Live Bid/Ask).
- The original price at which you entered the trade.
- Any additional costs, such as commissions and swap fees.
3. The "Profit Filter" The script acts as a gatekeeper. It checks if the net profit of a position is greater than zero. If a trade is currently in a loss or is sitting at a neutral balance (zero), the script ignores it. If a trade shows even the smallest amount of profit, it qualifies for immediate closure.
4. Execution and Error Handling Once a profitable trade is identified, the script attempts to close it instantly at the current market price. Because markets move very quickly, the script includes "retry" logic: if a trade fails to close due to a momentary price fluctuation (slippage), the script refreshes its data and tries again until the order is successfully finalized.
5. Final Reporting After the script has finished scanning all your open positions, it provides a summary in the terminal log. This summary displays the total amount of profit secured by closing those specific trades, giving you an immediate overview of the capital captured during that session.
//+------------------------------------------------------------------+
//| TakeAllProfit.mq4
//+------------------------------------------------------------------+
#property copyright "Copyright K Lam"
#property link "http://www.MXQ4.net/"
#property show_confirm
extern string Name_Expert = "Take All Trades";
extern double TakeProfitPoint = 1; //any profit will close the easy to take at a day inside!
//+------------------------------------------------------------------+
//| cal the point range |
//+------------------------------------------------------------------+
double GetSlippage() { return((Ask-Bid)/Point); }
//+------------------------------------------------------------------+
//| script "close Profit last to 0 order"
//+------------------------------------------------------------------+
int start()
{
bool result;
double price,OrderProfitCash,OrderProfitPoint,RealProfit;
int cmd,error;
int TradeTick;
double TimeOut;
for(TradeTick=OrdersTotal()-1; TradeTick > 0; TradeTick--)
{ TimeOut=0;
while(!IsTradeAllowed()) {if(TimeOut > 50) break;TimeOut++;}
if(OrderSelect(TradeTick,SELECT_BY_POS,MODE_TRADES))
{
cmd=OrderType();
//---- first order is buy or sell
if(cmd==OP_BUY || cmd==OP_SELL)
{
while(true)
{// Cal the Profit Point
RefreshRates();
if(cmd==OP_BUY) { price =Bid; OrderProfitPoint =(price-OrderOpenPrice())/Point; }
else { price =Ask; OrderProfitPoint =(OrderOpenPrice()-price)/Point; }
OrderProfitCash = OrderProfit()+OrderCommission()+OrderSwap();
if(OrderProfitCash <= 0) continue;
//if((TakeProfitPoint > OrderProfitPoint) || !OrderProfitCash) break;
result=OrderClose(OrderTicket(),OrderLots(),price,GetSlippage(),CLR_NONE);
if(result!=TRUE) { error=GetLastError(); Print("LastError = ",error); }
else { error=0; Print("OrderProfit = ", OrderProfit());RealProfit+=OrderProfit();}
if(error==129 || error==135) RefreshRates();
else break;
}
}
}
else Print( "Error when order select ", GetLastError());
}
Print("Close Profit Order Call, Profit Take =",RealProfit);
return(0);
}
//+------------------------------------------------------------------+
Comments