Need help Closing orders with an EA

 

I ran into a problem that I dont know how to solve. My program has a few open orders and closes them all together but sometimes I think that some of the orders are not filled therefore it keeps waiting till they are filled and that locks up my program. What have you guys found to be the best way to close all open orders? Thank you kindly for your help.

here is the code:

int OrdersTotalLong()

{

int order_total = 0;

for (int i = 0; i < OrdersTotal(); i++)

{

OrderSelect(i,SELECT_BY_POS, MODE_TRADES);

if (OrderType() == OP_BUY)

order_total++;

}

return(order_total);

}

int MaxLongOrder()

{

int order = 0;

double lotsbuyed = 0;

for (int i = 0; i < OrdersTotal(); i++)

{

OrderSelect(i,SELECT_BY_POS, MODE_TRADES);

if ((OrderType() == OP_BUY) && (OrderLots() > lotsbuyed))

{

lotsbuyed = OrderLots();

order = i;

}

}

return(order);

}

int CloseLong()

{

while (OrdersTotalLong() > 0) //until there's no Orders open (close all)

{

if (OrderSelect(MaxLongOrder(), SELECT_BY_POS, MODE_TRADES) == false) continue;

OrderClose(OrderTicket(),OrderLots(), Bid, 3, Green);

}

}

return(0);

}