--- name: mql5-ea-troubleshooting description: MQL5 EA runtime errors and attach failures — init failures, INVALID_HANDLE 32757, build-specific enum differences --- # MQL5 EA Troubleshooting ## Common runtime errors (attach failed) ### Error 32757 (0x8001 = E_HANDLE / INVALID_HANDLE) Usually means an indicator handle creation failed, or a **function call/argument mismatch** in `OnInit()`. **Stack mismatch diagnosis:** ```mql5 // Call site passes 9 args: g_orderManager.Init(g_symbol, InputMagicNumber, InputComment, InputEntryPrice, InputSL, InputTP, InputMaxFollowOrders, InputUseTrailingSL, InputFollowDelayBars); // ← 9 args // But declaration only accepts 8: void Init(string symbol, int magicNumber, string comment, double inputEntry, double inputSL, double inputTP, int maxFollow, bool useTrailing, int followDelayBars); // ← 8 params // ↑ stack corrupted ``` Fix: match declaration to call. **Indicator handle failure:** - `iATR(symbol, PERIOD_M5)` → in some builds requires 3 params: `iATR(symbol, PERIOD_M5, period)` - `iATR(handle)` returns `INVALID_HANDLE` if symbol/timeframe unavailable - Always check: `if(handle == INVALID_HANDLE) return INIT_FAILED;` --- ## Build-specific enums (tested on build 3570+) | Enum | Wrong | Correct | |------|-------|---------| | ATR params | `iATR(sym, TF)` (2 args) | `iATR(sym, TF, period)` (3 args) | | Account margin | `ACCOUNT_MARGIN_MODE_NETTING` | `ACCOUNT_MARGIN_MODE_RETAIL_NET` | | Account free margin | `ACCOUNT_FREEMARGIN` (deprecated) | `ACCOUNT_MARGIN_FREE` | | Symbol contract size | `SYMBOL_CONTRACT_SIZE` | `SYMBOL_TRADE_CONTRACT_SIZE` | | Margin rate | `SYMBOL_MARGIN_RATE` (doesn't exist) | `SYMBOL_MARGIN_HEDGED` or use `OrderCalcMargin()` | | TRADE_TRANSACTION | `TRADE_TRANSACTION_POSITION_CLOSE`, `_OPEN` don't exist | Use `TRADE_TRANSACTION_DEAL_ADD` + `TRADE_TRANSACTION_POSITION` | | `CTrade.SetComment()` | doesn't exist | Pass comment string directly to `Buy()`/`Sell()` | --- ## Type coercion warnings (benign — still compile) - `SymbolInfoInteger()` returns `long`; assign to `double` with explicit cast: `(double)SymbolInfoInteger(sym, SYMBOL_SPREAD)` - `rates[].tick_volume` is `long`; cast: `(double)rates[0].tick_volume` --- ## Debug approach for attach/init failures 1. Check if error is an `INIT_*` code ( INIT_PARAMETERS_INCORRECT = 5, INIT_FAILED = 7) 2. If not INIT code → look for INVALID_HANDLE or E_HANDLE 3. Search all files for `IndicatorCreate`, `iATR`, `iAC`, `iADX` — ensure all handles checked 4. Count arguments in every Init/constructor call vs. declaration 5. Check global-scope code that runs before `OnInit()` (including class member constructors)