MT5 Netting vs Hedging Accounts for EA Developers
MT5 netting vs hedging is a choice the broker makes for each account, not one the EA makes: a netting account holds at most one position per symbol, while a hedging account holds as many independent positions as the EA opens. An EA that assumes the wrong mode adds to a position it meant to open fresh, or loses track of its own tickets, so it should read the mode at start-up and either handle both or refuse to run.
QuantFX7 min read
Position vs order in MT5
Both modes share three objects. An order is a request: buy or sell at market, or a pending order waiting for a price. A deal is the execution the server records when an order fills, with its own ticket, price, volume and costs. A position is the exposure the deals leave open. OrdersTotal() counts pending orders only and PositionsTotal() counts open positions. Every deal carries the identifier of the position it belongs to, DEAL_POSITION_ID, which is how an EA ties executions back to exposure. The two modes differ only in how deals combine into positions.
How the two account modes differ
Netting: one position per symbol
On a netting account (ACCOUNT_MARGIN_MODE_RETAIL_NETTING, or ACCOUNT_MARGIN_MODE_EXCHANGE for exchange-traded instruments) every deal on a symbol changes the same position. A buy against an open long adds volume and moves the position price to the volume-weighted average. A sell reduces the position, closes it or, when it is larger, reverses it. The position has one SL, one TP and one magic number, so two strategies on the same symbol share a single position and cannot tell their exposure apart.
Hedging: one position per entry
On an MT5 hedging account (ACCOUNT_MARGIN_MODE_RETAIL_HEDGING) every entry opens a separate position with its own ticket, SL and TP, and long and short positions on one symbol can be open together. This is how MT4 works, which is why many FX and CFD brokers default to it. Closing needs the position ticket: an opposite deal without one opens a new position instead. Where the symbol allows it, a close-by request (TRADE_ACTION_CLOSE_BY) closes one position against an opposite one.
Partial closes
On a hedging account, a partial close is an opposite deal for part of the volume that names the position ticket, which CTrade::PositionClosePartial(ticket, volume) builds for you. The position keeps its identifier and carries on with the remaining volume; in MT4 the remainder reappeared under a new ticket. On a netting account that ticket overload is for hedging mode only and returns false: PositionClosePartial(symbol, volume), or an opposite deal for that volume, reduces the symbol's one position. In both modes the closed volume must be a multiple of SYMBOL_VOLUME_STEP and at least SYMBOL_VOLUME_MIN, so a rule such as "close a third" needs rounding, and on a small position the rounding can leave nothing to close. The specification should say whether the EA then skips the step or closes the minimum.
Reversals
Only netting accounts reverse. If the EA is long 1.00 lot and sells 1.50, the server executes one deal with entry type DEAL_ENTRY_INOUT: it closes the long, realises its P&L and leaves a short of 0.50. The position identifier stays the same, but the position ticket changes to the ticket of the order that reversed it, so an EA that tracks its trade by ticket loses it at exactly that moment. Track positions by POSITION_IDENTIFIER instead. On a hedging account the same sell opens a separate 1.50 short next to the long: the net exposure is identical, but there are two positions, two sets of stops and two swap charges.
Brokers that offer only one mode
The mode belongs to the account and is set by the broker when the account is opened; no EA can change it. Some brokers offer only hedging accounts for FX and CFDs, exchange-traded instruments such as futures generally trade on netting, and some account types refuse opposite positions on one symbol altogether. The strategy tester runs in the mode of the account the terminal is logged into, so testing both behaviours needs one login of each type, usually two demo accounts. Ask the broker which mode the live account uses before the EA is written.
MT5 netting vs hedging in code: detect the mode, then stay neutral
Read the mode once in OnInit() with AccountInfoInteger(ACCOUNT_MARGIN_MODE). If the EA was not built for that mode, stop it with INIT_FAILED and a clear log line. Then keep the rest of the code mode-neutral:
- Find the EA's exposure by looping over
PositionsTotal()withPositionGetTicket()and filtering on symbol and magic number.PositionSelect(symbol)is a netting habit: on a hedging account it selects only one of possibly several positions. - Close and modify by position ticket, which works in both modes.
- Before a new entry on a netting account, check for an open position: the order will add to it, average its price and leave one set of stops for the combined volume.
- Key the EA's state on
POSITION_IDENTIFIERand update it fromOnTradeTransaction(), where each new deal's entry type (in, out, reversal or close-by) says what changed. - On netting, give each strategy its own symbol or its own account, because magic numbers cannot split one position.
input ulong InpMagic = 1001;
int OnInit()
{
const ENUM_ACCOUNT_MARGIN_MODE mode =
(ENUM_ACCOUNT_MARGIN_MODE)AccountInfoInteger(ACCOUNT_MARGIN_MODE);
if(mode != ACCOUNT_MARGIN_MODE_RETAIL_HEDGING)
{
PrintFormat("Built for hedging accounts only; this account is %s", EnumToString(mode));
return(INIT_FAILED);
}
return(INIT_SUCCEEDED);
}
// Open volume of this EA's positions of one type on the chart symbol.
// Works in both modes: on netting the loop finds at most one position.
double OwnVolume(const ENUM_POSITION_TYPE type)
{
double volume = 0.0;
for(int i = PositionsTotal() - 1; i >= 0; i--)
{
const ulong ticket = PositionGetTicket(i); // also selects the position
if(ticket == 0)
continue;
if(PositionGetString(POSITION_SYMBOL) != _Symbol)
continue;
if((ulong)PositionGetInteger(POSITION_MAGIC) != InpMagic)
continue;
if((ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE) == type)
volume += PositionGetDouble(POSITION_VOLUME);
}
return(volume);
}EURUSD with made-up prices. The EA buys 1.00 lot at 1.08500, a second entry rule buys 0.50 at 1.08200, and the exit rule then closes the first entry at 1.08700.
Hedging: two positions. Closing the first realises 20 pips on 1.00 lot. The 0.50 lot position stays open from 1.08200 with its own stops, 50 pips up.
Netting: one position of 1.50 lots at the average price, (1.00 × 1.08500 + 0.50 × 1.08200) / 1.50 = 1.08400. Closing 1.00 lot at 1.08700 realises 30 pips against that average, and the remaining 0.50 lot keeps the 1.08400 price, 30 pips up.
Measured in pips times lots, the total is 45 in both cases, but it splits differently: 20 closed and 25 floating on hedging, 30 and 15 on netting. A daily loss rule that reads closed deals sees different numbers on the two accounts, and so does any report that compares them trade by trade.
Prices are invented to show the arithmetic; they describe no real trade.
What the two modes mean for a trade copier
A copier that mirrors a master account into other MT5 accounts meets every case above, often at once, because the master and each connected account can run in different modes.
- Master on hedging, connected account on netting: a hedge on the master, long and short together, is flat on the connected account, and closing one leg on the master opens exposure there.
- Master on netting, connected account on hedging: one reversal deal on the master becomes a close plus a new position on the connected account.
- Partial closes are scaled per account and rounded to the volume step, so a small account can have nothing to close; the copier logs that decision instead of dropping it silently.
- Tickets break on reversals, so the copier maps each master position identifier to the matching positions on every connected account.
Our MT5 trade copier handles both modes and logs every copied order with the master price, the client fill and the delay. It is built for companies that hold their own permissions: the client runs the service under its own brand, QuantFX supplies and operates the technology, and each connected account stays at its owner's broker, in the owner's name.
In practice at QuantFX
In the written specification we agree which account modes an EA supports and what it does on any other. Our MQL5 developers write the order layer against MT5 positions from the start, test it on real ticks in the strategy tester and forward-test it on a demo account. See custom MT5 Expert Advisor development for new builds, fixed-price from £6,000, or MT4 to MT5 EA conversion for existing code; our note on what changes when you convert an MT4 EA to MT5 covers the rest of that rewrite.
Questions about netting and hedging
Can I switch an MT5 account from netting to hedging?
Not from the terminal or from an EA. The mode belongs to the account and is set by the broker. If your broker offers both, the usual route is a second account of the other type.
How does an EA know which mode it is running on?
It reads AccountInfoInteger(ACCOUNT_MARGIN_MODE) in OnInit(). A hedging account reports ACCOUNT_MARGIN_MODE_RETAIL_HEDGING; a netting account reports ACCOUNT_MARGIN_MODE_RETAIL_NETTING or ACCOUNT_MARGIN_MODE_EXCHANGE.
Does a grid or hedge EA need an MT5 hedging account?
Yes. A grid holds several positions on one symbol and a hedge holds opposite positions at once. On a netting account both collapse into one averaged position, so the EA does not behave as designed.
Can several EAs trade the same symbol on one account?
On a hedging account, yes: each EA finds its own positions by magic number. On a netting account they share one position per symbol and interfere, so give each its own symbol or account.
Does the strategy tester simulate both modes?
It runs in the mode of the account the terminal is logged into. To test both, log in to a netting and a hedging account in turn, usually two demo accounts at the same broker.
Trading leveraged products such as CFDs and FX carries a high risk of loss. This article explains MetaTrader software; it says nothing about whether any strategy should be traded.