What's wrong with this simple code?

 

The code below is supposed to pick up any pending and market orders that are open and set the particular variable to value 1.

Although there are open pending orders or market orders the code does not pick them up and the variables stay at value 0.

Why does it not see the open orders? Is there something wrong with my code?

              int Sell = 0;
              int SPending = 0;
              int Buy = 0;
              int BPending = 0;
              
              for (Count = OrdersTotal()-1; Count >= 0; Count--)
                {
                       if (OrderSelect(Count, SELECT_BY_POS)
                       && OrderMagicNumber() == MagicNumber
                       && OrderSymbol() == Symbol()
                       && OrderCloseTime() == 0)
                       
                       {
                          if (OrderType()== OP_SELL)Sell = 1;
                          if (OrderType()== OP_SELLSTOP)SPending = 1;
                          if (OrderType()== OP_BUY)Buy = 1;
                          if (OrderType()== OP_BUYSTOP)BPending = 1;
                       }
                    }
 
                       if (OrderSelect(Count, SELECT_BY_POS)
                       && OrderMagicNumber() == MagicNumber
                       && OrderSymbol() == Symbol()
                       && OrderCloseTime() == 0)

This code can cause the problems, try this:

      if (OrderSelect(Count, SELECT_BY_POS))
               if (OrderMagicNumber() == MagicNumber
                       && OrderSymbol() == Symbol()
                       && OrderCloseTime() == 0)
 
I can't see a problem with your code, add some Print statements to make sure everything is as you expect . . .
Reason: