MQL4 vs MQL5: Language Differences for EA Source Code
EABuild Team · · 13 min read

On this page
- How MQL4 and MQL5 differ at the language level
- Order handling: OrderSend in MQL4 against requests and positions in MQL5
- Prices and indicators: direct values or handles and buffers
- Event functions and file types in MQL4 and MQL5 projects
- MQL4 and MQL5 side by side
- Porting an MQL4 EA to MQL5: where the effort goes
- Three source code packages as MQL4 and MQL5 examples
- Which reader should work in MQL4 and which in MQL5
- FAQ
- Opening one MQ4 file and one MQ5 file in MetaEditor this week
MQL4 vs MQL5 is the first question for anyone who opens an expert advisor's source file and wants to change it, or move it to the other platform. The two languages share much of their syntax, yet the trading functions, price access and some events work differently. This post compares them for EA source code: order handling, event functions, file types and the effort of porting, with three source code packages from the shop as examples. It makes no promise that a port will compile or trade the same way.
How MQL4 and MQL5 differ at the language level
MQL4 is the language of MetaTrader 4 and MQL5 is the language of MetaTrader 5. Each has its own compiler inside MetaEditor, the code editor that installs with the platform. A program written for one is compiled by that platform's MetaEditor and runs only in that platform's terminal.
Where the two feel the same
Both languages look like C. An EA in either one declares input variables that appear in the inputs window, defines functions, uses #include lines to pull in shared files, and reacts to events that the terminal calls. A trader who can read one will recognise the layout of the other. The names of many helper functions also match, such as iClose, iHigh and Print.
Where they diverge
MQL5 was designed with classes, structures and a larger standard library in mind, and it separates trading into orders, deals and positions. MQL4 later gained classes too, so the syntax gap is smaller than older tutorials suggest. The wide gap is the trading model and the way the code reads prices and indicators. That is where most of the rewriting lands.
For the platform side of the story, such as netting and hedging accounts and the strategy tester, read the post on MT4 vs MT5 for expert advisors. This post stays inside the code.
Order handling: OrderSend in MQL4 against requests and positions in MQL5
Order handling is the part of an EA that changes most between the two languages, so it is the part to read first when you compare two versions of one EA.
How MQL4 handles orders
In MQL4 a single function, OrderSend, takes the symbol, order type, volume, price, slippage, stop loss, take profit, a comment and a magic number (an integer that tags the orders of one EA). It returns a ticket number. To manage trades, the code loops through them with OrdersTotal and OrderSelect, then reads fields such as the type, the open price and the magic number. Closing and modifying use OrderClose and OrderModify. Here an open trade and a pending order are both called orders, and one list holds them.
How MQL5 handles orders
MQL5 splits the idea in three. An order is an instruction to the server, a deal is the result of an execution, and a position is what remains open on a symbol. To send an instruction, the code fills a trade request structure (MqlTradeRequest), calls OrderSend and then reads a result structure (MqlTradeResult) for the return code. Open trades are read with PositionsTotal and PositionSelect, and past trades through the history functions.
Many MQL5 EAs hide this detail behind the CTrade class from the standard library, which offers methods for buying, selling, closing and modifying. A source file that includes the trade header and calls these methods reads much shorter than one that fills the request structure by hand.
Details that trip up a port
- Same name, different meaning. The function name OrderSend exists in both languages but takes different arguments and returns a different type.
- Filling mode. An MQL5 request can carry a filling type, which tells the server how to treat partial execution. MQL4 has no such field, so the porter must pick a value that the broker accepts.
- Stops and lots. Minimum stop distance and volume step come from MarketInfo in MQL4 and from SymbolInfoInteger and SymbolInfoDouble in MQL5. Code that rounds lots or checks stop levels needs those calls replaced.
- Account type. Logic that expects several positions on one symbol behaves differently on a netting account, where orders merge. Ask your broker which account type you have.
Prices and indicators: direct values or handles and buffers
The second big difference is how code asks for market data. MQL4 is direct. MQL5 is indirect, and that costs a porter some work.
In MQL4 the predefined variables Bid and Ask hold the current prices, and the arrays Open, High, Low, Close and Time hold the bars of the chart. An indicator call such as iMA returns a number for the bar you name. One line gives you a moving average value.
In MQL5 the same call returns a handle, a reference to an indicator that the terminal calculates for you. You create it once, usually in the initialisation event, and then copy values into an array with CopyBuffer when you need them. Prices come from SymbolInfoDouble or the MqlTick structure, and bar data from functions such as CopyRates and CopyClose. Arrays can be set to read newest-first with ArraySetAsSeries, a detail that causes off-by-one mistakes when the order is not checked.
This is why a short MQL4 signal block often grows into several steps in MQL5: create the handle, check it is valid, copy the buffer, test that the copy succeeded, then read the value. The logic is the same, but the code is longer. It also means a port needs care with bar indexes, because a signal computed on bar 1 in one language must still be computed on bar 1 in the other.
Event functions and file types in MQL4 and MQL5 projects
Both languages call standard functions for you when something happens in the terminal. Most EAs rely on three of them.
- OnInit runs once when the EA is attached. In MQL5 it returns a status code such as INIT_SUCCEEDED, which lets the EA refuse to start when a check fails.
- OnTick runs on each new price tick for the chart symbol. Most trading logic starts here.
- OnDeinit runs when the EA is removed and releases what it used, for example indicator handles in MQL5.
Beyond these, OnTimer and OnChartEvent exist in both. MQL5 adds events that report on trading itself, such as OnTradeTransaction, and one for market depth changes. An MQL4 EA has no counterpart for these, so a port to MQL5 can use them, while a port back must do without them. A source file that depends on such an event cannot simply be pasted into MT4.
File types you will meet
The file names follow the platform. MT4 source files end in .mq4 and compile to .ex4. MT5 source files end in .mq5 and compile to .ex5. Include files use .mqh in both, but the contents are written in one language or the other, so an include file from one project is not a free download for the other. The post on MQ4 and MQ5 source code goes through these formats and what each file is for.
Compile the file in the MetaEditor that belongs to the platform. MQ4 code compiled in the MT5 editor, or the reverse, fails with many errors at once. The steps for the MT5 side are in the guide to compiling an MQ5 file in MetaEditor. The full language reference is on the MQL5 documentation site.
MQL4 and MQL5 side by side
| Topic | MQL4 | MQL5 |
|---|---|---|
| Platform | MetaTrader 4 | MetaTrader 5 |
| Source and compiled files | .mq4 and .ex4 | .mq5 and .ex5 |
| Sending a trade | One OrderSend call that returns a ticket | A request structure, OrderSend, then a result structure; often wrapped by CTrade |
| Trade model in code | Orders, one list for open and pending | Orders, deals and positions |
| Current prices | Predefined Bid and Ask | SymbolInfoDouble or MqlTick |
| Indicator values | Function returns the value | Function returns a handle; CopyBuffer reads values |
| Trade events | None specific to trades | OnTrade and OnTradeTransaction |
| Standard library | Smaller | Larger, with trade, array and indicator classes |
Porting an MQL4 EA to MQL5: where the effort goes
Porting means rewriting the parts that differ and then checking that the EA still behaves as intended. No compiler converts one language to the other for you. The usual work falls into a few places.
- Copy the project and keep the downloaded files untouched. Work on a duplicate in a separate folder so you can compare it with the starting point.
- Replace the order code. Rebuild the opening, closing and modification functions with requests or CTrade, and rewrite any loop that walks through open trades.
- Replace the data access. Swap direct indicator calls for handles and buffers, and review every bar index and every array direction.
- Update symbol and account checks. Replace the MarketInfo calls and decide how the EA should behave on netting and hedging accounts.
- Compile, then fix errors one group at a time. Read warnings as well, since a value that is silently converted can change a lot size or a stop.
- Test in stages. Run the strategy tester to see that orders open and close as the logic says, then watch a demo account for several sessions.
The effort depends on how much the EA does. A short EA that reads two indicators and opens one position at a time is a smaller job than one that manages many positions, pending orders and custom files. Even a port that compiles cleanly can trade differently, because execution, tick data and account rules differ between the platforms. A tester run shows mechanics, not future results. Going the other direction, from MQL5 to MQL4, has its own problems, since features such as the extra trade events have no equivalent.
If you are not ready to port, the plain answer is to use the version written for your platform. A source package is built for one language, and the listing says which.
Three source code packages as MQL4 and MQL5 examples
Three downloads in the shop show the split in practice. Each is described by its listing facts only, and none of them is a ready-made port.
An MQL4 package for MT4
HFT Gold EA Source Code (MQ4) is the editable MQL4 source of a scalping EA for MetaTrader 4, version 1.0. It is listed for XAUUSD and BTCUSD on the M1 timeframe, with a recommended minimum deposit of $100. The download holds one compiled file and one source file, with no set files and no DLL. It suits a reader who wants to study how an MT4 EA is written in MQL4.
Two MQL5 packages for MT5
Smart Gold Hunter EA Source Code (MQ5) is the MQL5 source of an EA for MetaTrader 5, version 3.5, listed for XAUUSD on M5 and M15. The listing names scalping, news trading and hedging, and the package holds one compiled file, one source file and six set files. Because the listing mentions hedging, check your account type with your broker before you attach it. It is MQL5 source, so it is a way to read an MT5 EA's code.
Adaptive Gold Scalper EA Source Code (MQ5) is the MQL5 source of a scalping, breakout and price action EA for MetaTrader 5, version 2.2. It is listed for XAUUSD on H1 with a recommended minimum deposit of $500, and the package has one compiled file, one source file and two set files. A set file is a saved list of input settings that you load in the EA's inputs window. Each is a starting configuration, not a recommendation of results.
Putting the MQ4 and MQ5 files in two windows of MetaEditor is a practical way to see the differences in this post. The packages differ in platform, symbols, strategy and timeframe, so they are not versions of one EA. Compare the structure, not the strategy.
Which reader should work in MQL4 and which in MQL5
MQL4 may fit if
- your broker and your EAs are on MetaTrader 4;
- you want a smaller language and a single list of orders to read;
- you plan to edit an existing MQ4 file rather than start a new project.
MQL5 may fit if
- your broker and your EAs are on MetaTrader 5;
- you want classes, a larger standard library and trade events;
- you are comfortable with handles, buffers and the order, deal and position model.
In both cases, the language follows the platform you trade on. Choose the platform first, then read the source written for it.
FAQ
Can an MQL4 EA run on MetaTrader 5 without changes?
No, an MQL4 EA cannot run on MetaTrader 5 without changes, because each language compiles only in its own platform's MetaEditor. MT4 source files end in .mq4 and compile to .ex4, while MT5 files end in .mq5 and compile to .ex5. The trading functions and price access also differ, so a port means rewriting the order code and indicator calls, then testing on a demo account.
Why does OrderSend behave differently in MQL4 and MQL5?
OrderSend behaves differently because MQL4 takes the symbol, type, volume, price and stops as arguments and returns a ticket, while MQL5 expects a filled trade request structure and reports the outcome in a separate result structure. The function name exists in both languages, but the arguments and return type differ. Many MQL5 EAs use the CTrade class from the standard library to hide that detail.
Why does an indicator call return a handle in MQL5 but a value in MQL4?
An indicator call returns a handle in MQL5 because the terminal calculates the indicator for you and the code reads it later. In MQL4 a call such as iMA returns the value for the bar you name. In MQL5 you create the handle once, then copy values with CopyBuffer, so a short MQL4 signal block grows into several steps and bar indexes need checking.
Which language should I learn if I only trade on MetaTrader 4?
Learn MQL4 if you only trade on MetaTrader 4, because the language follows the platform you run. An MQL4 program compiles in MT4's MetaEditor and runs only in the MT4 terminal. Choose the platform your broker offers first, then read the source written for it. MQL5 matters when your broker and your EAs are on MetaTrader 5.
Opening one MQ4 file and one MQ5 file in MetaEditor this week
A short exercise makes the differences concrete. Open one MQ4 file in MT4's MetaEditor and one MQ5 file in MT5's MetaEditor. In each, find the inputs, the OnTick function, the code that sends an order and the code that reads an indicator or price. Write down where the two differ, then compile and test any change on a demo account before a live one.
You can browse the EA source code downloads in the catalogue. Every product is also included in the membership, which gives access to the whole catalogue as digital downloads.
Trading foreign exchange and CFDs carries a high level of risk and may not be suitable for all investors. Past performance does not guarantee future results; only trade with money you can afford to lose.
Tags: EA Source Code, MQL5, MQL4, MetaEditor


