Download MetaTrader 5

OrderClose() closing incorrect order

To add comments, please log in or register
DeanDeV
330
DeanDeV  

Hi guys,

I have a function, namely:

void CloseOrders()
 { 
  for(int d=OrdersTotal()-1; d>=0; d--) 
   {
    if(!OrderSelect(d,SELECT_BY_POS,MODE_TRADES)) continue;  
     if(OrderMagicNumber()==(MagicNumber1 || MagicNumber2 || MagicNumber3)    
      && OrderSymbol()==Symbol()                
       && OrderType()==(OP_BUY || OP_SELL) 
    RefreshRates();
     if(!OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),10))          
      Print("Order Close failed, order number: ",OrderTicket()," Error: ",GetLastError());        
   }
 }

 The EA places 4 orders, each issued with a unique magic number (MagicNumber1, MagicNumber2, MagicNumber3 and MagicNumber4). I only want the EA to close MagicNumber1, MagicNumber2, MagicNumber3 orders, and NOT the MagicNumber4 order, however, it is closing all 4. Any ideas what could be causing this?

 Thanks! 

darksideofthemoon
57
darksideofthemoon  

Yeah, the code is total bogus :D

Try this: 


 

void CloseOrders() { 
  for(int d=OrdersTotal()-1; d>=0; d--) {
    if(!OrderSelect(d,SELECT_BY_POS,MODE_TRADES)) continue;  
    if((OrderMagicNumber()==MagicNumber1 || OrderMagicNumber()==MagicNumber2 || OrderMagicNumber()==MagicNumber3)
      && OrderSymbol()==Symbol()                
      && (OrderType()==OP_BUY || OrderType()==OP_SELL)) { 
        RefreshRates();
        if(!OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),10))          
          Print("Order Close failed, order number: ",OrderTicket()," Error: ",GetLastError());        
    }
  }
}
Keith Watford
Moderator
10612
Keith Watford  
  
     if((OrderMagicNumber()==MagicNumber1 || OrderMagicNumber()==MagicNumber2 || OrderMagicNumber()==MagicNumber3)    
      && OrderSymbol()==Symbol()                
       && (OrderType()==OP_BUY || OrderType()==OP_SELL)) 
Try that
DeanDeV
330
DeanDeV  
2cent:

Yeah, the code is total bogus :D

Try this: 


 

 

 

Hahah! I love the word "bogus". Will give this a try. Thanks!
DeanDeV
330
DeanDeV  
GumRai:
Try that
Thank you. I know I have tried this. Let's see if it works! 
DeanDeV
330
DeanDeV  
2cent:

Yeah, the code is total bogus :D

Try this: 


 

 

 

It seems my code was sheer bogus indeed. thanks for the help! Your code works. :) 
To add comments, please log in or register