Pls Help, Why does this code open more orders at similar price

 
extern double PB1=104.000; // Buy price of Phase 1
extern double OS1=0.01; // Order size of Phase 1
extern double SL1=0.150; // Stop Loss of Phase 1
extern double TP1=0.150; // Take Profit of Phase 1
extern double PBDip1=1.000; // Dip Level for new buy in Phase 1

  int start()
   {
   bool OrderResult;
    while(PB1<Ask)
      {
      if (Ask<(PB1-PBDip1))
        {break;}
        else
         {for (int x=0; x <= OrdersTotal(); x++)
         {
          OrderResult=OrderSelect(x,SELECT_BY_POS,MODE_TRADES);
            if ((PB1-PBDip1) <= OrderOpenPrice() && OrderOpenPrice() <= (PB1+PBDip1))
            {
            continue;
            }
         else
            {OrderResult=OrderSend (Symbol(),OP_BUY,OS1,PB1,Ask,Ask-SL1,Ask+TP1);
            break;
            }
         }
         }
      break;
      }
     return 0;
   }

Hi,

My objective is that if there is an existing open order at a price between (PB1-PBDip1) and (PB1+PBDip1) then the loop should not open another order.

When i test this code it keeps opening new orders every second irrespective of existing open order price

Kindly guide

 
  1. Finish your loop setting a bool if there is; then test it. If any (not all) of the open orders are not in your range, you open a new order.

  2.          {for (int x=0; x <= OrdersTotal(); x++){
              OrderResult=OrderSelect(x,SELECT_BY_POS,MODE_TRADES);

    Check your return codes, and report your errors (including market prices and your variables). Don't look at GLE/LE unless you have an error. Don't just silence the compiler (MT5/MT4+strict), it is trying to help you.
              What are Function return values ? How do I use them ? - MQL4 programming forum 2012.05.20
              Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles 25 March 2014

  3. If there are n orders, their positions are [0 … n-1]. You try and fail to select n but continue anyway.