OrderDelete error 4108?

 

With the code below the instructions are as follows :

After 10 bars it opens both a buy and sell pending orders each 200 points from the close price at 10th bar. Then, when price closes higher than one of the pending order price points it closes the opposite order.  

// Input variables
input int MagicNumber = 101;                        
input int Slippage = 10;
input double LotSize = 0.1;

input int StopLoss = 200;
input int TakeProfit = 200;

// Global variables
int gBuyTicket,gSellTicket;

void OnTick()   
{  
   // Current order counts      
   int buyCount = 0, sellCount = 0;  // sets object to 0
   
   for(int order = 0; order <= OrdersTotal() - 1; order++) 
   {
      bool select = OrderSelect(order,SELECT_BY_POS);
      
      if(OrderMagicNumber() == MagicNumber && select == true) 
      {
         if(OrderType() == OP_BUY) buyCount++;
         else if(OrderType() == OP_SELL) sellCount++;
      }   
   }  
   
   // Our bar count for our condition
   static int barCount = TimeMinute(TimeCurrent());
   barCount++;
   
   // Our order condition  
   if(barCount > 10 && buyCount == 0 && gBuyTicket == 0 && sellCount == 0 && gSellTicket == 0) // Maybe i can extract a value from barCount using getvalue() function
   {  
      // Create pendingorder price points  
      static int b = TimeMinute(TimeCurrent());
      b++;
      double bar = Close[b];  // Here I use my bar counting code to hold the price at condition start    
      double pendingBuyPrice = bar + (200 * _Point);
      double pendingSellPrice = bar - (200 * _Point);
            
      // Create stop and take profit values for our pendingorder parameters
      double buyStopLoss = 0, buyTakeProfit = 0, sellStopLoss = 0, sellTakeProfit = 0;
      if(StopLoss > 0) 
      {
         buyStopLoss = pendingBuyPrice - (StopLoss * _Point);
         sellStopLoss = pendingSellPrice + (StopLoss * _Point);         
      }
         
      if(TakeProfit > 0) 
      {
         buyTakeProfit = pendingBuyPrice + (TakeProfit * _Point);
         sellTakeProfit = pendingSellPrice - (TakeProfit * _Point);               
      }  
                        
      // Open Buy and Sell pending orders                                    
      gBuyTicket = OrderSend(_Symbol,OP_BUYSTOP,LotSize,pendingBuyPrice,Slippage,buyStopLoss,buyTakeProfit,"BuyOrder",MagicNumber,0,clrGreen);
      gSellTicket = OrderSend(_Symbol,OP_SELLSTOP,LotSize,pendingSellPrice,Slippage,sellStopLoss,sellTakeProfit,"SellOrder",MagicNumber,0,clrRed);     
   }

   // Close opposite order on buy/sell order open   
   if(barCount > 10)
   {
      // Create pendingorder price points  
      static int b = TimeMinute(TimeCurrent());
      b++;
      double bar = Close[b];  // Here I use my bar counting code to hold the price at condition start     
      double pendingBuyPrice = bar + (200 * _Point);
      double pendingSellPrice = bar - (200 * _Point);
      
      if(Close[1] > pendingBuyPrice) bool deleteSell = OrderDelete(gSellTicket,clrRed);
      if(Close[1] < pendingSellPrice) bool deleteBuy = OrderDelete(gBuyTicket,clrGreen);      
   } 
}

 

It places trades like I want no problems and compiles with no warnings or errors. But I have noticed on the MT4 journal it says OrderDelete error 4108 and unknown ticket 1 for OrderDelete function. 

I cant see why it says this. 

 

I don't know what you are trying to achieve with your static variables. Doesn't seem logical to me

      if(Close[1] > pendingBuyPrice) bool deleteSell = OrderDelete(gSellTicket,clrRed);
      if(Close[1] < pendingSellPrice) bool deleteBuy = OrderDelete(gBuyTicket,clrGreen);   

 You don't check whether the order has already been deleted or whether it has been triggered.

Before deleting the order check that gSellTicket>0 Check for a successful delete and then set the  gSellTicket to 0 or -1 

Personally, I would select the orders and check if gSellTicket is now an OP_SELL. If it is, you know that it has been triggered, so delete the other one 

Reason: