What is 'Invalid slippage for OrderSend function'?

Hello,

I’ve been using a couple EAs and all of them use the same order sending algorithm. Everything works normally, but sometimes the EA stops sending orders, printing this error instead. I have error handling in the code, but apparently this error is not returned by GetLastError() function, since EA would print it differently (see in the code). I contacted the broker to see if it is on their side, but they never even receive the orders - MT4 stops them from being sent in the first place.

When this issue starts happening, the EA will not work again even if it is restarted. I have to restart MT4 and then everything works fine once again.

A couple notes:

  1. EA does not use any DLL or non-standard functionality
  2. EA is trading in ECN account - I thought that slippage is not being used at all there, as all orders are executed at Market Price, and requotes are not possible
  3. The slippage is set constant at 3 full pips and never changes, and yet the issue starts happening intermittently
  4. Attaching a screenshot how the error looks in the logs. It appears only in Experts tab, not in Journal. There are no other messages preceding it, such as an attempt to open an order - the order does not even start being executed.
  5. Error checking function (in the code below) should print any error, but in this case it doesn’t, so GetLastError() does not return anything. It seems to be internal MT4 error.

Hopefully someone experienced this issue before and figured it out. The error is really hard to repeat, as EA can work normally for weeks (it is running on VPS) but then fail with this error. In another situation it would fail right after starting MT4.

Here is OrderSend function:

int openOrder(string o_type, double o_lot_size, double o_open_price, 
                          double o_stop_loss, double o_take_profit, string o_comment = "", 
                          int o_order_magic = 0, color o_color = clrWhite, datetime o_expiration = 0)
{
        bool check;
        int ticket, 
            order_type;
        int slippage = 3;
        int i;
        int order_send_retries = 5;
        color o_buy_color, o_sell_color;        
        
        // preparing slippage for 5 digit broker
        if (Digits == 3 || Digits == 5) slippage *= 10;
        
        if (o_color == clrWhite)
        {
                o_buy_color = clrDodgerBlue;
                o_sell_color = clrCrimson;
        }
        else
        {
                o_buy_color = o_color;
                o_sell_color = o_color;
        }
    
//+-------------------------------------------  orders open cycle ---+

  for (i=0; i < order_send_retries; i++)
  {    
                RefreshRates();
                
    if (o_type == "buy") //--- buy trade ---+
    {
                        order_type = OP_BUY;
                        o_open_price = Ask;

      ticket = OrderSend(Symbol(), 
                         order_type, 
                         o_lot_size, 
                         NormalizeDouble(o_open_price, Digits), 
                         slippage, 
                         0, 
                         0, 
                         o_comment, 
                         o_order_magic, 
                         o_expiration, 
                         o_buy_color);  

//+------------------------------  modify order to set SL and TP ---+
     
      if (ticket > 0)             
      {
        OrderSelect(ticket,SELECT_BY_TICKET); 
        check = OrderModify(OrderTicket(),
                        OrderOpenPrice(), 
                        NormalizeDouble(o_stop_loss, Digits),
                        NormalizeDouble(o_take_profit, Digits),
                        o_expiration,
                        o_buy_color); 
        if(!check)
           Print("Error in OrderModify. Error code = ",GetLastError());
        else
           Print("Order modified successfully.");
        return(ticket);
      }             
//+-----------------------------------  check OrderSend errors ---+
      else 
      { 
        if (!tradeErrors(GetLastError())) return(-1);
      }
    } // end of buy opening IF              
    else
    if (o_type == "sell") //--- sell trade ---+ 
    {
      order_type = OP_SELL;
      o_open_price = Bid;
             
      ticket = OrderSend(Symbol(), 
                         order_type, 
                         o_lot_size, 
                         NormalizeDouble(o_open_price, Digits),
                         slippage, 
                         0, 
                         0, 
                         o_comment, 
                         o_order_magic, 
                         o_expiration, 
                         o_sell_color);
                           
//+------------------------------  modify order to set SL and TP ---+

      if (ticket > 0)             
      {
        OrderSelect(ticket,SELECT_BY_TICKET); 
        check = OrderModify(OrderTicket(),
                        OrderOpenPrice(), 
                        NormalizeDouble(o_stop_loss, Digits),
                        NormalizeDouble(o_take_profit, Digits),
                        o_expiration,
                        o_sell_color);    
        if(!check)
           Print("Error in OrderModify. Error code = ",GetLastError());
        else
           Print("Order modified successfully.");  
        return(ticket);
      }          
//+-----------------------------------  check modification errors ---+
      else 
      { 
        if (!tradeErrors(GetLastError())) return(-1);
      }
    } // end of sell opening IF
  } // end of order open cycle
}
//+------------------------------------------------------------------+
//| managing errors                                                                      |
//+------------------------------------------------------------------+
bool tradeErrors(int error)
{
        int i;
        
        switch(error)
        {
                case 6: 
                        i = 0;
                        while (!IsConnected() && i<5)
                        {
                                inform(error);
                                Sleep(5000);
                        }
                        if (IsConnected())      return(true);  
                else                            return(false);
                case 129:  
                        inform(error);
                        RefreshRates();
                return(true);
                case 138: 
                        inform(error);
                        RefreshRates();
                return(true);
                case 135:  
                        inform(error);
                        RefreshRates();
                return(true);
                case 136: // no price
                        inform(error);
                        i = 0;
                while(!RefreshRates() && i<10000)
                {
                Sleep(1);   
                i++;
              }
                return(true);               
                case 146: 
                        inform(error);
                        i = 0;
                    while(!IsTradeAllowed() && i<10)
                    {    
                        Sleep(500); 
                        RefreshRates(); 
                        i++;
                }
                if (IsTradeAllowed())   return(true);  
                else                                    return(false);
                default:  
                        inform(error);  
                return(false);  
        }
}
//+------------------------------------------------------------------+
//| printing error                                                                       |
//+------------------------------------------------------------------+
void inform(int index = 0)
{
  string message;
       switch(index)
        {
        case 2:         message = "Common error. Please try reloading MT4 or OS";             break;
        case 6:         message = "No internet connection, will try again in 5 seconds..."; break;
        case 129:       message = "Invalid price. EA will try again in 5 seconds";       break;
        case 135:       message = "Price has changed, EA is trying once again...";         break;
        case 136:       message = "NO price on the market, will try again in 5 seconds..."; break;
        case 146:       message = "Trade context busy, waiting until it is free...";break;
        case 5 :        message = "MT4 version is too old, please update the software";  break;
        case 64:        message = "Account disabled";        break;
        case 133:       message = "Trading disabled";        break;
        default:        message = "Error #" + index + " occured";
        }
        message = message + " (" + TimeToStr(TimeCurrent(), TIME_MINUTES) + ")";
        Print("OrderSend error occured, " + message);                          
}

Hi,

Have you found a solution to the problem above? I’ve a similar problem with my EA, but I’m not a programmer. I made my EA with an online EA builder.

Any advice would be much appreciated.

Cheers.

The OP reported an error which doesn’t exist in MT4 but in his EA code. Though he didn’t show the code which printed this error.

You should post ALL relevant code and the exact output in the log.

Hello Saarang,

From my understanding you are implying that there is a code somewhere in my EA which actually prints this error. However all the error handling is done in the code that I posted and “inform” function is printing these errors. Just to make sure, I have searched all my code for “slippage” and did not find that exact error message anywhere.

To my knowledge, this error is not printed by any code that I have written but seems to be generated by MT4. Since @pdeeny1z is getting the same issue it is unlikely to come from the code written by me.

I have just encountered this error again, first time in all these months. I am adding some debugging code to my order sending algorithm and will post back a screenshot of the log when this issue occurs.

Thank you,

Ok you are right. I never saw this error and it is not documented, so my first answer. After some researches, I found very few reference and all useless.

Are you using any library (ex4) or dll ?

I can only suggest you to ask to your broker. Thank you to keep us posted.

My experience regarding this is like that, that the slippage value has to be adjusted suitable to the broker. Nevertheless i never could figure out any difference in the results in view of executions. I also could not figure out, what the value of slippage really is. Is it points or pips or digit-based? And we all know about the weird interpretation of points by MetaTrader.

But I also figured out a difference when one of my brokers changed from 0 digits in a specific symbol to two digits. I had to multiply the slippage by 100 to get any order successfully sent. If your symbol has 5 digits and you assume, that a value of 3 is interpreted as 3 pips, this is maybe just wrong and a value of 30 may be the right one.

The other way round is to set the slippage to almost infinite and to track/log the execution price and to compare this with the sent-price. If the result is always against you, drop the broker.

Thank you for your replies.

First, concerning the slippage value. Because the broker is ECN, to my understanding it is supposed to ignore the slippage parameter. It is supposed to execute at whatever is the latest price available. This slippage parameter is left from many years ago when orders were operating with “Instant execution”, not “Market execution”. Back then you could specify slippage in MT4 even when you manually sent an order. Right now the slippage field is not there anymore.

Second, the issue is intermittent. When I first encountered it I started testing with all slippage values: 10, 50, 100, 1000, 10000, etc. It did not depend on the value - sometimes order goes through and sometimes it doesn’t. For example, I came back to this thread because this error was reported to me by one of my clients. He was using the EA I wrote for him and received this error first time in many months. After that one instant we were not able to repeat it with him. In my first post you can see a screenshot where I show hundreds of instances of this error. It happened in an EA that was trying to reopen the order repeatedly and I was not watching it at the time. The issue was gone after we restarted the MT4, but now and then it comes back.

Also, please note that I am adjusting slippage parameter in case the broker is 5 digit:

// preparing slippage for 5 digit broker
        if (Digits == 3 || Digits == 5) slippage *= 10;

I have also reported it to the broker and they told me that the order never gets to them, it is not anywhere in their logs. In other words, as I mentioned in the first post, it seems that MT4 is giving EA this error and then preventing the order from being sent in the first place. However, because as you can see I am checking all OrderSend messages, this error is not available in GetLastError() function.

There are no DLLs or ex4s that I am using in my EAs. All EAs use the same order execution include file which I update as necessary.

Thank you for your suggestion about ServiceDesk @baarang_solio , I am going to write them as soon as I get this error with updated code which I am designing for debugging purposes.

Hello all

Based on my understanding and based on mql4 documentation I understand that slippage parameter is the maximum slippage possible or customer accepts to open the position.

I faced this problem , I set slippage as slippage = 100 points

and since my broker uses 5 digits I multiply it by 10 and becomes 1000, and I don’t expect that the price will make slippage more than 1000 before placing the position.

The strange thing is

when I decrease the value of slippage to 10 , and then multiplied by 10 , it goes fine and order placed , so I conclude that slippage has a maximum allowed by broker and you should not exceed this limit

so make it feasible not too high , not too small.

hope this comment will help someone.