MQL4 error diagnosis
MQL4 OrderSend failed: find the exact cause
MQL4 OrderSend failed when the function returns -1. Capture GetLastError immediately, then log the symbol, command, volume, requested price, stops, slippage, account margin, and trade permission. The error code determines whether to correct parameters, wait for market state, or stop for a permission issue.
Open the Coding Agent, then choose MQL4 from the language menu.
OrderSendDiagnostic.mq4.mq4
#property strict
#property script_show_inputs
input double Lots = 0.10;
input int Slippage = 3;
void OnStart()
{
if(!IsConnected() || !IsTradeAllowed())
{
Print("OrderSend skipped: trading is unavailable.");
return;
}
RefreshRates();
double price = NormalizeDouble(Ask, Digits);
ResetLastError();
int ticket = OrderSend(Symbol(), OP_BUY, Lots, price, Slippage,
0, 0, "diagnostic", 0, 0, clrNONE);
if(ticket >= 0)
{
Print("Order opened. ticket=", ticket);
return;
}
int error_code = GetLastError();
Print("OrderSend failed. error=", error_code,
" symbol=", Symbol(),
" lots=", DoubleToString(Lots, 2),
" ask=", DoubleToString(price, Digits),
" spread=", MarketInfo(Symbol(), MODE_SPREAD));
}