What Is an EA Magic Number? Why Two EAs Need Different Ones
EABuild Team · · 11 min read

An EA magic number is a whole number that an expert advisor attaches to every order it sends, so it can later recognise which trades belong to it. This post explains what the number is, how MetaTrader uses it, what goes wrong when two EAs share one, and where to look for it in MQ4 source code.
What an EA magic number does inside MetaTrader
Every order and position in MetaTrader carries a few fields: the symbol, the lot size, the open price, the comment and the magic number. The first four are visible in the Trade tab. The magic number is stored with the order too, but the terminal does not show it in the default view.
The number has no meaning to the platform. It is simply a tag that the EA chooses when it places the order. Later, the same EA reads the tag back to decide whether an open trade is one of its own.
Think of it as a name written on a coat hanger in a shared cloakroom. The cloakroom does not care what the name is. It only matters that you can find your own coat and do not pick up someone else's.
Orders that are placed by hand
A trade you open manually from the terminal has a magic number of 0. An EA that is set to use 0 would therefore treat your manual trades as its own. This is one reason EA authors ask you to pick a non-zero value.
Magic number and comment are not the same thing
The order comment is free text, and a broker can change or shorten it. The magic number is a number, and it stays with the order. For that reason, most EAs use the magic number as the main identifier and treat the comment as an extra note.
How the EA uses the number while it trades
An EA does not keep a private list of its tickets. On each tick or each new bar, it asks the terminal for the orders that are open, then filters them. In MQL4 this usually means a loop that selects each order, then checks the symbol and the magic number before doing anything.
If both match, the EA treats the order as its own. It may then modify the stop loss, close it, count it against a limit or add the next level of a grid. If either does not match, a well-written EA skips the order.
Typical jobs that depend on this filter include:
- counting how many trades the EA has open, so it respects its own maximum;
- calculating the combined profit of its own basket before closing it;
- moving a trailing stop only on its own positions;
- closing its own trades at the end of a session or before news.
Because all of these steps lean on the same filter, the number must be unique to the EA that is running on the account.
MT4 and MT5 differences in plain terms
In MetaTrader 4 the magic number is an integer passed to the order function when the trade is sent, and read back with a function on the selected order. In MetaTrader 5 the same idea applies, but the platform separates orders, deals and positions, and the number is read from the position or deal. The MQL documentation lists the exact functions for each platform.
The idea does not change between platforms: a label is set when the trade is sent, and read back when the EA manages it.
What goes wrong when two EAs share a magic number
Suppose you run two EAs on one account, and both use the number 12345. Each one looks at the open orders, sees trades with the right number, and assumes they are its own. The result depends on the logic, but the problems tend to look alike.
Counting and limits break
An EA that allows, say, five open trades will count the other EA's trades as well. It may stop opening orders because it thinks the limit is reached, or it may report a basket size that is wrong.
One EA closes or edits the other's trades
If an EA closes all its orders when a profit target or a time rule is met, it can close the other EA's orders in the same sweep. A trailing stop can also be moved on a trade that belongs to a different strategy, with settings that were never meant for it.
Grid and basket logic gets confused
A grid strategy opens extra orders at set price steps, and it often measures the whole group together. When two grids share a number on the same symbol, each one sees the other's orders as part of its own grid. Exposure and drawdown (the drop from an account's peak balance) then become hard to read, and the reasons for a bad outcome are hard to trace afterwards.
Same EA on several charts
The same issue appears when you attach one EA to two charts of the same symbol with identical inputs. Because the symbol and the magic number match, the two copies can interfere. If you want that setup, give each copy its own number.
Giving each EA a different number is the simple fix. Orders on different symbols are separated by the symbol check in many EAs, but you should not depend on that alone.
How to choose and set a magic number for each EA
Most EAs expose the number as an input, often named something like MagicNumber. You can see and change it in the inputs window when you attach the EA to a chart. If you are not sure what an input does, the post on EA input parameters in MQL5 explains how inputs are declared and shown.
- Make a short list of every EA that will run on the account, including any you may add later.
- Give each EA its own number, and write the pairing down in a note or a spreadsheet.
- Use a pattern you can remember. For example, one block of numbers per EA and a step for each chart or symbol.
- Avoid 0, because manual trades use it.
- Open the inputs window of each EA and enter the number before the first trade is placed.
- Test on a demo account with all the EAs attached together, and check that each one only manages its own orders.
Changing the number after the EA has opened trades is a common mistake. The EA then no longer recognises its open orders, so it may stop managing them, or it may open a fresh set on top of them. If you must change it, close or hand over the open trades first.
Set files can carry the number
A set file is a saved list of input values that you load in MetaTrader. If a set file includes the magic number, loading it on a second chart copies the same number as well. After you load any set file, open the inputs and check this value before you start. The guide on how to load a set file in MT4 and MT5 is a good companion for that step.
Finding the magic number in MQ4 source code
When you own the source code, the magic number stops being a black box. You can see where the value is declared, where it is sent with an order, and where it is used to filter orders. This is also the quickest way to learn whether an EA treats magic numbers in the way you expect. The guide to opening an MQ4 file in MetaEditor covers the steps to open and compile a source file.
What to search for
Open the file in MetaEditor and use the search function for the word magic. Check three places:
- the input or variable that holds the number;
- the order-sending call, where the number is passed with the trade;
- the loops that select orders, where the number is compared before the EA acts.
If the number is read from an input, you can change it from the inputs window without touching the code. If it is written into the code as a fixed value, you need to edit it and compile again, which makes it your own build. Test any change on a demo account before using it live. A source package is covered in more general terms in the MQ4 and MQ5 source code guide.
Two MT4 source packages to read it in
The shop has MT4 source packages in which you can do this check. The Gold Reaper EA Source Code (MQ4) is the editable MQL4 source of an XAUUSD M15 EA that the listing describes as breakout, neural network / AI and grid. The download includes one compiled file, one source file and four set files. The listing states grid/martingale as yes, so a grid EA is the kind of tool where an unclear order filter matters most.
Waka Waka EA Source Code (MQ4) is a source-only package for MetaTrader 4. The listing describes grid, news trading and neural network / AI logic on AUDCAD, AUDNZD and NZDCAD, M15, and says the download has no compiled file and no set files. You compile it yourself, and the values that the code defines are the values you see in the inputs window.
Neither listing says how it handles the magic number, so read the source to find out. All products are digital downloads, and every product is also included in the membership.
FAQ
Is it safe to leave an EA's magic number at 0?
Leaving an EA's magic number at 0 is not safe, because trades you open by hand from the terminal also carry a magic number of 0. An EA set to 0 would treat your manual trades as its own and might modify or close them. Give each EA a non-zero number and write down which number belongs to which EA.
Do two copies of the same EA on one symbol need different magic numbers?
Yes, two copies of the same EA attached to charts of one symbol need different magic numbers. When the symbol and the number match, each copy can count the other's trades and manage the other's orders. Giving every chart its own number keeps the trades of each copy separate, even when the inputs are identical.
What happens if I change an EA's magic number while it has open trades?
If you change an EA's magic number while it has open trades, the EA no longer recognises those trades as its own. It may stop managing them, or it may open a fresh set on top of them. If you must change the number, close or hand over the open trades first and test the change on a demo account.
Why does a shared magic number matter more for a grid EA?
A shared magic number matters more for a grid EA because a grid measures all of its orders as one group. A grid opens extra orders at set price steps and often calculates the combined profit of its basket. If two grids on the same symbol share a number, each counts the other's orders, so exposure and drawdown become hard to read.
How to check magic numbers on a demo account
Before you run several EAs on a live account, set up a demo account and attach them as you plan to run them. Give each a different number, let them open trades, and then compare the orders against your list. Check each EA's own journal or log output if it prints the magic number, and compare the open orders against your list.
Look for an EA that touches an order it should not. Look for counts that seem off. Look for any trade with a magic number of 0 that you did not place yourself. The article on testing an EA on a demo account and then a live one describes how to structure that test.
Magic numbers are a small setting with a large effect on how several programs share one account. Treat the number as part of each EA's configuration, record it, and check it each time you add a new EA.
Trading foreign exchange and CFDs carries a high level of risk and may not be suitable for all investors. Past performance does not guarantee future results; only trade with money you can afford to lose.
Tags: EA Source Code, MQL4


