Your EA Sent a Sell. MT5 Closed the Buy Instead.

Your EA Sent a Sell. MT5 Closed the Buy Instead.

17 November 2026, 03:59
Lee Kai Lun
0
11

Picture an EA with a perfectly reasonable instruction: “Open a Sell.”

It sends the order. The trade server accepts it. No dramatic error code. No red warning. Yet the trader opens the MT5 Trade tab and discovers that the existing Buy has shrunk instead of a new Sell appearing.

The EA didn't necessarily lose its mind. It may have walked into a different position-accounting system.

Two accounts, one instruction, two outcomes

MT5 supports netting and hedging position accounting. The distinction matters far more to an EA than the account's color scheme or the number of monitors on your desk.

On a netting account, only one open position can exist per symbol. An executed trade in the same direction increases that position; an opposite trade reduces, closes, or reverses it.

On a hedging account, multiple positions can exist on the same symbol. An opposite-direction trade can open a separate position, subject to the account's trading permissions and the order's actual execution.

Neither arrangement is automatically better. But code written for one can behave very differently on the other.

The disappearing Sell: a hypothetical example

Assume an EA starts with an executed 0.30-lot XAUUSD Buy and later sends an executed 0.10-lot Sell.

On a netting account, the resulting open position is a 0.20-lot Buy. The Sell deal still exists in trading history, but it does not create a second open position.

On a hedging account, the trader may instead see Buy 0.30 and Sell 0.10 as two separate open positions.

Now imagine the Sell is 0.50 lots rather than 0.10.

On a netting account, that would reverse the exposure to a 0.20-lot Sell, assuming the entire order executes as described. On a hedging account, the Buy and new Sell can coexist.

Same order direction. Same nominal volume. Different position inventory.

That's not a small bookkeeping detail if your EA expects to close Entry 3 by its own ticket.

The magic-number illusion

A magic number helps identify trading activity associated with an EA or strategy. It does not carve the broker's netting account into private mini-accounts.

Suppose EA A and EA B both trade XAUUSD, each with a different magic number. On a netting account, their executed deals still affect the same symbol-level position.

An EA that reads only the current position's magic number and assumes it represents all historical contributors can make a dangerous attribution mistake. Orders and deals provide additional history, but reconstructing strategy ownership is a design problem, not a free feature supplied by the magic number.

On a hedging account, separate position tickets make per-entry management possible, although developers still need correct ticket selection and ownership checks.

Check the mode before the first order

MT5 exposes the account's mode directly. An EA can check it during initialization rather than waiting for the first confusing position.

int OnInit() { ENUM_ACCOUNT_MARGIN_MODE mode = (ENUM_ACCOUNT_MARGIN_MODE)AccountInfoInteger(ACCOUNT_MARGIN_MODE); if(mode == ACCOUNT_MARGIN_MODE_RETAIL_HEDGING) Print("Position accounting: hedging"); else if(mode == ACCOUNT_MARGIN_MODE_RETAIL_NETTING) Print("Position accounting: retail netting"); else if(mode == ACCOUNT_MARGIN_MODE_EXCHANGE) Print("Position accounting: exchange (net position)"); else Print("Unrecognized account margin mode"); return INIT_SUCCEEDED; }

This is a diagnostic example, not a complete compatibility gate. A production EA that requires separately managed positions should explicitly reject unsupported modes or implement and test a different position-management path.

Also distinguish ACCOUNT_MARGIN_MODE from ACCOUNT_TRADE_MODE . The latter tells you whether the account is demo, contest, or real—not whether its positions are hedged or netted.

Five things to test before blaming the strategy

  1. Account mode: Read ACCOUNT_MARGIN_MODE and document which values your EA supports.
  2. Opposite orders: On a demo account, observe what a smaller, equal, and larger opposite trade does to an existing position.
  3. Position selection: Verify whether the EA selects by symbol, ticket, or both. On hedging accounts, symbol-only selection can be ambiguous when several positions exist.
  4. Other strategies: Check whether manual trades or another EA touch the same symbol, especially on netting accounts.
  5. Exit behavior: Test stop-loss, take-profit, partial close, and basket logic in the actual intended accounting mode. Do not assume position-level exits transfer unchanged between modes.

For every test, inspect orders, deals, and positions separately. An order is an instruction; a deal records execution; a position describes current exposure. Mixing these three concepts is an excellent way to spend a weekend debugging the wrong thing.

Hedging is not a magic risk eraser

Holding Buy and Sell positions simultaneously does not make spreads, commissions, financing costs, or margin requirements disappear. Hedged-margin treatment depends on the symbol and broker settings.

Likewise, a netting account's single visible position does not mean the underlying strategy is simple. Multiple fills can contribute to one net exposure, and an EA must still understand how it got there.

The useful question is not “Which mode wins?” It is “Which mode did this EA actually design for—and did I test that assumption?”

Before changing indicators, optimization settings, or the EA itself, inspect the account mode. Sometimes the Sell did exactly what MT5 was instructed to do. It was the developer's mental model that needed an update.