[Help] What's the reason that some order can not be closed by OrderClose(), when using "OrderSelect(i,SELECT_BY_POS,MODE_TRADES); "

 

I need someone to help me to find the mistake in the code segment below,

the problem is: when the order which is OP_BUY found, there's always some order can not be closed by OrderClose(), shouldn't I use the "OrderSelect(i,SELECT_BY_POS,MODE_TRADES); " statement here ?

void CloseOrder(int closetype)
{
int i;
int count=1;
if(1==closetype)//
{
for(i=0;i<OrdersTotal();i++)
{
OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
if(OP_BUY==OrderType())
{
OrderClose(OrderTicket(),OrderLots(),Bid,5,White);
}
}
}

......

 
yeswan:

I need someone to help me to find the mistake in the code segment below,

the problem is: when the order which is OP_BUY found, there's always some order can not be closed by OrderClose(), shouldn't I use the "OrderSelect(i,SELECT_BY_POS,MODE_TRADES); " statement here ?

void CloseOrder(int closetype)
{
int i;
int count=1;
if(1==closetype)//
{
for(i=0;i<OrdersTotal();i++)
{
OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
if(OP_BUY==OrderType())
{
OrderClose(OrderTicket(),OrderLots(),Bid,5,White);
}
}
}

......

there is something wrong in the loop "for()", it should be “for(i=OrdersTotal()-1; i>=0;i--)”; then you can select all orders.

if you code "for(i=0;i<OrdersTotal();i++)", only orders 0,2,4,6....will be selected, because "OrdersTotal()" is different for each cycle.

in addition, your other codes are not good enough even there is not big logic disadvantage.