How to compile an MQL4 file with a command-line tool?

To compile a source code file from a command line, you can use MetaEditor for that. For example:

metaeditor.exe /compile:"C:\Program Files\Platform\MQL5\Scripts\myscript.mq5"

For 64-bit use metaeditor64.exe instead.

In Linux/macOS, this can be achieved using Wine, e.g.:

wine metaeditor.exe /compile:"MQL4/Experts/MACD Sample.mq4"

For mass compilation, you can specify folder, like:

metaeditor.exe" /compile:"MQL5\Scripts"

To specify custom MQL5/MQL4 folder with include files, you can use /inc parameter, for example:

metaeditor.exe /compile:"./Scripts" /inc:"C:\Program Files\TradingPlatform 2\MQL5"

For additional information about the compilation process, you can use /log:

metaeditor.exe /compile:"C:\Program Files\Platform\MQL5\Scripts\myscript.mq5" /log 

To check for the syntax only, add extra /s.

If the compilation fails, the MQL4.log file would be created in the platform folder with the relevant details. It's going to be in UTF-16 format, so you may need a special tool for it (such as Vim, Ruby, findstr or rg).

To specify the custom compilation log file, use /log:file.log parameter, e.g.

metaeditor.exe /log:errors.log /compile:.

Note: Display to the standard output is not supported (although on Linux you can use: /log:CON).

For more information, check: Compilation from the Command Line


Some time ago you could download the compiler of MQL4/MQL5 programs that runs separately from MetaEditor — MQL.exe. It was distributed separately from the terminal and you could download it at the following addresses:

https://download.mql5.com/cdn/web/metaquotes.software.corp/mt5/mql.exe

https://download.mql5.com/cdn/web/metaquotes.software.corp/mt5/mql64.exe

Usage (as per MQL4/MQL5 Compiler build 1162 from 02 Jul 2015):

mql.exe [<flags>] filename.mq5
        /mql5     - compile mql5 source
        /mql4     - compile mql4 source
        /s        - syntax check only
        /i:<path> - set working directory
        /o        - use code optimizer

However the standalone compiler was intentionally removed, so now links point to the installer in favor of MetaEditor.


Much older version of MetaTrader prior to build 600 had metalang.exe included with the platform.

However in build 616, MetaQuotes intentionally has removed the compiler (mql.exe/mql64.exe) from the standard MetaTrader installation.

This means if you upgrade your MT platform (>616), the compiler executable will be removed.


This is a little late, but since I wrote a little script for UltraEdit/UEStudio and have received heaps of help from stackoverflow, here is my script. It compiles then copies the ex4 to a number of test MT4 installations:

The "Compile" button on UE does:

"MT4Compile.bat" "%FilePath" "%FileName"
Start in path eg: D:\Development\MQ4 (Location of MT4Compile.bat)

Normally my source code is in a library tree under D:\Development\MQ4[Group][ExpertName][FileName].mq4

Contents of D:\Development\MQ4\MT4Compile.bat:

@echo off
rem Version: 1.1
rem Date:   24 Sep 2013
rem Author: Shawky
rem Refer to HELP: for info

SET XC=xcopy /D /Y /V /F /I
SET PROGDIR=D:\Development\Go Pro Demo (MQ4 Testing)
SET DSTPATH=%PROGDIR%\experts

SET SIMPATH1=G:\Apps\MT4\BackTest IC (Recent)\experts
SET SIMPATH2=G:\Apps\MT4\BackTest IC (All)\experts
SET SIMPATH3=G:\Apps\MT4\BackTest Go (All)\experts
SET DEPLOYPATH=D:\Development\Deployment\experts

SET SRCPATH=%1
SET SRCPATH=%SRCPATH:"=%
IF "%SRCPATH%"=="" (
    SET SRCPATH=[Arg1]
)

SET APPNAME=%2
SET APPNAME=%APPNAME:"=%
IF "%APPNAME%"=="" (
    SET APPNAME=[Arg2]
)

SET SRCFILE=%APPNAME%.mq4
SET DSTFILE=%APPNAME%.ex4


SET CMD="%PROGDIR%\metalang.exe" "%SRCFILE%" "%DSTFILE%"

IF "%SRCPATH%"=="[Arg1]"  GOTO HELP
IF "%APPNAME%"=="[Arg2]"  GOTO HELP

cd %SRCPATH%

IF NOT EXIST "%SRCFILE%"  (
    SET ERROR=Error: File "%SRCFILE%" does not exist in %SRCPATH%
    GOTO HELP
)

echo .
echo Compiling %SRCFILE% to %DSTPATH%\%DSTFILE%
echo .
DEL *.log
%CMD%
IF EXIST "%DSTFILE%" (
    echo .
    echo Distributing executable to SIM and Deployment paths
    %XC% "%DSTFILE%" "%DSTPATH%\"
    IF EXIST "%SIMPATH1%"   %XC% "%DSTFILE%" "%SIMPATH1%\"
    IF EXIST "%SIMPATH2%"   %XC% "%DSTFILE%" "%SIMPATH2%\"
    IF EXIST "%SIMPATH3%"   %XC% "%DSTFILE%" "%SIMPATH3%\"
    IF EXIST "%DEPLOYPATH%" copy /B /Y "%DSTFILE%" "%DEPLOYPATH%\%APPNAME% (Dev).ex4"
)

goto END

:HELP
echo .  Metatrader 4 Command Line utility for compiling MT4 programmes.
echo .
echo .  This batch files allows MT4 applications to be compiled from a directory other than .\experts.
echo .  The output will be copied to experts after compilation.
echo .
echo .  [Arg1] = Path to MT4 application directory
echo .  [Arg2] = Name (without extension) of the main MQ4 source code to compile.
echo .
echo .  Example:
echo .      MT4Compile.bat "D:\Development\MQ4\MyExpert\" "PrimaryMQ4FileName"
echo .
echo .  Programme Directory: %PROGDIR%
echo .  Source Path:         %SRCPATH%
echo .  Source File:         %SRCFILE%
echo .  Destination File:    %DSTFILE%
echo .  Target Path:         %DSTPATH%
echo .
echo .  Argument 1:          %SRCPATH%
echo .  Argument 2:          %APPNAME%
echo .
echo .  Commands to execute would be:
echo .
echo .      %CMD%
echo .      %XC% "%DSTFILE%" "%DSTPATH%\"
echo .
echo .  %ERROR%
echo .

pause

:END

All the best.

Shawky


Yes, there is an executable in the install directory of the terminal. It is called metalang.exe.