Compile MQL4 (.mq4) and MQL5 (.mq5) sources to .ex4/.ex5 on Linux with the mql-compiler command, which runs MetaQuotes' MetaEditor under Wine, and read its compile errors. Use when the user wants to compile MetaTrader expert advisors, indicators, scripts or libraries without Windows.
---
name: mql-compiler
description: "Compile MQL4 (.mq4) and MQL5 (.mq5) sources to .ex4/.ex5 on Linux with the mql-compiler command, which runs MetaQuotes' MetaEditor under Wine, and read its compile errors. Use when the user wants to compile MetaTrader expert advisors, indicators, scripts or libraries without Windows."
homepage: https://github.com/psyb0t/docker-mql-compiler
user-invocable: true
metadata:
{ "openclaw": { "emoji": "🛠️", "requires": { "bins": ["mql-compiler"] } } }
permissions:
network: "The first compile downloads the MT4 and MT5 installers from MetaQuotes' download hosts and checks their MetaQuotes signature. No source code is sent anywhere."
shell: "The mql-compiler command."
filesystem: "Reads the .mq4/.mq5 sources and include directories you name. Writes the .ex4/.ex5 next to each source."
---
# mql-compiler
`mql-compiler` compiles MetaTrader sources with MetaQuotes' own MetaEditor, running under Wine. It needs no Windows, no MetaTrader terminal and no trading account, and runs on x86-64 (amd64) machines only.
## Setup
The `mql-compiler` command must already be installed and Docker must be running. Installation and upgrade instructions are in the docs at https://psyb0t.github.io/docker-mql-compiler/. The command needs no URL or token: it reads the paths you pass it. If it is missing or fails to start, see [troubleshooting](references/troubleshooting.md) and tell the user.
## Safety
- The command runs as the caller's user and group IDs, mounts only the directories you name (and each source's `MQL4`/`MQL5` directory), and writes nothing into your tree except the `.ex4`/`.ex5` next to each source. Terminal directories passed with `--mt4-terminal`/`--mt5-terminal` are mounted read-only.
- The first compile downloads the MT4 and MT5 setup programs and installs them into the command's toolchain cache, after checking that they are signed by MetaQuotes.
- Do not delete the toolchain cache or uninstall the command unless the user asks. The cache is shared by every user on the machine and rebuilding it is a full download.
## Compile
```bash
mql-compiler [OPTIONS] [FILE or DIRECTORY ...]
mql-compiler 2>/dev/null # every .mq4 and .mq5 under the current directory
mql-compiler /abs/path/MQL5/Experts/Foo.mq5 2>/dev/null # one file, from any directory
mql-compiler MQL5/Indicators MQL4 2>/dev/null # directories, searched recursively
mql-compiler -I /path/to/shared/lib MQL5 2>/dev/null # a shared include directory outside the tree
mql-compiler --werror MQL5 2>/dev/null # warnings count as failures
```
Options: `-I DIR` / `--include DIR` (extra include directory, repeatable), `--mt4-terminal DIR` / `--mt5-terminal DIR` (compile with that portable terminal's MetaEditor and standard library, mounted read-only; the terminal must have been started once so its standard library is unpacked), `--werror`, `--watch` (recompile on change until stopped; do not use it in an agent run, it never exits on its own), `--text` and `--format json|text|gnu` (output format; leave the JSON default), `--setup` (prepare the toolchain only), `--update` (refresh MT4/MT5 to the newest builds), `--version` (image version and MetaEditor builds), `-h`/`--help`.
- Arguments are files or directories, relative or absolute, anywhere on the machine. No argument means the current directory. No `cd` is needed. A path that does not exist stops the run with exit `2` before anything compiles.
- A directory is searched recursively for `.mq4` and `.mq5` (any case), sorted by path; symlinked files inside it are skipped. Arguments are handled in the order given; a file named twice compiles once, in its first position. `.mqh` and other files are skipped in directories and rejected (exit `2`) when named directly. A run that finds no sources exits `2`.
- One container runs the whole list, one file at a time, never in parallel. A failing file does not stop the run. Pass everything to one call: two runs at the same time take turns (the second logs that it is waiting), so splitting a job only makes it slower.
- Paths with spaces work; the messages still show the real paths.
- The extension alone picks the compiler: `.mq4` goes to MT4's MetaEditor, `.mq5` to MT5's. The directory only decides the includes: a `.mq4` uses the nearest directory named `MQL4` (any case) above it, a `.mq5` the nearest `MQL5`. Experts, indicators, scripts and libraries all compile the same way.
- Each `.ex4`/`.ex5` is written next to its source with the same base name, owned by the caller. The old one is deleted right before the file compiles, so after a run an `.ex4`/`.ex5` exists only if this run produced it. Still report success from the file's `"status": "ok"`.
- The container sees only the mounted directories (each source's `MQL4`/`MQL5` directory, or a loose file's own directory). Includes, resources and symlinks that point outside them are missing files inside the container.
- The first run downloads and installs MT4 and MT5 and unpacks their standard libraries, about a minute and a half. A compile installs only the platforms its sources need, and never one given with `--mt4-terminal`/`--mt5-terminal`. Allow for it in timeouts; later runs take seconds. `mql-compiler --setup` does only this.
## How includes resolve
- `#include <...>` searches one merged directory built for the run: the matching platform's MetaQuotes standard library (`<Trade/Trade.mqh>`, `<Arrays/ArrayObj.mqh>`, `<stdlib.mqh>`, ...), then each `-I` directory, then the `Include` directory of the nearest enclosing `MQL4` (for `.mq4`) or `MQL5` (for `.mq5`) directory, each laid over the one before. When two have the same path, the later one wins, so the user's own `Include` always wins.
- An `-I` directory stands in for `Include`: with `-I /libs` and `/libs/MyLib/x.mqh`, `#include <MyLib/x.mqh>` works.
- `#resource "\\Files\\x.txt"` and other `\\`-rooted paths start at that same `MQL4`/`MQL5` directory.
- `#include "..."` resolves next to the source.
- A source outside any `MQL4`/`MQL5` directory gets the standard library only, and no `\\`-rooted resources.
- An error inside a standard header is reported at the header's path in the toolchain (`/cache/uid-<UID>/wine/drive_c/...`, inside the toolchain cache, not on the host). It usually means the user's code broke something the header relies on, such as a macro with the same name as a standard class; fix the user's code, never the toolchain.
- Nothing is written into the user's directories or the toolchain; the merged directory is temporary.
So keep a terminal-style tree: sources under `MQL5/Experts`, `MQL5/Indicators`, ..., headers under `MQL5/Include` (same for `MQL4`). When copying a source elsewhere to compile it, copy its `MQL4`/`MQL5` directory with it, or its custom includes go missing.
## Read the result
stdout is JSON Lines, one object per line with a `type`. Parse it; do not scrape text.
```json
{"type":"file","source":"MQL5/Experts/Foo.mq5","platform":"mt5","status":"ok","output":"MQL5/Experts/Foo.ex5","errors":0,"warnings":0,"messages":[]}
{"type":"file","source":"MQL5/Experts/Bar.mq5","platform":"mt5","status":"failed","output":null,"errors":1,"warnings":0,"messages":[{"severity":"error","file":"MQL5/Experts/Bar.mq5","line":41,"column":7,"code":256,"text":"undeclared identifier 'lots'"}]}
{"type":"summary","total":2,"compiled":1,"failed":1,"werror":false}
```
- `file` (one per source): `source`, `platform` (`mt4`/`mt5`), `status` (`ok`/`failed`), `output` (the `.ex4`/`.ex5`, or `null`), `errors` and `warnings` (counts, `null` when MetaEditor wrote no result), `messages` (each `{severity, file, line, column, code, text}`), and `log` (MetaEditor's raw lines) only when it wrote no result.
- `summary` (last): `total`, `compiled`, `failed`, `werror`.
- `error` (instead of the rest, when the run cannot start): `message`. A bad path, a bad option or a failed setup.
- `version` (from `--version`, `--setup`, `--update`): `version`, `mt4_metaeditor`, `mt5_metaeditor`.
Paths are relative to the current directory when inside it, absolute otherwise. Fix the source at each message's `file`, `line` and `column`, and compile again. Progress and diagnostics go to stderr as JSON log lines; `2>/dev/null` hides them.
| Exit | Meaning |
|---|---|
| `0` | Every file compiled |
| `1` | At least one file failed: compile errors, warnings under `--werror`, or MetaEditor wrote no result (see the `failed` records) |
| `2` | Bad path or option, no sources found, or the toolchain could not be set up (see the `error` record) |
| `125` and other Docker codes | Docker itself failed: daemon down, image not available, a bad mount (stdout may be empty) |
## Rules for agents
- Use the `mql-compiler` command. Do not call the Docker image directly unless the user asks.
- Report compile errors with their file, line, column, code and text from the `messages`. A file compiled when its record says `"status": "ok"`, even if the run exits `1` because of another file. Claim the whole build succeeded only when the exit status is `0` and the summary shows `"failed": 0`.
- Mention warnings to the user even when the build passes; they are in `messages` with `"severity": "warning"`.
- `file '...' not found` on an `#include <...>` means the header is in neither the user's `Include` nor the standard library. Check the source sits under the right `MQL4`/`MQL5` directory before editing code.
don't have the plugin yet? install it then click "run inline in claude" again.
added explicit intent, inputs with env vars and docker prereqs, procedure with 7 numbered steps and input/output pairs, decision points for 12 edge cases (first run, missing paths, docker failures, symlinks, werror, duplicate files, partial success), output contract with all json schemas, and outcome signal with success criteria and performance notes.
compile mql4 and mql5 sources to executable binaries (.ex4 and .ex5) on linux using metaquotes' metaeditor, which runs under wine in the psyb0t/mql-compiler docker image. the toolchain needs docker on x86-64 (amd64) only: no windows os, no metatrader terminal, no trading account, no arm support.
use this skill when you need to compile mql4 or mql5 expert advisors, indicators, scripts, or libraries to binary form on linux. the skill handles multi-file compilation, include path resolution, and error reporting. it's the right choice when you want to avoid installing metatrader or windows, or when you need to automate builds in ci/cd pipelines.
external connections:
mql-compiler command on PATH. if missing, install with:curl -fsSL https://raw.githubusercontent.com/psyb0t/docker-mql-compiler/master/install.sh | bash
installer lands in ~/.local/bin; add to PATH if needed.environment variables (optional):
MQL_COMPILER_MT4_URL: custom mt4 metaeditor installer url (first run only; pair with fresh volume).MQL_COMPILER_MT5_URL: custom mt5 metaeditor installer url (first run only; pair with fresh volume).MQL_COMPILER_INSTALL_TIMEOUT: seconds to wait for each platform install (default 600). increases only timeout; does not speed up download.MQL_COMPILER_SECCOMP: custom seccomp profile path (leave alone unless user requests).MQL_COMPILER_VOLUME: docker volume name for mt4/mt5 cache (default mql-compiler-cache). shared across all users on the machine.docker prerequisites:
mql-compiler-cache volume (about 90 seconds). subsequent runs use cached installs (seconds).MQL4/MQL5 folder, or the loose file's own folder. includes, resources, or symlinks pointing outside these folders are invisible inside the container.source files and structure:
.mq4 and .mq5 source files (case-insensitive extension)..mqh) under Include/ subfolder of the enclosing MQL4 or MQL5 folder.Files/ subfolder of the enclosing MQL4 or MQL5 folder.MQL4/Experts, MQL4/Indicators, MQL4/Include, MQL5/Experts, MQL5/Indicators, MQL5/Include, etc.compilation options (flags):
-I DIR or --include DIR: add extra include folder (repeatable; user includes override standard library).--mt4-terminal DIR: compile with that portable mt4 terminal's metaeditor and standard library (read-only mount; terminal must have started once).--mt5-terminal DIR: compile with that portable mt5 terminal's metaeditor and standard library (read-only mount; terminal must have started once).--werror: treat warnings as failures (exit 1 if any warnings appear).--watch: recompile on file change until stopped (incompatible with agent runs; never exits on its own).--format json|text|gnu: output format (default json; use json for parsing).--text: legacy alias for --format text.--setup: install mt4/mt5 only, do not compile.--update: reinstall mt4 and mt5 with newest metaeditor builds.--version: print image version and metaeditor build numbers.-h or --help: print usage.arguments (files or folders):
cd required..mq4 and .mq5 (any case), sorted by path; symlinked files inside folders are skipped..mqh and other files are skipped in folders and rejected (exit 2) when named directly.verify docker is running and mql-compiler is installed
docker ps to confirm daemon.which mql-compiler to confirm command on PATH.determine source files and enclosing folder structure
MQL4 or MQL5 folder for each source (case-insensitive).build the mql-compiler command line
-I folder, add it to the command.--mt4-terminal or --mt5-terminal given, add those flags.--werror requested, add it.invoke mql-compiler and capture output
mql-compiler [options] [paths] 2>/dev/null (or keep stderr if debugging).wait for first-run setup (if needed)
error message from stdout.parse json output
type field.file records: extract source, status, output, errors, warnings, messages.summary record: extract total, compiled, failed.error record: extract message.report success or failure
file record with status: "failed", print the source path and all messages (severity, line, column, code, text).if a path does not exist:
error record in stdout.if no sources are found (no .mq4 or .mq5 files):
error record in stdout.if docker daemon is not running:
if this is the first run (mt4/mt5 not yet installed):
if a file compile succeeds but the run exits 1 (because another file failed):
status field for each file record in stdout.status: "ok", even if the overall exit code is 1.if an #include error says "file not found" for a standard header:
Include folder and the metaquotes standard library.MQL4/MQL5 folder.messages and suggest checking the include path and folder structure.if --werror is used and the summary shows warnings:
errors: 0.messages with severity: "warning").--werror.if the user provides both --mt4-terminal and --mt5-terminal:
if a source is outside any MQL4/MQL5 folder:
#resource "\\Files\\x.txt" paths are not found (no Files folder to resolve against).MQL4 or MQL5 folder tree.if the user compiles the same file twice in one command:
if a symlink inside a source folder points outside the enclosing MQL4/MQL5 tree:
stdout (json lines):
file record (one per source):
{
"type": "file",
"source": "<path>",
"platform": "mt4" or "mt5",
"status": "ok" or "failed",
"output": "<.ex4/.ex5 path>" or null,
"errors": <int> or null,
"warnings": <int> or null,
"messages": [
{
"severity": "error" or "warning",
"file": "<path>",
"line": <int>,
"column": <int>,
"code": <int>,
"text": "<message>"
}
],
"log": [<raw metaeditor lines>] (only if no result was written)
}
summary record (last):
{
"type": "summary",
"total": <int>,
"compiled": <int>,
"failed": <int>,
"werror": boolean
}
error record (if the run cannot start):
{
"type": "error",
"message": "<error description>"
}
version record (from --version, --setup, or --update):
{
"type": "version",
"version": "<image version>",
"mt4_metaeditor": "<build string>",
"mt5_metaeditor": "<build string>"
}
output files:
.mq4 produces a .ex4 next to the source (same base name)..mq5 produces a .ex5 next to the source (same base name).diagnostic copy:
docker run --rm -v mql-compiler-cache:/c busybox cat /c/uid-$(id -u)/logs/last-run.logthe skill worked if:
"failed": 0.file record with "status": "ok"..ex4 or .ex5 binary next to it (readable, executable permissions for the user).partial success (some files compiled, some failed):
"compiled": <N> and "failed": <M> where M > 0.messages array; each message has file, line, column, code, and text.the skill failed to run at all if:
error record.warnings are present if:
file record has messages with "severity": "warning".status: "ok", mention these to the user (code quality concern).--werror was used and warnings exist, the exit code is 1.performance clues:
docker logs on the running container) or the last-run.log in the volume.