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 trade | Typical magic number | Typical comment | How to recognise it |
|---|---|---|---|
| Placed by hand in the terminal | 0 | Often empty, or a server tag such as [sl] | Magic zero |
| Placed by an EA | The EA’s own number | The EA name or a tag, if the author set one | Magic number, if you can read it |
| Copied from a signal or copy-trading service | Depends on the service | Often a service tag | Comment tag or the service’s own report |
| Placed by a mobile or web terminal | 0 | Often empty | Magic 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
- Honest attribution. A good month made by one EA and a bad month made by manual overrides look like a mediocre month in the totals. Splitting them shows which part of your process works.
- Override tracking. If you close an EA’s trade by hand, the position keeps the EA’s magic number but the closing comment will not be the EA’s. Counting those cases tells you how often you interfere.
- Several EAs, one account. Without the magic number, the account result is a blend you cannot untangle.
- Fair comparison with a backtest. A backtest tests one EA. To compare it with live results you have to isolate that EA’s live trades.
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
- Two different EAs can be configured with the same magic number, which makes them look like one source. Check the settings of each.
- A magic number is not a security feature. Anything on the account can see it.
- Changing an EA’s magic number partway through a month splits its history into two groups.
- Netting accounts merge deals into one position per symbol, so two EAs on the same symbol of a netting account share a position even if their magic numbers differ. See hedging vs netting.
- Copy-trading services may set their own magic numbers and comments.
Limits of this guide
- Column availability and menu names vary by build and broker.
- The worked example is hypothetical and says nothing about EA performance.
- This page does not recommend any EA or automated approach.
Frequently asked questions
What is a magic number in MT5?
Where can I see the magic number in MT5?
What if my EA writes no comment?
Can two EAs share a magic number?
Does the journal app record the magic number?
Related guides
- A local trading journal for MT5
- P&L calendar for MT5
- Trade notes, ratings and screenshots
- Trading progress and goals
- Back up your journal and move it to a new PC
- An offline trading journal for Windows
- How to set monthly trading goals
- Import your whole MT5 history
- How to tag trades
- Trade screenshots for your journal
- All trading journal comparisons
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