Blog / Code & convert EAs · MQL5 · Expert Advisors
Orders vs Positions vs Deals in MT5
Understand the difference between orders, deals and positions in MetaTrader 5. Learn how to retrieve and track each using MQL5.
· 4 min read
In MT5, orders, deals and positions are three different trading objects. Confusing them can cause an Expert Advisor to misread trade history, overlook pending orders or manage the wrong position.
The distinction is simple:
- Order: An instruction to buy or sell.
- Deal: A record of an executed trade.
- Position: The resulting open market exposure.
Here's how each works and how to access it in MQL5.
1. Orders: Trading Instructions
An order tells the broker what you want to do.
There are two main categories:
- Market orders: Requests to buy or sell at the available market price.
- Pending orders: Instructions to execute when specified price conditions are met.
For example, a Buy Stop order for 0.10 lots of EURUSD at 1.10500 is an instruction to buy when the specified conditions are met. It is not yet an executed trade.
To retrieve active orders, use OrdersTotal() and OrderGetTicket():
void CheckActiveOrders()
{
for(int i = 0; i < OrdersTotal(); i++)
{
ulong ticket = OrderGetTicket(i);
if(ticket == 0)
continue;
string symbol = OrderGetString(ORDER_SYMBOL);
double volume = OrderGetDouble(
ORDER_VOLUME_INITIAL
);
ENUM_ORDER_TYPE type =
(ENUM_ORDER_TYPE)OrderGetInteger(ORDER_TYPE);
PrintFormat(
"Order #%I64u | %s | Type: %s | Volume: %.2f",
ticket,
symbol,
EnumToString(type),
volume
);
}
}OrdersTotal() counts current active orders. It does not count open positions or historical orders.
An executed market order normally leaves the active-order list and its execution is recorded in deal history.
2. Deals: Actual Trade Executions
A deal records an executed trading operation.
For example, when a broker executes your EA's Buy request, MT5 records the execution as a deal.
One order can produce multiple deals. This can happen when an order is filled in separate parts.
Deals are important when calculating actual execution prices, traded volume and commissions.
To retrieve historical deals, select a history period using HistorySelect():
void CheckDealHistory()
{
datetime from = TimeCurrent() - 86400;
datetime to = TimeCurrent();
if(!HistorySelect(from, to))
{
Print("Failed to select deal history");
return;
}
for(int i = 0; i < HistoryDealsTotal(); i++)
{
ulong ticket = HistoryDealGetTicket(i);
if(ticket == 0)
continue;
string symbol = HistoryDealGetString(
ticket,
DEAL_SYMBOL
);
double volume = HistoryDealGetDouble(
ticket,
DEAL_VOLUME
);
double price = HistoryDealGetDouble(
ticket,
DEAL_PRICE
);
ENUM_DEAL_TYPE type =
(ENUM_DEAL_TYPE)HistoryDealGetInteger(
ticket,
DEAL_TYPE
);
PrintFormat(
"Deal #%I64u | %s | Type: %s | Volume: %.2f | Price: %.5f",
ticket,
symbol,
EnumToString(type),
volume,
price
);
}
}This example retrieves deals from the previous 24 hours.
Deal history can also contain balance operations, commissions and other transaction types. If your EA only needs Buy and Sell executions, filter the records using DEAL_TYPE.
3. Positions: Current Market Exposure
A position represents the market exposure resulting from executed deals.
For example:
- Your EA submits a Buy order for 0.10 lots.
- The broker executes it.
- MT5 records the execution as a deal.
- The account now has the resulting Buy exposure.
A position remains open until it is reduced, closed or reversed.
To retrieve open positions, use PositionsTotal() and PositionGetTicket():
void CheckOpenPositions()
{
for(int i = 0; i < PositionsTotal(); i++)
{
ulong ticket = PositionGetTicket(i);
if(ticket == 0)
continue;
string symbol = PositionGetString(POSITION_SYMBOL);
double volume = PositionGetDouble(
POSITION_VOLUME
);
double openPrice = PositionGetDouble(
POSITION_PRICE_OPEN
);
ENUM_POSITION_TYPE type =
(ENUM_POSITION_TYPE)PositionGetInteger(
POSITION_TYPE
);
PrintFormat(
"Position #%I64u | %s | Type: %s | Volume: %.2f | Open: %.5f",
ticket,
symbol,
EnumToString(type),
volume,
openPrice
);
}
}PositionGetTicket() automatically selects the position at the specified index, allowing you to retrieve its properties.
If you need to work with a specific position later, use PositionSelectByTicket() before reading its properties to refresh the selection.
4. Why Netting and Hedging Matter
The difference between orders, deals and positions becomes especially important when an account receives multiple trades on the same symbol.
MT5 supports two main position-accounting systems, covered in detail in Hedging vs Netting Accounts.
Netting accounts
A netting account maintains one position per symbol.
For example:
| Operation | Resulting position |
|---|---|
| Buy 0.10 lots | Buy 0.10 |
| Buy 0.20 lots | Buy 0.30 |
| Sell 0.15 lots | Buy 0.15 |
The Sell deal reduces the existing Buy exposure instead of opening a separate Sell position.
Hedging accounts
A hedging account can maintain multiple independent positions on the same symbol.
For example:
| Operation | Result |
|---|---|
| Buy 0.10 lots | Buy position 0.10 |
| Buy 0.20 lots | Separate Buy position 0.20 |
| Sell 0.15 lots | Separate Sell position 0.15 |
This means an EA must account for the account's position mode before implementing position selection, closing or risk-management logic.
You can check the account mode with:
void CheckAccountMode()
{
ENUM_ACCOUNT_MARGIN_MODE mode =
(ENUM_ACCOUNT_MARGIN_MODE)AccountInfoInteger(
ACCOUNT_MARGIN_MODE
);
if(mode == ACCOUNT_MARGIN_MODE_RETAIL_HEDGING)
Print("Hedging account");
else if(mode == ACCOUNT_MARGIN_MODE_RETAIL_NETTING)
Print("Netting account");
else if(mode == ACCOUNT_MARGIN_MODE_EXCHANGE)
Print("Exchange account (netting)");
}5. Track Which Order Produced a Deal
When debugging an EA, you may need to identify which order produced a particular deal.
MT5 provides properties for tracing these relationships:
| Property | What it identifies |
|---|---|
ORDER_TICKET |
Order ticket |
DEAL_TICKET |
Deal ticket |
DEAL_ORDER |
Order associated with a deal |
DEAL_POSITION_ID |
Position identifier associated with a deal |
POSITION_TICKET |
Current position ticket |
POSITION_IDENTIFIER |
Stable identifier for the position's lifetime |
For example, you can retrieve the order ticket associated with a historical deal:
void InspectDeal(ulong dealTicket)
{
if(!HistoryDealSelect(dealTicket))
{
Print("Deal not found in history");
return;
}
ulong orderTicket =
(ulong)HistoryDealGetInteger(
dealTicket,
DEAL_ORDER
);
ulong positionID =
(ulong)HistoryDealGetInteger(
dealTicket,
DEAL_POSITION_ID
);
PrintFormat(
"Deal #%I64u | Order #%I64u | Position ID: %I64u",
dealTicket,
orderTicket,
positionID
);
}One important distinction: POSITION_TICKET and POSITION_IDENTIFIER are not interchangeable. The ticket is used to select the current position, while the identifier links that position to its associated trading history. The ticket can change during certain server operations, but the identifier remains stable throughout the position's lifetime.
6. Monitor Trade Changes with OnTradeTransaction()
If your EA needs to respond to new deals or trade-request events, use OnTradeTransaction().
For example:
void OnTradeTransaction(
const MqlTradeTransaction &trans,
const MqlTradeRequest &request,
const MqlTradeResult &result
)
{
if(trans.type == TRADE_TRANSACTION_DEAL_ADD)
{
PrintFormat(
"New deal: #%I64u | Order: #%I64u | Symbol: %s",
trans.deal,
trans.order,
trans.symbol
);
}
if(trans.type == TRADE_TRANSACTION_REQUEST)
{
PrintFormat(
"Trade request processed | Retcode: %u",
result.retcode
);
}
}The first condition detects when a new deal is added to the account history. The second handles trade-request processing and reports the server's return code.
A single trading operation can generate multiple transaction events as orders, deals and positions change. Keep this handler efficient because MT5 processes trade transactions through a queue.
Conclusion
Orders, deals and positions represent different parts of the MT5 trading process:
- Orders are trading instructions.
- Deals record executions.
- Positions represent current market exposure.
Use the appropriate MQL5 functions for each:
- Active orders:
OrdersTotal()andOrderGetTicket(). - Historical deals:
HistoryDealsTotal()andHistoryDealGetTicket(). - Open positions:
PositionsTotal()andPositionGetTicket().
Understanding these differences helps prevent common EA errors when tracking executions, reading trade history and managing open positions.
Rather not track orders, deals and positions by hand? In PositionAlpha's Bot Builder you describe when the strategy buys or sells and how it gets out, and the AI writes the MT5 Expert Advisor with every rule explained. Compile it and test it in the Strategy Tester and on a demo account before you rely on it.
Build a bot from your idea
Describe your trading idea and the AI writes MT4 or MT5 code for you to review and test, with a plain-English explanation and setup steps. Your first bot is free.