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));
  }
  • Failure sentinel is ticket -1
  • Read GetLastError immediately
  • Log the complete request context
  • Use error-specific retry rules

Start with the OrderSend return value

OrderSend returns a ticket number on success and -1 on failure. GetLastError is meaningful only after that failure result, and it should be captured before another call changes the error state.

Do not diagnose from the phrase “OrderSend failed” alone. Error 130 points to stops, 131 to volume, 134 to margin, 138 to a requote, and 4109 to local trading permission. Each requires a different response.

Log the request that the server actually received

Record the exact order symbol, command, lots, request price, stop loss, take profit, slippage, magic number, Bid, Ask, Digits, spread, stop level, and free margin. A value recomputed later may not match the failed request.

  • Use current Ask for buys and current Bid for sells
  • Normalize symbol prices and step-align volume
  • Check local permission and current connection
  • Treat server and broker rules as runtime inputs

Retry by failure class, not by habit

Invalid parameters should be corrected before another call. Requotes, off quotes, and a busy trade context may justify a small delayed retry after refreshing state. Permission, closed-market, and margin failures need an external state or sizing change.

Every retry needs an attempt limit, time budget, fresh quote, and a new check that the original entry condition still holds.

Review the complete example

This bounded script checks connection and permission, refreshes Ask, tests the OrderSend ticket, and records the request context when the function returns -1. It deliberately leaves stop and sizing policy to the strategy specification.

Running this script can submit a market order. Use only a demo account after adding the strategy’s volume, margin, stop, and duplicate-order controls.

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));
  }
Where Pineify fits

Move from the rule to editable MQL4 source

Pineify can generate or revise one self-contained .mq4 file with MQL4-specific reference context. Its independent static checker returns MetaEditor diagnostics so you can repair source issues before testing in your own MT4 environment.

Open MQL4 Coding Agent

Static diagnostics do not prove runtime behavior, backtest results, broker compatibility, or live-trading safety.

Pineify MQL4 Coding Agent with MQL4 selected and an editable MetaTrader 4 code artifact

A practical workflow

  1. 1

    Check for ticket -1

    Branch on the documented OrderSend failure result before reading the error state.

  2. 2

    Capture the code and request

    Read GetLastError immediately and log every material input used by the failed call.

  3. 3

    Classify the failure

    Separate invalid input, market state, account state, permission, and transient transport failures.

  4. 4

    Correct and retest

    Apply the error-specific fix, compile, and test against the target broker and symbol.

Frequently asked questions

What does MQL4 OrderSend failed mean?

It means OrderSend returned -1. The accompanying GetLastError code identifies the next diagnostic branch.

Why does OrderSend return -1 with error 0?

The error may have been read too late, reset, or consumed. Capture it immediately after the -1 result and log both values together.

Should I retry every failed OrderSend?

No. Correct invalid prices, stops, or volume first. Use bounded retries only for conditions that can change, such as a fresh quote or busy context.

What should an OrderSend failure log contain?

Include error code, symbol, order type, lots, price, stops, slippage, Bid, Ask, spread, stop level, margin, permission, and timestamp.

Can Pineify repair OrderSend code?

Pineify can revise a self-contained .mq4 file and add validation, logging, and bounded retry logic. Broker acceptance and live behavior still require testing in MT4.

Last verified against the linked official references on . Compile and test the final source in the MetaTrader 4 build, broker, symbol, and account environment you plan to use.

Continue with one reviewable MQL4 file

Define the requirement, inspect the generated source, repair diagnostics, and test the behavior in your own MT4 environment.

Open Coding Agent