4105 OrderModify Error

 

Purpose: Add a set stop loss and take profit to all open trades (regardless of currency pair)

Problem:

This script generates error 4105 when placed on a chart. Invalid ticket for OrderModify function. My demo account currently has an open trade with no stops or limits attached.

What causes the invalid ticket selection?

int start(){

   ChangeStopsAndTPs(); 
   return (0);
}

void ChangeStopsAndTPs() 
{
  int total = OrdersTotal();
  for(int i=0;i<total;i++)
  {
    OrderSelect(1, SELECT_BY_POS,MODE_TRADES);
    int type   = OrderType();
    int sl = 20*Point;
    int tp = 20*Point;

    bool result = false;
    
    switch(type)
    {
      //Close opened long positions
      case OP_BUY       : 
         result = OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()-sl,OrderOpenPrice()+tp,0,NULL);
         break;
      
      //Close opened short positions
      case OP_SELL      : 
         result = OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()+sl,OrderOpenPrice()-tp,0,NULL);
         break;            
                          
    }
    
    if(result == false)
    {
      Alert("Order " , OrderTicket() , " failed to close. Error:" , GetLastError() );
      Sleep(3000);
    }  
   }
}

Files:
 

There was a small glitch in the OrderSelect function. I typed 1 instead of i while trying to fix it. Even with i, the problem still remains.

 
replace
OrderSelect(1, SELECT_BY_POS,MODE_TRADES);
to
OrderSelect(i, SELECT_BY_POS,MODE_TRADES);
and
int sl = 20*Point;
int tp = 20*Point;
to
double sl = 20*Point;
double tp = 20*Point;
 

Now I'm gettting error 4109 Trade is not Allowed.

The script finds the correct ticket, so that's a big improvement. Any idea why it wouldn't allow a 20 pip sl/tp to be added?

 
ERR_TRADE_NOT_ALLOWED 4109 Trade is not allowed. Enable checkbox "Allow live trading" in the expert properties.
 
Roger:
replace
OrderSelect(1, SELECT_BY_POS,MODE_TRADES);
to
OrderSelect(i, SELECT_BY_POS,MODE_TRADES);
and
int sl = 20*Point;
int tp = 20*Point;
to
double sl = 20*Point;
double tp = 20*Point;


May someone please take a look at my code.

Trying to implement the same code but trying to modify stop & limit orders as well.  The code works with OP_Buy & OP_Sell, but not with any other orders.

Thank you.


void ChangeStopsAndTPs() 
{
  int total = OrdersTotal();
   int l_Try = 0;
   int l_MaxTry = 10;

  for(int i=0;i<total;i++)
  {
    OrderSelect(i, SELECT_BY_POS,MODE_TRADES);
    int type   = OrderType();
    double sl = StopLoss*Point;
    double tp = TakeProfit*Point;

    bool result = false;
    
    switch(type)
    {
   case OP_BUY:
   case OP_BUYLIMIT:
   case OP_BUYSTOP:
      for (l_Try = 0; l_Try < l_MaxTry; l_Try++) 
      {
         if(type==OP_BUY) 
      //modify opened long positions
         result = OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()-sl,OrderOpenPrice()+tp,0,NULL);
         if (result>0) break;
      }
    break;
    
      case OP_SELL:
      case OP_SELLLIMIT:
      case OP_SELLSTOP:
      for (l_Try = 0; l_Try < l_MaxTry; l_Try++) {
         if(type==OP_SELL)
      //modify opened short positions
         result = OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()+sl,OrderOpenPrice()-tp,0,NULL);
         if (result>0) break;
    }
    
    if(result == false)
    {
      ReportError();
      Sleep(3000);
    }  
   }
  }
  return(0);
}
 
I_Need_Money:


May someone please take a look at my code.

Trying to implement the same code but trying to modify stop & limit orders as well.  The code works with OP_Buy & OP_Sell, but not with any other orders.

In your code you need to make sure you comply with the requirements in this document:  Requirements and Limitations in Making Trades


Why aren't you printing the error if your OrderModify() fails ?  don't you want to know what the error is ?  Read this:   What are Function return values ? How do I use them ?

 
RaptorUK:

In your code you need to make sure you comply with the requirements in this document:  Requirements and Limitations in Making Trades


Why aren't you printing the error if your OrderModify() fails ?  don't you want to know what the error is ?  Read this:   What are Function return values ? How do I use them ?

Thank you for your input.

 If there is an error it calls: 

ReportError();

Which is working fine, I have no problem there.   Errors are being printed, alert, and notified. 

 

The EA is opening all the orders with not problem.  I have the original version (listed above) of the "ChangeStopsAndTPs()" working and able to modify all OP_BUY & OP_SELL, but it dosen't modify STOP or LIMIT orders.   I was trying to also modify STOP & LIMIT orders not just open.

Thank you. 

 
I_Need_Money:

Thank you for your input.

 If there is an error it calls: 

Which is working fine, I have no problem there.   Errors are being printed, alert, and notified. 

 

The EA is opening all the orders with not problem.  I have the original version (listed above) of the "ChangeStopsAndTPs()" working and able to modify all OP_BUY & OP_SELL, but it dosen't modify STOP or LIMIT orders.   I was trying to also modify STOP & LIMIT orders not just open.

Thank you. 

Sorry,  read in haste repent at leisure  :-)

If I understand correctly your OP_BUY and OP_SELL Order are being modified but not your limit orders,  isn't this causing the issue ?

  if(type==OP_SELL)   // <-- only executes the following code (the OrderModify() )  for a OP_SELL
      //modify opened short positions
         result = OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()+sl,OrderOpenPrice()-tp,0,NULL);

         if (result>0) break;

 and similar for the code that modifies the Buy orders.

 

For a Sell Limit  (OP_SELLLIMIT) or Sell Stop (OP_SELLSTOP)  the OrderModify()  will not be called but the lines that follow will, result will be false even though no error has been generated.

 
RaptorUK:

Sorry,  read in haste repent at leisure  :-)

If I understand correctly your OP_BUY and OP_SELL Order are being modified but not your limit orders,  isn't this causing the issue ?

 and similar for the code that modifies the Buy orders.

 

For a Sell Limit  (OP_SELLLIMIT) or Sell Stop (OP_SELLSTOP)  the OrderModify()  will not be called but the lines that follow will, result will be false even though no error has been generated.

 

I grabed the idea from another EA, where it was opening orders based on the (switch(type) & CASE) and was trying to apply the same principle here.  Yes, I could out line each type of order, but was thrying to get the same result with the (switch & case).
 
    bool result = false;
:
         if (result>0) break; <====== What does this if test
Reason: