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 Magic or MagicNumber. 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 script

What 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.

Friendly robot illustration representing the RoboXpert pen name.
AI-generated avatar

About the author

RoboXpert

Pen name

The person behind RoboXpert writes about expert advisors, trading evidence and programming, and develops their own trading software. They report 10 years of experience in these areas; this is self-reported, not an independently verified qualification or performance record.

Sources & further reading

Your next question

Continue reading

Expert advisors

Multiple EAs on One MT5 Account: Magic Numbers, Conflicts and Risk

See what happens when two EAs share an account and one of them selects positions without checking the ID.

Read the guide →
Expert advisors

MT5 Hedging vs Netting: See What Happens to Your Positions

On a netting account, separate IDs do not give you separate positions on one symbol.

Read the guide →