Summary: This article provides an advanced deep dive into MQL4's OrderSend function, covering return value validation, systematic error handling, slippage models, and order modification patterns.




The `OrderSend()` function is the gateway between your Expert Advisor and the trade server. Despite being one of the most frequently used MQL4 functions, its proper implementation remains widely misunderstood. Poor error handling and execution logic are responsible for 70% of EA runtime failures.

Function Signature and Return Values
```cpp
int OrderSend(string symbol, int cmd, double volume, double price,
int slippage, double stoploss, double takeprofit,
string comment, int magic, datetime expiration,
color arrow_color);
```
Return value: ticket number (>=0) on success, -1 on failure with `GetLastError()` providing the error code.

Systematic Error Handling Framework
Every `OrderSend()` call must be wrapped with retry logic. Never assume first-attempt success.

```cpp
// MQL4 - Production-ready OrderSend with retry and error classification
int OrderSendWithRetry(string sym, int cmd, double vol, double price,
int slip, double sl, double tp, string cmt,
int magic, datetime exp) {
int attempt = 0;
int maxAttempts = 3;
int ticket = -1;

while(attempt < maxAttempts) {
attempt++;
RefreshRates();

ticket = OrderSend(sym, cmd, vol, price, slip, sl, tp, cmt, magic, exp, clrNONE);
int error = GetLastError();

if(ticket > 0) {
return ticket;
}

// Error classification and recovery
switch(error) {
case 129: // Invalid price
case 136: // Off quotes
case 138: // Requote
Sleep(100 * attempt); // Exponential backoff
price = NormalizeDouble(MarketInfo(sym, MODE_ASK), Digits);
break;
case 146: // Trading context busy
Sleep(150 * attempt);
while(IsTradeContextBusy()) Sleep(50);
break;
case 148: // Too many orders
return -1; // Immediate failure
default:
Print("OrderSend error ", error, " on attempt ", attempt);
Sleep(100);
}
}
return -1;
}
```

Slippage Calculation Models

Slippage parameter accepts values in points. The actual execution price deviation is:
\[
\Delta p_{\text{actual}} = |p_{\text{requested}} - p_{\text{executed}}| \leq \text{slippage} \times \text{Point}
\]

For aggressive execution, use dynamic slippage based on volatility:
```cpp
int DynamicSlippage() {
double spread = MarketInfo(Symbol(), MODE_SPREAD);
double atr = iATR(NULL, 0, 14, 1);
double volatilitySlippage = MathMax(spread, atr / Point * 0.05);
return (int)MathMin(volatilitySlippage, 50);
}
```

Pending Order Modification Logic

Market orders use price = Ask/Bid. Pending orders require specific price calculations:
  • Buy Limit: price below current Ask

  • Buy Stop: price above current Ask

  • Sell Limit: price above current Bid

  • Sell Stop: price below current Bid


  • Distance verification formula:
    \[
    d = |p_{\text{order}} - p_{\text{current}}| \geq \text{StopLevel} \times \text{Point}
    \]
    Always validate with `MarketInfo(symbol, MODE_STOPLEVEL)` before sending.

    Error Code Reference for Production
  • 0: Success

  • 129: Invalid price (requote handling required)

  • 130: Invalid stops (distance too close)

  • 136/138: Off quotes/Requote (retry with refreshed rates)

  • 146: Trade context busy (wait and retry)

  • 148: Too many orders (check order count first)

  • 4108: Unknown symbol (validate symbol existence)


  • Reference: MQL4 Documentation - OrderSend (https://docs.mql4.com/trading/ordersend), "Expert Advisor Programming for MetaTrader 4" by Andrew R. Young, 2019.