Error 4108 backtesting Trailing stop EA

 

I've been trying to make a trailing stop EA for a while, and this is the first time I've nail it, sort of. when I do the back test I get the error 4108 (Invalid Ticket), when the EA tries to modify an order. I think it shouldn't happen since I use the OrderTicket() function to get it, has anyone any idea what's going on?

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---Open orders loop
   int
   i=0,
   OO=0;
   for(i=1; i<=OrdersTotal(); i++)
     {
      if(OrderSelect(i-1,SELECT_BY_POS,MODE_TRADES)==true)
        {
         if(OrderSymbol()!=Symbol())
            continue;
         if(OrderType()>=2)
            continue;
         OO++;
         break;
        }
     }
//---Trading logic
   double
   ADXma1=iADX(NULL, 0, 14, 0, 0, 0),
   ADXpl1=iADX(NULL, 0, 14, 0, 1, 0),
   ADXmi1=iADX(NULL, 0, 14, 0, 2, 0),
   ADXma2=iADX(NULL, 0, 14, 0, 0, 1),
   ADXpl2=iADX(NULL, 0, 14, 0, 1, 1),
   ADXmi2=iADX(NULL, 0, 14, 0, 2, 1),
   ADXma3=iADX(NULL, 0, 14, 0, 0, 2),
   ADXma4=iADX(NULL, 0, 14, 0, 0, 3);
   int
   Trend1=Trend1(ADXpl1, ADXmi1),
   TrendForce1=TrendForce1(ADXma1, ADXma2, ADXma3),
   Force1=Force1(ADXma1);
   bool
   Buy=false,
   Sell=false;
   if(Trend1==1)
     {
      if(TrendForce1==1)
         if(Force1==2)
            Buy=true;
      Sell=false;
     }
   if(Trend1==2)
     {
      if(TrendForce1==1)
         if(Force1==2)
            Sell=true;
      Buy=false;
     }
//---Trailing stop
   double
   SLb=NormalizeDouble(Bid-StopLoss*Point, Digits),
   SLs=NormalizeDouble(Ask+StopLoss*Point, Digits);
   while(OO>=1)
     {
      if(Bid>NormalizeDouble(OrderStopLoss()+TrailingStop*Point, Digits))
        {
         Print("Modifying ", OrderTicket());
         RefreshRates();
         if(OrderModify(OrderTicket(), OrderOpenPrice(), SLb, NULL, NULL, clrWhite))
            Print(OrderTicket(), " modified");
        }
      if(Ask<NormalizeDouble(OrderStopLoss()-TrailingStop*Point, Digits))
        {
         Print("Modifying ", OrderTicket());
         RefreshRates();
         if(OrderModify(OrderTicket(), OrderOpenPrice(), SLs, NULL, NULL, clrWhite))
            Print(OrderTicket(), " modified");
        }
     }
//---Open loop
   while(OO==0)
     {
      if(Buy==true)
        {
         Print("Buying ", Symbol());
         RefreshRates();
         if(OrderSend(NULL, 0, Lotaje, Ask, 10, SLb, NULL, NULL, 0, 0, clrBlue)==true)
            Buy=false;
         break;
        }
      if(Sell==true)
        {
         Print("Selling ", Symbol());
         RefreshRates();
         if(OrderSend(NULL, 0, Lotaje, Bid, 10, SLs, NULL, NULL, 0, 0, clrBlue)==true)
            Sell=false;
         break;
        }
      break;
     }
  }
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| Trend funtion                                                    |
//+------------------------------------------------------------------+
int Trend1(double dplus, double dminus)
  {
   if(dplus>dminus)
      return(1);
   if(dplus<dminus)
      return(2);
   return(0);
  }
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| Trend force funtion                                              |
//+------------------------------------------------------------------+
int TrendForce1(double a1,double a2,double a3)
  {
   if(a1>a2&&a2>a3)
      return(1);
   if(a1<a2&&a2<a3)
      return(2);
   return(0);
  }
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| Force funtion                                                    |
//+------------------------------------------------------------------+
int Force1(double adx)
  {
   if(adx<=19.99)
      return(1);
   if(adx>=20)
     {
      if(adx<=34.99)
         return(2);
     }
   if(adx>=35)
     {
      if(adx<=49.99)
         return(3);
     }
   if(adx>=50)
      return(4);
   return(0);
  }
//+------------------------------------------------------------------+
 

OrderTicket(), OrderOpenPrice(), etc. are only valid if you have a currently selected order.

However, in your code, your OrderSelect() inside a loop, is very far away from when you eventually use OrderModify(), and any other functions you call that may themselves call trade functions will move the focus or selection away.

Instead, save the ticket number, and then reselect just before modifying (or using other trade functions for the process), or alternatively restructure your entire code logic as it is very stringy and convoluted.

EDIT: It may also happen when by the time you try to modify an order, it is already closed.