OrderModify

 

Dear All,

I have this code in my Expert:

for(i=0;i<OrdersTotal()-1;i++)
{
Print("Profits modified");
OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
if(OrderTakeProfit()!=new_profit)
{
while(OrderModify(OrderTicket(),OrderOpenPrice(),0,new_profit,NULL)==false)
{
Print("Error:",GetLastError());
if(GetLastError()==4108)
{
Print("Error");
break;
}
else
{
Print("Error:",GetLastError());
break;
}
}
}
}

If a condition is met, the expert should modify all the opening position's take profit. This expert works well most of the time, but once or twice a day. It does not work. For example, it modifies the take profit of two positions of total five positions and then expert halts!!! By halt, I mean it seems that enters a non-defined state or endless loop. I should manually restart the expert and then everything again would be OK. Then, is there any way to prevent this problem? Is there anyway that generally avoid that EA enters such states?

 

As written I think this code will go 'endless loop' when a modify fails with a 'client side' error, e.g. TP too close to market price, a buy being given a TP below its open price, etc

Keep in mind that the GetLastError() return is volatile and you have to capture it immediately, as even a subsequent Print line will reset it to 0

-BB-

PS

This still wont prevent endless loop but at least you'll see the error first time round in the Experts or Journal tabs of the Terminal?

Use the SRC button to post code if its not too lengthy

int iError;

for(i=0;i<OrdersTotal()-1;i++)
{
  Print("Profits modified");
  if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES));
   if(OrderTakeProfit()!=new_profit)
    {
     while(OrderModify(OrderTicket(),OrderOpenPrice(),0,new_profit,NULL)==false) // OrderModify      
     {
       iError = GetLastError();

       Print("Error:",iError );
       if(iError ==4108)
         {
          Print("Error");
          break;
         }
       else
         {
          Print("Error:", iError);
          break;
         }
      }  // OrderModify 
   }
} // OrdersTotal()