MT5 magic number: telling EA and manual trades apart

The magic number is how an Expert Advisor recognises its own orders. See where it shows up and how to split EA results from manual ones.

What a magic number is

A magic number is an integer that a program attaches to the orders it sends, so that it can recognise its own orders later. If you run an Expert Advisor (EA) on MetaTrader 5, the EA stamps a magic number on every order it places. When it next looks at the open positions it asks, “which of these are mine?”, and the magic number is how it answers. Orders you place by hand carry a magic number of zero.

That single number is the cleanest way to answer one of the most useful questions in a review: which of my results came from the robot and which came from me? It also matters when two EAs trade the same symbol on the same account, where only the magic number keeps their positions apart.

Where the number lives

In the MQL5 trading API the magic number is a property of orders, deals and positions, and it flows from the order to the deals it creates and to the position they build. If you read history from code, the MetaTrader 5 Python package exposes a magic field on deals and orders, and MQL5 offers equivalent properties. In the terminal itself, the standard History table does not always show a magic column by default, and what you can add depends on the build and the view. Look at the right-click menu of your History tab for column options and check your own terminal rather than assuming.

When the column is not available, the comment field is the practical fallback. Many EAs write their name or a short tag into the comment. Brokers can rewrite or truncate it, and tags such as [sl] and [tp] are added by the server when a stop or a target closes the position, so the comment is a hint, not a record. See MT5 history comments explained.

Manual, EA and copy trades side by side

Source of the tradeTypical magic numberTypical commentHow to recognise it
Placed by hand in the terminal0Often empty, or a server tag such as [sl]Magic zero
Placed by an EAThe EA’s own numberThe EA name or a tag, if the author set oneMagic number, if you can read it
Copied from a signal or copy-trading serviceDepends on the serviceOften a service tagComment tag or the service’s own report
Placed by a mobile or web terminal0Often emptyMagic zero

The table is a guide, not a rule. EA authors choose their own numbers and comments, and a trader who runs several instances of one EA usually gives each a different magic number.

Why bother separating manual from automated results

A worked example (hypothetical)

A trader runs one EA on gold and also trades by hand. In a month the account closes 62 positions with a net result of +$140. Splitting by magic number shows 48 positions from the EA with a net result of +$310, and 14 manual positions with a net result of -$170. The total of +$140 hid a pattern: the manual trades lost money, and several of them were manual closes of EA trades before their target. The trader does not conclude anything about the future from one month, but the split gives a specific question for the next review: were those early closes planned, or emotional? The numbers are invented and are only an illustration of the method.

A simple way to keep the split in your journal

Whatever the terminal shows, you can add the distinction yourself in the journal. Add a tag such as “EA” or “manual” to each entry, and write the EA name in the notes when it matters. AI Trading Journal reads each closed position from your MetaTrader 5 history in read-only mode and records the net result, profit plus commission plus swap; you add tags, notes, setup, emotion and screenshots, and can filter by tag in the Journal page. It does not read or display the magic number itself, so the tag is your own label. See how to tag trades in a trading journal for a tagging scheme that stays consistent.

Reading the magic number with code

If you are comfortable with Python, the MetaTrader 5 package can list deals for a date range, and each deal record includes the magic number and the position identifier. Grouping by position identifier reunites entries and exits, and filtering on the magic field separates the EA from manual trades. A longer walk-through is in reading MT5 history deals in Python, and the relationship between the identifiers is in MT5 deal, order and position IDs.

Pitfalls

Limits of this guide

Frequently asked questions

What is a magic number in MT5?
An integer that an EA attaches to its orders so it can recognise them. Manual orders carry zero.
Where can I see the magic number in MT5?
It is a property of orders, deals and positions available through the API and Python package. Whether the History table in your terminal can show it depends on the build, so check the right-click column options.
What if my EA writes no comment?
Use the magic number if you can read it, or tag the trades yourself in your journal.
Can two EAs share a magic number?
Technically yes, but then their trades cannot be told apart. Give each a separate number.
Does the journal app record the magic number?
No. It records the net result of each closed position and lets you add your own tags and notes, such as EA or manual.

Related guides

Tag every closed trade EA or manual: free for 7 days

Free for 7 days, no card required. After that, a one-time $19.99 purchase. No subscription. Also on the Microsoft Store.

One-time $19.99, no monthly fee · your MT5 login never leaves your PC · trades go into your own journal on your PC, not through our servers.

Start free 7-day trial