Summary: Advanced deep dive into MQL4 OrderSend function for professional EA development. Covers slippage control algorithms, error code handling, requote bypass, and backtest-realtime consistency.
The `OrderSend()` function is the heart of any MQL4 Expert Advisor, yet most traders only scratch its surface. Understanding its six parameters, return values, and error handling mechanisms directly impacts backtest accuracy and live trading performance.
1. Function Signature And Hidden Parameters
```cpp
int OrderSend(
string symbol, // symbol name
int cmd, // operation type (OP_BUY, OP_SELL, etc.)
double volume, // lot size
double price, // opening price
int slippage, // maximum slippage in points
double stoploss, // stop loss level
double takeprofit, // take profit level
string comment, // order comment
int magic, // EA identifier
datetime expiration, // pending order expiration
color arrow_color // chart arrow color
);
```
Critical insight: The `slippage` parameter is measured in points, not pips. For EURUSD with 5-digit broker, 10 points = 1 pip. Common mistake leads to unrealistic backtest fills.
2. Slippage Control Algorithm For Realtime Trading
```cpp
double GetOptimalEntryPrice(int cmd, int maxSlippagePoints) {
double bid = MarketInfo(Symbol(), MODE_BID);
double ask = MarketInfo(Symbol(), MODE_ASK);
double point = MarketInfo(Symbol(), MODE_POINT);
if(cmd == OP_BUY) {
double requiredAsk = ask;
double maxAllowed = ask + maxSlippagePoints * point;
return requiredAsk;
} else {
double requiredBid = bid;
double maxAllowed = bid - maxSlippagePoints * point;
return requiredBid;
}
}
int SendWithSlippageProtection(int cmd, double volume, int maxPoints) {
double price = (cmd == OP_BUY) ? MarketInfo(Symbol(), MODE_ASK) : MarketInfo(Symbol(), MODE_BID);
int ticket = OrderSend(Symbol(), cmd, volume, price, maxPoints, 0, 0, "EA", magic, 0, clrNONE);
// Requote handling loop
int retries = 0;
while(ticket < 0 && retries < 5 && GetLastError() == 138) { // 138 = requote
RefreshRates();
price = (cmd == OP_BUY) ? MarketInfo(Symbol(), MODE_ASK) : MarketInfo(Symbol(), MODE_BID);
ticket = OrderSend(Symbol(), cmd, volume, price, maxPoints, 0, 0, "EA", magic, 0, clrNONE);
retries++;
Sleep(50);
}
return ticket;
}
```
3. Error Code Handling Matrix
Complete error handling for production EA:
```cpp
bool HandleOrderSendError(int error, int &retryCount) {
switch(error) {
case 0: // no error
return true;
case 138: // requote
retryCount++;
return (retryCount < 5);
case 130: // invalid stops (too close to market)
return false; // need to recalculate SL/TP
case 148: // max orders reached
return false;
case 146: // trading context busy
Sleep(100);
return (retryCount++ < 3);
default:
Print("Unhandled error: ", error);
return false;
}
}
```
4. Backtest Vs Realtime Consistency
MT4 backtester uses `MODE_TRADES` execution model without actual slippage. To simulate real slippage in backtest:
```cpp
int BacktestFriendlyOrderSend(int cmd, double volume, double price, int slippagePoints) {
double point = Point;
double simulatedSlippage = slippagePoints * point;
if(cmd == OP_BUY) {
price += simulatedSlippage; // buy gets worse price in real market
} else {
price -= simulatedSlippage;
}
return OrderSend(Symbol(), cmd, volume, price, 0, 0, 0, "BacktestEA", magic, 0, clrNONE);
}
```
5. Complete Production-Ready OrderSend Wrapper
```cpp
int SafeOrderSend(int cmd, double volume, double sl, double tp, int maxSlippagePoints = 10) {
if(!IsTradeAllowed()) return -1;
RefreshRates();
double price = (cmd == OP_BUY) ? Ask : Bid;
double sl_price = (sl > 0) ? (cmd == OP_BUY ? price - sl * Point : price + sl * Point) : 0;
double tp_price = (tp > 0) ? (cmd == OP_BUY ? price + tp * Point : price - tp * Point) : 0;
int ticket = OrderSend(Symbol(), cmd, volume, price, maxSlippagePoints, sl_price, tp_price, "ProductionEA", magic, 0, clrNONE);
int error = GetLastError();
int retry = 0;
while(ticket < 0 && retry < 5 && (error == 138 || error == 146)) {
Sleep(100);
RefreshRates();
price = (cmd == OP_BUY) ? Ask : Bid;
ticket = OrderSend(Symbol(), cmd, volume, price, maxSlippagePoints, sl_price, tp_price, "ProductionEA", magic, 0, clrNONE);
error = GetLastError();
retry++;
}
if(ticket < 0) Print("OrderSend failed: error ", error);
return ticket;
}
```
Reference: MQL4 Documentation, "OrderSend" (docs.mql4.com/trading/OrderSend); Young, Andrew. "Expert Advisor Programming" (2019).