Cutoff
Download scriptThis script is designed as a utility tool for traders who need to perform a rapid, comprehensive cleanup of their open market positions and pending orders. Its primary function is to systematically identify every active trade and "cut off" the current session by closing or removing them instantly.
The logic follows a specific sequence to ensure that all types of active requests are handled correctly:
Comprehensive Order Scanning The tool begins by scanning your entire account history for active trades. It works backward through your open positions, ensuring that every single ticket—regardless of its size or individual status—is accounted for during the process.
Handling Active Market Positions When the script identifies an open "Buy" or "Sell" position (live trades), it immediately attempts to close them. To ensure these closures are successful despite market fluctuations, the script performs a real-time check of the current market prices. If a trade fails to close due to a momentary price discrepancy, the script is designed to refresh the data and try again until the position is successfully exited.
Management of Pending Orders Beyond just closing live trades, the script also identifies "Pending Orders"—these are orders you have placed that are waiting for a specific price to be hit before they become active (such as Buy Limits or Sell Stops). The logic automatically detects these and removes them from the market entirely, clearing your pending queue.
Execution Summary Once the process is complete, the script provides a final confirmation message in the log, displaying the exact time of completion and confirming that the total number of remaining open orders has been updated. This serves as a clear indicator that the "CutOff" command was successfully executed across your entire portfolio.
//+------------------------------------------------------------------+
//| CutOff.mq4
//+------------------------------------------------------------------+
#property copyright "Copyright K Lam 2009"
#property link "http://www.FxKillU.net/"
#property show_confirm
// extern string Name_Expert = "Close All Trades";
//+------------------------------------------------------------------+
//| cal the point range |
//+------------------------------------------------------------------+
double GetSlippage() { return((Ask-Bid)/Point); }
//+------------------------------------------------------------------+
//| script "close Profit last to 0 order"
//+------------------------------------------------------------------+
int start()
{
bool result;
double price;
int cmd,error;
int cnt;
//close profit frist
for(cnt=OrdersTotal(); cnt > 0; cnt--) {//while(OrdersTotal()!=0) // very slow
if(OrderSelect(cnt-1,SELECT_BY_POS,MODE_TRADES)) {
if (OrderProfit()>0)
while(true) {
cmd=OrderType();
if (cmd==OP_BUY || cmd==OP_SELL) {
RefreshRates();
if(cmd==OP_BUY) price=Bid;
if(cmd==OP_SELL) price=Ask;// not the chart price!!
result=OrderClose(OrderTicket(),OrderLots(),price,GetSlippage(),CLR_NONE);
if(result!=TRUE) { error=GetLastError(); Print("LastError = ",error); }
else error=0;
if(error==129 || error==135) RefreshRates();
else break;
}
//---- delete first pending order
if(cmd!=OP_BUY && cmd!=OP_SELL)
{
result=OrderDelete(OrderTicket());
break;
}
}//while
} else Print( "Error when order select ", GetLastError());
}
//close profit frist
for(cnt=OrdersTotal(); cnt > 0; cnt--) {//while(OrdersTotal()!=0) // very slow
if(OrderSelect(cnt-1,SELECT_BY_POS,MODE_TRADES)) {
while(true) {
cmd=OrderType();
//if (OrderProfit()>0)
if (cmd==OP_BUY || cmd==OP_SELL) {
RefreshRates();
if(cmd==OP_BUY) price=Bid;
if(cmd==OP_SELL) price=Ask;// not the chart price!!
result=OrderClose(OrderTicket(),OrderLots(),price,GetSlippage(),CLR_NONE);
if(result!=TRUE) { error=GetLastError(); Print("LastError = ",error); }
else error=0;
if(error==129 || error==135) RefreshRates();
else break;
}
//---- delete first pending order
if(cmd!=OP_BUY && cmd!=OP_SELL)
{
result=OrderDelete(OrderTicket());
break;
}
}//while
} else Print( "Error when order select ", GetLastError());
}
Print("CutOff All Order at ",TimeToStr(TimeCurrent(),TIME_DATE|TIME_SECONDS)," Now Total Order =",OrdersTotal());
return(0);
}
//+------------------------------------------------------------------+
Comments