Can You Just Convert an MT4 Expert Advisor to MT5?
Many traders still run Expert Advisors written in MQL4 for MetaTrader 4. When they move to MetaTrader 5, the natural assumption is that the code can be translated: rename a few functions, fix the compiler errors and carry on.
Some of it can. Account information, maths, strings and chart objects mostly map across one function at a time. The parts that matter most to an EA, namely how it places and tracks trades, how it reads indicators and how it reacts to trade events, work on a different model in MT5. This article looks at what translates, what has to be redesigned, and how to check that a migrated EA behaves as intended.
What Translates Cleanly
A large share of MQL4 has a direct MQL5 equivalent. The long-standing community guide on mql5.com maps hundreds of functions side by side. AccountBalance() becomes AccountInfoDouble(ACCOUNT_BALANCE), MarketInfo() is split across SymbolInfoDouble() and SymbolInfoInteger(), and the maths, string and file functions are largely the same (MQL5 Articles, Migrating from MQL4 to MQL5).
The same guide is clear about where that approach stops. It deliberately leaves trading functions out of its tables, saying that in MQL5 the concept is different, the trading logic should be changed and there is no sense in converting them directly (Migrating from MQL4 to MQL5). That is the heart of the matter.
Orders, Deals and Positions
In MQL4 an EA usually treats an order ticket as the trade itself. MetaTrader 5 separates three things. An order is an instruction to the broker, a deal is the actual buy or sell that results from it, and a position is the resulting open obligation. One order can produce several deals, and each order and deal carries the identifier of the position it belongs to, so the full history of a position, including partial closes and added volume, can be traced (MetaTrader 5 Help, Basic Principles of trading operations).
The trade call itself changes model too. MQL4’s OrderSend() takes the symbol, volume, price, stops and other values as separate arguments and returns a ticket number (MQL4 Reference, OrderSend). MQL5’s OrderSend() takes an MqlTradeRequest and an MqlTradeResult structure and returns true or false, and a true return only means the request passed basic checks. The result structure has to be read to learn what the trade server actually did (MQL5 Reference, OrderSend).
In practice this means the code that opens, modifies, counts and closes trades has to be rewritten around positions and deal history rather than renamed.
Netting or Hedging: The Broker Decides
MT5 supports two ways of accounting for positions, and the broker sets which one applies to each account. On a netting account there is only one position per symbol: a deal in the same direction adds to it, and an opposite deal reduces, closes or reverses it. On a hedging account every new deal can open a separate position, which has to be closed explicitly (MetaTrader 5 Help, Basic Principles).
This matters most for EAs that open several tickets on one symbol, such as grid or scaling logic. On a netting account those trades merge into a single position, so the EA’s view of what it holds no longer matches the account. An EA can detect the mode with AccountInfoInteger(ACCOUNT_MARGIN_MODE), and a separate ACCOUNT_FIFO_CLOSE property shows whether positions must be closed in the order they were opened (MQL5 Reference, Account Properties). A well-built migration checks this in OnInit() and either supports both modes or declines to run on the one it was not designed for.
Execution settings vary in the same way. MT5 has several execution modes and fill policies, set by the broker per symbol, and the fill policies available depend on the execution mode (MetaTrader 5 Help, Basic Principles). Hard-coding one fill policy is a common reason a migrated EA works on one account and has its orders rejected on another; reading what the symbol allows avoids it.
Indicators Return Handles, Not Values
In MQL4, calling iMA() calculates the moving average and returns its value for a given bar (MQL4 Reference, iMA). In MQL5, iMA() returns a handle, or INVALID_HANDLE if it fails. Values are then copied out with CopyBuffer(), and the handle should be released with IndicatorRelease() when it is no longer needed (MQL5 Reference, iMA).
The sound pattern is to create each handle once in OnInit() and copy values as needed on each tick. Wrapper functions that recreate a handle on every call make old code compile quickly, but they are a lookup aid rather than a production structure.
Price Data, Timeframes and Other Quiet Changes
MQL4’s predefined Bid, Ask and Bars, and the Open[], High[], Low[], Close[], Time[] and Volume[] series, have no direct equivalents in MQL5. Prices come from SymbolInfoTick(), and bar data is copied into local arrays with functions such as CopyClose() and CopyTime(), with ArraySetAsSeries() used to keep MQL4-style indexing (Migrating from MQL4 to MQL5).
Timeframes are a subtler trap. In MQL5, timeframe constants from H1 upwards are not equal to the number of minutes in a bar: PERIOD_H1 is 16385 in MQL5 but 60 in MQL4. Code that stores or calculates timeframes as minutes can compile and still behave wrongly; PeriodSeconds() divided by 60 gives the minutes (Migrating from MQL4 to MQL5).
Trade Events Are Richer, and Unordered
MQL5 adds OnTradeTransaction(), which is called for each step of a trade’s life on the account. MetaQuotes describes a single buy request producing a chain of seven transactions, from the request being accepted to a new position being created. The order in which these arrive is not guaranteed, the account can change while the handler is running, and the queue holds 1,024 transactions, so a slow handler can lose older ones. One OnTrade() call can correspond to several OnTradeTransaction() calls (MQL5 Reference, OnTradeTransaction).
This lets an EA react to fills and changes as they happen instead of scanning every open order on every tick, but only if the handler is written to cope with events arriving out of order.
Testing the Migrated EA
A clean compile shows the code is valid MQL5. It does not show the EA behaves as it did on MT4. The migrated EA should be run in the MT5 Strategy Tester on both netting and hedging settings, with its trade handling, indicator values and event handling checked against what the original was designed to do, before it goes anywhere near a live account. This is a check of behaviour, not of results.
This is the kind of work our MetaTrader Services cover, and it often runs alongside Strategy Optimisation & Testing when an EA needs checking after a rebuild.
Limits and Caveats
The most widely used migration reference dates from 2010. Its function mappings still match the current reference pages cited above, but it is best treated as a lookup aid alongside the official documentation, not as a conversion method.
Compatibility libraries that emulate MQL4 functions can get code compiling quickly, but they hide the order, deal and position model rather than removing the need to handle it. Broker settings also vary, so an EA that works correctly on one MT5 account may need adjusting for another.
None of this says anything about whether MT4 or MT5 is better for trading, or how any strategy will perform. Migration is an engineering task: making sure the code does on the new platform what it was designed to do on the old one.
Conclusion
Can you just convert an MT4 Expert Advisor to MT5? Partly. The utility code translates, often almost line by line. The trading core does not: orders, deals and positions, account modes, indicator handles and trade events need a deliberate redesign, followed by careful testing. Treating migration as a rebuild of those parts, rather than a find-and-replace, is what makes the result dependable.
Drafted with AI assistance; reviewed, edited and approved by Ionut Claudiu Fratila.