Since the release of MetaTrader 5, MetaQuotes has actively encouraged traders and developers to migrate from MT4. MT5 offers structural advantages, including support for netting and hedging, a multi-threaded backtester, order execution types, and integration with stock and futures exchanges.
However, migrating to MT5 requires converting MQL4 code (MQ4) to MQL5 code (MQ5). The two programming languages have different execution models, event handlers, and API libraries.
Converting code requires rewriting syntax parameters, indicator buffers, and trading operations to avoid compile errors in the MT5 compiler.
The first step in converting code is mapping MQL4 event handlers to their MQL5 equivalents:
OnInit(): MQL5 retains OnInit() for initialization, but returning values must map to INIT_SUCCEEDED or INIT_FAILED instead of standard integer codes.OnTick(): For Expert Advisors, the MT4 entry point start() maps directly to OnTick() in MQL5.OnCalculate(): For custom indicators, start() must be converted to OnCalculate(), which provides arrays of historical OHLC price data directly as function arguments.
Handling indicator buffers in MQL5 is fundamentally different from MQL4:
In MQL4, buffers are declared in the global scope and set using SetIndexBuffer(). MQL5 requires declaring buffer indices, setting the buffer mapping, and then defining specific drawing plots using compile directives (e.g. #property indicator_label1).
Additionally, indexing arrays in MQL5 is handled differently. By default, arrays index from the oldest bar (0) to the newest. To search bars in reverse chronological order (newest bar first, like in MT4), you must explicitly call ArraySetAsSeries(buffer, true).
The biggest challenge in converting Expert Advisors from MQ4 to MQ5 is order management. MT5 does not use a flat order table; it separates transactions into:
- Orders: Instructions sent to the broker to buy or sell.
- Deals: The executed transaction records.
- Positions: The active exposure in a specific currency pair or asset.
Functions like OrderSend(), OrderSelect(), and OrderClose() do not exist in MQL5. You must use the CTrade class from the standard library or build custom MqlTradeRequest structures.
No automated converter can achieve 100% accurate conversion for EAs or complex indicators. Our tool automates syntax mappings, but you must perform a manual review to finalize code migration:
Open the converted code in **MetaEditor 5** and hit compile. The compiler will flag any remaining incompatible APIs or warning codes. Review each compiler error and use MQL5 documentation to update MQL4 patterns to native MQL5 classes.
Pay special attention to indicator handles and historical data extraction (e.g. replacing `iHighest` with `CopyHigh` arrays).
Follow this playbook to convert and compile your MetaTrader code:
1. Upload or Paste MQ4 Code
Upload your MQL4 source file or paste the code directly into the workspace code editor.
2. Review Automated Warnings
Review the warnings generated by the converter to identify blocks of code that require manual rewrites.
3. Copy and Open in MetaEditor 5
Copy the converted code and paste it into a new MQL5 project inside MetaEditor 5.
4. Test and Compile
Compile the code, resolve any remaining compiler errors, and test the Expert Advisor in the MT5 strategy tester.