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.
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.
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.
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.
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.
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
- MQL4 docs: sending an order
- Trade docs: buy with CTrade, fill mode by symbol, pending order count, position ticket, trade history
- Indicator docs: moving average, copy buffer, custom indicator, event handlers
- Account docs: account data, symbol data, return codes
- MQL4 to MQL5 migration table
- Netting and hedging in MT5



