MQL4 to MQL5: How to Convert an MT4 EA to MT5 (With Code)
MQL4 code doesn't run in MetaTrader 5. The two languages look similar, but MT5 changed how orders, positions and indicators work, so an MT4 Expert Advisor has to be rewritten, not just recompiled. This guide shows the main differences, side-by-side code for the parts that change, and a checklist for converting an EA without changing how it trades.
Why MQL4 code doesn't compile in MT5
- Orders vs positions. MT4 has one list of orders. MT5 separates orders, deals and positions.
- Indicators return handles. In MT4,
iMA()returns a value. In MT5 it returns a handle, and you read values withCopyBuffer(). - Trading functions.
OrderSend()in MT5 takes a request structure. Most EAs use theCTradeclass instead. - Netting and hedging accounts. MT5 accounts can be netting (one position per symbol) or hedging (several). MT4 behaves like hedging.
- Predefined variables.
Bid,Ask,BarsandClose[]don't exist in the same form in MT5.
Reading prices
// MQL4
double ask = Ask;
double lastClose = Close[1];
// MQL5
double ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
double lastClose = iClose(_Symbol, PERIOD_CURRENT, 1);
Indicators
// MQL4: value directly
double ma = iMA(NULL, 0, 50, 0, MODE_EMA, PRICE_CLOSE, 1);
// MQL5: create the handle once in OnInit()
int maHandle;
int OnInit() {
maHandle = iMA(_Symbol, PERIOD_CURRENT, 50, 0, MODE_EMA, PRICE_CLOSE);
return maHandle == INVALID_HANDLE ? INIT_FAILED : INIT_SUCCEEDED;
}
// then read values when needed
double buf[];
ArraySetAsSeries(buf, true);
if (CopyBuffer(maHandle, 0, 0, 3, buf) == 3) {
double ma = buf[1]; // previous closed bar
}
Creating the handle inside OnTick() on every tick is a common conversion mistake that slows the EA and wastes memory.
Opening a trade
// MQL4
int ticket = OrderSend(Symbol(), OP_BUY, 0.10, Ask, 3,
Ask - 200 * Point, Ask + 400 * Point, "EA", 12345, 0, clrGreen);
// MQL5 with CTrade
#include <Trade/Trade.mqh>
CTrade trade;
trade.SetExpertMagicNumber(12345);
trade.SetDeviationInPoints(3);
double ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
trade.Buy(0.10, _Symbol, ask, ask - 200 * _Point, ask + 400 * _Point, "EA");
Looping over open trades
// MQL4
for (int i = OrdersTotal() - 1; i >= 0; i--)
if (OrderSelect(i, SELECT_BY_POS) && OrderMagicNumber() == 12345)
Print(OrderTicket(), " ", OrderProfit());
// MQL5
for (int i = PositionsTotal() - 1; i >= 0; i--) {
ulong ticket = PositionGetTicket(i);
if (PositionGetInteger(POSITION_MAGIC) == 12345)
Print(ticket, " ", PositionGetDouble(POSITION_PROFIT));
}
In MT5, OrdersTotal() counts only pending orders. Open trades are positions.
Conversion checklist
- List every rule the MT4 EA follows before touching code.
- Move indicator creation to
OnInit()and read withCopyBuffer(). - Replace
OrderSend,OrderModifyandOrderClosewithCTrade. - Replace order loops with position loops, and pending-order loops with
OrdersTotal(). - Decide how the EA should behave on a netting account, or require a hedging account.
- Check the broker's filling mode (FOK, IOC or Return) and set it in
CTrade. - Check every trade result and log the error code.
- Run both versions in their Strategy Testers on the same period and compare trades one by one.
Frequently asked questions
Can I convert an EA if I only have the .ex4 file?
No. A compiled file can't be converted. You need the .mq4 source, or the strategy rules so the EA can be rebuilt from scratch.
Is there an automatic MQL4 to MQL5 converter?
There are compatibility libraries that make some MQL4 code compile in MT5, but trading logic still needs checking by hand. Most professional conversions are rewrites.
Will the MT5 version give the same results?
The logic can be identical, but the testers and brokers differ, so small differences in fills are normal. Large differences mean something was converted wrong.
How long does a conversion take?
Simple EAs often take a few days. EAs with many indicators, grids or complex trade management take longer.
Want your EA converted?
We convert MT4 Expert Advisors to MQL5 and verify the result trade by trade against the original. See MT5 Expert Advisor development. Related: build your first MQL5 EA.