Newbie Alert!! OrderModify help needed, please! - page 2

 
if (OrderSelect(ticket_buy,SELECT_BY_TICKET))
   {
   Tip=OrderType();                        
   if (Tip==0 && OrderCloseTime()==0)                             
      {
      SL=OrderStopLoss();                             
      TP=OrderTakeProfit();
      if (NormalizeDouble(SL+Trailing_Stop*Point,Digits)<NormalizeDouble(Bid,Digits))  
         {
         Precio=OrderOpenPrice();      
         resp=OrderModify(ticket_buy,Precio,Bid-Trailing_Stop*Point,TP,0);   
         if (resp>0)
         {
         err=GetLastError();
         Alert("Modificacion ",Symbol()," ",Texto," ",ticket_buy," Error (trail)= ",err);
         }
 
landmeister:

What errors do you get before the 141 ?
 

Eror 141 is the first and only error I get. Just when I try to add the first stop loss.

 
landmeister:

Eror 141 is the first and only error I get. Just when I try to add the first stop loss.

And you get it as an Alert ?  or printed to the experts tab/log ?
 
landmeister:

Hi, I did the changes you told me:

 1) I simplified the "if" statement.

2) I used GetLastError() only when OrderModify is not valid.

And still the same 141 error as soon as the stop loss is added.

  1. I NEVER told you to simplify the IF. I ONLY showed you that you were NOT checking current vs new values, thus your 141.
  2. NormalizeDouble(Bid,Digits)
    Don't use normalizeDouble EVER
 

I get it when I print the GetLastError result.

 

Let's do this easier. I just tried a new, very simple and stupid ea. Just to test purposes. I want to send a new operation every new bar. This is the code:

 

extern int TP;
extern double num_lotes;
extern int trailing_stop;


int barras=0;
int tb;


//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----

  if (Digits==5 || Digits==3)
     {
     TP=TP*10;
     trailing_stop=trailing_stop*10;
     }

  

   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
  
  bool compra;
  double SL,precio_entrada;
  int modificacion,err;
//----
  
  if (barras<Bars)
     {
     compra=true;
     tb=Lanzar_Operacion(OP_BUY,num_lotes,0,TP,compra);
     if (OrderSelect(tb,SELECT_BY_TICKET))
        {
        RefreshRates();
        precio_entrada=OrderOpenPrice();
        SL=Ask-trailing_stop*Point;
        modificacion=OrderModify(tb,precio_entrada,SL,0,0);
        if (modificacion==false)
           {
           err=GetLastError();
           Alert("Error=",err);
        }
     barras=Bars;
     }
  
   
//----
   return(0);
  }
Lanzar_Operacion is a function that simply sends an operation and works fine. Once I start this ea, the operation is created with no problems, but when it tries the modification, once again the error 141 shows.
 
landmeister:

I get it when I print the GetLastError result.

 Let's do this easier. I just tried a new, very simple and stupid ea. Just to test purposes. I want to send a new operation every new bar. This is the code:

 Lanzar_Operacion is a function that simply sends an operation and works fine. Once I start this ea, the operation is created with no problems, but when it tries the modification, once again the error 141 shows.

Please show ALL the code . . . what you have shown will not cause an error 141 

Also,  using Bars in they way you have is not a reliable method,  you should use time instead.  When the number of bars on the chart chart reaches "Max bars on chart"  Bars stops incrementing.
 
landmeister:  Lanzar_Operacion is a function that simply sends an operation and works fine. Once I start this ea, the operation is created with no problems, but when it tries the modification, once again the error 141 shows.
  1.  if (Digits==5 || Digits==3)
         {
         TP=TP*10;
         trailing_stop=trailing_stop*10;
         }
    
    Every time you change period, pair, etc. Your EA goes through a deinit()/init() sequence. so TP becomes 10x 100x, 1000x... Never modify the externals.

  2.      if (OrderSelect(tb,SELECT_BY_TICKET))
            {
            RefreshRates();
            precio_entrada=OrderOpenPrice();
            SL=Ask-trailing_stop*Point;
            modificacion=OrderModify(tb,precio_entrada,SL,0,0);
    
    On last time. Where are you comparing the new SL to the current? You are trying to modify the SL every single tick.
 

I know that every time I change period, etc, the ea goes through deinit()/init() sequence. When I do that, I just remove the ea, and add it agin to initialize all correctly.

As I already mentioned in my last comment, this is just an ea to test the error 141. I'm not doing trailing stop in this ea. As you can see, this ea just sends an order (with no problems), and when that order is modified, it returns always an error 141. I'm just asking why always returns error 141 when the order is modified.

This is all the code:

 

extern int TP;
extern double num_lotes;
extern int trailing_stop;

int barras=0;
int tb,ts;


//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----

  if (Digits==5 || Digits==3)
     {
     TP=TP*10;
     trailing_stop=trailing_stop*10;
     }

  

   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
  
  bool compra;
  double SL,precio_entrada;
  int modificacion,err;
//----
  
  if (barras<Bars)
     {
     compra=true;
     tb=Lanzar_Operacion(OP_BUY,num_lotes,0,TP,compra);
     if (OrderSelect(tb,SELECT_BY_TICKET))
        {
        RefreshRates();
        precio_entrada=OrderOpenPrice();
        SL=Ask-trailing_stop*Point;
        modificacion=OrderModify(tb,precio_entrada,SL,0,0);
        if (modificacion==false)
           {
           err=GetLastError();
           Alert("Error=",err);
           }
        }
     barras=Bars;
     }
  
   
//----
   return(0);
  }
  
  
// ----

double Lanzar_Operacion(int tipo_op,double lotes,double sl,double tp,bool compra)
   {
      int err=1;
      int aleatorio;
      double ticket;
      string simbolo=Symbol();
      double take_profit;
      
      RefreshRates();
      if (compra)
         {
         take_profit=Ask+tp*Point;
         ticket=OrderSend(simbolo,tipo_op,lotes,Ask,3,sl,take_profit);
         }
      else
         {
         take_profit=Bid-tp*Point;
         ticket=OrderSend(simbolo,tipo_op,lotes,Bid,3,sl,take_profit);
         }
   return(ticket);
   }  
  
 
landmeister:

I know that every time I change period, etc, the ea goes through deinit()/init() sequence. When I do that, I just remove the ea, and add it agin to initialize all correctly.

As I already mentioned in my last comment, this is just an ea to test the error 141. I'm not doing trailing stop in this ea. As you can see, this ea just sends an order (with no problems), and when that order is modified, it returns always an error 141. I'm just asking why always returns error 141 when the order is modified.

What other EAs or scripts do you have running on the terminal you are running this on ?
Reason: