Editing and compiling MQ5 source code in MetaEditor

How to Edit and Compile an MQ5 Source Code in MetaEditor (Step-by-Step)

Buying an Expert Advisor with its MQ5 source code gives you something a compiled .ex5 file never will: full control. You can read the logic, change the inputs, add filters and recompile your own version. This guide shows you, step by step, how to open, edit and compile an .mq5 file in MetaEditor, the free code editor built into MetaTrader 5.

MQ5 vs EX5: What Is the Difference ?

  • .mq5 is the human-readable source code written in MQL5. You can open and edit it.
  • .ex5 is the compiled file MetaTrader 5 actually runs. It cannot be edited.

When you compile an .mq5 file, MetaEditor generates a new .ex5 file. That is the version you attach to your chart. For a broader overview, read our guide to EAs with source code.

Step 1: Place the File in the Right Folder

In MetaTrader 5, click File > Open Data Folder, then open MQL5:

  • Experts for Expert Advisors
  • Indicators for custom indicators
  • Include for any .mqh library files delivered with the source code

If your package contains .mqh files, copy them into Include (or the exact subfolder named in the code). Missing include files are the most common reason a source code fails to compile.

Step 2: Open MetaEditor

Press F4 in MetaTrader 5, or click the MetaEditor icon in the toolbar. In the Navigator panel on the left, expand Experts and double-click your .mq5 file to open it.

Step 3: Make a Backup First

Before you change anything, save a copy with File > Save As and a new name, for example MyEA_v2.mq5. If an edit breaks the logic, you can always return to the original.

Step 4: Find and Edit the Inputs

Most of what traders want to change lives in the input parameters near the top of the file. They look like this:

input double LotSize      = 0.01;
input int    StopLoss     = 300;
input int    MagicNumber  = 12345;

Changing a default value here changes what appears in the EA settings window. Typical safe edits include default lot size, stop loss and take profit distances, the magic number, trading hours and on/off switches for filters.

Deeper changes, such as new entry conditions, a news filter or a daily loss limit, are added inside functions like OnTick(). Make one change at a time and test it before adding the next.

Step 5: Compile with F7

Press F7 or click Compile. The result appears in the Toolbox > Errors tab at the bottom:

  • 0 errors: MetaEditor has created the .ex5 file in the same folder.
  • Errors: double-click each line to jump to the problem. Most are a missing semicolon, an undeclared variable or a missing include file.
  • Warnings: the file still compiles, but read them. They often point to real logic issues such as implicit type conversions.

Step 6: Load the New Version in MetaTrader 5

Back in MetaTrader 5, right-click Expert Advisors in the Navigator and choose Refresh. Your recompiled EA appears in the list. Attach it to a chart, check the inputs and enable Algo Trading. Our MT5 installation guide covers this part in detail.

Step 7: Test Every Change

Never put an edited EA straight onto a live account. Run it in the Strategy Tester on real ticks, compare the results with the original version, then forward test on demo. Follow our MT5 backtesting guide for the exact settings.

Useful Edits Traders Make to MQ5 Source Code

  • Adding a maximum spread filter to avoid trading during wide spreads.
  • Adding a daily loss limit or equity stop for prop firm rules.
  • Restricting trading to London and New York sessions.
  • Changing lot sizing from fixed lots to a percentage of balance.
  • Adapting symbol names for brokers that use suffixes (XAUUSD.m, GOLD, XAUUSDc).

FAQ

Do I need programming experience to edit an MQ5 file ?

Not for input changes. Editing default values is as simple as changing a number. Adding new logic requires basic MQL5 knowledge.

Can I convert an EX5 file back to MQ5 ?

No. The compiled file cannot be turned back into readable code, which is why the source code is valuable.

Can I use MQ5 code in MetaTrader 4 ?

No. MT4 uses MQL4 (.mq4). The languages are similar but not compatible without conversion.

Conclusion

With MetaEditor, an MQ5 source code becomes a strategy you can audit, adapt and improve. Back up the original, change one thing at a time, compile with F7 and test before going live. Explore our MQ5 gold EA source codes and MQ5 forex EAs, indicators and tools.

Risk disclaimer: trading forex and CFDs with automated systems involves significant risk of loss.

Back to blog