Close All Orders not working properly.

 

I am trying to code an EA and at times I want the EA to close all open orders. I am using this code:

if(CloseAll==true) 
{
for(i=0;i<OrdersTotal();i++)
{
OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
if(OrderType()==OP_BUY)
{
OrderClose(OrderTicket(),OrderLots(),Bid,3,Red);
continue;
}
if(OrderType()==OP_SELL)
{
OrderClose(OrderTicket(),OrderLots(),Ask,3,Red);
continue;
}
if(OrderType()==OP_BUYSTOP||OP_BUYLIMIT||OP_SELLSTOP||OP_SELLLIMIT)
{
OrderDelete(OrderTicket());
continue;
}

}
CloseAll=false;
} 

However, not all the open position close. I have attached a screenshot of the order close problem.

Order 1 is a Sell and closes properly
Order 2 & 3 are Buys. They both should close toghter, but you you see order 2 closes and order 3 closes well after 4 and 5 open.

Anytime there are multiple buys or sells in a row, they should all close at the same time, but they don't.

Any help would be appreciated.

Thanks,

C.J.

 

try travel backwards trought the open orders.

for(int i=OrdersTotal()-1;i>=0;i--){
  OrderSelect(i,SELECT_BY_POS);
  ......
}

also you should call RefreshRates() between the orders...

try following code.

   for(int i=OrdersTotal()-1;i>=0;i--){
      if(OrderSelect(i,SELECT_BY_POS)>=0){
         RefreshRates();
         if(OrderType()==OP_BUY || OrderType()==OP_SELL){
            OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),0,CLR_NONE);   
         }else{
            OrderDelete(OrderTicket());
         }  
      }
   }

Befor going live with such code you should add further errorhandling

 

Excellent! That works! Thanks so much!

While we are on the subject, I take it the Order numbers is 0,1,2,3... which is the reason for the OrderTotal()-1.

In order to get the difference in pips between the first and last position, it would be something like this?

if (OrderSelect(OrdersTotal()-1,SELECT_BY_POS,MODE_TRADES)==True)
{LastOrder = OrderOpenPrice();}     
if(OrderSelect(0,SELECT_BY_POS,MODE_TRADES)==True)
{FirstOrder = OrderOpenPrice();}
PriceDiff = abs(LastOrder - FirstOrder);
 
CoeyCoey:

In order to get the difference in pips between the first and last position, it would be something like this?

Yes, but u should be asking yourself what is meant by "first" and "last" order? They will not necessarily be the oldest and latest orders opened...
 
gordon wrote >>
Yes, but u should be asking yourself what is meant by "first" and "last" order? They will not necessarily be the oldest and latest orders opened...

Why is that? Doesn't it pull up the first and last currently open orders?
 
CoeyCoey:

Why is that? Doesn't it pull up the first and last currently open orders?
No. It pulls up the first and last orders in the trade pool. They might or might not be the first and last orders opened. If those are the ones u want to 'find', then u should loop on all orders in the trade pool and find them. That's the only reliable way to do it.
 

If you modify an open order by reducing the lot size, you'll get a new ticket number and it will be at the end of the list, even though it was opened first.

If the ordering is important, you must loop and find them

int ticket[100]; double price[100]; datetime oot[100];
int count=0;
for(int index = OrdersTotal() - 1; index >= 0; index--) if (
        OrderSelect(index, SELECT_BY_POS)   // Only my orders w/
    &&  OrderMagicNumber()  == MagicNumber  // my magic number
    &&  OrderSymbol()       == Symbol() ) { // and period and symbol
    ticket[count]= OrderTicket(); 
    price[count] = OrderOpenPrice();
    oot[count]   = OrderOpenTime();
    count++;
}
int order[100];    // Insertion sort
for (int kk=0; kk<count; kk++) { // Keep ordering ticket[*], price[*] and oot[*]
    for (int ii=kk; ii>0 && oot[order[ii-1]] > oot[kk]; ii--) {
        order[ii]=order[ii-1];  // kk   oot[kk] order[kk]   oot[order[kk]]
    }                           // 0    3.      1           1.
    order[ii]=kk;               // 1    1.      2           2.
}                               // 2    2.      0           3.

// ticket[order[0]]       is the oldest
// ticket[order[count-1]] is the latest
 

Wow, thanks a lot guys. I have much to learn about MQL.

Reason: