Unknown ticket xxxxxxxx for OrderModify function?

 

Hello Every Guru in the house,

Please I was working on a trailing stop code culled from the Mql4 book, the code is pasted below, I was to tweak it to move stop loss to break even level and also to start trailing after certain pips in profit is attained. It worked fine in doing those two things but after the price hits the trailing line to close the order, it starts and continues printing this message while it stops ea from opening further trades on the concerned currency pair: "unknown ticket 12345678 for OrderModify function", can somebody with good knowledge of coding than I review my code and tell me where I did it wrong?

Thanks in advance while waiting for assistance.


   extern double TrailingStop=35;
   extern double TrailingStart=30;
     int      Spread=3;
     int      sPrd=Spread;
     double   sTS=TrailingStop;
     double   tTS=TrailingStart;
        Symb=Symbol();       
        for(int i=1; i<=OrdersTotal(); i++)          // Cycle searching in orders     
        {      
        if(OrderSelect(i,SELECT_BY_POS)==true)               
        {     
        if(OrderSymbol()==Symb) {
    double    SL=OrderStopLoss(); // Stop Loss
    double    OP=OrderOpenPrice();
    int    Tip=OrderType();
        RefreshRates();
//-----  MoveStopLoss Strategies by Levels  --------  
         while(true)                            // 
           {
            double TTS=tTS; 
            double Min_Dist=MarketInfo(Symb,MODE_STOPLEVEL); //
            if(TTS<Min_Dist)                    // 
            TTS=Min_Dist; 
            
            double STS=sTS;
            double Min_Dist=MarketInfo(Symb,MODE_STOPLEVEL); // 
            if(STS<Min_Dist)                    // 
            STS=Min_Dist;                     // 
            //--------------------------------------------------- 4 --
            bool Modify=false;                  // 
            switch(Tip)                         // 
              {
                  case 0 :                          // Buy                
                  if(NormalizeDouble(OP,Digits)>NormalizeDouble(SL,Digits) && 
                  NormalizeDouble(Bid-STS*Point,Digits)>NormalizeDouble(OP,Digits))
                    {
                     SL=OP+sPrd*Point;            // Move stoploss to breakeven plus the spread
                     Text="Buy ";                //  Buy 
                     Modify=true;                // 
                    }
                    if(NormalizeDouble(OP,Digits)<NormalizeDouble(Bid-TTS*Point,Digits))
                    {
                     SL=Bid-TTS*Point;            // Start trailing the price
                     Text="Buy ";                //  Buy 
                     Modify=true;                // 
                    }
                  break;                         // switch
                  case 1 :                          //  Sell
                   if(NormalizeDouble(OP,Digits)<NormalizeDouble(SL,Digits) && 
                   NormalizeDouble(Ask+STS*Point,Digits)<NormalizeDouble(OP,Digits))
                    {
                     SL=OP-sPrd*Point;           //  Move the stoploss to breakeven plus the spread
                     Text="Sell ";              //   Sell 
                     Modify=true;               //  
                    }
                     if(NormalizeDouble(Ask+TTS*Point,Digits)<NormalizeDouble(OP,Digits))
                    {
                     SL=Ask+TTS*Point;           //  Start trailing the price
                     Text="Sell ";              //   Sell 
                     Modify=true;               //  
                    }
               }                                 //  switch
            if (Modify==false)                  // 
               break;                           // while
           //----------------------------------------------------
              double TP=OrderTakeProfit();
              double Price=OrderOpenPrice();
              int    Ticket=OrderTicket();
              bool Ans=OrderModify(Ticket,Price,SL,TP,0);//
            //--------------------------------------------------- 
             if (Ans==true)                      // 
              {
               Print("Order ",Text," ", Ticket," has been modified!");
               break;                           // 
              }
                }
                  }
                     }              
                       }
//======================================================
 

To be honest, I find your code confusing

Things like

   double   sTS=TrailingStop;

               double STS=sTS;
 
               if(STS<Min_Dist)
                  STS=Min_Dist;                   

// sTS is just an internmediary variable, not used elsewhere. So Why not

               double STS=TrailingStop;
 
               if(STS<Min_Dist)
                  STS=Min_Dist;     

 Also it is better to use descriptive names for your variables, especially when you expect other people to read and understand your code

When I see STS later in the code, I have no idea what it is.

 

As for your problem, check the return/reasons why the OrderModify failed.

Initially the report would be that the modify failed and this would be repeated over and over because of being stuck in a while(true) loop

I guess that the trade hit either the original SL or TP, but the loop continues and then is trying to modify a ticket that has already closed. That is why you are getting the invalid ticket error.

Get rid of the while loop 

 
GumRai:

To be honest, I find your code confusing

Things like

 Also it is better to use descriptive names for your variables, especially when you expect other people to read and understand your code

When I see STS later in the code, I have no idea what it is.

 

As for your problem, check the return/reasons why the OrderModify failed.

Initially the report would be that the modify failed and this would be repeated over and over because of being stuck in a while(true) loop

I guess that the trade hit either the original SL or TP, but the loop continues and then is trying to modify a ticket that has already closed. That is why you are getting the invalid ticket error.

Get rid of the while loop


Thanks GumRai,

I will try all your suggestions and get back to you whether it solves the problem or not.

You are blessed

 

Super GumRai,

I want to appreciate your help on this matter. I simply remove the while loop from the code as you have suggested and now the code is working fine, there no such error anymore.

You are a blessing to your generation, thanks so much.

I am grateful.

Reason: