MQL5 Symbol Mapping
Definition
Symbol mapping in MQL5 handles the variation in how brokers name the same instrument — EUR/USD might be 'EURUSD', 'EURUSD.r', 'EURUSDpro', or 'EURUSD-Pro' depending on the broker. EAs must either be configured for the specific broker's naming or include symbol-mapping logic that detects the correct symbol name automatically.
In-depth: MQL5 Symbol Mapping
Symbol naming conventions vary substantially across forex brokers, creating one of the most common 'why doesn't my EA work?' issues when traders switch brokers or run EAs on multiple brokers.
Common naming variations for EUR/USD: - `EURUSD` (most common; many brokers) - `EURUSD.r` (raw spread account suffix at some ECN brokers) - `EURUSDpro` (premium account variant) - `EURUSD-T1`, `EURUSD-T2` (tiered liquidity at some ECN brokers) - `EUR/USD` (less common; some platforms display slash but storage is different) - `EURUSDm` (mini account variant at some brokers) - `EURUSDc` (cent account variant)
Gold (XAU/USD) naming variations: - `XAUUSD` (most common) - `GOLD` (some brokers) - `XAUUSD.r`, `XAUUSDpro`, etc. (suffix patterns same as forex)
Indices (Dow Jones, NASDAQ) have even more variation: - `US30`, `US30Cash`, `DJ30`, `WallStreet30`, `DJIA`, `Dow30` - `NAS100`, `USTECH100`, `NDX100`, `Nasdaq100`
Why EAs need symbol mapping:
1. Vendor distribution: a vendor selling an EA across multiple brokers cannot hardcode `EURUSD` because some buyers use brokers where the name is different.
2. Multi-broker accounts: traders running the same EA on multiple brokers need configuration flexibility.
3. Robustness: even at the same broker, account-type changes (raw → standard) may change symbol suffix.
Implementation approaches:
**Approach 1 — Input parameter (simplest):** ```mql5 input string TradingSymbol = ""; // Empty = use chart symbol
string GetTradingSymbol() { return (TradingSymbol == "") ? _Symbol : TradingSymbol; } ``` User configures the exact symbol name as broker-shown. Works but requires manual configuration per broker.
**Approach 2 — Suffix detection:** ```mql5 string FindSymbol(string base) { int total = SymbolsTotal(false); for(int i = 0; i < total; i++) { string sym = SymbolName(i, false); if(StringFind(sym, base) == 0) { return sym; } } return base; // Fallback } ``` Iterates all symbols looking for one starting with the base name. Picks up suffixes automatically.
**Approach 3 — Comprehensive mapping:** ```mql5 string NormalisedToBrokerSymbol(string normalised) { string variations[] = {normalised, normalised + ".r", normalised + "pro", normalised + "m", normalised + "-T1"}; for(int i = 0; i < ArraySize(variations); i++) { if(SymbolInfoInteger(variations[i], SYMBOL_VISIBLE)) { return variations[i]; } } return normalised; // Fallback } ``` Tries common variations explicitly. More robust than suffix detection because it handles non-suffix variations.
Best practices for EAs: 1. Default to `_Symbol` (the chart symbol) — works when user attaches EA to the correct chart 2. Optionally allow override via input parameter for multi-symbol EAs 3. Document expected symbol names in the EA manual 4. Test on multiple broker types during EA development 5. Log the actual symbol being traded at EA initialisation; surface errors clearly if a required symbol is not found
For vendors: provide a `Broker Compatibility Notes` document listing tested brokers and their symbol naming conventions.