ForexBestRobots.comCompare EAs →
EN

MT4 & Expert Advisors · Order identification

MT4 Magic Number Explained: How Expert Advisors Track Trades

Understand how an MT4 Magic Number identifies orders and helps separate EA strategies when the code checks the intended ownership scope.

13 min readReviewed by ForexBestRobots Editorial Team
MT4 Magic Number diagram showing separate Expert Advisors and manual trades routed to different order groupsMT4 & Expert AdvisorsPractical MT4 guidance

Original illustration created for ForexBestRobots.com.

A Magic Number is an integer that an Expert Advisor writes into an MT4 order when the trade is created. The EA can later scan open and historical orders, find its own identifier and decide which positions it is allowed to manage.

What a Magic Number does

The number does not make trades profitable, limit drawdown or license an account. Its purpose is operational: it separates one automated strategy from another and, when the code is written correctly, prevents an EA from modifying unrelated orders.

MT4 Magic Number diagram showing separate Expert Advisors and manual trades routed to different order groups
A Magic Number is an order identifier, not a risk control or account license.
A unique number prevents confusion, not market loss

Position size, Stop Loss, correlation and maximum drawdown still require separate risk controls.

Order workflow

Use a unique, documented integer for each independent strategy. Keep the same value when restarting MT4 or moving the same installation to a VPS. A symbol suffix does not require a new Magic Number by itself, but a separate strategy instance usually does. If the vendor supplies a fixed value or hides the input, follow that design instead of forcing a replacement.

1

Choose the ownership model

Read the vendor instructions first. Some EAs expect one fixed Magic Number, while portfolio systems intentionally share a number across several charts.

2

Record every assignment

Keep a simple table with terminal, account, EA, symbol, timeframe and Magic Number. This is more reliable than trying to remember several installations.

3

Set the value before attachment

Enter the number in EA Inputs before live use. Do not copy a set file blindly if another EA already uses the same number.

4

Preserve it during open trades

If positions are active, do not change the identifier unless the developer gives a migration procedure. The new instance may no longer recognize the existing basket.

Common conflicts

ScenarioSuggested assignmentRisk if confused
One EA on one chartOne stable number for that strategy instanceChanging it while trades are open can orphan positions
Same EA on two pairsDifferent number per symbol unless the vendor states otherwiseThe EA may combine baskets or close the wrong symbol group
Two different EAsA unique number for each EABoth programs may attempt to manage the same orders
Manual tradesUsually Magic Number 0An EA coded to manage 0 may touch manual orders

Common mistakes

Duplicate numbers used by unrelated EAs on the same account
A copied set file that silently overwrites the planned identifier
Running the same account in two terminals with different settings
Using Magic Number 0 when the EA is allowed to manage manual orders
Changing the value after an EA has opened a basket

Safe setup

Check the EA’s documented ownership scope before choosing identifiers. Some programs filter by both Magic Number and symbol; portfolio EAs may intentionally manage several symbols together. An identifier only works when the code checks the intended scope before modifying or closing an order.

Do not use order comments as an ownership lock. Comments can be changed by the server, and Magic Number 0 is commonly used for manual orders. Verify the actual magic field with a trusted diagnostic script or report, not the displayed comment alone.

How to verify

Open Terminal → Trade and display the order comment if the EA writes one.
In Account History, inspect the order with a script or report that exposes the magic field.
Confirm that closing or changing one test order does not affect another EA’s position.
Check Experts and Journal after restart for messages about missing or unmanaged orders.
Repeat the test on Demo before running several EAs on one live account.

A Magic Number does not lock another terminal out

Two terminals logged into the same account can read and act on the same order pool. A shared Magic Number does not create a server-side mutex or appoint one terminal as the owner. Even different identifiers do not stop two copies from opening duplicate strategy exposure if both independently detect the same signal.

Keep one intended trading instance for a given deployment unless the developer explicitly supports coordination. During a VPS move, preserve identifiers needed to recognize open positions, stop the old instance and confirm the new one manages those positions. Do not change a live basket’s identifier to solve a duplicate-terminal problem.

The OrderSend reference describes the magic field as a user-defined identifier and notes that the server can alter the end of an order comment.

Verify ownership before combining independent instances

Assume the vendor explicitly supports two independent demo instances with identifiers 11401 and 11402. Record the account, broker server, EA version, exact symbols and timeframes for each. When a normal test signal creates an order, inspect its actual magic field and symbol. Confirm that each instance recognizes only the orders within its documented scope, including after a restart. Different numbers alone do not prove that the code filters correctly.

If an unrelated position is modified or an existing basket is not recognized, stop adding new exposure and retain the evidence. Do not change the identifiers of open positions as an experiment. A coordinated portfolio EA may intentionally use a different ownership model, so the vendor’s instructions take precedence over this independent-instance example.

Use the VPS recovery and handover checks when moving the deployment, and review combined portfolio exposure separately. Correct order ownership prevents operational confusion; it does not make correlated strategies independent or limit their market losses.

Official reference

Read the official documentation.