Migrating from MQL4 to MQL5: A Complete Guide for MetaTrader Developers
Introduction
As MetaTrader 5 continues to gain popularity among traders and brokers worldwide, many developers are looking to migrate their existing MQL4 projects to the newer MQL5 environment. Whether you have created Expert Advisors (EAs), custom indicators, scripts, or trading utilities, moving your code to MQL5 allows you to take advantage of a more powerful programming language, faster execution, improved testing capabilities, and support for additional financial markets.
Although MQL4 and MQL5 share many similarities, they are not identical programming languages. An MQL4 program cannot simply be copied into MetaEditor 5 and compiled successfully. The migration process requires understanding the architectural differences between the two languages and adapting your code accordingly.
1. Chart Periods
In MQL5 chart period constants changed, and some new time periods (M2, M3, M4, M6, M10, M12, H2, H3, H6, H8, H12) were added. To convert MQL4 time periods you can use the following function:
ENUM_TIMEFRAMES TFMigrate(int tf) { switch(tf) { case 0: return(PERIOD_CURRENT); case 1: return(PERIOD_M1); case 5: return(PERIOD_M5); case 15: return(PERIOD_M15); case 30: return(PERIOD_M30); case 60: return(PERIOD_H1); case 240: return(PERIOD_H4); case 1440: return(PERIOD_D1); case 10080: return(PERIOD_W1); case 43200: return(PERIOD_MN1); case 2: return(PERIOD_M2); case 3: return(PERIOD_M3); case 4: return(PERIOD_M4); case 6: return(PERIOD_M6); case 10: return(PERIOD_M10); case 12: return(PERIOD_M12); case 16385: return(PERIOD_H1); case 16386: return(PERIOD_H2); case 16387: return(PERIOD_H3); case 16388: return(PERIOD_H4); case 16390: return(PERIOD_H6); case 16392: return(PERIOD_H8); case 16396: return(PERIOD_H12); case 16408: return(PERIOD_D1); case 32769: return(PERIOD_W1); case 49153: return(PERIOD_MN1); default: return(PERIOD_CURRENT); } }
2. Declaring Contants
Some of standard MQL4 constants are absent in MQL5, therefore they should be declared:
//+------------------------------------------------------------------+ //| InitMQL4.mqh | //| Copyright DC2008 | //| https://www.mql5.com | //+------------------------------------------------------------------+ #property copyright "keiji" #property copyright "DC2008" #property link "https://www.mql5.com" //--- Declaration of constants #define OP_BUY 0 //Buy #define OP_SELL 1 //Sell #define OP_BUYLIMIT 2 //Pending order of BUY LIMIT type #define OP_SELLLIMIT 3 //Pending order of SELL LIMIT type #define OP_BUYSTOP 4 //Pending order of BUY STOP type #define OP_SELLSTOP 5 //Pending order of SELL STOP type //--- #define MODE_OPEN 0 #define MODE_CLOSE 3 #define MODE_VOLUME 4 #define MODE_REAL_VOLUME 5 #define MODE_TRADES 0 #define MODE_HISTORY 1 #define SELECT_BY_POS 0 #define SELECT_BY_TICKET 1 //--- #define DOUBLE_VALUE 0 #define FLOAT_VALUE 1 #define LONG_VALUE INT_VALUE //--- #define CHART_BAR 0 #define CHART_CANDLE 1 //--- #define MODE_ASCEND 0 #define MODE_DESCEND 1 //--- #define MODE_LOW 1 #define MODE_HIGH 2 #define MODE_TIME 5 #define MODE_BID 9 #define MODE_ASK 10 #define MODE_POINT 11 #define MODE_DIGITS 12 #define MODE_SPREAD 13 #define MODE_STOPLEVEL 14 #define MODE_LOTSIZE 15 #define MODE_TICKVALUE 16 #define MODE_TICKSIZE 17 #define MODE_SWAPLONG 18 #define MODE_SWAPSHORT 19 #define MODE_STARTING 20 #define MODE_EXPIRATION 21 #define MODE_TRADEALLOWED 22 #define MODE_MINLOT 23 #define MODE_LOTSTEP 24 #define MODE_MAXLOT 25 #define MODE_SWAPTYPE 26 #define MODE_PROFITCALCMODE 27 #define MODE_MARGINCALCMODE 28 #define MODE_MARGININIT 29 #define MODE_MARGINMAINTENANCE 30 #define MODE_MARGINHEDGED 31 #define MODE_MARGINREQUIRED 32 #define MODE_FREEZELEVEL 33 //--- #define EMPTY -1
3. Predefined Variables
| MQL4 | MQL5 | Description |
|---|---|---|
double Ask
|
MqlTick last_tick; SymbolInfoTick(_Symbol,last_tick); double Ask=last_tick.ask; |
Ask The latest known ask price for the current symbol. SymbolInfoTick |
int Bars |
int Bars=Bars(_Symbol,_Period); |
Bars Number of bars in the current chart. Bars |
double Bid
|
MqlTick last_tick; SymbolInfoTick(_Symbol,last_tick); double Bid=last_tick.bid; |
Bid The latest known bid price of the current symbol. SymbolInfoTick |
double Close[]
|
double Close[]; int count; // number of elements to copy ArraySetAsSeries(Close,true); CopyClose(_Symbol,_Period,0,count,Close); |
Close Series array that contains close prices for each bar of the current chart. CopyClose, ArraySetAsSeries |
int Digits |
int Digits=_Digits; |
Digits Number of digits after the decimal point for the current symbol prices. _Digits |
double High[]
|
double High[]; int count; // number of elements to copy ArraySetAsSeries(High,true); CopyHigh(_Symbol,_Period,0,count,High); |
High Series array that contains the highest prices of each bar of the current chart. CopyHigh, ArraySetAsSeries |
double Low[]
|
double Low[]; int count; // number of elements to copy ArraySetAsSeries(Low,true); CopyLow(_Symbol,_Period,0,count,Low); |
Low Series array that contains the lowest prices of each bar of the current chart. CopyLow, ArraySetAsSeries |
double Open[]
|
double Open[]; int count; // number of elements to copy ArraySetAsSeries(Open,true); CopyOpen(_Symbol,_Period,0,count,Open); |
Open Series array that contains open prices of each bar of the current chart. CopyOpen, ArraySetAsSeries |
double Point |
double Point=_Point; |
Point The current symbol point value in the quote currency. _Point |
datetime Time[]
|
datetime Time[]; int count; // number of elements to copy ArraySetAsSeries(Time,true); CopyTime(_Symbol,_Period,0,count,Time); |
Time Series array that contains open time of each bar of the current chart. Data like datetime represent time, in seconds, that has passed since 00:00 a.m. of 1 January, 1970. CopyTime, ArraySetAsSeries |
double Volume[]
|
long Volume[]; int count; // number of elements to copy ArraySetAsSeries(Volume,true); CopyTickVolume(_Symbol,_Period,0,count,Volume); |
Volume Series array that contains tick volumes of each bar of the current chart. CopyTickVolume, ArraySetAsSeries |
4. Account Information
| MQL4 | MQL5 | Description |
|---|---|---|
double AccountBalance()
|
double AccountInfoDouble(ACCOUNT_BALANCE) |
AccountBalance Returns balance value of the current account (the amount of money on the account). AccountInfoDouble |
double AccountCredit()
|
double AccountInfoDouble(ACCOUNT_CREDIT) |
AccountCredit Returns credit value of the current account. AccountInfoDouble |
string AccountCompany()
|
string AccountInfoString(ACCOUNT_COMPANY) |
AccountCompany Returns the brokerage company name where the current account was registered. AccountInfoString |
string AccountCurrency()
|
string AccountInfoString(ACCOUNT_CURRENCY) |
AccountCurrency Returns currency name of the current account. AccountInfoString |
double AccountEquity()
|
double AccountInfoDouble(ACCOUNT_EQUITY) |
AccountEquity Returns equity value of the current account. Equity calculation depends on trading server settings. AccountInfoDouble |
double AccountFreeMargin()
|
double AccountInfoDouble(ACCOUNT_FREEMARGIN) |
AccountFreeMargin Returns free margin value of the current account. AccountInfoDouble |
double AccountFreeMarginCheck(string symbol, int cmd, double volume) |
- | AccountFreeMarginCheck Returns free margin that remains after the specified position has been opened at the current price on the current account. |
double AccountFreeMarginMode()
|
- | AccountFreeMarginMode Calculation mode of free margin allowed to open positions on the current account. |
int AccountLeverage()
|
int AccountInfoInteger(ACCOUNT_LEVERAGE) |
AccountLeverage Returns leverage of the current account. AccountInfoInteger |
double AccountMargin()
|
double AccountInfoDouble(ACCOUNT_MARGIN) |
AccountMargin Returns margin value of the current account. AccountInfoDouble |
string AccountName()
|
string AccountInfoString(ACCOUNT_NAME) |
AccountName Returns the current account name. AccountInfoString |
int AccountNumber()
|
int AccountInfoInteger(ACCOUNT_LOGIN) |
AccountNumber Returns the number of the current account. AccountInfoInteger |
double AccountProfit()
|
double AccountInfoDouble(ACCOUNT_PROFIT) |
AccountProfit Returns profit value of the current account. AccountInfoDouble |
string AccountServer()
|
string AccountInfoString(ACCOUNT_SERVER) |
AccountServer Returns the connected server name. AccountInfoString |
int AccountStopoutLevel()
|
double AccountInfoDouble(ACCOUNT_MARGIN_SO_SO) |
AccountStopoutLevel Returns the value of the Stop Out level. AccountInfoDouble |
int AccountStopoutMode()
|
int AccountInfoInteger(ACCOUNT_MARGIN_SO_MODE) |
AccountStopoutMode Returns the calculation mode for the Stop Out level. AccountInfoInteger |
5. Array Functions
| MQL4 | MQL5 | Description |
|---|---|---|
int ArrayBsearch(double array[], double value, int count=WHOLE_ARRAY, int start=0, int direction=MODE_ASCEND) |
int ArrayBsearch(double array[], double searched_value ) |
ArrayBsearch The function searches for a specified value in a one-dimension numeric array. ArrayBsearch |
int ArrayCopy(object&dest[], object source[], int start_dest=0, int start_source=0, int count=WHOLE_ARRAY) |
int ArrayCopy(void dst_array[], void src_array[], int dst_start=0, int src_start=0, int cnt=WHOLE_ARRAY ) |
ArrayCopy Copies an array to another one. Arrays must be of the same type, but arrays with type double[], int[], datetime[], color[], and bool[] can be copied as arrays of the same type. Returns the amount of copied elements. ArrayCopy |
int ArrayCopyRates(double&dest_array[], string symbol=NULL, int timeframe=0) |
- | ArrayCopyRates Copies data of the current chart bars to the two-dimensional array of RateInfo[][6] type and returns copied bars amount, or -1 if failed. |
int ArrayCopySeries(double&array[], int series_index, string symbol=NULL, int timeframe=0) |
int ArrayCopySeriesMQL4(double &array[], int series_index, string symbol=NULL, int tf=0) { ENUM_TIMEFRAMES timeframe=TFMigrate(tf); int count=Bars(symbol,timeframe); switch(series_index) { case MODE_OPEN: return(CopyOpen(symbol,timeframe,0,count,array)); case MODE_LOW: return(CopyLow(symbol,timeframe,0,count,array)); case MODE_HIGH: return(CopyHigh(symbol,timeframe,0,count,array)); case MODE_CLOSE: return(CopyClose(symbol,timeframe,0,count,array)); default: return(0); } return(0); } |
ArrayCopySeries Copies a timeseries array to a custom array and returns the count of the copied elements. CopyOpen, CopyLow, CopyHigh, CopyClose, Bars |
int ArrayDimension( object array[])
|
- | ArrayDimension Returns the multidimensional array rank. |
bool ArrayGetAsSeries( object array[]) |
bool ArrayGetAsSeries(void array) |
ArrayGetAsSeries Returns TRUE if an array is organized as a timeseries array (array elements are indexed from the last to the first one), otherwise returns FALSE. ArrayGetAsSeries |
int ArrayInitialize(double &array[], double value) |
int ArrayInitializeMQL4(double &array[], double value) { ArrayInitialize(array,value); return(ArraySize(array)); } |
ArrayInitialize Sets all elements of a numeric array to the same value. Returns the count of initialized elements. ArrayInitialize, ArraySize |
bool ArrayIsSeries( object array[]) |
bool ArrayIsSeries(void array[]) |
ArrayIsSeries Returns TRUE if the array under check is a timeseries array (Time[],Open[],Close[],High[],Low[], or Volume[]), otherwise returns FALSE. ArrayIsSeries |
int ArrayMaximum(double array[], int count=WHOLE_ARRAY, int start=0) |
int ArrayMaximumMQL4(double &array[], int count=WHOLE_ARRAY, int start=0) { return(ArrayMaximum(array,start,count)); } |
ArrayMaximum Searches for the element with the maximal value. The function returns position of this maximal element in the array. ArrayMaximum |
int ArrayMinimum(double array[], int count=WHOLE_ARRAY, int start=0) |
int ArrayMinimumMQL4(double &array[], int count=WHOLE_ARRAY, int start=0) { return(ArrayMinimum(array,start,count)); } |
ArrayMinimum Searches for the element with the minimal value. The function returns position of this minimal element in the array. ArrayMinimum |
int ArrayRange(object array[], int range_index) |
int ArrayRange(void array[], int rank_index ) |
ArrayRange Returns the count of elements in the given dimension of the array. ArrayRange |
int ArrayResize(object &array[], int new_size) |
int ArrayResize(void array[], int new_size, int allocated_size=0 ) |
ArrayResize Sets a new size for the first dimension. ArrayResize |
bool ArraySetAsSeries(double &array[], bool set) |
bool ArraySetAsSeries(void array[], bool set ) |
ArraySetAsSeries Returns the count of elements in the given dimension of the array. Since indexes are zero-based, the size of dimension is 1 greater than the largest index. ArraySetAsSeries |
int ArraySize( object array[]) |
int ArraySize(void array[]) |
ArraySize Returns the count of elements contained in the array. ArraySize |
int ArraySort(double &array[], int count=WHOLE_ARRAY, int start=0, int sort_dir=MODE_ASCEND) |
int ArraySortMQL4(double &array[], int count=WHOLE_ARRAY, int start=0, int sort_dir=MODE_ASCEND) { switch(sort_dir) { case MODE_ASCEND: ArraySetAsSeries(array,true); case MODE_DESCEND: ArraySetAsSeries(array,false); default: ArraySetAsSeries(array,true); } ArraySort(array); return(0); } |
ArraySort Sorts numeric arrays by first dimension. Series arrays cannot be sorted by ArraySort(). ArraySort, ArraySetAsSeries |
6. Checkup
| MQL4 | MQL5 | Description |
|---|---|---|
int GetLastError() |
Related Articles
How to Convert an Expert Advisor or Indicator from MQL4 to MQL5 (or Vice Versa)
M1 Quantum Assistant MT5 EA: Complete User Guide & Money Management System
How to Detect RSI Divergence in Forex Trading: Bullish & Bearish Signals
The Ichimoku Cloud System: A Deep Dive into One of the Most Powerful Forex Trading Strategies |