try this maybe?
// close all open and pending orders
void closeAllOrders() {
for(int c=0;c<OrdersTotal();c++) {
OrderSelect(c,SELECT_BY_POS,MODE_TRADES);
if (OrderType() == OP_BUY) {
OrderClose(OrderTicket(), OrderLots(),Bid,3, Red); }
if (OrderType() == OP_SELL) {
OrderClose(OrderTicket(), OrderLots(), Ask,3, Red); }
if (OrderType() > 1) { OrderDelete(OrderTicket()); }
}
} // end closeAllOrders()You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
I have been using this function to try and close all my orders, both pending and open. However for some unknown reason, there's always unclosed orders remaining. How do I ensure that every single pending/open order closes?
void closeAllOrders() {
int total = OrdersTotal();
for (int i=total-1; i>=0; i--) {
if (OrderSelect(i, SELECT_BY_POS)) {
if (OrderType() == OP_BUY) {
OrderClose(OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 3, Red);
} else if (OrderType() == OP_SELL) {
OrderClose(OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 3, Red);
} else if (OrderType() > 1) {
OrderDelete(OrderTicket());
}
}
}
} // end closeAllOrders()