MQ4 and MQ5 Source Code Explained: What You Get and How to Use It
EABuild Team · · 9 min read

On this page
- What MQ4 and MQ5 files are
- Source file versus compiled file
- What the source of an EA contains
- How to open and compile source code in MetaEditor
- What editing the source lets you do
- Licence limits and what the download is
- An example: Smart Gold Hunter source code for MT5
- Before you trade a changed or new EA live
If you are weighing up MQ5 source code against a ready-to-run expert advisor, the first thing to understand is what the file actually is. Source code is the human-readable text a program is written in, and MetaTrader cannot run it until it is turned into a compiled file. This guide explains the MQ4 and MQ5 formats, what the compiled EX4 and EX5 files are, how MetaEditor opens and compiles code, what editing lets you do and what it does not, and what to check before you trade a changed EA.
What MQ4 and MQ5 files are
MetaTrader uses its own programming language for expert advisors (EAs, programs that place and manage trades automatically), indicators and scripts. MetaTrader 4 uses MQL4 and saves source files with the .mq4 extension. MetaTrader 5 uses MQL5 and saves source files with .mq5. Both are plain text, so you can open them in any text editor, although MetaEditor is the tool made for the job.
The two languages look similar but they are not interchangeable. MQL5 has a different trading model and a larger standard library, so code written for MT4 usually needs real changes before it compiles for MT5, and the other way round. Treat an MQ4 file as an MT4 project and an MQ5 file as an MT5 project.
Include files and other parts you may see
A source download can contain more than one text file. An .mqh file is a header or include file that holds shared functions, and the main file pulls it in with an #include line. Keep these files together in the folder structure they came in, or the compiler will report that it cannot find them.
Source file versus compiled file
A compiled file is the result of running the source through the compiler. On MT4 the result is an .ex4 file, and on MT5 it is an .ex5 file. This is the file MetaTrader loads when you attach an EA to a chart. It runs the logic, but you cannot read the logic from it.
| Point | Source file (MQ4 or MQ5) | Compiled file (EX4 or EX5) |
|---|---|---|
| Readable by a person | Yes, it is text | No |
| Runs on a chart | No, it must be compiled first | Yes |
| Can be edited | Yes, in MetaEditor or a text editor | No, you need the source to change the logic |
| Where you use it | MetaEditor | The Experts or Indicators folder of the terminal |
Because of this, a plain EA download and a source code download serve different readers. With the EA you attach the compiled file and adjust the inputs. With source code you can also read how the EA reaches its decisions and change them. If you only want to run an EA, our guide to installing an EA in MT5 covers the steps, and there is a matching guide for MT4 at installing an EA in MT4.
What the source of an EA contains
You do not need to be a programmer to get something out of the code, but it helps to know its usual layout. An MQL expert advisor is built around a few standard functions that MetaTrader calls for you.
- Properties and inputs. Lines near the top declare the version and the input variables. These are the settings you see in the EA's inputs window, such as lot size, stop loss or the hours it may trade.
- OnInit. Runs once when the EA is attached. It usually checks the symbol and the inputs and prepares any indicators the EA reads.
- OnTick. Runs every time a new price tick arrives for the chart symbol. Most of the trading logic lives here or in functions it calls.
- OnDeinit. Runs when the EA is removed or the terminal closes, and cleans up.
- Helper functions. Code that calculates lot sizes, checks the spread, counts open positions or sends the order request.
Reading these parts tells you things a product description cannot: which indicators the EA reads, what conditions open a trade, how stops are placed and which filters block a trade. The full language reference is in the MQL5 documentation, which also documents the functions MQL4 shares with it.
How to open and compile source code in MetaEditor
MetaEditor is the code editor that installs with MetaTrader. The steps below work the same way in both platforms, apart from the extension.
- Open MetaTrader and choose File, then Open Data Folder. The window that opens is the terminal's data folder.
- For MT5, go into MQL5 and then Experts. For MT4, go into MQL4 and then Experts. Copy the source folder or file there. Put include files in the same relative place they had in the download.
- Press F4 in the terminal to launch MetaEditor, or use the toolbar button.
- In the Navigator panel on the left, find your file under Experts and double-click it to open it.
- Press F7, or use the Compile button. MetaEditor builds the file and lists any errors and warnings in the Errors tab at the bottom.
- When the build finishes with no errors, a new EX4 or EX5 file appears next to the source file.
- Back in the terminal, refresh the Navigator, and the EA shows up under Expert Advisors, ready to attach to a chart.
Common compile problems
- File not found for an include. A header file is missing or in the wrong folder. Restore the folder layout from the download.
- Wrong platform. MQ4 code compiled in MT5, or the reverse, produces many errors at once. Use MetaEditor from the platform the code was written for.
- Warnings. A warning does not stop the build, but read it. It often points at a value that may be converted in a way you did not expect.
What editing the source lets you do
Having the source means you can make changes that the inputs window does not offer. Typical examples are changing the default input values, adding a time filter, limiting the symbols the EA accepts, adjusting how the lot size is calculated or adding extra log messages so you can see why a trade was or was not opened. You can also study the code to learn how an EA is structured, which is a common way to learn MQL.
Keep the limits in view as well. A change that compiles is not a change that works. Every edit can alter how the EA trades, and an error in order handling can open positions you did not intend. Nothing in a source download promises that your modification will behave well, and no one is obliged to help you debug it.
A sensible way to change code
- Keep an untouched copy of the download before you edit anything.
- Change one thing at a time and compile after each change.
- Test in the Strategy Tester first, then on a demo account, before any live account. A tester run shows how the logic behaves on stored price data, but it does not predict future results.
- Write a short note of what you changed, so you can undo it.
Licence limits and what the download is
Owning source files does not by itself tell you what you may do with them. Terms differ from one seller and product to the next, so read the product page and the site's terms of use before you share, resell or publish code you have changed. eabuild.app is a shop that sells digital downloads. It is not the developer or publisher of the products, so questions about the code's design go to its author, not to us.
Also be careful with compiled files and code from unknown places. A file you cannot read can do anything an EA is allowed to do. With source code, you can at least check what it does before you compile it, for example whether it calls external libraries or sends web requests.
An example: Smart Gold Hunter source code for MT5
To make this concrete, the Smart Gold Hunter EA Source Code (MQ5) listing is an MT5 download of an EA that trades XAUUSD on the M5 and M15 timeframes. According to its listing, the strategy uses scalping, news trading and hedging, the version is 3.5 and the recommended minimum deposit is $100. The package holds one compiled file, one source file and six set files, and it does not use a DLL.
The six set files are saved lists of input settings that you load from the EA's inputs window, each a starting configuration rather than a recommendation of results. The source file is the part this guide is about: you can open it in MetaEditor, read how the entry and exit rules are written and recompile it if you change it.
Scalping means many short trades that aim at small price moves, so spread and execution speed matter. Hedging means holding buy and sell positions on the same symbol at once, so check that your broker and account type allow it. News trading means trading around economic releases, when spreads can widen and slippage is common. Reading the code is a good way to see how an EA handles each of these.
Before you trade a changed or new EA live
- Compile it with no errors in the correct version of MetaEditor.
- Check that the inputs, symbol and timeframe match what the code was written for.
- Run it on a demo account long enough to see it open, manage and close trades.
- Check how it behaves around spread widening and in fast markets.
- Start live with a lot size you can comfortably afford and keep a record of changes.
If you want to explore more than one project, every product in the shop is included in the membership, and you can browse all EA source code downloads in the catalogue.
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, MetaEditor, MQL5


