The Magic Number System:
Running Multiple Bots on One MT5 Account
MetaTrader 5 gives every trader one account, one balance, and one position list. So how do you run three independent bots on the same account — each with its own strategy, its own capital, and its own risk limits — without them stepping on each other?
The answer is magic numbers. Here's exactly how ForgeAlpha uses them — and why it matters for anyone serious about automated trading at scale.
What Is a Magic Number?
Every position opened in MT5 can carry a custom integer identifier — called a magic number — set at the moment the order is placed. It's just a field in the order request. MT5 attaches it to the position record and exposes it in every subsequent query about that position.
Brokers don't care about it. It doesn't affect execution or P&L. It's purely a tag you attach to say: this position belongs to me, specifically this version of me.
Most retail traders never use magic numbers. They run a single EA (Expert Advisor) and it manages everything. But the moment you want multiple systems running in parallel — or you want to isolate one bot's trades from another's — magic numbers become the only primitive MT5 gives you to do it.
The Problem: Shared Account, Independent Bots
Imagine you want to run three strategies simultaneously:
- Bot A: EUR/USD trend-following, allocated $1,000, max 3 positions
- Bot B: Gold scalper, allocated $2,000, max 5 positions
- Bot C: Volatility 75 index grid, allocated $500, 1 active position at a time
All three share one MT5 account with a $5,000 balance. When Bot A needs to close its positions, how does it know which positions belong to it? If Bot C is running a grid, how does it not accidentally close Bot A's trades?
Without a tagging system, you'd need three separate MT5 accounts. Three separate broker logins. Three separate MetaApi connections. Three times the cost, three times the complexity, three times the failure points.
Magic numbers solve this cleanly.
How ForgeAlpha Assigns Magic Numbers
When you create a bot in ForgeAlpha, the system auto-assigns it a unique magic number starting from 100000 and incrementing with each new bot. Your first bot gets 100000, your second gets 100001, and so on. This happens automatically — you never set it manually.
Every order ForgeAlpha places on your MT5 account includes that bot's magic number in the trade request. Every MetaApi event that arrives — position updates, deal records, P&L ticks — carries the magic number back, allowing ForgeAlpha to route it to exactly the right bot.
The Filter in Action
The magic number filter runs at multiple layers in ForgeAlpha:
1. Position Monitoring
When a price tick arrives, ForgeAlpha fetches all open positions from MetaApi and immediately filters by magic number. Bot A's strategy only receives Bot A's positions. It makes decisions — open more, close all, adjust stops — based exclusively on its own trades. It is physically impossible for Bot A's strategy to accidentally close Bot B's position.
2. Trade Recording
When MetaApi fires a onDealAdded event, it includes the magic number. ForgeAlpha uses this to route the deal to the correct bot's trade history. Bot A's P&L records only include trades with magic number 100000. Bot B's equity curve is calculated solely from trades tagged 100001.
3. Virtual Capital Isolation
This is where magic numbers combine with ForgeAlpha's virtual balance system to create true capital isolation. Each bot has its own assigned_capital, virtual_balance, and virtual_equity — tracked independently of the real MT5 balance. A margin check for Bot A uses only Bot A's virtual equity. A 20% drawdown on Bot B's allocation doesn't affect Bot A's ability to open new positions.
Without this, a drawdown in one strategy would reduce the shared account equity and starve other bots. With it, each bot operates as if it has its own independent account — they just share the execution infrastructure.
4. Reconciliation
When ForgeAlpha runs reconciliation — checking for missed trades, marking stale records closed, or backfilling gaps in trade history — it uses the magic number as the primary filter. It only processes positions and deals that belong to each bot. This means you can safely add a new bot while existing bots are live, and the reconciliation for the new bot will never touch the other bots' records.
Why Not Just Use Separate Accounts?
The obvious alternative is multiple broker accounts — one per strategy. This works, but comes with real costs:
- Capital fragmentation: $5,000 spread across 3 accounts means none has the margin headroom of a single $5,000 account.
- MetaApi licensing: Each MetaApi connection costs money. Three accounts means three connections.
- Operational complexity: Three logins, three withdrawal processes, three broker relationships to maintain.
- No cross-strategy hedging: If two strategies are offsetting each other naturally, having them in the same account can net out the positions and reduce margin usage. Separate accounts lose this benefit.
Magic numbers give you logical isolation without physical separation. You get the simplicity of one account with the behavioral independence of many.
The Practical Limit
MT5 magic numbers are 32-bit unsigned integers. ForgeAlpha starts at 100000, which means you can technically run tens of thousands of bots per account before exhausting the namespace. In practice, your broker's margin and the MT5 terminal's concurrent position limits are the actual constraints — not the magic number system.
What This Enables
The magic number architecture isn't just a technical detail — it enables a whole class of trading strategies that would otherwise require significant infrastructure:
- Portfolio diversification: Run a trend strategy, a mean-reversion strategy, and a grid strategy simultaneously, each with its own risk allocation
- A/B testing: Run two versions of the same strategy with identical capital allocations to compare live performance
- Staged scaling: Start small on a new strategy while letting an existing proven strategy run at full size — all on one account
- Copy trading simulation: Simulate how a copier account would perform by running a "copier bot" alongside a "master bot" with scaled lot sizes
The Bottom Line
Magic numbers are one of the most underused features in MT5. Most retail EA developers treat them as an optional tag. ForgeAlpha treats them as infrastructure — the foundational primitive that makes multi-bot capital isolation possible without requiring multiple broker accounts.
Every bot you create in ForgeAlpha gets its own magic number, automatically. Every trade it places is tagged. Every P&L calculation, every reconciliation, every risk check — all filtered to that bot's universe of positions. The broker sees one account. ForgeAlpha sees as many independent portfolios as you want to run.
Run multiple strategies on one account.
ForgeAlpha handles the magic number plumbing automatically — you focus on building strategies.
Get Early Access