Order Delete error 4108 (On pending order...)

 
This gets called and checked every hour with regards to the bias. For example "H1_Bias" will be checked every hour and can change to "None", Down, or Up based upon MA's. It works in conjunction with "IsNewCandle()" function.

I am getting the error 4108 and market order cannot be deleted... I know OrderClose is for open trades and OrderDelete is for pending, but I cannot see why I am getting this error?
THIS PART IS FIRST AND IS CALLED IF(IsNewCandl()) within the IN START section.

H1_high  = iHigh(NULL, PERIOD_H1, 1);
H1_close = iClose(NULL, PERIOD_H1, 1);

   if(H1_Bias=="Down" && H4_Bias=="4 Hour is Down" && D1_Bias=="Daily is Down" && H1_high >= ema21 && H1_close < CurrentBigFish6)
      {
      OrderEntry(1); // Pending order Sell Stop function is called.
      }

   if(H1_Bias=="Down" || H1_Bias=="None" || H4_Bias=="None" || D1_Bias=="None" && H1_close > CurrentBigFish6)
      {
      H1_Bias="None";
      Comment("Bias is: "+H1_Bias+" since: "+TimeToStr(triggerBarTime,TIME_DATE|TIME_MINUTES));
      DeleteOrder();
      }
THIS IS CALLED NEXT IF THE TOP CODE SENDS IT THROUGH "OrderEntry(1)"

if(direction==1)
{//--Sell--//
      double stp=sell_takeprofit_price;
      double MinLot = MarketInfo(Symbol(),MODE_MINLOT);
        //Print("The minimum lots are: ",DoubleToStr(Min_Lot,Digits));
      double LotStep = MarketInfo(Symbol(),MODE_LOTSTEP);
        //Print("The Lotstep is: ",DoubleToStr(Lot_Step,Digits)); 
      double SellLotSize =(RiskedAmount/(pips_to_ssl/pips))/10;
      double lots = NormalizeDouble(SellLotSize,2);
      LotSize = MathFloor(lots/LotStep)*LotStep;
        //Print("The Lots to close is: ",DoubleToStr(LotSize,Digits));
      
      static double Stored_SellPrice; 

      if(OpenOrdersThisPair(Symbol())==0)
         {
         int SellTicketOrder=OrderSend(Symbol(),OP_SELLSTOP,LotSize,sellPrice,3,SellStopPrice,stp,NULL,MagicNumber,0,Red);
         }

      for(int o=OrdersTotal()-1; o>=0; o--)   
        {

       if(!OrderSelect(o,SELECT_BY_POS,MODE_TRADES))continue;
         if(OrderType()==OP_SELLSTOP)
           if(OrderMagicNumber()==MagicNumber)
             if(OrderSymbol() != Symbol())continue;        
                {
                   if(OrderStopLoss() - BuyStopPrice > Point / 2.) 
                        {
                        Stored_SellPrice = OrderOpenPrice(); 
                        DeleteOrder=OrderDelete(OrderTicket());
                        if(DeleteOrder!=TRUE)Print("Sell Delete Order Failed = ",GetLastError());
                        } 

                   if(OpenOrdersThisPair(Symbol())==0 && DeleteOrder)
                        {
                        int NewSellOrder = OrderSend(Symbol(),OP_SELLSTOP,LotSize,Stored_SellPrice,3,SellStopPrice,stp,NULL,MagicNumber,0,Green);
                        }    
                 }
        } 
    }
}	
THIS SHOULD DELETE THE PENDING ORDERS IF THE BIAS TELLS IT SO. HOWEVER I THINK IT IS CLASHING WITH THE FACT I AM ALTERING THE ORDER IN THE CODE ABOVE?


//+------------------------------------------------------------------+
//| Delete Order Function                                            |
//+------------------------------------------------------------------+

void DeleteOrder()
  {

   for(int i=OrdersTotal()-1; i>=0; i--) //  <-- for loop to loop through all Orders . .   COUNT DOWN TO ZERO !
     {
      if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) continue;   // <-- if the OrderSelect fails advance the loop to the next PositionIndex
       if(OrderMagicNumber()==MagicNumber) 
        if(OrderSymbol() != Symbol())continue; 
         if(OrderType() > OP_SELL)
          {  
          if(!OrderDelete(OrderTicket(),Magenta)){ // <-- try to close the order
            Print("Order Delete failed, order number: ",OrderTicket()," Error: ",GetLastError());  // <-- if the Order Close failed print some helpful information 
            }
            else{
            Print("Pending Order was deleted: ", OrderTicket());
            }
          }
      } //  end of For loop

  }
 
DomGilberto: I am getting the error 4108 and market order cannot be deleted... I know OrderClose is for open trades and OrderDelete is for pending, but I cannot see why I am getting this error?
Your code
    for(int o=OrdersTotal()-1; o>=0; o--)   
        {

       if(!OrderSelect(o,SELECT_BY_POS,MODE_TRADES))continue;
         if(OrderType()==OP_SELLSTOP)
           if(OrderMagicNumber()==MagicNumber)
             if(OrderSymbol() != Symbol())continue;        
                {
                   if(OrderStopLoss() - BuyStopPrice > Point / 2.) 
                   :
        } 
    }
Your code properly formatted
    for(int o=OrdersTotal()-1; o>=0; o--){
       if(!OrderSelect(o,SELECT_BY_POS,MODE_TRADES))continue;
       if(OrderType()==OP_SELLSTOP) if(OrderMagicNumber()==MagicNumber) if(OrderSymbol() != Symbol())continue;        
       {
           // OrderType, MN and symbol could be anything here except a sellStop from another chart from this EA.
                   if(OrderStopLoss() - BuyStopPrice > Point / 2.) 
                   :
        } 
    }
 
DomGilberto:
This gets called and checked every hour with regards to the bias. For example "H1_Bias" will be checked every hour and can change to "None", Down, or Up based upon MA's. It works in conjunction with "IsNewCandle()" function.

I am getting the error 4108 and market order cannot be deleted... I know OrderClose is for open trades and OrderDelete is for pending, but I cannot see why I am getting this error?

I told you about this yesterday ( https://www.mql5.com/en/forum/146416 ) . . . you will continue ( pardon the pun ) to get issues like this and not see them unless you either change your coding style or learn to check your code more carefully . . .

       if(!OrderSelect(o,SELECT_BY_POS,MODE_TRADES))continue;
         if(OrderType()==OP_SELLSTOP)
           if(OrderMagicNumber()==MagicNumber)
             if(OrderSymbol() != Symbol())continue;        
                {                                                    // the contents of this brace will be executed regardless of any of the checks above
                   if(OrderStopLoss() - BuyStopPrice > Point / 2.) 
                        {
                        Stored_SellPrice = OrderOpenPrice(); 
                        DeleteOrder=OrderDelete(OrderTicket());
                        if(DeleteOrder!=TRUE)Print("Sell Delete Order Failed = ",GetLastError());
                        } 
//  this is what your code does . . .

       if(!OrderSelect(o,SELECT_BY_POS,MODE_TRADES))continue;
         if(OrderType()==OP_SELLSTOP)
           if(OrderMagicNumber()==MagicNumber)
             if(OrderSymbol() != Symbol())
                continue;        
                
       if(OrderStopLoss() - BuyStopPrice > Point / 2.) 
          {
          Stored_SellPrice = OrderOpenPrice(); 
          DeleteOrder=OrderDelete(OrderTicket());
          if(DeleteOrder!=TRUE) Print("Sell Delete Order Failed = ",GetLastError());
          } 
 

Man, I am so confused. I swear I saw you brace it in a previous post you recommended to me... (I just tried to find it?)

The brace above where you have written "// the contents of this brace...", are you saying that brace pair does not need to be there, and if it remains, every single check I do above that, is irrelevant ?

I think what you meant was this . . . // (Your post)

     for(int b=OrdersTotal()-1; b>=0; b--)
         {
         if(!OrderSelect(b, SELECT_BY_POS, MODE_TRADES)) continue;
           
         if( OrderType() == OP_BUYSTOP &&
            OrderMagicNumber() == MagicNumber &&
            OrderSymbol() == Symbol() )      <<<<< I guess because you have not written OrderSymbol() != Symbol())continue; the brace immediately below it, is appropriate?     
            {
            if(OrderStopLoss() < iMA(NULL, 60, MA_Period, 0, 1, 0, 0) - ATR)  
               {
               Stored_BuyPrice = OrderOpenPrice();   
               DeleteOrder = OrderDelete(OrderTicket()); 
               }

            if(OpenOrdersThisPair(Symbol()) == 0 && DeleteOrder)  // If there are no open orders = place a new order. 
               {
               int NewBuyOrder = OrderSend(Symbol(), OP_BUYSTOP, LotSize, Stored_BuyPrice, 3, BuyStopPrice, btp, NULL, MagicNumber, 0, Green);
               if(NewBuyOrder == -1) Print("New Buy Order Last Error = ", GetLastError());
               }
            }
         }
 
Yea forget this thread. I'm being a complete idiot!

Appreciate all your help for pointing out my amnesia...
 
DomGilberto:
xxx sake - yea forget this thread. I'm being a complete idiot!

Appreciate all your help for pointing out my amnesia...


Please edit your post . . . and please keep in mind all the Forum rules

4. Any use of obscene expressions is forbidden.

 
Yea - sorry!
 
DomGilberto:
Yea - sorry!
Thank you