Guide

Convert MQL4 to MQL5: 8 Easy Fixes for Common Errors

To convert MQL4 to MQL5, fix the eight language changes behind most compile errors. Then compare both versions in the testers and run the MT5 build on a demo account.

Converting MQL4 to MQL5: old MQL4 code with compile errors next to clean MQL5 code using CTrade and an indicator handle
On this page

Workflow to convert MQL4 to MQL5, from the .mq4 source file to a demo test

Save an old .mq4 file as .mq5 and compile it in MetaEditor 5. The Errors tab fills with dozens of lines, but most trace back to eight language changes, so the list shrinks fast once you fix those.

Why a rename will not convert MQL4 to MQL5

MT5 does not run .ex4 files. The language also changed in two big places. Trades now split into orders (requests), deals (fills) and positions (what stays open). Indicator calls such as iMA() return a handle where they once returned a number. Inputs, loops and maths barely change, so the real work sits in the trade and indicator code.

MQL4 calls next to their MQL5 replacements

First find the .mq4 source, since a compiled .ex4 holds no code you can read. Then list each input and feature, and open the same symbol on an MT5 demo.

Common MetaEditor errors and the MQL5 fix for each

1. Replace OrderSend() with CTrade

The first error most people meet is “wrong parameters count” on 'OrderSend'. In MQL4 that call takes eleven values: seven you must pass and four you can skip, the magic number among them. The new language wants a request and a result structure. The CTrade class from the standard library fills in the request for you. It also keeps the result of the last call.

#include <Trade\Trade.mqh>
CTrade trade;
// MQL4: OrderSend(Symbol(), OP_BUY, 0.10, Ask, 3, sl, tp, "", 123, 0, clrNONE);
trade.SetExpertMagicNumber(123);
trade.Buy(0.10, _Symbol, 0.0, sl, tp);   // price 0.0 = current Ask

The magic number moves out of the call and into SetExpertMagicNumber().

2. Rewrite order loops for positions

Errors on 'MODE_TRADES' (undeclared identifier) and 'OrderSelect' (parameter count) often turn up together. MQL4 keeps market and pending orders in one list. In MQL5, open trades are positions, and OrdersTotal() counts pending orders only.

for(int i = PositionsTotal() - 1; i >= 0; i--)
  {
   ulong ticket = PositionGetTicket(i);           // also selects the position
   if(PositionGetInteger(POSITION_MAGIC) != 123) continue;
   // manage the position here
  }

PositionGetTicket() also selects the position, so the magic check can follow. For closed trades, call HistorySelect() for a time range and read the deals from that list, where each fill is a deal and one position can have several.

3. Use indicator handles and CopyBuffer()

A parameter count error on 'iMA' means the call still has its old shape, where it gave back the value. Now you create the handle once in OnInit(). Then CopyBuffer() reads values from it in OnTick().

int maHandle = INVALID_HANDLE;                                   // global
maHandle = iMA(_Symbol, _Period, 50, 0, MODE_EMA, PRICE_CLOSE);  // OnInit()
double ma[];                                                     // OnTick()
ArraySetAsSeries(ma, true);
if(CopyBuffer(maHandle, 0, 0, 3, ma) < 3) return;                // ma[1] = last closed bar

CopyBuffer() counts from the present, so position 0 is the current bar. With the array set as a series, ma[1] is the last closed bar. Free the handle with IndicatorRelease() in OnDeinit().

4. Replace Bid, Ask and the Close[] series

The new language has no built-in Bid, Ask or Close[]. That is why MetaEditor lists 'Bid' and 'Close' as undeclared identifiers.

MqlTick tick;
if(!SymbolInfoTick(_Symbol, tick)) return;
double ask = tick.ask, bid = tick.bid;
double lastClose = iClose(_Symbol, _Period, 1);   // last closed bar

For a block of bars, use CopyRates() or CopyClose() and watch the array’s direction. Old MQL4 code counts bars backwards, with 0 as the newest bar, so call ArraySetAsSeries() on your arrays first or index 0 points at the oldest bar.

5. Rename the event handlers

Older MQL4 EAs often run on init(), start() and deinit(). MT5 only calls handlers whose name and signature match its built-in events, so it never runs those three. Rename them to OnInit(), OnTick() and OnDeinit().

OnInit() returns a code as well. INIT_SUCCEEDED lets the EA carry on. INIT_FAILED makes MT5 unload it from the chart. Custom indicators move their work from start() into OnCalculate().

6. Swap MarketInfo() and the account functions

The new language reads symbol and account data through a few new functions. So the old calls fail with an “undeclared identifier” error on 'MarketInfo'. Each one has a direct match: MODE_STOPLEVEL becomes SYMBOL_TRADE_STOPS_LEVEL, and AccountBalance() becomes AccountInfoDouble(ACCOUNT_BALANCE). Symbol() and Period() shorten to _Symbol and _Period. The MQL5 docs list every name.

MQL4 market and account calls next to their MQL5 match

7. Handle netting and hedging accounts

On MT4 you can hold many trades on one symbol. A forex account in MT5 works in one of two modes. Netting keeps one position per symbol, and each new deal adds to it, cuts it, closes it or reverses it. Hedging keeps each trade as its own position, as MT4 does. MT5 also has a third margin mode for exchange markets.

Read the mode at start-up with AccountInfoInteger(ACCOUNT_MARGIN_MODE). Close logic and magic filters change with the mode. If other traders will run the EA, test it on both account types.

8. Set the filling mode and read return codes

This error often hides until a live order fails. Then the Journal tab shows Unsupported filling mode. Each symbol allows only some filling modes out of FOK, IOC and RETURN. The server rejects an order that asks for another. Let CTrade pick a valid mode, then check the return code after every order.

trade.SetTypeFillingBySymbol(_Symbol);
if(!trade.Buy(lots, _Symbol, 0.0, sl, tp) || trade.ResultRetcode() != TRADE_RETCODE_DONE)
   Print("Order failed: ", trade.ResultRetcode(), " ", trade.ResultRetcodeDescription());

When a symbol allows both FOK and IOC, SetTypeFillingBySymbol() picks FOK. Round prices to the symbol’s digits, and lots to its volume step. The server turns down 0.033 lots on a symbol with a 0.01 step.

How do you test the converted EA?

Once it compiles, run the MT4 and MT5 versions on the same symbol, timeframe, dates and inputs, then compare the two trade lists for entries, exits and lots. Small gaps are normal, since each platform has its own price data and spreads. A large gap often points to a logic bug.

Test checklist for a converted EA

After that, forward test the MT5 build on a demo for at least a week. Watch the Experts tab for errors, and check each order against your rules.

Convert it yourself or ask a developer?

A small EA with one strategy may need only these eight fixes. Panels, multi-symbol logic and custom indicators take longer.

Whoever does the work, the steps to convert MQL4 to MQL5 stay the same. Collect the source and inputs, rewrite, compile, compare in both testers, then run a demo.

Five steps for an MQL4 to MQL5 conversion

If you would rather hand the job over, our strategy conversion service handles MQL4 rewrites too, and you test a demo build before the final payment. New features go through MT5 EA development, and an indicator through custom MT5 indicators. For prices, check our pricing page.

Sources

Frequently asked questions

Can an MT4 EA run on MT5 without changes?

MT5 does not load .ex4 files. The .mq4 source has to be rewritten and compiled into an .ex5 file before the EA runs there.

Is there an automatic MQL4 to MQL5 converter?

Some include files map old MQL4 calls onto MQL5 and help with simple code. They cannot choose how the EA acts on a netting account or which filling mode a symbol takes. The trade logic still needs a check by hand.

How long does an MQL4 to MQL5 conversion take?

It depends on the code's size, so we read the .mq4 file before we set a date. Coding starts after a 50% deposit in USDT. You get 7 days of free fixes after delivery.

Will the MT5 version trade exactly like the MT4 version?

Results come close but are rarely identical. Fills and price feeds differ between the platforms, and a netting account merges trades on one symbol. The tester comparison we send with the build lists every gap and its cause.

Is the .ex4 file enough, or do you need the .mq4 source?

We need the .mq4 source, since we do not decompile files. If it is lost, send a written list of the EA's rules and we rebuild it for MT5 from that. An NDA is available on request.

Can you convert custom indicators as well?

Custom indicators get converted too, starting with any your EA reads through iCustom(). In MQL5 that call returns a handle. It also needs the indicator compiled as an .ex5 file in MQL5/Indicators.

WhatsApp · Email · Telegram

Tell us what you want to automate

Send your rules on WhatsApp. You get a clear quote, then a demo build to test before you pay in full.