mql5-x-compile

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

MQL5 X-Drive CLI Compilation

MQL5 X盘CLI编译

Compile MQL5 indicators/scripts via command line using X: drive mapping to avoid "Program Files" path spaces that cause silent compilation failures.
通过命令行编译MQL5指标/脚本,使用X:驱动器映射来避免因“Program Files”路径空格导致的静默编译失败。

When to Use (Proactive Triggers)

适用场景(主动触发条件)

ALWAYS use this skill after:
  • Editing any
    .mq5
    file (indicator, script, EA)
  • Editing any
    .mqh
    file (library/include)
  • User says "compile", "build", "test it", "try it", "run it"
  • User asks to verify changes work
  • Making code changes that need validation
Also use when:
  • User mentions MetaEditor, compilation errors, or path issues
  • Exit code 1 appears (reminder: exit 1 is normal, check .ex5 file)
  • Need to verify .ex5 file was created
请在以下场景后务必使用此方法:
  • 编辑任何.mq5文件(指标、脚本、EA)
  • 编辑任何.mqh文件(库/头文件)
  • 用户提到“编译”“构建”“测试一下”“试试看”“运行它”
  • 用户要求验证修改是否生效
  • 进行了需要验证的代码修改
以下场景也适用:
  • 用户提到MetaEditor、编译错误或路径问题
  • 出现退出代码1(提示:退出代码1是正常现象,请检查.ex5文件)
  • 需要验证.ex5文件是否已生成

Prerequisites

前置条件

X: drive must be mapped (one-time setup):
bash
BOTTLE="$HOME/Library/Application Support/net.metaquotes.wine.metatrader5"
cd "$BOTTLE/dosdevices"
ln -s "../drive_c/Program Files/MetaTrader 5/MQL5" "x:"
Verify mapping exists:
bash
ls -la "$BOTTLE/dosdevices/" | grep "x:"
必须映射X:驱动器(一次性设置):
bash
BOTTLE="$HOME/Library/Application Support/net.metaquotes.wine.metatrader5"
cd "$BOTTLE/dosdevices"
ln -s "../drive_c/Program Files/MetaTrader 5/MQL5" "x:"
验证映射是否存在:
bash
ls -la "$BOTTLE/dosdevices/" | grep "x:"

Compilation Instructions

编译步骤

Step 1: Construct X: Drive Path

步骤1:构建X:驱动器路径

Convert standard path to X: drive format:
  • Standard:
    C:\Program Files\MetaTrader 5\MQL5\Indicators\Custom\MyIndicator.mq5
  • X: drive:
    X:\Indicators\Custom\MyIndicator.mq5
Pattern: Remove
Program Files/MetaTrader 5/MQL5/
prefix, replace with
X:\
将标准路径转换为X:驱动器格式:
  • 标准路径:
    C:\\Program Files\\MetaTrader 5\\MQL5\\Indicators\\Custom\\MyIndicator.mq5
  • X:驱动器路径:
    X:\\Indicators\\Custom\\MyIndicator.mq5
转换规则:移除
Program Files/MetaTrader 5/MQL5/
前缀,替换为
X:\\

Step 2: Execute Compilation

步骤2:执行编译

bash
WINE="/Applications/MetaTrader 5.app/Contents/SharedSupport/wine/bin/wine64"
WINEPREFIX="$HOME/Library/Application Support/net.metaquotes.wine.metatrader5"
ME="C:/Program Files/MetaTrader 5/MetaEditor64.exe"
bash
WINE="/Applications/MetaTrader 5.app/Contents/SharedSupport/wine/bin/wine64"
WINEPREFIX="$HOME/Library/Application Support/net.metaquotes.wine.metatrader5"
ME="C:/Program Files/MetaTrader 5/MetaEditor64.exe"

Compile using X: drive path

Compile using X: drive path

WINEPREFIX="$WINEPREFIX" "$WINE" "$ME"
/log
/compile:"X:\Indicators\Custom\YourFile.mq5"
/inc:"X:"

**Critical flags**:

- `/log`: Enable compilation logging
- `/compile:"X:\\..."`: Source file (X: drive path with escaped backslashes)
- `/inc:"X:"`: Include directory (X: drive root = MQL5 folder)
WINEPREFIX="$WINEPREFIX" "$WINE" "$ME" \ /log \ /compile:"X:\\Indicators\\Custom\\YourFile.mq5" \ /inc:"X:"

**关键参数**:

- `/log`: 启用编译日志
- `/compile:"X:\\\\..."`: 源文件(X:驱动器路径,反斜杠需转义)
- `/inc:"X:"`: 头文件目录(X:驱动器根目录 = MQL5文件夹)

Step 3: Verify Compilation

步骤3:验证编译

CRITICAL: Wine returns exit code 1 even on successful compilation. Ignore the exit code - always verify by checking the .ex5 file and per-file log.
Check if .ex5 file was created:
bash
BOTTLE="$HOME/Library/Application Support/net.metaquotes.wine.metatrader5"
EX5_FILE="$BOTTLE/drive_c/Program Files/MetaTrader 5/MQL5/Indicators/Custom/YourFile.ex5"
LOG_FILE="$BOTTLE/drive_c/Program Files/MetaTrader 5/MQL5/Indicators/Custom/YourFile.log"
重要提示:即使编译成功,Wine也会返回退出代码1。请忽略退出代码,务必通过检查.ex5文件和单个文件的日志来验证编译结果。
检查.ex5文件是否已生成:
bash
BOTTLE="$HOME/Library/Application Support/net.metaquotes.wine.metatrader5"
EX5_FILE="$BOTTLE/drive_c/Program Files/MetaTrader 5/MQL5/Indicators/Custom/YourFile.ex5"
LOG_FILE="$BOTTLE/drive_c/Program Files/MetaTrader 5/MQL5/Indicators/Custom/YourFile.log"

Check .ex5 exists with recent timestamp

Check .ex5 exists with recent timestamp

if [ -f "$EX5_FILE" ]; then ls -lh "$EX5_FILE" echo "✅ Compilation successful" else echo "❌ Compilation failed" fi
if [ -f "$EX5_FILE" ]; then ls -lh "$EX5_FILE" echo "✅ Compilation successful" else echo "❌ Compilation failed" fi

Check per-file log (UTF-16LE, but often readable with cat)

Check per-file log (UTF-16LE, but often readable with cat)

cat "$LOG_FILE" | grep -i "error|warning|Result"

**Per-file log location**: The `.log` file is created in the same directory as the `.mq5` file (e.g., `Fvg.mq5` → `Fvg.log`). This is more reliable than `logs/metaeditor.log`.
cat "$LOG_FILE" | grep -i "error\|warning\|Result"

**单个文件日志位置**:.log文件会生成在对应.mq5文件的同一目录下(例如`Fvg.mq5`对应`Fvg.log`)。该日志比`logs/metaeditor.log`更可靠。

Common Patterns

常见示例

Example 1: Compile CCI Neutrality Indicator

示例1:编译CCI中性指标

bash
WINE="/Applications/MetaTrader 5.app/Contents/SharedSupport/wine/bin/wine64"
WINEPREFIX="$HOME/Library/Application Support/net.metaquotes.wine.metatrader5"
WINEPREFIX="$WINEPREFIX" "$WINE" "C:/Program Files/MetaTrader 5/MetaEditor64.exe" \
  /log \
  /compile:"X:\\Indicators\\Custom\\Development\\CCINeutrality\\CCI_Neutrality_RoC_DEBUG.mq5" \
  /inc:"X:"
bash
WINE="/Applications/MetaTrader 5.app/Contents/SharedSupport/wine/bin/wine64"
WINEPREFIX="$HOME/Library/Application Support/net.metaquotes.wine.metatrader5"
WINEPREFIX="$WINEPREFIX" "$WINE" "C:/Program Files/MetaTrader 5/MetaEditor64.exe" \\
  /log \\
  /compile:"X:\\\\Indicators\\\\Custom\\\\Development\\\\CCINeutrality\\\\CCI_Neutrality_RoC_DEBUG.mq5" \\
  /inc:"X:"

Result: CCI_Neutrality_RoC_DEBUG.ex5 created (23KB)

Result: CCI_Neutrality_RoC_DEBUG.ex5 created (23KB)

undefined

**结果**:生成CCI_Neutrality_RoC_DEBUG.ex5文件(23KB)

Example 2: Compile Script

示例2:编译脚本

bash
WINEPREFIX="$WINEPREFIX" "$WINE" "C:/Program Files/MetaTrader 5/MetaEditor64.exe" \
  /log \
  /compile:"X:\\Scripts\\DataExport\\ExportAligned.mq5" \
  /inc:"X:"
bash
WINEPREFIX="$WINEPREFIX" "$WINE" "C:/Program Files/MetaTrader 5/MetaEditor64.exe" \\
  /log \\
  /compile:"X:\\\\Scripts\\\\DataExport\\\\ExportAligned.mq5" \\
  /inc:"X:"

Example 3: Verify X: Drive Mapping

示例3:验证X:驱动器映射

bash
WINE="/Applications/MetaTrader 5.app/Contents/SharedSupport/wine/bin/wine64"
WINEPREFIX="$HOME/Library/Application Support/net.metaquotes.wine.metatrader5"
WINEPREFIX="$WINEPREFIX" "$WINE" cmd /c "dir X:\" | head -10
bash
WINE="/Applications/MetaTrader 5.app/Contents/SharedSupport/wine/bin/wine64"
WINEPREFIX="$HOME/Library/Application Support/net.metaquotes.wine.metatrader5"
WINEPREFIX="$WINEPREFIX" "$WINE" cmd /c "dir X:\\" | head -10

Should list: Indicators, Scripts, Include, Experts, etc.

Should list: Indicators, Scripts, Include, Experts, etc.

undefined
undefined

Troubleshooting

应显示:Indicators、Scripts、Include、Experts等目录

Issue: Exit code 1 but compilation succeeded

故障排除

问题:返回退出代码1但编译成功

Cause: Wine always returns exit code 1, even on success Solution: Ignore exit code. Always verify by:
  1. Check
    .ex5
    file exists with recent timestamp:
    ls -la YourFile.ex5
  2. Check per-file log for "0 errors, 0 warnings":
    cat YourFile.log
原因:即使编译成功,Wine也始终返回退出代码1 解决方案忽略退出代码,务必通过以下方式验证:
  1. 检查.ex5文件是否存在且时间戳最新:
    ls -la YourFile.ex5
  2. 检查单个文件日志中是否有“0 errors, 0 warnings”(0错误,0警告):
    cat YourFile.log

Issue: 42 errors, include file not found

问题:出现42个错误,头文件未找到

Cause: Compiling from simple path (e.g.,
C:/file.mq5
) without X: drive Solution: Use X: drive path with
/inc:"X:"
flag
原因:未使用X:驱动器,直接通过简单路径(如
C:/file.mq5
)编译 解决方案:使用X:驱动器路径并添加
/inc:"X:"
参数

Issue: Exit code 0 but no .ex5 file

问题:返回退出代码0但未生成.ex5文件

Cause: Path contains spaces or special characters Solution: Use X: drive path exclusively (no spaces)
原因:路径包含空格或特殊字符 解决方案:仅使用X:驱动器路径(无空格)

Issue: X: drive not found

问题:找不到X:驱动器

Cause: Symlink not created Solution: Run prerequisite setup to create
x:
symlink in dosdevices
原因:未创建符号链接 解决方案:执行前置条件中的设置步骤,在dosdevices目录下创建
x:
符号链接

Issue: Wrong MetaTrader app path

问题:MetaTrader应用路径错误

Cause: MetaTrader 5.app path is different on this machine Solution: Verify with
ls /Applications/MetaTrader\ 5.app
原因:当前机器上MetaTrader 5.app的路径不同 解决方案:通过
ls /Applications/MetaTrader\\ 5.app
命令验证路径

Issue: Can't find metaeditor.log

问题:找不到metaeditor.log

Cause: Looking in wrong location Solution: Use per-file log instead - it's in the same directory as your
.mq5
file (e.g.,
Fvg.mq5
creates
Fvg.log
)
原因:查找位置错误 解决方案:改用单个文件日志,它位于对应.mq5文件的同一目录下(例如
Fvg.mq5
会生成
Fvg.log

Benefits of X: Drive Method

X:驱动器方法的优势

Eliminates path spaces: No "Program Files" in path ✅ Faster compilation: ~1 second compile time ✅ Reliable: Works consistently unlike direct path compilation ✅ Includes resolved:
/inc:"X:"
finds all MQL5 libraries ✅ Simple paths:
X:\Indicators\...
instead of long absolute paths
消除路径空格:路径中不再包含“Program Files” ✅ 编译速度更快:约1秒完成编译 ✅ 稳定性高:与直接路径编译不同,此方法运行稳定 ✅ 头文件自动解析
/inc:"X:"
参数可找到所有MQL5库文件 ✅ 路径更简洁:使用
X:\\Indicators\\...
替代冗长的绝对路径

Comparison: X: Drive vs Manual GUI

对比:X:驱动器方法 vs 手动GUI方法

MethodSpeedAutomationReliability
X: drive CLI~1s✅ Yes✅ High
Manual MetaEditor~3s❌ No✅ High
Direct CLI pathN/A⚠️ Unreliable❌ Fails silently
方法速度自动化程度可靠性
X:驱动器CLI~1秒✅ 是✅ 高
手动MetaEditor~3秒❌ 否✅ 高
直接CLI路径不适用⚠️ 不稳定❌ 静默失败

Integration with Git Workflow

与Git工作流的集成

X: drive mapping is persistent and git-safe:
  • Symlink stored in bottle's
    dosdevices/
    folder
  • Doesn't affect MQL5 source files
  • Works across git branches
  • No configuration files to commit
X:驱动器映射是持久化且对Git友好的:
  • 符号链接存储在bottle的
    dosdevices/
    目录下
  • 不会影响MQL5源文件
  • 在所有Git分支中均可使用
  • 无需提交任何配置文件

Security Notes

安全说明

  • X: drive is used for compilation, which generates .ex5 / .log files, resulting in write operations.
  • No execution of compiled files during compilation
  • MetaEditor runs in sandboxed Wine environment
  • No network access during compilation
  • X:驱动器用于编译过程,会生成.ex5/.log文件,涉及写入操作。
  • 编译过程中不会执行已编译的文件
  • MetaEditor在沙箱化的Wine环境中运行
  • 编译过程中无网络访问

Quick Reference

快速参考

Compilation command template:
bash
WINEPREFIX="$HOME/Library/Application Support/net.metaquotes.wine.metatrader5" \
  /Applications/MetaTrader\ 5.app/Contents/SharedSupport/wine/bin/wine64 \
  "C:/Program Files/MetaTrader 5/MetaEditor64.exe" \
  /log /compile:"X:\\Path\\To\\File.mq5" /inc:"X:"
Bottle location:
~/Library/Application Support/net.metaquotes.wine.metatrader5/
X: drive maps to:
MQL5/
folder inside bottle
Verification: Check for
.ex5
file and review per-file
.log
(ignore exit code - it's always 1)
编译命令模板
bash
WINEPREFIX="$HOME/Library/Application Support/net.metaquotes.wine.metatrader5" \\
  /Applications/MetaTrader\\ 5.app/Contents/SharedSupport/wine/bin/wine64 \\
  "C:/Program Files/MetaTrader 5/MetaEditor64.exe" \\
  /log /compile:"X:\\\\Path\\\\To\\\\File.mq5" /inc:"X:"
Bottle目录
~/Library/Application Support/net.metaquotes.wine.metatrader5/
X:驱动器映射到:bottle内部的
MQL5/
文件夹
验证方式:检查.ex5文件是否存在,并查看单个文件的.log日志(忽略退出代码,它始终为1)",