Code Library / Order Management
MQL4 Close All Orders
Close all open orders and delete pending orders.
Language: MQL4 · Category: Order Management · Tags: close all, orders, MQL4, EA, emergency
Source code
void CloseAll() {
for(int i=OrdersTotal()-1;i>=0;i--) {
if(OrderSelect(i,SELECT_BY_POS)) {
if(OrderType()==OP_BUY) OrderClose(OrderTicket(),OrderLots(),Bid,3);
else if(OrderType()==OP_SELL) OrderClose(OrderTicket(),OrderLots(),Ask,3);
else OrderDelete(OrderTicket());
}
}
}How to use this snippet
This is a building block, not a finished trading system. Paste it into your own MQL4 project, compile it, and test it on a demo account with your broker's real spreads before relying on it. If MetaEditor reports errors, the compilation error fixer can help.