the following code snippet is used to close short positions:
total = OrdersTotal();
for(i=0;i<total;i++){
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)){
if(OrderSymbol()==Symbol() && OrderMagicNumber()==magic_base){
if(OrderType()==OP_SELL){
OrderClose(OrderTicket(),OrderLots(),Ask,Slippage,clr_close);
}
}
}
}
however, I've discoverd that it doesn't always do that. if I have only one open position, it is closed correctly. but when I have multiple positions running, only some of them will be closed at the correct time, others are forgotten.
I guess that this strange behavior is caused by the index parameter of OrderSelect(). everytime an order is closed or deleted, the index diminishes and reassigns itself to the remaining orders. thus causing some orders to be left out.
my crude solution is to use multiple instances of the for() loop. this works but there has got be a more elegant way. does anybody know how to solve this more professionally?
total = OrdersTotal();
for(i=0;i<total;i++){
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)){
if(OrderSymbol()==Symbol() && OrderMagicNumber()==magic_base){
if(OrderType()==OP_SELL){
OrderClose(OrderTicket(),OrderLots(),Ask,Slippage,clr_close);
}
}
}
}
however, I've discoverd that it doesn't always do that. if I have only one open position, it is closed correctly. but when I have multiple positions running, only some of them will be closed at the correct time, others are forgotten.
I guess that this strange behavior is caused by the index parameter of OrderSelect(). everytime an order is closed or deleted, the index diminishes and reassigns itself to the remaining orders. thus causing some orders to be left out.
my crude solution is to use multiple instances of the for() loop. this works but there has got be a more elegant way. does anybody know how to solve this more professionally?