mt5-log-reader
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseMT5 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.logFile 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 file next to each compiled :
.log.mq5undefinedMetaEditor会在每个已编译的文件旁生成一个文件:
.mq5.logundefinedExample: 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:
- - Find specific indicator output
CCI Rising Test|CCI_Rising_Test - - Find error messages
error|ERROR|failed - - Find specific version output
v0\\.4\\.0|v0\\.3\\.0 - - Find initialization messages
Phase \\d+|Test arrow created - - Find calculation output
DEBUG Bar
使用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编译完成后,验证新的文件是否已加载:
.ex5bash
undefinedSearch 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 beforeAnalyze 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 numbersVerify 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 2Check 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):
- Use Grep exclusively (more efficient than reading entire file)
- Use time-based filtering with and
-Acontext-B - Search for specific indicator names to narrow results
当日志文件超过5MB(读取工具限制)时:
- 仅使用Grep工具(比读取整个文件效率更高)
- 使用和
-A参数结合时间范围过滤-B - 搜索特定指标名称缩小结果范围
Integration with Dual Logging
与双重日志模式的集成
This skill accesses one half of the dual logging pattern:
- MT5 Log Files (this skill) - Human-readable Print() output
- CSV Files (CSVLogger.mqh) - Structured audit trails
Both are accessible programmatically without user intervention.
本工具访问双重日志模式的其中一部分:
- MT5日志文件(本工具处理)- 人类易读的Print()输出
- 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: Phase 3-4
docs/plans/cci-rising-pattern-marker.yaml - CSVLogger library:
Program Files/MetaTrader 5/MQL5/Indicators/Custom/Development/CCINeutrality/lib/CSVLogger.mqh
- MT5文件位置:
docs/guides/MT5_FILE_LOCATIONS.md - 双重日志模式实现:第3-4阶段
docs/plans/cci-rising-pattern-marker.yaml - CSVLogger库:
Program Files/MetaTrader 5/MQL5/Indicators/Custom/Development/CCINeutrality/lib/CSVLogger.mqh