OrderSend Error 4051: Diagnose Invalid Parameters in MT4
By FXNook Lab team
MT4 error 4051 is ERR_INVALID_FUNCTION_PARAMVALUE: a function received a parameter value it could not accept. When it appears around OrderSend(), the number alone does not identify which argument is wrong, and it is not a specific broker rejection; log the actual request and capture the error immediately after the failed call. Invalid stops and volume have their own common trade errors, 130 and 131, so do not treat those codes as interchangeable with 4051.
What OrderSend error 4051 means in MT4
OrderSend() submits a request containing several values: a symbol, order type, lot size, price, slippage, stop loss, take profit, comment, magic number and expiration. The terminal or a function in the code can report 4051 when a parameter value is invalid, but the error code does not point to one particular argument. You need the call site and the values passed to it to narrow down the cause.
That distinction matters because some familiar order problems have more specific trade errors. A price that is invalid or out of date commonly produces 129, invalid stops commonly produce 130, and volume outside the broker's permitted range or step commonly produces 131. These codes indicate different checks; a 4051 should send you back to the exact function call rather than make you assume a particular broker-side rule was violated.
For example, an EA can calculate a valid-looking lot size but pass an empty symbol to OrderSend(), or pass an unexpected value as the order command. The log must show what the program actually passed at runtime, not what you expect its inputs to contain.
Make sure 4051 came from OrderSend
In MQL4, GetLastError() returns the most recently recorded error and resets the stored error value. Call it immediately after the function that failed, before calling another function that could set a different error. If the EA reports 4051 later in a long block of code, the source may be a nearby function such as a price or indicator calculation, not the order request itself.
After reproducing the issue, read both the Experts and Journal tabs in the MT4 terminal. The Experts log often shows EA messages and order attempts; the Journal records terminal activity and trade-server responses. Match the timestamp and symbol to your own diagnostic messages, because a line printed after the failure may describe a later operation.
A useful first step is to put the send call in a clear branch, save the return value, and read the error right away if the result is negative:
int ticket = OrderSend(Symbol(), OP_BUY, lots, Ask, slippage, stopLoss, takeProfit, "EA entry", magic, 0, clrNONE);
if(ticket < 0)
{
int err = GetLastError(); // Capture before another operation can change it
Print("OrderSend failed. Error=", err);
}
This small check tells you that the failed operation was OrderSend(), but it does not yet reveal which argument caused the problem. Add a log of the full request before the call, then compare those values with the symbol and account conditions in which the EA is running.
Audit every OrderSend argument
Use the argument order in the MQL4 function signature as a checklist. A value that looks reasonable in the input panel can still be wrong after a calculation, a symbol change or a conversion between pips and points.
| Argument | What to inspect |
|---|---|
symbol | It must identify a symbol available to the terminal and broker. An empty string, a misspelled name or a name without the broker's suffix can make the request invalid for the intended instrument. Symbol() is a useful choice when the EA should trade the chart symbol. |
cmd | Use a valid MQL4 order command, such as OP_BUY, OP_SELL or one of the pending-order constants. Check that a variable has not been left uninitialized or assigned an unrelated integer. |
volume | Lots must be positive and must fit the symbol's minimum, maximum and step. Do not assume that rounding to two decimal places makes a volume valid; the broker may use a different lot step. |
price | For a market buy, use the current Ask; for a market sell, use the current Bid. Pending orders use an entry price appropriate to their order type. Normalize the price to the symbol's digits, and make sure the quote is available and current. |
slippage | This is a nonnegative tolerance in points, not pips. Verify the value passed is the intended integer and that a calculation has not made it negative or unreasonably large. |
stoploss | Use zero when no stop loss is requested. Otherwise, pass a correctly normalized price on the appropriate side of the market and account for the broker's minimum stop distance. |
takeprofit | Use zero when no take profit is requested. A nonzero value must be a price, not a distance in points or pips, and must satisfy the relevant price and stop-distance rules. |
comment | Pass a string. Keep it simple while diagnosing, so accidental values or string-building mistakes are easier to rule out. |
magic | Pass the intended integer identifier. Log it to catch uninitialized or unexpectedly converted values; it distinguishes the EA's orders but does not replace validation of the other arguments. |
expiration | For a market order, use zero. For a pending order, use zero for no expiration or a valid future time if supported. A broker that refuses pending-order expiration can return trade error 147, which is distinct from 4051. |
The table is a diagnostic checklist, not a claim that each bad value always produces 4051. In particular, invalid volume is commonly reported as 131 and invalid stops as 130. Record all the values first, then use the returned error and the call site to decide which validation to investigate.
Log the request immediately before OrderSend
The example below is MQL4 code for a market buy on the chart symbol. It prints the values used for the request immediately before sending, then captures GetLastError() as the first operation in the failure branch. Pass stop-loss and take-profit as prices, or pass zero if they are not used.
int SendLoggedBuy(double lots, int slippage, double stopLoss, double takeProfit, int magic)
{
string symbol = Symbol();
int cmd = OP_BUY;
double price = Ask;
int digits = (int)MarketInfo(symbol, MODE_DIGITS);
price = NormalizeDouble(price, digits);
if(stopLoss != 0.0)
stopLoss = NormalizeDouble(stopLoss, digits);
if(takeProfit != 0.0)
takeProfit = NormalizeDouble(takeProfit, digits);
string comment = "EA entry";
datetime expiration = 0;
Print("OrderSend request: symbol=", symbol,
" side=BUY cmd=", cmd,
" lots=", DoubleToString(lots, 8),
" price=", DoubleToString(price, digits),
" slippage=", slippage,
" SL=", DoubleToString(stopLoss, digits),
" TP=", DoubleToString(takeProfit, digits),
" comment=", comment,
" magic=", magic,
" expiration=", TimeToString(expiration, TIME_DATE|TIME_MINUTES));
int ticket = OrderSend(symbol, cmd, lots, price, slippage,
stopLoss, takeProfit, comment, magic,
expiration, clrNONE);
if(ticket < 0)
{
int err = GetLastError();
Print("OrderSend failed: error=", err,
" symbol=", symbol,
" cmd=", cmd,
" lots=", DoubleToString(lots, 8),
" price=", DoubleToString(price, digits),
" SL=", DoubleToString(stopLoss, digits),
" TP=", DoubleToString(takeProfit, digits),
" expiration=", TimeToString(expiration, TIME_DATE|TIME_MINUTES));
return(-1);
}
Print("OrderSend accepted: ticket=", ticket);
return(ticket);
}
The example uses the chart's Ask and Symbol(), so it is intended for a market buy on that chart symbol. For a market sell, use the current Bid and OP_SELL. If your EA sends to another symbol, obtain that symbol's quote and properties rather than reusing the chart's Ask, Bid or digits.
Normalizing the price controls its decimal representation; it does not make an invalid price valid. It cannot ensure that the quote is current, that a pending price is on the correct side of the market, or that stops are far enough away from the market. Likewise, the example logs the lot size but does not make it compliant with the broker's volume limits.
Validate volume and prices against the symbol
For symbol-specific properties in MQL4, use MarketInfo(). The lot settings are MODE_MINLOT, MODE_MAXLOT and MODE_LOTSTEP; price precision is MODE_DIGITS, and the point size is MODE_POINT. These checks help you find a mismatch between the EA's calculation and the instrument's trading settings.
A lot size needs to satisfy all three volume constraints: it must not be below the minimum or above the maximum, and it must align with the allowed step. For example, a volume can be within the minimum and maximum but still fall between permitted step values. Rounding it with NormalizeDouble() alone does not prove that it is a valid multiple of the lot step.
For prices, get the digits for the same symbol being traded and normalize each nonzero price to those digits. Market orders should use the correct current side of the quote: buy at Ask and sell at Bid. A pending order has its own relationship to the market price, and both pending entry prices and stops need to meet the broker's rules.
Check whether your code uses pips where the function expects points. On symbols with fractional-pip quotes, one pip can be more than one point, so a value copied directly from a pip-based setting may produce a different distance than intended. Print the symbol, digits, point size, requested lots and prices together so you can see the units and values that reached OrderSend().
Distinguish 4051 from nearby trade errors
| Error | Meaning | First check |
|---|---|---|
4051 | ERR_INVALID_FUNCTION_PARAMVALUE: a function received an invalid parameter value. | Identify which function produced it and inspect the actual values at that call. The code alone does not name the bad OrderSend argument. |
129 | Invalid price. | Check whether the market quote is current, the correct Bid or Ask was used, and the price is normalized for the symbol. |
130 | Invalid stops. | Check stop-loss and take-profit prices, their direction relative to the order, and the broker's minimum stop distance. See How to Fix OrderSend Error 130: Invalid Stops in MT4. |
131 | Invalid trade volume. | Compare the lots with the symbol's minimum, maximum and lot step. See OrderSend Error 131: Fix Invalid Volume in MT4. |
133 | Trading is disabled. | Check whether trading is permitted for the account and symbol, and whether terminal or EA trading permissions allow the request. |
These codes point to different kinds of checks. A broker can reject a trade request for a specific trading constraint, while 4051 tells you that a function parameter value was invalid; do not translate a 4051 into “bad lot size” or “invalid stops” without evidence from the logged request and the actual error.
If the logged values appear correct but the EA still does not trade, trace the code path that builds the request. An input may be changed by a calculation, a condition may prevent the expected send call, or the EA may be sending from a different branch than the one you edited. The MT4 EA not opening trades checklist covers other common reasons a trade request is never reached or is refused.
Retest the correction
- Compile in MetaEditor. Fix any compilation errors introduced by the change. A clean compile confirms that the source builds, but it does not prove that the runtime values satisfy the broker's trading rules.
- Run the EA under the intended conditions. Use the intended symbol and account settings, and test the same order type and input values that produced the error. Strategy Tester conditions may not match every live-account or broker restriction.
- Read the request log. Compare the printed symbol, command, lots, price, stops and expiration with the properties and quote for that symbol. If a value is unexpected, trace where the EA calculates or changes it before sending.
- Verify the result. A successful call returns a ticket greater than zero; a failed call returns a negative value and should have an immediately captured error. Read the Experts and Journal logs to make sure the corrected request did not produce a new error.
If the request values do not explain why an EA is still waiting or failing to reach the trade logic, the FXNook EA debugger can follow the EA's entry path and show what stops it from trading. For the next test, start by adding the pre-request log, reproduce the failure once, and inspect the exact values beside the captured error code.
Questions
What does error 4051 mean in MQL4?
It means a function received an invalid parameter value; the MQL4 constant is ERR_INVALID_FUNCTION_PARAMVALUE. The code does not identify which parameter is wrong, so capture it immediately after the failing call and log that call's inputs.
Can an invalid lot size cause OrderSend error 4051?
An invalid lot size is possible to investigate when you see a parameter error, but an OrderSend volume that violates the broker's lot rules commonly produces error 131. Compare the requested lots with the symbol's minimum, maximum and step, and use the actual returned code to guide the diagnosis.
How do I print the OrderSend parameters and error code in MT4?
Print the symbol, command, volume, price, slippage, stop loss, take profit and expiration immediately before calling OrderSend. If the returned ticket is negative, call GetLastError as the first operation in that branch, save the value, and print it with the request details.
How is OrderSend error 4051 different from error 131?
Error 4051 is a general invalid-function-parameter-value error and does not name a particular OrderSend argument. Error 131 specifically indicates invalid trade volume, so check the requested lots against the symbol's allowed minimum, maximum and lot step.