Can´t find the reason why OrderModify Error 4051 is happening

 

Hi guys,

I just can´t find why my EA is showing this error when I use the Trailing Stop option.

I am attaching here the code where I call the function Trailing Stop and then the function itself

After that I am showing the messsages from the Alerts That I have inserted in the code

In short, The StopLoss of the Selected Order is the same as the new Stop Loss that the trailing Stop is calculating

If I am right, I have specifically coded that in such case the EA will not run the OrderModify function

But the Error 4051 is happening even though the OrderModify function is not being called

Well, at least that is my deduction

Will someone please check what is wrong with my code?

 //-------------------------------------------------------------------------------------------+
 // Set Trailing Stop
 //-------------------------------------------------------------------------------------------+
 
 if(OrdersTotal()>0)                                           // Check if there are Open Orders
   {
    if(Use_Fixed_TS==true || Use_Variable_TS==true)                  // Check if Trail Stop is needed 
      {
       for(i=0; i<=OrdersTotal(); i++)                                 // Cycle searching in orders
          {
           if(OrderSelect(i,SELECT_BY_POS)==true)                      // Select if the next is available
             {                                                         // Analysis of orders:
              int Tipo=OrderType();
              if(OrderSymbol()!=Symbol()||Tipo>1)continue;             // The order is not "ours" continue with next for i - and next OrderSelect          
              if(Send_Alerts) Alert("Hay una orden para modificar el SL");  //bool Modify=false;                                       // start with a Not to be modified instruction
              int X=0,F=0,V=0;
              if(Use_Fixed_TS==true && Tipo==OP_BUY) {F=1; X=1;}
              if(Use_Fixed_TS==true && Tipo==OP_SELL) {F=1; X=2;}
              if(Use_Variable_TS==true && Tipo==OP_BUY) {V=1; X=3;}
              if(Use_Variable_TS==true && Tipo==OP_SELL) {V=1; X=4;}
              if(F==1&&V==1) X=5;
              bool A=false;
              int Ticket=OrderTicket();
              double Price=OrderOpenPrice();
              double Loss=OrderStopLoss();
              double Profit=OrderTakeProfit();
              double Puntos;
              if(Send_Alerts) Alert("Caso de Trailing Stop=",X," Ticket=",Ticket," OrderOpenPrice=",Price," OrderSL=",Loss," OrderTakeProfit=",Profit);
              switch(X)
                    {
                     case 0:                                         // Case 0 is Not possible, any way, just get out of the switch loop
                      A=false;
                      break;
     
                     case 1:                                         // Call the Trailing Stop Function - fixed pips trail stop
                      A=Trailing_Stop(Tipo,Fixed_Trail_Stop*pips2points,Ticket,Price,Loss,Profit);
                      break;
      
                     case 2:                                         // Call the Trailing Stop Function - fixed pips trail stop
                      A=Trailing_Stop(Tipo,Fixed_Trail_Stop*pips2points,Ticket,Price,Loss,Profit);
                      break;
                                 
                     case 3:                                         // Call the Trailing Stop Function - variable pips trail stop
                     {
                      double TS_EMAL0=iMA(NULL,0,TS_EMA,0,1,PRICE_LOW,0);
                      Puntos=NormalizeDouble((Bid-TS_EMAL0)/Point,0);
                      A=Trailing_Stop(Tipo,Puntos,Ticket,Price,Loss,Profit);
                      if(Send_Alerts) Alert("Tipo=",Tipo," Puntos=",Puntos," OrderSL=",Loss," Answer=",A);  
                      break;
                     }     
                     case 4:                                         // Call the Trailing Stop Function - variable pips
                     {
                      double TS_EMAH0=iMA(NULL,0,TS_EMA,0,1,PRICE_HIGH,0);
                      Puntos=NormalizeDouble((TS_EMAH0-Ask)/Point,0);
                      A=Trailing_Stop(Tipo,Puntos,Ticket,Price,Loss,Profit);
                      if(Send_Alerts) Alert("Tipo=",Tipo," Puntos=",Puntos," OrderSL=",Loss," Answer=",A);
                      break;
                     }                     
                     case 5:
                      if(Send_Alerts==true) Alert("Wrong setting, choose one type of trailing stop only");
                    }                                               // end of switch(x)
         if(A==true) Alert("Trail Stop ok");
         else    
         {
          Alert("Error in Trailing Stop");
          //-------------------------------------------------------------------------------- 7 --
          // Error handling section
          //--------------------------------------------------------------------------------------
          int Error=GetLastError();                                // Failed :(
          switch(Error)                                            // Overcomable errors
            {
               case 130:
                  Alert("Wrong stops. Retrying.");
                  RefreshRates();                                 // Update data
                  break;                                          // At the next iteration
               case 136:
                  Alert("No prices. Waiting for a new tick..");
                  while(RefreshRates()==false)                    // To the new tick
                  Sleep(1);                                    // Cycle delay
                  break;                                          // At the next iteration
               case 146:
                  Alert("Trading subsystem is busy. Retrying ");
                  Sleep(500);                                     // Simple solution
                  RefreshRates();                                 // Update data
                  break;                                          // At the next iteration
                                                                  // Critical errors
               case 2: 
                  Alert("Common error.");
                  break;                                          // Exit 'switch'
               case 5: 
                  Alert("Old version of the client terminal.");
                  break;                                          // Exit 'switch'
               case 64: 
                  Alert("Account is blocked.");
                  break;                                          // Exit 'switch'
               case 133:
                  Alert("Trading is prohibited");
                  break;                                          // Exit 'switch'
               default: 
                  Alert("Occurred error ",Error);                 // Other errors
             }                                                    // End of switch
         }                                                        // End of Error section ("If !A" condition)           
       }                                                          // end of Order Select
     }                                                            // End of for i condition
   }                                                              // End of Fixed or Variable Trail Stop
  }                                                               // End of If OrdersTotal>0 
return(0);                                                       
}                                                                 // End of start function

This is the Trailing Stop function

//-----------------------------------------------------------------------------------+  
//  function Trailing Stop
//-----------------------------------------------------------------------------------+

bool Trailing_Stop(int Tipo, double Stop,int ticket, double price, double loss, double profit)
{
 bool Modify=false,Answer=0;
 string Text;
 double SL=0;                               
 while(true)                                                             // Modification Cycle
      {
       Alert("while true"," Tipo=",Tipo);
       switch(Tipo)                                                      // Evaluate By order type
             {
               case 0 :                                                  // Case Buy Order
               {
                SL=NormalizeDouble(Bid-Stop*Point,Digits);               // calculate new SL
                Alert("case 0:"," SL=",SL," OrderSL=",loss);
                //if(Wait_for_BE==true) {if(SL<=OrderOpenPrice())break;}   // Do not change SL, exit switch
                if(SL>loss+pips2dbl)                          // If new SL is higher than the current order SL + 0.00010 
                  {
                   if(Send_Alerts) Alert("New SL of Buy Order=",SL);
                   Text=" Buy ";                                         // Text for Buy 
                   Modify=true;                                          // To be modified
                  }
                break;                                                   // Exit 'switch'
               }                                                
               case 1 :                                                  // Case Sell Order
               {
                SL=NormalizeDouble(Ask+Stop*Point,Digits);               // calculate new SL
                Alert("case 1:"," SL=",SL," OrderSL=",loss);
                //if(Wait_for_BE==true) {if(SL>=OrderOpenPrice())break;}   // Do not change SL, exit switch
                if(SL<loss-pips2dbl)                                     // If Order Stop Loss is higher than what we want
                  {
                   if(Send_Alerts) Alert("New SL of sell order=",SL);
                   Text=" Sell ";                                        // Text for Sell 
                   Modify=true;                                          // To be modified
                  }
                 break;                                                  // Exit 'switch'
               }                                                         // End of case OP_SELL
             }                                                           // End of switch
            if(Modify==false){Alert("Modify=false");break;}              // If it is not modified exit while            
            Alert("Modify=true"," ,OrderTicket=",ticket," ,OrderOpenPrice=",price," ,SL=",SL," ,OrderTakeProfit=",profit," ,0", " ,Green");
            Answer=OrderModify(ticket,price,SL,profit,0,Green);    //Modify it!
            if(Answer)
              {
               if(Send_Alerts) Alert("Order ",Text,OrderTicket()," is modified :-)");
               break;                                                    // From Modification cycle
              }
          }                                                              // End of modification cycle while (true)
  return(Answer);
}

so, after I check that Modify==false, there is a break instruction, so the EA should get out of the while(true) loop and should NOT execute the OrderModify instruction, but somehow it is beeing called and if my new SL is equal to the OrderStopLoss then error 4051 is understandable. But why is the OrderModify instruction being executed, will someone please explain? 

here are the messages when I backtested the EA:

2014.10.13 01:00:54.698 2014.08.05 11:59  POWER PRO ALERT V33 OCT 2014 EURUSD,M30: Alert: Occurred error 4051
2014.10.13 01:00:54.698 2014.08.05 11:59  POWER PRO ALERT V33 OCT 2014 EURUSD,M30: Alert: Error in Trailing Stop
2014.10.13 01:00:54.698 2014.08.05 11:59  POWER PRO ALERT V33 OCT 2014 EURUSD,M30: Alert: Tipo=1 Puntos=240.0 OrderSL=1.3418 Answer=false
2014.10.13 01:00:54.698 2014.08.05 11:59  POWER PRO ALERT V33 OCT 2014 EURUSD,M30: Alert: Modify=false
2014.10.13 01:00:54.698 2014.08.05 11:59  POWER PRO ALERT V33 OCT 2014 EURUSD,M30: Alert: case 1: SL=1.3418 OrderSL=1.3418
2014.10.13 01:00:54.698 2014.08.05 11:59  POWER PRO ALERT V33 OCT 2014 EURUSD,M30: Alert: while true Tipo=1
2014.10.13 01:00:54.698 2014.08.05 11:59  POWER PRO ALERT V33 OCT 2014 EURUSD,M30: Alert: Caso de Trailing Stop=4 Ticket=3 OrderOpenPrice=1.34082 OrderSL=1.3418 OrderTakeProfit=1.33882
2014.10.13 01:00:54.698 2014.08.05 11:59  POWER PRO ALERT V33 OCT 2014 EURUSD,M30: Alert: Hay una orden para modificar el SL
2014.10.13 01:00:54.698 2014.08.05 11:59  POWER PRO ALERT V33 OCT 2014 EURUSD,M30: Alert: Occurred error 4051

 What I see here is that the new SL (variable SL) is 1.3418 and the OrderStopLoss (variable loss) is exactly 1.3418

That is why the condition if(SL<loss-pips2dbl) under the case 1: (sell) is not met.

That is why the message "Modify=false" is being showed

That is why the Answer (A)=false is being showed

So then why is the EA showing "Occurred error 4051"?

Maybe the int Error=GetLastError is not being initialized ? So it is showing this error just because it was the last error and not because this error is happening over and over? 

Thanks in advance for your help

 

Have you looked up the meaning of 4051:Invalid function parameter value. So try to change the values of OrderModify(..), like price, SL, and profit.

Check the minimal difference (MODE_STOPLEVEL and MODE_FREEZELEVEL)

Maybe instead of price use either OrderOpenPrice() or 0 as you don't change a pending order, ....
 

Hi gooly,

There is another section in my code where I modify the initial Sl and TP values for a recently opened trade.

Might it be possible that the error is being generated  THERE and that the sentence Error=GetLastError()  is receiving the Error generated on that part of the code?

BTW, thanks for your answer , I will check the parameters' values on both OrderModify funtions.

best regards. JHF 

 
You can use ResetLastError() before each Order - look it up
 
Justhavingfun:


So then why is the EA showing "Occurred error 4051"?


That is the last error that happened . . . it may be completely somewhere else in your code . . .  and completely unrelated to your Trailing Stop function,  if you want to check this then at the start of your Trailing Stop function add . . . ResetLastError();

 

Part of the issue is that your trailing stop function returns False even if there was no failure . . .  then you go looking for an error . . . why ?  you need to fix this design flaw. 

 
RaptorUK:

That is the last error that happened . . . it may be completely somewhere else in your code . . .  and completely unrelated to your Trailing Stop function,  if you want to check this then at the start of your Trailing Stop function add . . . ResetLastError();

 

Part of the issue is that your trailing stop function returns False even if there was no failure . . .  then you go looking for an error . . . why ?  you need to fix this design flaw. 

Hi everyone,

 

I am getting the same kind of error message but with openselect function (OrderSelect returned error of 4051):

Can anyone help please?

Here is copy of my journal: 

2014.12.08 15:25:41.375 2014.03.25 00:00  Testing_loop_v1 EURGBP-,Daily: OrderSelect returned error of 4051  

2014.12.08 15:25:41.375 2014.03.25 00:00  Testing_loop_v1 EURGBP-,Daily: Type : 1 - Position : 2 Total Trades : 3

2014.12.08 15:25:41.375 2014.03.25 00:00  Testing_loop_v1 EURGBP-,Daily: Type : 1 - Position : 1 Total Trades : 3

2014.12.08 15:25:41.375 2014.03.25 00:00  Testing_loop_v1 EURGBP-,Daily: Type : 0 - Position : 0 Total Trades : 3

2014.12.08 15:25:41.375 2014.03.25 00:00  Testing_loop_v1 EURGBP-,Daily: open #6 sell 0.10 EURGBP- at 0.83859 tp: 0.83429 ok

2014.12.08 15:25:41.375 2014.03.25 00:00  Testing_loop_v1 EURGBP-,Daily: Alert:           ###   --  New BAR (UP) --   ### : 4

2014.12.08 15:25:41.093 2014.03.24 00:00  Testing_loop_v1 EURGBP-,Daily: OrderSelect returned error of 4051  

2014.12.08 15:25:41.093 2014.03.24 00:00  Testing_loop_v1 EURGBP-,Daily: Type : 1 - Position : 1 Total Trades : 2 

014.12.08 15:25:41.093 2014.03.24 00:00  Testing_loop_v1 EURGBP-,Daily: Type : 0 - Position : 0 Total Trades : 2

2014.12.08 15:25:41.093 2014.03.24 00:00  Testing_loop_v1 EURGBP-,Daily: open #5 sell 0.10 EURGBP- at 0.83601 tp: 0.83421 ok

2014.12.08 15:25:41.093 2014.03.24 00:00  Testing_loop_v1 EURGBP-,Daily: Alert:           ###   --  New BAR (UP) --   ### : 3

2014.12.08 15:25:40.906 2014.03.21 10:38  Tester: take profit #4 at 0.83650 (0.83650 / 0.83657)

2014.12.08 15:25:40.843 2014.03.21 00:00  Testing_loop_v1 EURGBP-,Daily: OrderSelect returned error of 4051 

2014.12.08 15:25:40.843 2014.03.21 00:00  Testing_loop_v1 EURGBP-,Daily: Type : 0 - Position : 1 Total Trades : 2

2014.12.08 15:25:40.843 2014.03.21 00:00  Testing_loop_v1 EURGBP-,Daily: Type : 0 - Position : 0 Total Trades : 2

2014.12.08 15:25:40.843 2014.03.21 00:00  Testing_loop_v1 EURGBP-,Daily: open #4 buy 0.10 EURGBP- at 0.83475 tp: 0.83650 ok

2014.12.08 15:25:40.843 2014.03.21 00:00  Testing_loop_v1 EURGBP-,Daily: Alert:              ###   --  New BAR  (DOWN) --   ### : 2

2014.12.08 15:25:40.656 2014.03.20 13:47  Tester: take profit #1 at 0.83415 (0.83408 / 0.83415)

2014.12.08 15:25:40.500 2014.03.20 00:00  Testing_loop_v1 EURGBP-,Daily: OrderSelect returned error of 4051 

2014.12.08 15:25:40.500 2014.03.20 00:00  Testing_loop_v1 EURGBP-,Daily: Type : 0 - Position : 1 Total Trades : 2

2014.12.08 15:25:40.500 2014.03.20 00:00  Testing_loop_v1 EURGBP-,Daily: Type : 1 - Position : 0 Total Trades : 2

2014.12.08 15:25:40.500 2014.03.20 00:00  Testing_loop_v1 EURGBP-,Daily: open #3 buy 0.10 EURGBP- at 0.83625 tp: 0.83969 ok

2014.12.08 15:25:40.500 2014.03.20 00:00  Testing_loop_v1 EURGBP-,Daily: Alert:              ###   --  New BAR  (DOWN) --   ### : 1

2014.12.08 15:25:40.437 2014.03.19 22:26  Tester: take profit #2 at 0.83607 (0.83600 / 0.83607)

2014.12.08 15:25:40.125 2014.03.19 00:00  Testing_loop_v1 EURGBP-,Daily: OrderSelect returned error of 4051  

2014.12.08 15:25:40.125 2014.03.19 00:00  Testing_loop_v1 EURGBP-,Daily: Type : 1 - Position : 1 Total Trades : 2

2014.12.08 15:25:40.125 2014.03.19 00:00  Testing_loop_v1 EURGBP-,Daily: Type : 1 - Position : 0 Total Trades : 2

2014.12.08 15:25:40.125 2014.03.19 00:00  Testing_loop_v1 EURGBP-,Daily: open #2 sell 0.10 EURGBP- at 0.83967 tp: 0.83607 ok

2014.12.08 15:25:40.125 2014.03.19 00:00  Testing_loop_v1 EURGBP-,Daily: Alert:           ###   --  New BAR (UP) --   ### : 2

2014.12.08 15:25:36.718 2014.03.18 00:00  Testing_loop_v1 EURGBP-,Daily: OrderSelect returned error of 4051  

2014.12.08 15:25:36.718 2014.03.18 00:00  Testing_loop_v1 EURGBP-,Daily: Type : 1 - Position : 0 Total Trades : 1

2014.12.08 15:25:36.718 2014.03.18 00:00  Testing_loop_v1 EURGBP-,Daily: open #1 sell 0.10 EURGBP- at 0.83672 tp: 0.83415 ok

2014.12.08 15:25:36.718 2014.03.18 00:00  Testing_loop_v1 EURGBP-,Daily: Alert:           ###   --  New BAR (UP) --   ### : 1   

And here is part my code:

v_total_trades = OrdersTotal();

for(int pos=0;pos<=v_cnt;pos++)

           { 

            v_total_trades = OrdersTotal();

        if(OrderSelect(pos, SELECT_BY_POS)==true)

           {          

                   // I am just displaying a message for now so I can find the problem,   

              Print("Type : ", OrderType() +  " - Position : ", pos + " Total Trades : " + v_total_trades);

               }              

             else

                  Print("OrderSelect returned error of ",GetLastError());

          } // End of FOR Loop 

 

 
  1. Don't paste code
    Play video
    Please edit your post.
    For large amounts of code, attach it.

     for(i=0; i<=OrdersTotal(); i++)                                 // Cycle searching in orders
              {
               if(OrderSelect(i,SELECT_BY_POS)==true)    
    
    There are OrdersTotal() count of orders. Order positions are [0 .. OrdersTotal()-1]
  2.                   // I am just displaying a message for now so I can find the problem,   
    
                  Print("Type : ", OrderType() +  " - Position : ", pos + " Total Trades : " + v_total_trades);
    
                   }              
    
                 else
    
                      Print("OrderSelect returned error of ",GetLastError());
    You print pos and v_total on success. Why don't you print them on failure so you find out why?
 
WHRoeder:

  1. Play video
    Please edit your post.
    For large amounts of code, attach it.

    There are OrdersTotal() count of orders. Order positions are [0 .. OrdersTotal()-1]
  2. You print pos and v_total on success. Why don't you print them on failure so you find out why?
It is the same values for both pos and v_tolal.. on success and failure..
 
v_cnt = 0;
         v_cnt = OrdersTotal();
         for(int pos=0;pos<=v_cnt;pos++)
           { 
                v_total_trades = OrdersTotal();
                      
                   if(OrderSelect(pos, SELECT_BY_POS)==true)
                {  
                   //if (your_modify_order_condition && OrdersTotal!=0)   // This way he just modifies orders that are still open.
                
                   Print("On Success: - Type : ", OrderType() +  " - Position : ", pos + " Total Trades : " + v_total_trades);
                   
               }              
             else
                  Print("OrderSelect returned error of ",GetLastError());
                  Print("On Failure: - Type : ", OrderType() +  " - Position : ", pos + " Total Trades : " + v_total_trades);
                  
          } // End of FOR Loop

 Thank you for the reply..

Here is the journal after updating to code to include the values on failure and success:

2014.12.09 13:56:15.734 2014.03.20 00:00  Testing_loop_v1 EURGBP-,Daily: OrderSelect returned error of 4051
2014.12.09 13:56:15.734 2014.03.20 00:00  Testing_loop_v1 EURGBP-,Daily: On Failure: - Type : 0 - Position : 1 Total Trades : 2
2014.12.09 13:56:15.734 2014.03.20 00:00  Testing_loop_v1 EURGBP-,Daily: On Success: - Type : 0 - Position : 1 Total Trades : 2
2014.12.09 13:56:15.734 2014.03.20 00:00  Testing_loop_v1 EURGBP-,Daily: On Failure: - Type : 1 - Position : 0 Total Trades : 2
2014.12.09 13:56:15.734 2014.03.20 00:00  Testing_loop_v1 EURGBP-,Daily: On Success: - Type : 1 - Position : 0 Total Trades : 2
2014.12.09 13:56:15.734 2014.03.20 00:00  Testing_loop_v1 EURGBP-,Daily: open #3 buy 0.10 EURGBP- at 0.83625 tp: 0.83969 ok
2014.12.09 13:56:15.734 2014.03.20 00:00  Testing_loop_v1 EURGBP-,Daily: Alert:              ###   --  New BAR  (DOWN) --   ### : 1
2014.12.09 13:56:15.671 2014.03.19 22:26  Tester: take profit #2 at 0.83607 (0.83600 / 0.83607)
2014.12.09 13:56:15.468 2014.03.19 00:00  Testing_loop_v1 EURGBP-,Daily: Failure: - Type : 0 - Position : 2 Total Trades : 2
2014.12.09 13:56:15.468 2014.03.19 00:00  Testing_loop_v1 EURGBP-,Daily: OrderSelect returned error of 4051
2014.12.09 13:56:15.468 2014.03.19 00:00  Testing_loop_v1 EURGBP-,Daily: On Failure: - Type : 1 - Position : 1 Total Trades : 2
2014.12.09 13:56:15.468 2014.03.19 00:00  Testing_loop_v1 EURGBP-,Daily: On Success: - Type : 1 - Position : 1 Total Trades : 2
2014.12.09 13:56:15.468 2014.03.19 00:00  Testing_loop_v1 EURGBP-,Daily: On Failure: - Type : 1 - Position : 0 Total Trades : 2
2014.12.09 13:56:15.468 2014.03.19 00:00  Testing_loop_v1 EURGBP-,Daily: On Success: - Type : 1 - Position : 0 Total Trades : 2
2014.12.09 13:56:15.468 2014.03.19 00:00  Testing_loop_v1 EURGBP-,Daily: open #2 sell 0.10 EURGBP- at 0.83967 tp: 0.83607 ok
2014.12.09 13:56:15.468 2014.03.19 00:00  Testing_loop_v1 EURGBP-,Daily: Alert:           ###   --  New BAR (UP) --   ### : 2
2014.12.09 13:56:12.234 2014.03.18 00:00  Testing_loop_v1 EURGBP-,Daily: On Failure: - Type : 0 - Position : 1 Total Trades : 1
2014.12.09 13:56:12.234 2014.03.18 00:00  Testing_loop_v1 EURGBP-,Daily: OrderSelect returned error of 4051
2014.12.09 13:56:12.234 2014.03.18 00:00  Testing_loop_v1 EURGBP-,Daily: On Failure: - Type : 1 - Position : 0 Total Trades : 1
2014.12.09 13:56:12.234 2014.03.18 00:00  Testing_loop_v1 EURGBP-,Daily: On Success: - Type : 1 - Position : 0 Total Trades : 1
2014.12.09 13:56:12.234 2014.03.18 00:00  Testing_loop_v1 EURGBP-,Daily: open #1 sell 0.10 EURGBP- at 0.83672 tp: 0.83415 ok
2014.12.09 13:56:12.234 2014.03.18 00:00  Testing_loop_v1 EURGBP-,Daily: Alert:           ###   --  New BAR (UP) --   ### : 1

2014.12.09 13:56:12.234 Testing_loop_v1 inputs: BuyLots=0.1; SellLots=0.1; Barsize_min=20; Barsize_max=60; Spread=10; Take_profit_level=100; high_low=1; 

 


 

1) form a habit to count down OrdersTotal()!!

v_cnt = OrdersTotal();
for(int pos=v_cnt-1;pos>=0;pos--) { 
...

2) E.g. TotalOrders() = 4; Please count now how many order do you request in your code if you loop from pos=0 to pos<=4.

 
  1. 2014.12.09 13:56:15.468 2014.03.19 00:00  Testing_loop_v1 EURGBP-,Daily: Failure: - Type : 0 - Position : 2 Total Trades : 2
    2014.12.09 13:56:15.468 2014.03.19 00:00  Testing_loop_v1 EURGBP-,Daily: OrderSelect returned error of 4051
    Asked and previously answered
    There are OrdersTotal() count of orders. Order positions are [0 .. OrdersTotal()-1]
    There are two trades, at positions zero and one. There is no position 2
  2. You must count down when closing/deleting in a position loop. Get in the habit of always counting down. Loops and Closing or Deleting Orders - MQL4 forum
Reason: