mt5-log-reader

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

MT5 Log Reader

MT5日志读取工具

Overview

概述

Read MetaTrader 5 log files programmatically to access Print() output from indicators, scripts, and expert advisors. Implements dual logging pattern where Print() statements go to MT5 logs (human-readable) and CSV files provide structured data for validation.
以编程方式读取MetaTrader 5日志文件,获取指标、脚本及EA(智能交易系统)的Print()输出。实现双重日志模式:Print()语句输出到MT5日志(人类易读格式),同时CSV文件提供结构化数据用于验证。

When to Use

使用场景

Use this skill when:
  • Validating MT5 indicator/script execution
  • Checking compilation or runtime errors
  • Analyzing Print() debug output from indicators
  • Verifying unit test results (Test_PatternDetector, Test_ArrowManager)
  • User mentions checking "Experts pane" or "log" manually
  • User asks "did it work?" or "check the output"
在以下场景中使用该工具:
  • 验证MT5指标/脚本的执行情况
  • 检查编译或运行时错误
  • 分析指标的Print()调试输出
  • 验证单元测试结果(如Test_PatternDetector、Test_ArrowManager)
  • 用户提及手动查看“Experts面板”或“日志”时
  • 用户询问“是否运行成功?”或“检查输出结果”时

Log File Structure

日志文件结构

Runtime Logs (Experts Pane)

运行时日志(Experts面板)

MT5 runtime logs are stored at:
$MQL5_ROOT/Program Files/MetaTrader 5/MQL5/Logs/YYYYMMDD.log
File Format:
  • Encoding: UTF-16LE (Little Endian) - Read tool handles automatically
  • Structure: Tab-separated fields (timestamp, source, message)
  • Size: Grows throughout day (typically 10-100KB, can reach 5MB+)
MT5运行时日志存储路径:
$MQL5_ROOT/Program Files/MetaTrader 5/MQL5/Logs/YYYYMMDD.log
文件格式
  • 编码:UTF-16LE(小端序)- 本工具会自动处理
  • 结构:制表符分隔字段(时间戳、来源、消息)
  • 大小:全天持续增长(通常10-100KB,最大可达5MB以上)

Compilation Logs (Per-File)

编译日志(按文件生成)

MetaEditor creates a
.log
file next to each compiled
.mq5
:
undefined
MetaEditor会在每个已编译的
.mq5
文件旁生成一个
.log
文件:
undefined

Example: Fvg.mq5 creates Fvg.log in the same directory

示例:Fvg.mq5会在同一目录生成Fvg.log

$MQL5_ROOT/Program Files/MetaTrader 5/MQL5/Indicators/Custom/Development/FVG/Fvg.log

**File Format**:
- Encoding: UTF-16LE (often readable with plain `cat`)
- Contains: Compilation progress, error count, warnings, timing
- Success indicator: "0 errors, 0 warnings"

**Important**: Wine/CrossOver returns exit code 1 even on successful compilation. Always check the `.log` file or verify `.ex5` exists.
$MQL5_ROOT/Program Files/MetaTrader 5/MQL5/Indicators/Custom/Development/FVG/Fvg.log

**文件格式**:
- 编码:UTF-16LE(通常可直接用`cat`命令读取)
- 内容包含:编译进度、错误数量、警告信息、耗时
- 成功标识:“0 errors, 0 warnings”

**重要提示**:在Wine/CrossOver环境下,即使编译成功也会返回退出码1。请务必检查`.log`文件或确认`.ex5`文件是否存在。

Workflow

工作流程

Step 1: Construct Log Path

步骤1:构建日志路径

Determine today's date and build the absolute path:
bash
TODAY=$(date +"%Y%m%d")
LOG_FILE="$MQL5_ROOT/Program Files/MetaTrader 5/MQL5/Logs/${TODAY}.log"
获取当前日期并生成绝对路径:
bash
TODAY=$(date +"%Y%m%d")
LOG_FILE="$MQL5_ROOT/Program Files/MetaTrader 5/MQL5/Logs/${TODAY}.log"

Step 2: Search for Relevant Entries

步骤2:搜索相关条目

Use Grep tool to filter log entries (more efficient than reading entire file):
Pattern: indicator name, "error", "ERROR", "v0\\.4\\.0", "Phase 2", etc.
Path: Log file from Step 1
Output mode: "content"
-n: true (show line numbers)
-A: 5 (show 5 lines after matches for context)
Common search patterns:
  • CCI Rising Test|CCI_Rising_Test
    - Find specific indicator output
  • error|ERROR|failed
    - Find error messages
  • v0\\.4\\.0|v0\\.3\\.0
    - Find specific version output
  • Phase \\d+|Test arrow created
    - Find initialization messages
  • DEBUG Bar
    - Find calculation output
使用Grep工具过滤日志条目(比读取整个文件更高效):
匹配规则:指标名称、"error"、"ERROR"、"v0\.4\.0"、"Phase 2"等
路径:步骤1中获取的日志文件
输出模式:"content"
-n: true(显示行号)
-A: 5(显示匹配结果后5行上下文)
常用搜索规则
  • CCI Rising Test|CCI_Rising_Test
    - 查找特定指标的输出
  • error|ERROR|failed
    - 查找错误消息
  • v0\.4\.0|v0\.3\.0
    - 查找特定版本的输出
  • Phase \d+|Test arrow created
    - 查找初始化消息
  • DEBUG Bar
    - 查找计算输出

Step 3: Analyze Results

步骤3:分析结果

Check the grep output for:
  • Timestamps - Verify output matches expected execution time
  • Error messages - Identify specific failures with context
  • Version numbers - Confirm correct .ex5 file is loaded
  • DEBUG output - Validate calculation results
  • Test results - Verify unit test pass/fail status
检查Grep输出的以下内容:
  • 时间戳 - 验证输出与预期执行时间匹配
  • 错误消息 - 结合上下文定位具体失败原因
  • 版本号 - 确认加载的是正确的
    .ex5
    文件
  • DEBUG输出 - 验证计算结果
  • 测试结果 - 确认单元测试的通过/失败状态

Common Validation Patterns

常见验证模式

Check indicator version loaded

检查加载的指标版本

After compilation, verify the new .ex5 file is loaded:
bash
undefined
编译完成后,验证新的
.ex5
文件是否已加载:
bash
undefined

Search for version string in recent log entries

在最新日志条目中搜索版本字符串

Pattern: "v0\.4\.0|=== CCI Rising Test" Output mode: content Context: -A 2

Look for initialization message with version number and timestamp matching the reload time.
匹配规则:"v0.4.0|=== CCI Rising Test" 输出模式:content 上下文:-A 2

查找包含版本号的初始化消息,确认其时间戳在编译完成时间之后。

Find runtime errors

查找运行时错误

bash
Pattern: "error|ERROR|failed|Failed"
Output mode: content
Context: -A 3 -B 1  # Show 3 lines after, 1 before
Analyze error codes (e.g., 4003 = invalid time) and error context.
bash
匹配规则:"error|ERROR|failed|Failed"
输出模式:content
上下文:-A 3 -B 1  # 显示匹配结果后3行、前1行上下文
分析错误代码(如4003 = 无效时间)及错误上下文。

Monitor calculation output

监控计算输出

bash
Pattern: "DEBUG Bar"
Output mode: content
Context: -A 0  # No extra context needed
-n: true  # Show line numbers
Verify calculation values are reasonable (not EMPTY_VALUE, not reversed).
bash
匹配规则:"DEBUG Bar"
输出模式:content
上下文:-A 0  # 无需额外上下文
-n: true  # 显示行号
验证计算值是否合理(不是EMPTY_VALUE,没有反转)。

Verify test arrow creation

验证测试箭头创建

bash
Pattern: "Phase 2|Test arrow created|Failed to create"
Output mode: content
Context: -A 2
Check for success message with timestamp.
bash
匹配规则:"Phase 2|Test arrow created|Failed to create"
输出模式:content
上下文:-A 2
检查包含时间戳的成功创建消息。

Large Log Files

大日志文件处理

If log file exceeds 5MB (Read tool limit):
  1. Use Grep exclusively (more efficient than reading entire file)
  2. Use time-based filtering with
    -A
    and
    -B
    context
  3. Search for specific indicator names to narrow results
当日志文件超过5MB(读取工具限制)时:
  1. 仅使用Grep工具(比读取整个文件效率更高)
  2. 使用
    -A
    -B
    参数结合时间范围过滤
  3. 搜索特定指标名称缩小结果范围

Integration with Dual Logging

与双重日志模式的集成

This skill accesses one half of the dual logging pattern:
  1. MT5 Log Files (this skill) - Human-readable Print() output
  2. CSV Files (CSVLogger.mqh) - Structured audit trails
Both are accessible programmatically without user intervention.
本工具访问双重日志模式的其中一部分:
  1. MT5日志文件(本工具处理)- 人类易读的Print()输出
  2. CSV文件(CSVLogger.mqh)- 结构化审计追踪数据
两者均可通过编程方式访问,无需人工干预。

Security Considerations

安全注意事项

  • Log files may contain sensitive trading data (symbol names, account info)
  • Do not expose absolute paths unnecessarily in user-facing output
  • Filter sensitive information when reporting results
  • No file modification operations allowed (read-only access)
  • 日志文件可能包含敏感交易数据(品种名称、账户信息等)
  • 避免在面向用户的输出中暴露绝对路径
  • 报告结果时过滤敏感信息
  • 仅允许只读访问,禁止修改文件

Examples

示例

Example 1: Verify indicator reloaded after compilation

示例1:验证编译后指标是否重新加载

User: "I reloaded the indicator, can you check if v0.4.0 is running?"

Action:
1. Grep for "v0\\.4\\.0|CCI Rising Test v0\\.4"
2. Check most recent match timestamp
3. Verify it's after compilation time (16:59)

Output: "Yes, v0.4.0 loaded at 17:01 on EURUSD M1 chart"
用户:“我重新加载了指标,能帮我检查v0.4.0是否在运行吗?”

操作步骤:
1. 搜索"v0\.4\.0|CCI Rising Test v0\.4"
2. 检查最新匹配结果的时间戳
3. 确认时间戳在编译完成时间(16:59)之后

输出:“已确认,v0.4.0于17:01在EURUSD M1图表上加载完成”

Example 2: Find why histogram is empty

示例2:排查直方图为空的原因

User: "The last 19 bars are still empty, check the log"

Action:
1. Grep for "DEBUG Bar|CCI=" pattern
2. Look for EMPTY_VALUE or unusual values
3. Check for error messages with -A 3 context

Output: "Found 'CCI=179769313486231570...' which is EMPTY_VALUE constant. The indicator didn't calculate CCI for those bars."
用户:“最近19根K线的直方图还是空的,检查一下日志”

操作步骤:
1. 搜索"DEBUG Bar|CCI="规则
2. 查找EMPTY_VALUE或异常值
3. 使用-A 3参数查看错误消息上下文

输出:“发现‘CCI=179769313486231570...’,这是EMPTY_VALUE常量。指标未计算这些K线的CCI值。”

Example 3: Validate calculation fix

示例3:验证计算修复效果

User: "Check if the absolute value fix is working"

Action:
1. Grep for "DEBUG Bar.*\\|CCI\\|=" (looking for |CCI|= output)
2. Compare with old format "DEBUG Bar.*CCI=" (without |CCI|)
3. Check timestamp matches latest compilation

Output: "The log shows old format (no |CCI|=) at 16:56. Need to reload to get v0.4.0 from 16:59."
用户:“检查绝对值修复是否生效”

操作步骤:
1. 搜索"DEBUG Bar.*\|CCI\|="(查找|CCI|=格式的输出)
2. 与旧格式"DEBUG Bar.*CCI="(无|CCI|)对比
3. 检查时间戳是否与最新编译时间匹配

输出:“日志显示16:56的输出为旧格式(无|CCI|=)。需要重新加载以获取16:59编译的v0.4.0版本。”

References

参考资料

  • MT5 file locations:
    docs/guides/MT5_FILE_LOCATIONS.md
  • Dual logging implementation:
    docs/plans/cci-rising-pattern-marker.yaml
    Phase 3-4
  • CSVLogger library:
    Program Files/MetaTrader 5/MQL5/Indicators/Custom/Development/CCINeutrality/lib/CSVLogger.mqh
  • MT5文件位置:
    docs/guides/MT5_FILE_LOCATIONS.md
  • 双重日志模式实现:
    docs/plans/cci-rising-pattern-marker.yaml
    第3-4阶段
  • CSVLogger库:
    Program Files/MetaTrader 5/MQL5/Indicators/Custom/Development/CCINeutrality/lib/CSVLogger.mqh