It is a rewrite of the trading layer, not a find-and-replace
Converters and compatibility headers can make MQL4 code compile in MetaEditor 5. They cannot make it behave the same, because the two platforms disagree about what a trade is. Plan a migration as three pieces of work: the trading layer, the data and indicator layer, and re-validation in the tester.
Orders, deals and positions
In MT4 one object does everything. An order is opened with OrderSend(), found with OrderSelect(), and it carries its own profit, stop loss and history under a single ticket.
MT5 splits that into three:
- Orders are requests — a market order that is about to execute, or a pending order waiting for price.
- Deals are executions. Every fill, partial or full, is a deal in the account history.
- Positions are the net result of deals on a symbol, and they are what carries the stop loss, take profit and floating profit.
Loops over OrdersTotal() that manage open trades become loops over PositionsTotal(). Anything that reads closed-trade results has to select history with HistorySelect() and read deals. OrderSend() itself now takes an MqlTradeRequest structure and fills an MqlTradeResult; most code is cleaner using the CTrade class from the Standard Library.
Netting and hedging accounts
An MT5 account is either netting or hedging. On a hedging account several positions can exist on one symbol, each with its own ticket, much as in MT4. On a netting account there is one position per symbol: a second buy adds to it, and a sell reduces or reverses it.
An EA ported with MT4 assumptions — a grid, a hedge, separate stops for separate entries — will behave differently on a netting account without raising a single error. Check ACCOUNT_MARGIN_MODE at start-up and either support both modes or refuse to run on the one you have not tested.
Indicators return handles, not values
In MQL4, iMA() returns a number. In MQL5 it returns a handle. The usual pattern is to create the handle once in OnInit(), read values with CopyBuffer() on each tick, and release it in OnDeinit(). The same applies to iCustom().
Two side effects catch people out. CopyBuffer() can return fewer values than requested while history is still loading, so the result count must be checked. And arrays are not series-indexed by default: index 0 is the oldest element unless you call ArraySetAsSeries().
Price data and predefined variables
The predefined Bid, Ask, Open[], Close[] and Time[] are gone. Current prices come from SymbolInfoTick(), bars from CopyRates() or the iClose() family. MarketInfo() becomes SymbolInfoDouble() and SymbolInfoInteger(); AccountBalance() becomes AccountInfoDouble(ACCOUNT_BALANCE).
Timeframe constants also change value. PERIOD_H1 is 60 in MQL4 and a different number in MQL5, so any arithmetic done on Period() breaks. Use PeriodSeconds() instead.
Filling modes
MT5 trade requests carry a filling policy — fill or kill, immediate or cancel, or return — and the symbol decides which ones the broker accepts. A request with the wrong policy is rejected as an unsupported filling mode. Read SYMBOL_FILLING_MODE and set the policy per symbol rather than hard-coding one; this is the most common reason a freshly ported EA trades on one broker and not on another.
Event handlers
init(), start() and deinit() become OnInit(), OnTick() and OnDeinit(). Indicators use OnCalculate(), where prev_calculated replaces IndicatorCounted(). MQL5 also adds OnTradeTransaction(), which reports fills and modifications as they happen and removes the need to poll for changes.
The tester is the reason to migrate
The MT5 Strategy Tester runs multi-symbol strategies, uses real tick history where the broker provides it, and spreads optimisation across processor cores and agents. The MT4 tester works on one symbol with ticks simulated from minute bars.
That has a consequence: results after migration will not match the MT4 report, even when the logic is identical. Treat the port as a new product. Re-run the backtests on real ticks, compare trade by trade against the MT4 version on a short period, explain every difference, and forward-run on demo before going live.
A sensible order of work
- Write down what the MT4 version actually does, including the behaviour nobody remembers specifying.
- Replace the trading layer with position-based code and decide how netting accounts are handled.
- Convert indicators to handles and check every array's indexing direction.
- Replace predefined variables and period arithmetic.
- Handle filling modes, stop levels and volume steps from symbol properties.
- Re-validate in the tester and on a demo account, side by side with the original.
If you would rather hand the port to someone who does this regularly, our MQL5 developers migrate MT4 products to MT5 and deliver the source with the test reports.