Function to effectively close all orders.

 

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?

// close all open and pending orders

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()
 

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()
Reason: