Code check for open orders not working

 

Why the hell is this simple function not working, I use it in my if statements for buy !CheckOpenBuyOrders() and sell !CheckOpenSellOrders().


bool CheckOpenBuyOrders(){
   
   for( int i = 0 ; i < OrdersTotal() ; i++ ) {
      OrderSelect( i, SELECT_BY_POS, MODE_TRADES);
      if(OrderMagicNumber() == magicNumber && OrderType() == ORDER_TYPE_BUY){ 
      return true;      
      }else{
      return false;            
      }
}
return false;
}

bool CheckOpenSellOrders(){
   
   for( int i = 0 ; i < OrdersTotal() ; i++ ) {
      OrderSelect( i, SELECT_BY_POS, MODE_TRADES);
      if(OrderMagicNumber() == magicNumber && OrderType() == ORDER_TYPE_SELL){ 
      return true;      
      }else{
      return false;            
      }
}
return false;
}
 
angomes:

Why the hell is this simple function not working, I use it in my if statements for buy !CheckOpenBuyOrders() and sell !CheckOpenSellOrders().


bool CheckOpenBuyOrders(){
   
   for( int i = 0 ; i <= OrdersTotal()-1 ; i++ ) {
      if(!OrderSelect( i, SELECT_BY_POS, MODE_TRADES)){continue;}
      if(OrderMagicNumber() == magicNumber && OrderType() == OP_BUY){ 
      return true;      
      }else{
      return false;            
      }
}
return false;
}

bool CheckOpenSellOrders(){
   
   for( int i = 0 ; i <= OrdersTotal()-1 ; i++ ) {
      if(!OrderSelect( i, SELECT_BY_POS, MODE_TRADES)){continue;}
      if(OrderMagicNumber() == magicNumber && OrderType() == OP_SELL){ 
      return true;      
      }else{
      return false;            
      }
}
return false;
}
 
bool CheckOpenBuyOrders()
{
   bool buyExists=false;
   for( int i = 0 ; i < OrdersTotal() ; i++ )
     {
      if(OrderSelect( i, SELECT_BY_POS, MODE_TRADES))
         if(OrderMagicNumber() == magicNumber && OrderType() == ORDER_TYPE_BUY)
           {
            buyExists=true;
            break;
           }
     }
   return (buyExists);
}

Be very careful when using return in a loop, it can give you results that you do not expect.

The above is not tested but should work.

 
In future please post in the correct section
I will move your topic to the MQL4 and Metatrader 4 section.
 
Brian Lillard:

Your code makes the same error by using the return in a loop

bool CheckOpenBuyOrders(){
   
   for( int i = 0 ; i <= OrdersTotal()-1 ; i++ ) {
      if(!OrderSelect( i, SELECT_BY_POS, MODE_TRADES)){continue;}
      if(OrderMagicNumber() == magicNumber && OrderType() == OP_BUY){ 
      return true;      
      }else{
      return false;            
      }
}
return false;
}

If the first trade checked has the wrong magic number or  is a sell, it will return false without checking further, so there may be a buy but the function returns false.

 

Thank you guys for all the answers, I realized that the problem was that it was returning false while going trough all the other orders that were not assigned with this magic number.


bool CheckOpenBuyOrders(){
   
   for( int i = 0 ; i < OrdersTotal() ; i++ ) {
      OrderSelect( i, SELECT_BY_POS, MODE_TRADES);
      if(OrderMagicNumber() == magicNumber && OrderType() == ORDER_TYPE_BUY){ 
      return true;      
      }
     }
return false;
}

bool CheckOpenSellOrders(){
   
   for( int i = 0 ; i < OrdersTotal() ; i++ ) {
      OrderSelect( i, SELECT_BY_POS, MODE_TRADES);
      if(OrderMagicNumber() == magicNumber && OrderType() == ORDER_TYPE_SELL){ 
      return true;      
      }
     }
return false;
}

This works great now.
   

Reason: