mql5-indicator-patterns
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseMQL5 Visual Indicator Patterns
MQL5可视化指标开发模式
Battle-tested patterns for creating custom MQL5 indicators with proper display, buffer management, and real-time updates.
经过实战验证的模式,用于创建具备良好显示效果、缓冲区管理和实时更新功能的自定义MQL5指标。
When to Use This Skill
何时使用该技能
Use this skill when:
- Creating custom MQL5 indicators for MetaTrader 5
- Debugging indicator display or buffer issues
- Setting up OnCalculate with proper warmup handling
- Implementing new bar detection patterns
在以下场景中使用本技能:
- 为MetaTrader 5创建自定义MQL5指标
- 调试指标显示或缓冲区相关问题
- 配置带有预热处理的OnCalculate函数
- 实现新K线检测模式
Quick Reference
快速参考
Essential Patterns
核心模式
Display Scale (for small values < 1.0):
mql5
IndicatorSetDouble(INDICATOR_MINIMUM, 0.0);
IndicatorSetDouble(INDICATOR_MAXIMUM, 0.1);Buffer Setup (visible + hidden):
mql5
SetIndexBuffer(0, BufVisible, INDICATOR_DATA); // Visible
SetIndexBuffer(1, BufHidden, INDICATOR_CALCULATIONS); // HiddenNew Bar Detection (prevents drift):
mql5
static int last_processed_bar = -1;
bool is_new_bar = (i > last_processed_bar);Warmup Calculation:
mql5
int StartCalcPosition = underlying_warmup + own_warmup;
PlotIndexSetInteger(0, PLOT_DRAW_BEGIN, StartCalcPosition);显示范围设置(适用于小于1.0的数值):
mql5
IndicatorSetDouble(INDICATOR_MINIMUM, 0.0);
IndicatorSetDouble(INDICATOR_MAXIMUM, 0.1);缓冲区配置(可见+隐藏):
mql5
SetIndexBuffer(0, BufVisible, INDICATOR_DATA); // 可见缓冲区
SetIndexBuffer(1, BufHidden, INDICATOR_CALCULATIONS); // 隐藏缓冲区新K线检测(防止偏移):
mql5
static int last_processed_bar = -1;
bool is_new_bar = (i > last_processed_bar);预热计算:
mql5
int StartCalcPosition = underlying_warmup + own_warmup;
PlotIndexSetInteger(0, PLOT_DRAW_BEGIN, StartCalcPosition);Common Pitfalls
常见陷阱
Blank Display: Set explicit scale (see Display Scale reference)
Rolling Window Drift: Use new bar detection with hidden buffer (see Recalculation reference)
Misaligned Plots: Calculate correct PLOT_DRAW_BEGIN (see Complete Template reference)
Forward-Indexed Arrays: Always set
ArraySetAsSeries(buffer, false)空白显示:设置明确的显示范围(参考“显示范围设置”)
滚动窗口偏移:结合隐藏缓冲区使用新K线检测(参考“重新计算”部分)
绘图对齐错误:计算正确的PLOT_DRAW_BEGIN值(参考“完整模板”部分)
正向索引数组:始终调用
ArraySetAsSeries(buffer, false)Key Patterns
关键模式
For production MQL5 indicators:
- Explicit scale for small values (< 1.0 range)
- Hidden buffers for recalculation tracking
- New bar detection prevents rolling window drift
- Static variables maintain state efficiently
- Proper warmup calculation prevents misalignment
- Forward indexing for code clarity
These patterns solve the most common indicator development issues encountered in real-world MT5 development.
适用于生产环境的MQL5指标:
- 为小数值(范围<1.0)设置明确的显示范围
- 使用隐藏缓冲区跟踪重新计算情况
- 新K线检测可防止滚动窗口偏移
- 使用静态变量高效维护状态
- 正确的预热计算避免绘图对齐错误
- 正向索引提升代码清晰度
这些模式解决了实际MT5开发中最常见的指标开发问题。
Troubleshooting
故障排查
| Issue | Cause | Solution |
|---|---|---|
| Blank indicator window | Scale not set for small values | Set INDICATOR_MINIMUM/MAXIMUM explicitly |
| Values drifting over time | Rolling window not reset | Use new bar detection with hidden buffer |
| Misaligned plot start | Wrong PLOT_DRAW_BEGIN | Calculate: underlying_warmup + own_warmup |
| Reversed array indexing | Series mode enabled | Call ArraySetAsSeries(buffer, false) |
| Buffer values incorrect | Wrong INDICATOR_DATA type | Use INDICATOR_CALCULATIONS for hidden buffers |
| Compile error on buffer | Buffer count mismatch | Match #property indicator_buffers with SetIndexBuffer |
| Indicator not updating | OnCalculate return wrong | Return rates_total to signal successful calculation |
| Performance issues | Recalculating all bars | Only recalculate from prev_calculated onwards |
| 问题 | 原因 | 解决方案 |
|---|---|---|
| 指标窗口空白 | 未为小数值设置显示范围 | 显式设置INDICATOR_MINIMUM/MAXIMUM |
| 数值随时间偏移 | 滚动窗口未重置 | 结合隐藏缓冲区使用新K线检测 |
| 绘图起始位置对齐错误 | PLOT_DRAW_BEGIN设置错误 | 计算方式:underlying_warmup + own_warmup |
| 数组索引反转 | 启用了序列模式 | 调用ArraySetAsSeries(buffer, false) |
| 缓冲区数值错误 | INDICATOR_DATA类型设置错误 | 为隐藏缓冲区使用INDICATOR_CALCULATIONS类型 |
| 缓冲区编译错误 | 缓冲区数量不匹配 | 确保#property indicator_buffers与SetIndexBuffer数量一致 |
| 指标不更新 | OnCalculate返回值错误 | 返回rates_total以表示计算成功 |
| 性能问题 | 重新计算所有K线 | 仅从prev_calculated位置开始重新计算 |
Reference Documentation
参考文档
For detailed information, see:
- Display Scale - Fix blank indicator windows for small values
- Buffer Patterns - Visible and hidden buffer architecture
- Recalculation - Bar detection and rolling window state management
- Complete Template - Full working example with all patterns
- Debugging - Checklist for troubleshooting display issues
如需详细信息,请查看:
- 显示范围设置 - 修复小数值导致的指标窗口空白问题
- 缓冲区模式 - 可见与隐藏缓冲区架构
- 重新计算 - K线检测与滚动窗口状态管理
- 完整模板 - 包含所有模式的完整可用示例
- 故障排查 - 指标显示问题排查清单