MQL5 OrderSend Guide: MqlTradeRequest, Return Codes & Execution Code
Complete guide to the MQL5 OrderSend function. Learn how to configure MqlTradeRequest and MqlTradeResult structures, manage filling policies, inspect return codes, and send error-free market and pending orders.
What this MQL5 guide covers
OrderSend is the fundamental MetaTrader 5 API function for executing market deals, placing pending orders, and modifying stops.
Always clear request structures with ZeroMemory(request) before assigning fields to prevent garbage data in unused members.
Selecting the correct filling mode (ORDER_FILLING_FOK, IOC, or RETURN) via symbol properties is required to prevent immediate rejection.
Inspect result.retcode to verify execution: TRADE_RETCODE_DONE (10009) confirms a filled deal, while 10008 confirms a placed pending order.
OrderSend Function Signature and Struct Parameters
Unlike MetaTrader 4 where OrderSend accepted a long list of positional arguments, MQL5 uses structured parameters: bool OrderSend(const MqlTradeRequest &request, MqlTradeResult &result).
This architectural change accommodates complex institutional order types, filling modes, and exchange routing models.
| MqlTradeRequest Field | Type | Description |
|---|---|---|
| action | ENUM_TRADE_REQUEST_ACTIONS | Trade action: TRADE_ACTION_DEAL (market), TRADE_ACTION_PENDING, TRADE_ACTION_SLTP |
| magic | ulong | EA identifier stamp assigned to the order and position |
| symbol | string | Financial instrument name (e.g. "EURUSD", "XAUUSD") |
| volume | double | Trade volume in lots (must adhere to volume min, max, and step) |
| price | double | Requested open price (Ask for Buy, Bid for Sell, or pending price) |
| sl / tp | double | Stop loss and take profit absolute price levels |
| deviation | ulong | Maximum allowable slippage in points |
| type | ENUM_ORDER_TYPE | ORDER_TYPE_BUY, ORDER_TYPE_SELL, BUY_LIMIT, SELL_STOP, etc. |
| type_filling | ENUM_ORDER_TYPE_FILLING | Execution filling policy (FOK, IOC, or RETURN) |
Filling Policies: Avoiding Immediate Rejection
A frequent source of OrderSend failures in MQL5 is specifying an unsupported type_filling. Brokers configure allowed filling modes per symbol (SymbolInfoInteger(_Symbol, SYMBOL_FILLING_MODE)).
- ORDER_FILLING_FOK (Fill or Kill): Order must be executed in full immediately, or cancelled.
- ORDER_FILLING_IOC (Immediate or Cancel): Fill available volume immediately; cancel unfilled balance.
- ORDER_FILLING_RETURN: Used on exchanges where unfilled partial volume remains as a pending limit order.
Handling MqlTradeResult Return Codes
A true return value from OrderSend indicates that the request was processed by the trade server, but does not guarantee execution. You must verify result.retcode.
| Retcode Constant | Code | Meaning and Action |
|---|---|---|
| TRADE_RETCODE_DONE | 10009 | Request completed successfully; deal registered in result.deal |
| TRADE_RETCODE_PLACED | 10008 | Pending order placed; order ticket registered in result.order |
| TRADE_RETCODE_REQUOTE | 10004 | Price changed during execution; refresh quote and retry |
| TRADE_RETCODE_INVALID_STOPS | 10016 | Stop loss or take profit violates SYMBOL_TRADE_STOPS_LEVEL |
| TRADE_RETCODE_NO_MONEY | 10019 | Insufficient margin on account to open requested volume |
Implementation details & mechanics
Core implementation pattern
Construct an MqlTradeRequest, call ZeroMemory(request), and populate: action = TRADE_ACTION_DEAL, symbol = _Symbol, volume = InpLotSize, type = ORDER_TYPE_BUY, price = SymbolInfoDouble(_Symbol, SYMBOL_ASK), magic = InpMagicNumber, type_filling = GetSupportedFillingMode(_Symbol). Call OrderSend(request, result) and inspect result.retcode.
Error handling & safety rules
To modify stops via OrderSend: assign action = TRADE_ACTION_SLTP, position = ticket, sl = candidateSL, tp = candidateTP. To close a position: assign action = TRADE_ACTION_DEAL, position = ticket, type = opposite type (SELL for long), volume = position volume, price = SymbolInfoDouble(_Symbol, SYMBOL_BID).
MQL5 Code Implementation Code
//+------------------------------------------------------------------+
//| OrderSendGuide.mq5 |
//| Copyright 2026, Pineify Guides Series |
//| https://pineify.app |
//+------------------------------------------------------------------+
#property copyright "Pineify Guides Series"
#property link "https://pineify.app/mql5/ordersend-guide"
#property version "1.00"
#property script_show_inputs
//--- Input parameters
input double InpLotSize = 0.10; // Lot Size
input int InpStopLossPoints = 200; // Stop Loss in points
input int InpTakeProfitPoints = 400; // Take Profit in points
input ulong InpSlippagePoints = 20; // Slippage in points
input ulong InpMagicNumber = 770011; // EA Magic Number
//+------------------------------------------------------------------+
//| Helper: Determine supported filling mode for current symbol |
//+------------------------------------------------------------------+
ENUM_ORDER_TYPE_FILLING GetSymbolFillingMode(const string symbol)
{
uint filling = (uint)SymbolInfoInteger(symbol, SYMBOL_FILLING_MODE);
if((filling & SYMBOL_FILLING_FOK) != 0)
return ORDER_FILLING_FOK;
if((filling & SYMBOL_FILLING_IOC) != 0)
return ORDER_FILLING_IOC;
return ORDER_FILLING_RETURN;
}
//+------------------------------------------------------------------+
//| Helper: Send Market Buy Order with Low-Level OrderSend |
//+------------------------------------------------------------------+
bool SendMarketBuy(double lots, int slPoints, int tpPoints)
{
MqlTradeRequest request;
MqlTradeResult result;
// 1. Always zero out request struct to avoid garbage values
ZeroMemory(request);
ZeroMemory(result);
double point = SymbolInfoDouble(_Symbol, SYMBOL_POINT);
int digits = (int)SymbolInfoInteger(_Symbol, SYMBOL_DIGITS);
double ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
// 2. Populate trade request
request.action = TRADE_ACTION_DEAL;
request.symbol = _Symbol;
request.volume = lots;
request.type = ORDER_TYPE_BUY;
request.price = ask;
request.deviation = InpSlippagePoints;
request.magic = InpMagicNumber;
request.type_filling = GetSymbolFillingMode(_Symbol);
request.comment = "Pineify OrderSend";
if(slPoints > 0)
request.sl = NormalizeDouble(ask - (slPoints * point), digits);
if(tpPoints > 0)
request.tp = NormalizeDouble(ask + (tpPoints * point), digits);
// 3. Send trade request to server
if(!OrderSend(request, result))
{
PrintFormat("OrderSend critically failed! Retcode: %u, Error: %d",
result.retcode, GetLastError());
return false;
}
// 4. Verify execution retcode
if(result.retcode == TRADE_RETCODE_DONE || result.retcode == TRADE_RETCODE_PLACED)
{
PrintFormat("OrderSend SUCCESS! Order Ticket: %I64u, Deal: %I64u, Price: %.*f, Vol: %.2f",
result.order, result.deal, digits, result.price, result.volume);
return true;
}
PrintFormat("OrderSend rejected by broker! Retcode: %u (%s)",
result.retcode, result.comment);
return false;
}
//+------------------------------------------------------------------+
//| Script program start function |
//+------------------------------------------------------------------+
void OnStart()
{
Print("Initiating OrderSend execution test on ", _Symbol);
SendMarketBuy(InpLotSize, InpStopLossPoints, InpTakeProfitPoints);
}
Copy this code into MetaEditor, save it in your MQL5 folder (Experts, Scripts, or Include), and compile with F7.
Generate custom MQL5 code with Pineify AI
Describe the trade execution, indicator calculations, risk management, or data processing logic you need. Pineify produces clean, typed MQL5 source code ready to compile in MetaEditor.
Frequently Asked Questions
Related MQL5 pages
Technical references
Risk and testing note
Past performance is not indicative of future results. Backtest statistics are based on historical data and do not guarantee future profits. Trading involves significant risk of loss. This content is for educational purposes only and does not constitute financial advice.