Category
Platform
Difficulty
Beginner
Used in
MT5 operationEA evaluation

Magic number

An integer an Expert Advisor stamps on every order it sends, so it can recognise its own positions and leave everything else on the account alone.

also: Magic, EA identifier, MagicNumber

Updated · Reviewed

In plain English

A name tag on every trade. The terminal does not care who opened a position, so each EA writes its own number on the ticket and then only touches tickets carrying that number.

Why it matters

MetaTrader gives every EA on an account the same view of every position. Without a tag, an EA cannot tell its own trades from yours or from another EA's — and an EA that manages the wrong position is a bug that costs money rather than one that throws an error.

  • It is what makes running several EAs on one account possible at all.
  • It protects manual trades: those carry magic 0, so a correctly written EA never touches them.
  • It is how a trade history can be split per strategy after the fact — the tag survives into the deal history.

In MetaTrader 5

Where it appears in MT5

  • Chart → EA properties (F7) → Inputs — the EA's own MagicNumber input
  • Strategy Tester → Inputs — the same input when testing
  • MQL5 code: MqlTradeRequest.magic when sending, POSITION_MAGIC / ORDER_MAGIC / DEAL_MAGIC when reading back

How EAs use it

  • Set once at startup — CTrade::SetExpertMagicNumber(MagicNumber) — after which every order the EA sends carries the tag.
  • Filter on every pass: the position loop skips any ticket whose POSITION_MAGIC is not its own before it modifies, trails or closes anything.
  • Filter the history the same way, so per-EA statistics are computed from that EA's deals only.
  • Every EA generated by the mt5depot Builder ships a MagicNumber input with a value derived from the strategy, so two different generated EAs do not collide out of the box.

Typical settings

Setting Typical value Note
Manual trades 0 The terminal's default. An EA should never manage magic 0.
One EA, one symbol any unique integer Anything memorable works — 20260807, 1001.
Same EA on several symbols one value per chart Otherwise the EURUSD instance closes the GBPUSD instance's positions.
Portfolio EA running several strategies one value per leg Each leg needs its own tag to be trailed and closed independently.

Common operational problems

  • Two instances left on the same magic: each sees the other's positions as its own and closes them at the wrong moment.
  • Changing the magic while positions are open orphans them — the EA no longer recognises trades it opened minutes earlier, and nothing will ever close them automatically.
  • Leaving the magic at 0 puts the EA in charge of your manual trades.
  • Re-attaching an EA after a terminal restart with the default value instead of the one you configured produces the same orphaning, quietly.

Related MT5 functions

CTrade::SetExpertMagicNumber()
Sets the tag every subsequent order from this instance carries.
PositionGetInteger(POSITION_MAGIC)
Reads the tag off an open position.
OrderGetInteger(ORDER_MAGIC)
Reads it off a pending order.
HistoryDealGetInteger(ticket, DEAL_MAGIC)
Reads it off a closed deal, which is how per-EA history is separated.

Example

Three EAs on one account. Each stamps its own number, so each position loop touches only its own rows even though all of them see all six positions.

One terminal, one account, three owners — the tag is the only thing separating them.
Trend EA on EURUSD
20260101
Mean-reversion EA on USDJPY
20260102
Your manual trades
0
No EA claims this tag, so nothing automated will close them.

Calculation if (PositionGetInteger(POSITION_MAGIC) != MagicNumber) continue;

Result Each EA manages only the positions it opened

Illustrative figures — not the record of any listed EA.

How it is used

Treat the magic number as account configuration, not as a strategy setting: write it down before the EA goes live, and never change it while positions are open.

  • Assign a value per running instance, not per EA product — the same EA on two charts needs two values.
  • Keep a list. Once four or five instances are running, an unrecorded tag is indistinguishable from a duplicate one.
  • Never reuse a tag that still has open positions attached to it, even from an EA you have removed.
  • Check it after every terminal reinstall or profile restore, which is where defaults come back.

The value itself has no meaning to MetaTrader — any integer works. All that matters is that it is unique among the things running on that account.

Common mistakes

Thinking the magic number affects execution

It changes nothing about how an order fills. It is metadata: the broker does not act on it and it has no effect on price, spread or slippage.

Using one magic number for one EA across many charts

The identity that matters is the running instance, not the product. Two charts sharing a tag means two instances managing one shared pool of positions.

Changing it to 'reset' a misbehaving EA

The open positions keep the old tag. Changing the input abandons them: the EA stops recognising them, and they stay open until closed by hand or by a stop out.

Assuming a magic number separates the account statement

The terminal's Trade tab does not show it. Separating results per EA requires reading DEAL_MAGIC from the history, which is what reporting tools do — not something the default statement gives you.

In depth

What the tag does not buy you, counted on this shelf

Across the 14 EAs published hereFigure
Listings with a MagicNumber input10
Listings needing it changed during setup0
Listings telling you to run them where nothing else trades that symbol8
Of those, listings that carry a MagicNumber input anyway6
Symbols claimed by more than one listing7 of 15
EA pairs that cannot share one account15 of 91

Read rows three and four together. Tagging orders is one job; deciding whether a new signal is allowed is another, and an EA that caps itself at one open position has to count first. Count with PositionsTotal() and a symbol check and a foreign position lands in the total; filter on POSITION_MAGIC first and it does not. Six listings here carry the input and still print the warning, which tells you which of the two their position count does.

So the pairing question is a symbol question, and it is answerable before you buy. USD/JPY is claimed by four listings at once — Iridescence, Kestrel Hover, Lanternfish and Tessera — so no two of them belong together, and EUR/GBP now carries two, Ballast and Thunderhead. Only Orrery, Peregrine and Zerqon overlap nobody. Where two do overlap, separate accounts fix what a tag cannot: each EA then counts a pool that contains only its own trades. Our methodology records every published run per symbol, which is the same distinction seen from the measurement side.

Frequently asked questions

What magic number should I use?
Any integer that is not already in use on that account, and not 0 — that value belongs to manual trades. A date-like value such as 20260807 is easy to keep track of when several instances are running.
Can two EAs share the same magic number?
Technically yes, and it is a common cause of hard-to-diagnose losses: each instance treats the other's positions as its own and may close or trail them at the wrong moment. Give every running instance its own value.
Does the magic number matter in the Strategy Tester?
Not for a single-EA test, because nothing else is trading in the test account. It matters as soon as more than one EA runs on the same live or demo account.