getting "unknown ticket for OrderClose function" error, please help to solve. Thanks!

 

Hi all,

This is my first attempt at programming an EA. I am running into a error everytime i tried to do backtesting for my EA. Error is OrderClose error 4108, and unknown ticket for OrderClose function. I can't seem to find any bug in my code + the error seems to happen at random, i.e. the code works for some trades and failed at one particular condition which I cannot figure out. Can someone kindly help me spot my mistakes?


Below is the block of code that is likely to have problems. The below code is supposed to help monitor my trade.

1.) delete pending order if price hit my intended stoploss.

2.) bring stoploss to breakeven once price has reached a certain level in favour of my trade

3.) close trade once certain MA conditions are fulfilled.


Snapshot of the backtest with the error:

The error starts appearing at ticket 3. The error began to loop infinitely throughout the backtest. What is strange is that ticket 3 sell order and market order is not reflected at all in the log. It did appear in my result tab though.


if (Flag)
            {
               //Monitor trade
            if (OrderSelect(ticket, SELECT_BY_TICKET))
               {
                  //Alert("ticket num is: ", ticket);
                  
                     //check if order is closed
                  if ( OrderCloseTime()>OrderOpenTime() )
                  {
                     Alert("Order number " + ticket + " closed");
                     Flag = false;
                     breakevenSell = false;
                     selltradeclose = false;
                     ticket = 0;
                  }                  

                else switch (OrderType())
                  {
                     case OP_SELLSTOP:  //still pending order
                      if ( NormalizeDouble(MarketInfo(Symbol(), MODE_ASK), Digits) > OrderStopLoss())
                           {
                              //Alert("price rise above stoploss");
                              if ( OrderDelete(OrderTicket(), Red)) 
                                 { 
                                    //Alert("pending sell order deleted");
                                    Flag = false;
                                    ticket = 0;
                                 }
                           }
                           break;
                        
                     case OP_SELL: // Trade triggered, check when to set stoploss to breakeven
                        if (!breakevenSell)
                           {
                               //Alert("checking sell breakeven " + (OrderOpenPrice()- BE*(OrderOpenPrice()- OrderTakeProfit())) );
                               
                               if ( NormalizeDouble(MarketInfo(Symbol(), MODE_ASK), Digits) < OrderOpenPrice()- BE*(OrderStopLoss() - OrderOpenPrice()) )
                                 {
                                    //Alert("moving to breakeven");
                                    if ( OrderModify(OrderTicket(), OrderOpenPrice(), OrderOpenPrice()- 20*_point, OrderTakeProfit(), 99999999 ,Violet) )
                                    {
                                       breakevenSell = true;
                                      // Alert("order modified to BE");
                                    }
                                 }
                                 
                           }
                          
                        if ( sellMA1 > sellMA2 && selltradeclose==false)
                             {
                                 Alert("sell MA intercepted: ticket " + OrderTicket());
                                 RefreshRates();
                                 if ( OrderClose(OrderTicket(), OrderLots(), NormalizeDouble(MarketInfo(Symbol(), MODE_ASK), Digits), slippage ,Aqua) )
                                    
                                    { 

                                       buytradeclose = false;
                                       Alert("sell close time: " + TimeToString(OrderCloseTime()) );
                                    }
                              }
                     break;     
                  
                  }//end switch statement                 
                } //endif orderselect
         } //endif flag
   
   } // endif ticket > 0
 

1) The code you show doesn't help to find the address the problem: why does OrderSelect() fails?

2) If there is no valid ticket either the order has never been sent or has been closed already (by ST/TP?).

 
Jimi80:

Snapshot of the backtest with the error:

The error starts appearing at ticket 3. The error began to loop infinitely throughout the backtest. What is strange is that ticket 3 sell order and market order is not reflected at all in the log. It did appear in my result tab though.


Check the log FILE, if there is too much output not all the log is available in the Journal window.

What is this isolated lien at the end ?

   } // endif ticket > 0

Please show all the relevant code (and log).

 
gooly:

1) The code you show doesn't help to find the address the problem: why does OrderSelect() fails?

2) If there is no valid ticket either the order has never been sent or has been closed already (by ST/TP?).

  1. Agree. Check your return codes (OrderSelect) and print your variable, and find out why. What are Function return values ? How do I use them ? - MQL4 forum and Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
  2. If the ticket is valid (was sent,) then open, pending, or closed is irrelevant.
    The pool parameter is ignored if the order is selected by the ticket number.
    OrderSelect - Trade Functions - MQL4 Reference
    Reason: