Editorial coverage · Last reviewed
MQL5 Tutorial — Complete Developer Guide for MT5 EA Programming
Language fundamentals
MQL5 syntax is C++-derived: curly braces, semicolons, type declarations, classes with inheritance. Familiar to C/C++/Java/C# developers; somewhat alien to Python/JavaScript developers. Manual memory management is minimal (most allocations handled by MetaQuotes runtime) — easier than raw C++ in this regard.
Core types: int, double, string, datetime, bool, color. MQL5 also provides domain-specific types (MqlTick for tick data, MqlRates for OHLC bars, MqlTradeRequest for trade requests). Strong typing throughout — no dynamic typing like Python.
Object-oriented features: classes with single inheritance, virtual functions, encapsulation. MetaQuotes provides Standard Library (CTrade, COrderInfo, CPositionInfo, CSymbolInfo, etc) abstracting common trading operations. Most production EAs use Standard Library extensively.
Strict compilation: MQL5 catches many errors at compile time (type mismatches, undefined references, undeclared variables) — reduces runtime errors but requires upfront understanding of types and declarations.
Trading functions
CTrade class (in Trade/Trade.mqh) wraps low-level OrderSend with cleaner methods: trade.Buy(volume, symbol, price, sl, tp, comment), trade.Sell(...), trade.PositionClose(...), trade.PositionModify(...). Production EAs typically use CTrade rather than raw OrderSend.
Order types: ORDER_TYPE_BUY (market buy), ORDER_TYPE_SELL (market sell), ORDER_TYPE_BUY_LIMIT (pending buy below market), ORDER_TYPE_SELL_LIMIT (pending sell above market), ORDER_TYPE_BUY_STOP (pending buy above market), ORDER_TYPE_SELL_STOP (pending sell below market). Most EAs use market orders; pending orders are useful for breakout strategies.
Position management: CPositionInfo class accesses position properties (volume, price, stop loss, take profit, comment). Iterate via PositionsTotal() and PositionGetSymbol(i). Magic numbers distinguish EA's positions from manual trades and other EAs.
Error handling: every trade operation can fail (insufficient margin, market closed, broker restrictions, network issues). Production EAs check trade.ResultRetcode() and log/handle failures.
Market data and indicators
Price data: SymbolInfoTick() for current tick, CopyRates() for historical OHLC, iClose()/iOpen()/iHigh()/iLow() for specific bar indices. Time-series indexing: index 0 = current bar, index 1 = previous bar, etc.
Built-in indicators: iMA() (moving average), iRSI() (RSI), iMACD() (MACD), iBands() (Bollinger Bands), iATR() (ATR), iStochastic() (Stochastic), etc. Each returns a handle; CopyBuffer() reads values from the indicator buffer.
Custom indicators: write your own indicator in MQL5 with OnCalculate() handler. Calculate values per bar, store in buffer, expose to EAs via iCustom() handle. Useful for proprietary or strategy-specific signals.
Performance consideration: indicator calculations can be expensive; avoid recomputing on every tick. Cache indicator handles in OnInit() and reuse across OnTick() calls.
Frequently asked questions
How long does it take to learn MQL5 well enough to write a profitable EA?
MQL5 learning timeline analysis: Experienced programmer (1-3 years C/C++/C#/Java experience): • Week 1-2: language syntax, basic structure of an EA. • Week 3-4: trading functions, position management, error handling. • Month 2: market data access, indicators, basic strategy implementation. • Month 2-4: first functional EA, backtesting and optimisation. • Month 4-12: production-quality EA development if combined with effective trading strategy. • Bottleneck after month 4: not programming skill, but trading strategy validity. Many programmers write technically correct EAs that don't make money because strategy doesn't have edge. Non-programmer (no prior programming experience): • Month 1-3: programming fundamentals — variables, functions, control flow. Use Python or simpler language first if struggling with C++ syntax. • Month 3-6: MQL5 language specifics — types, classes, MetaQuotes Standard Library. • Month 6-9: trading functions and first functional EA implementation. • Month 9-12: backtesting, optimisation, refinement. • Year 2+: production-quality if combined with effective trading strategy development. Key insight — the programming is the easy part: The gap between 'functional EA' and 'profitable production EA' is usually NOT a programming gap. It's a strategy development gap. Programming a moving-average crossover EA takes ~20 hours of MQL5 work; developing a profitable strategy with appropriate risk management, parameter selection, and broker compatibility takes 200-1000+ hours of trading experience and iteration. For most aspiring EA developers: • Don't underestimate the strategy development side. • Don't expect that learning MQL5 alone produces profitable EAs. • Combine programming learning with trading strategy learning (manual trading experience helps understand what the EA should do). • Start with simple strategies (moving average, trend-following) before attempting complex (ML, multi-asset arbitrage). • Be skeptical of backtest results — most over-fitted to historical data and underperform live. Realistic expectation: dedicated developer with both programming and trading skills can produce a profitable production EA in 6-12 months of focused work. Without trading experience, the timeline extends significantly.