Checking if a market order is open "What's wrong with my code" ?

 

I need to check if a specific market order is open or not and return a Boolean result

It returns "not all control paths return a  value"

 bool IsMOrderOpen()
   
      {
      
         for(int h = OrdersTotal() - 1; h >= 0 ; h--)
         if (OrderSelect(h, SELECT_BY_POS, MODE_TRADES))
         if (OrderSymbol() == Symbol() && OrderMagicNumber() == Magic) 
         return(true);
      
      }
 
Toufik:

I need to check if a specific market order is open or not and return a Boolean result

It returns "not all control paths return a  value"

What is returned when one of the if statements is false, and when OrdersTotal() = 0 ?
 
Amir Yacoby:
What is returned when one of the if statements is false, and when OrdersTotal() = 0 ?

I don't know, my function returns an error and I'm asking for a solution, what do you think it is ?

 
bool IsMOrderOpen(){
   for(int h = OrdersTotal() - 1; h >= 0 ; h--) if (OrderSelect(h, SELECT_BY_POS, MODE_TRADES)) if (OrderSymbol() == Symbol() && OrderMagicNumber() == Magic) 
      return true;
  what are you returning here when you do not find any filtered orders?    
} // End of function
 
Amir Yacoby:
What is returned when one of the if statements is false, and when OrdersTotal() = 0 ?

Good attempt...I have a lot of fun this morning 

He only wants the solution, not interested to learn.

 
Toufik:

I need to check if a specific market order is open or not and return a Boolean result

It returns "not all control paths return a  value"


You were almost there. You just needed to check the order-type. In MT4, OP_BUY and OP_SELL are < 2 so...

bool IsMOrderOpen()
{
   for(int h=OrdersTotal()-1; h>=0; h--)
      if(OrderSelect(h,SELECT_BY_POS)&&OrderSymbol()==_Symbol&&OrderMagicNumber()==Magic)
         if(OrderType()<2)
            return(true);
   return false;
}


 

 
nicholishen:

You were almost there. You just needed to check the order-type. In MT4, OP_BUY and OP_SELL are < 2 so...


 

Of course I'm interested in learning, the fact that I couldn't find an answer to his question returns my anger on him, I've to recognize ... happy you had a fun too.

I can't understand the logic for the two consecutive return, and what's the difference between Symbol() and  _Symbol ?

Thank you

bool IsMOrderOpen()
{
   for(int h=OrdersTotal()-1; h>=0; h--)
      if(OrderSelect(h,SELECT_BY_POS)&&OrderSymbol()==_Symbol&&OrderMagicNumber()==Magic)
         if(OrderType()<2)
            return(true);
   return false;
}
 
Toufik:

Of course I'm interested in learning, the fact that I couldn't find an answer to his question returns my anger on him, I've to recognize ... happy you had a fun too.

I can't understand the logic for the two consecutive return, and what's the difference between Symbol() and  _Symbol ?

Thank you

Don't be angry on me, I just tried to help.
1. the second return, is the return from the function (it has to return a bool - true or false so you have to make sure that every decision inside your code returns something weather it is entered or not) when the if condition is false and thus not return the first one (or the for loop is not enrered at all because OrdersTotal() = 0)
2. you can read in the docs that they are the same.
 
Toufik:

Of course I'm interested in learning, the fact that I couldn't find an answer to his question returns my anger on him, I've to recognize ... happy you had a fun too.

I can't understand the logic for the two consecutive return, and what's the difference between Symbol() and  _Symbol ?

Thank you


Symbol() and _Symbol yield the same result, but _Symbol is a pre-defined variable and Symbol() is a function call. 

This is the same code with brackets... Does this help you see the logic?

bool IsMOrderOpen()
{
   for(int h=OrdersTotal()-1; h>=0; h--)
   {
      if(OrderSelect(h,SELECT_BY_POS)&&OrderSymbol()==_Symbol&&OrderMagicNumber()==Magic)
      {
         if(OrderType()<2)
         {
            return(true); // if order is market return true
         }
      }
   }
   return false; // no market orders have been found; return false;
}
 

Thanks guys you covered everything I want it.

 
Toufik:

Thanks guys you covered everything I want it.

And what have you learnt ?