Close pending order when there's no open order

 

Hi my EA open 1 order and several order stop. I tried to delete the order stop if the open order is closed or get stopped out.

void DeletePendOrder()
  {
   for(int i=OrdersTotal()-1; i >=0; i--)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) &&
         OrderSymbol() == Symbol() &&
         OrderMagicNumber() == MagicNumber)
        {
         if(OrderType()!=OP_BUY && OrderType()!=OP_SELL) //if there's no open order buy and sell
           {
            for(int a = OrdersTotal()-1; a >=0; a--)
              {
               if(OrderSelect(a,SELECT_BY_POS,MODE_TRADES) &&
                  OrderSymbol()==Symbol() &&
                  OrderMagicNumber() == MagicNumber)
                 {
                  if(OrderType()==OP_SELLSTOP)
                    {
                     bool res = OrderDelete(OrderTicket());
                     Print("Deleting SELL_STOP");
                     if(!res)
                        Print("Delete pending order failed for ticket # ",OrderTicket()," with error: ",GetLastError());
                    }
                  if(OrderType()==OP_BUYSTOP)// && par==Symbol())
                    {
                     bool res = OrderDelete(OrderTicket());
                     Print("Deleting BUY_STOP");
                     if(!res)
                        Print("Delete pending order failed for ticket # ",OrderTicket()," with error: ",GetLastError());
                    }
                 }
              }
           }
        }
     }
  }

Using the code above and since my EA works on every tick, the EA will close the order stop right after EA opens it. Basically the first tick will open it and the second tick is close it. My guess is this line of code. Is this the right way to tell that if there's no open order buy or sell close all pending order?

 
  1.          if(OrderType()!=OP_BUY && OrderType()!=OP_SELL) //if there's no open order buy and sell

    You don't know that yet; you are still in the loop. Loop through all the orders and count the open ones. Then, once you know, loop again and delete all the pending ones.

  2. There is no need to create pending orders in code.

    1. The pending has the slight advantage, A) you are closer to the top of the queue (filled quicker), B) there's no round trip network delay (filled quicker.)

      Don't worry about it unless you're scalping M1 or trading news.

    2. Humans can't watch the screen 24/7, so they use pending orders; EAs can, so no need for pending orders, have it wait until the market reaches the trigger price and just open an order.

 
    1. You don't know that yet; you are still in the loop. Loop through all the orders and count the open ones. Then, once you know, loop again and delete all the pending ones.
         I tried your suggestion and separating the code into 2 function. But the problem is still exist. Did I do it right?
         
      bool CheckOpenOrders()
        {
         for(int i=OrdersTotal()-1; i >=0; i--)
           {
            if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) &&
               OrderSymbol() == Symbol() &&
               OrderMagicNumber() == MagicNumber)
              {
               if(OrderType()==OP_BUY && OrderType()==OP_SELL)
                  return (true);
              }
           }
         return false;
        }
      
      //+------------------------------------------------------------------+
      void DeletePendOrder()
        {
         if(!CheckOpenOrders())
           {
            for(int a = OrdersTotal()-1; a >=0; a--)
              {
               if(OrderSelect(a,SELECT_BY_POS,MODE_TRADES) &&
                  OrderSymbol()==Symbol() &&
                  OrderMagicNumber() == MagicNumber)
                 {
                  if(OrderType()==OP_SELLSTOP)
                    {
                     bool res = OrderDelete(OrderTicket());
                     if(!res)
                        Print("Delete pending order failed for ticket # ",OrderTicket()," with error: ",GetLastError());
                    }
                  if(OrderType()==OP_BUYSTOP)
                    {
                     bool res = OrderDelete(OrderTicket());
                     if(!res)
                        Print("Delete pending order failed for ticket # ",OrderTicket()," with error: ",GetLastError());
                    }
                 }
              }
           }
        }
    2. Humans can't watch the screen 24/7, so they use pending orders; EAs can, so no need for pending orders, have it wait until the market reaches the trigger price and just open an order.

        Thanks for the knowledge, I will try your suggestion after this problem resolve.

     
    Luandre Ezra #:
      1.    I tried your suggestion and separating the code into 2 function. But the problem is still exist. Did I do it right?
               if(OrderType()==OP_BUY && OrderType()==OP_SELL)
      
      Condition will never be true.
       

      Yes, my mistake. I've change it into this and the problem still exist.

      bool CheckNoOpenOrders()
        {
         for(int i=OrdersTotal()-1; i >=0; i--)
           {
            if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) &&
               OrderSymbol() == Symbol() &&
               OrderMagicNumber() == MagicNumber)
              {
               if(OrderType()!=OP_BUY && OrderType()!=OP_SELL)
                  return (true);
              }
           }
         return false;
        }
      

      I have another EA that open 2 pending order. When price moves to one direction and hit the pending order the other one would be deleted. The code almost exact same with the first code that I post and it works but I don't know why it doesn't works on here.

       
      Luandre Ezra #:

      Yes, my mistake. I've change it into this and the problem still exist.

      William has already given you the solution

      William Roeder #:
      1. You don't know that yet; you are still in the loop. Loop through all the orders and count the open ones. Then, once you know, loop again and delete all the pending ones.

      if(OrderType()!=OP_BUY && OrderType()!=OP_SELL)
                  return (true);

      This will exit the loop as soon as it finds a pending order.

      So your function should not be called

      bool CheckNoOpenOrders()

      It should be

      bool CheckIfPendingOrderExists()
       
      William Roeder #: Condition will never be true.
               if(OrderType()==OP_BUY && OrderType()==OP_SELL)
      Your function CheckNoOpenOrders should be called AnyOpenOrders. It should return true if it finds a Buy or a Sell.
       
      Thanks for the answer William it already fix the problem, there's a minor issue with the calculation but after fix it everything works perfectly. 
       
      Luandre Ezra #:
      Thanks for the answer William it already fix the problem, there's a minor issue with the calculation but after fix it everything works perfectly. 

      Hi


      How you fix the error


      I too have same problem and unable to find the solution


      Pls if you post the working code means it will greatfull

       
      Raja Sekaran #How you fix the error

      here's the final code

      bool AnyBuyOrders()
        {
         for(int i=OrdersTotal()-1; i >=0; i--)
           {
            if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) &&
               OrderSymbol() == Symbol() &&
               OrderMagicNumber() == MagicNumber)
              {
               if(OrderType()==OP_BUY || OrderType()==OP_SELL)
                  return true;
              }
           }
         return false;
        }
       
      Luandre Ezra #: here's the final code

      The function is called AnyBuyOrders, but returns true if there are AnyOpenOrders.