Code not placing all pending orders and returns Error 130

 

Hi, i've managed  to resolve partially my problem with the pending orders described in this post: https://www.mql5.com/en/forum/294301, but now the code returns Error 130 on prices that it should and shouldn't place an order and doesn't place all the orders with the given pip interval between them:

 //PLACE PENDING ORDERS
   bool orderopen = 0;
   for(int cni=OrdersTotal(); cni>=0; cni--)
     {
      if(!OrderSelect(cni, SELECT_BY_POS, MODE_TRADES))
         continue;
         if(OrderMagicNumber()==MagicNumber)
           { 
            if(OrderType()==OP_SELLSTOP)    
              {
               //-SELLSTOP ORDERS
               if(Bid>=LastOrderPrice()+PipDistance*_Point && orderopen==0)
                 {
                  Print("openorder: ", orderopen," BID: ", Bid," OOP + PD: ", LastOrderPrice()+PipDistance*_Point);
                  int ticket=OrderSend(NULL,OP_SELLSTOP,Lots,LastOrderPrice()+OrderDistance*_Point,0,0,TP,NULL,MagicNumber,0,clrRed);
                 }
               if(Bid==LastOrderPrice()+difference*_Point)
                 {
                  orderopen=1;
                  Print("Order Open is: ", orderopen," bid: ", Bid, " OOP + diff: ", LastOrderPrice()+difference*_Point, "OOP + PD", LastOrderPrice()+PipDistance*_Point);
                 }
              }
           }
        } 

double LastOrderPrice()
   {
   datetime lastOpenTime = 0; 
   double lastOrderPrice = 0;
   
   for(int i = (OrdersTotal()-1); i >= 0; i --)
      {
      if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
        {
         datetime curOpenTime = OrderOpenTime();
         if(curOpenTime > lastOpenTime)
           {
            lastOpenTime = curOpenTime;
            lastOrderPrice = OrderOpenPrice();
           }
        }   
     }
   return(lastOrderPrice);
   }

The code doesn't work with OrderOpenPrice() for some reason.

Multiple Pending Orders EA Problem
Multiple Pending Orders EA Problem
  • 2018.12.10
  • www.mql5.com
Hi all, i'm having a tough time making my EA place stop orders only once at a given price. It places stop orders every tick which matches the price...
Reason: