int Try=10; int f_CloseAll() { for (int ii = OrdersTotal(); ii>=0; ii--) { res=OrderSelect(ii, SELECT_BY_POS, MODE_TRADES); if(OrderMagicNumber()==Magic && OrderSymbol()==Symbol()) { if(OrderType() == OP_BUY) { int k=0; res=false; while(!res && k<Try) { res=OrderClose(OrderTicket(),OrderLots(),Bid,0,clrWhite); k++; Sleep(100); } } if(OrderType() == OP_SELL) { int k=0; res=false; while(!res && k<Try) { res=OrderClose(OrderTicket(),OrderLots(),Ask,0,clrWhite); k++; Sleep(100); } } if(OrderType() == OP_BUYLIMIT) { int k=0; res=false; while(!res && k<Try) { res=OrderDelete(OrderTicket()); k++; Sleep(100); } } if(OrderType() == OP_SELLLIMIT) { int k=0; res=false; while(!res && k<Try) { res=OrderDelete(OrderTicket()); k++; Sleep(100); } } if(OrderType() == OP_BUYSTOP) { int k=0; res=false; while(!res && k<Try) { res=OrderDelete(OrderTicket()); k++; Sleep(100); } } if(OrderType() == OP_SELLSTOP) { int k=0; res=false; while(!res && k<Try) { res=OrderDelete(OrderTicket()); k++; Sleep(100); } } } } return(0); }
Use the type of Close/Delete you need.
Regards.
Use the type of Close/Delete you need.
Regards.
if(OrderType() == OP_BUY) { int k=0; res=false; while(!res && k<Try) { RefreshRates(); res=OrderClose(OrderTicket(),OrderLots(),Bid,0,clrWhite); k++; if(!res) Sleep(100); } }
Always RefreshRates() to make sure that Bid/Ask is current.
Probably best to sleep only if the OrderClose() fails.
- Why did you post your MT4 question in the Root / MT5 General section instead of the MQL4 section, (bottom of the Root page?)
- You can use OrderClosePrice() instead of Bid/Ask and be direction independent — no need to check order type for close price. But if you potentially close multiple orders, you must call RefreshRates after the server call and before the next OrderSelect.
General rules and best pratices of the Forum. - General - MQL5 programming forum
Next time post in the correct place. The moderators will likely move this thread there soon.
i m getting an error as followed doin so with the post...whts wrong..some one correct me
double CloseAllSellTrades(){
for (int ii = OrdersTotal(); ii>=0; ii--)
{
int Try=10;
res=OrderSelect(ii, SELECT_BY_POS, MODE_TRADES);
if(OrderMagicNumber()==Magic && OrderSymbol()==Symbol())
{
if(OrderType() == OP_SELL)
{
int k=0;
res=false;
while(!res && k<Try)
{
res=OrderClose(OrderTicket(),OrderLots(),Ask,7,clrWhite);
k++;
Sleep(100);
}
}
}
}
}
use the code button (Alt+S) when pasting code
What error do you get?

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use