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:
- EA does not use any DLL or non-standard functionality
- 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
- The slippage is set constant at 3 full pips and never changes, and yet the issue starts happening intermittently
- 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.
- 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);
}