Glossary
Magic Number in MT5: The ID an Expert Advisor Puts on Its Trades
At a glance
A magic number is an integer ID that an Expert Advisor (EA) attaches to the trade requests it sends in MetaTrader 5. The resulting orders and positions carry that number, so the EA and you can tell its trades from others on the same account. MetaQuotes documents it as an identifier for analysing orders. It is a label, not a lock: it does not stop another program or a manual action from changing a position.
On this page
A magic number is the ID an Expert Advisor writes into every trade request it sends. MetaTrader 5 stores the number with the order and the position, which lets a program find its own trades again.
The MQL5 documentation was checked on 1 October 2026. AI assisted with research and drafting. RoboXpert develops its own trading software, so we have a commercial interest in this subject; see our editorial standards and commercial disclosure.
What the magic number is in MetaTrader 5
MetaQuotes’ name for it is “Expert Advisor ID (magic number)”. The trade request reference describes the field this way: “It allows organizing analytical processing of trade orders. Each Expert Advisor can set its own unique ID when sending a trade request.”
The number is a whole number chosen by the EA’s developer or by you. The platform does not hand it out and does not check that it is unique. The order properties say the ID is “designed to ensure that each Expert Advisor places its own unique number”. Keeping it unique is the user’s job.
Where you see and set the magic number
- In the EA’s inputs. Many EAs offer the ID as an input, often named
MagicorMagicNumber. The name is the developer’s choice. - In the terminal. Since build 975, released on 1 August 2014, the ID “is displayed as a tooltip in the list of open positions and orders, as well as in the trading history”, according to the release notes. Hover over a position to read it.
- In code. With the standard library, one call sets the ID for all requests sent through a trade object, and one property reads it back from a selected position:
trade.SetExpertMagicNumber(InpMagic);
Print("Position magic: ", PositionGetInteger(POSITION_MAGIC));
Both lines come from our read-only script, which compiled with zero errors and zero warnings on 1 October 2026. We did not run it on an account, so we show no output.
Reader resource · ZIP
Read these values on your own account
A read-only MQL5 script that prints the properties named in this glossary, with its compiler record. It sends no order.
Download the read-out scriptWhat a magic number does not do
The ID does not protect a position. An EA that selects positions by symbol alone can modify or close a trade that carries another EA’s number. Our guide to multiple EAs on one MT5 account shows this with a reproducible example.
It also does not divide the account. Equity, margin and the stop out level belong to the account as a whole, whatever IDs the positions carry.
A typical mistake: the same magic number twice
Suppose, as a hypothetical case, you attach one EA to two charts and leave the ID input at its default on both. If the EA finds its trades by ID only, each instance now treats the other’s positions as its own. Give every running instance its own number and write the numbers down next to the symbol and chart they belong to.


