Check pending order

 

if everyone I tried to check if there's an open or pending order using this code,

int AnyOpenOrders()
  {
   int result = 0;
   for(int i=OrdersTotal()-1; i >=0; i--)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) &&
         OrderSymbol() == Symbol() &&
         OrderMagicNumber() == MagicNumber)
        {
         if(OrderType()==OP_BUY)
            result = 1;
         if(OrderType()==OP_SELL)
            result = 2;
         if(OrderType()==OP_BUYSTOP)
            result = 3;
         if(OrderType()==OP_SELLSTOP)
            result = 4;
         if(OrderType()==OP_BUYLIMIT)
            result = 5;
         if(OrderType()==OP_SELLLIMIT)
            result = 6;
        }
     }
   return result;
  }

if(AnyOpenOrders()!=5) Print("no buy limit order");
if(AnyOpenOrders()!=3) Print("no buy stop order");

I have an open limit order, but when I check it always return that there's no limit order. It only happened with limit order, I tried to check the stop order and it works. Anyone can tell me where's the problem?

 
Luandre Ezra:

if everyone I tried to check if there's an open or pending order using this code,

I have an open limit order, but when I check it always return that there's no limit order. It only happened with limit order, I tried to check the stop order and it works. Anyone can tell me where's the problem?

It really is about time that you actually start to think about what your code is actually doing.

I am sorry to seem harsh, but you are constantly posting code that you want help with and you are repeating the same mistakes over and over again. This is because you don't actually follow your code to see what it does.

What do you think that the value of result will be when the loop is complete?

I'll give you a clue. The loop achieves nothing!

You may just as well use

if(OrderSelect(0,SELECT_BY_POS,MODE_TRADES) &&
         OrderSymbol() == Symbol() &&
         OrderMagicNumber() == MagicNumber)
        {
        }
 
if(AnyOpenOrders()!=5) Print("no buy limit order");
This does not say, there are no buy limit orders. It says the earliest order on the chart is not. Write what you mean :
bool AnyOpenOrders(int type)
  {
   for(int i=OrdersTotal()-1; i >=0; i--) if(
      OrderSelect(i,SELECT_BY_POS)
   && OrderMagicNumber() == MagicNumber
   && OrderType()        == type         // What I am looking for.
   && OrderSymbol()      == Symbol()     // String comparisons last (slowest)
   ){
         return true; // found at least one
     }
   return false;
  }

if(!AnyOpenOrders(OP_BUYLIMIT)) Print("no buy limit order");
if(!AnyOpenOrders(OP_BUYSTOP))  Print("no buy stop order");
Reason: