home / skills / terrylica / cc-skills / mql5-indicator-patterns

mql5-indicator-patterns skill

/plugins/mql5/skills/mql5-indicator-patterns

This skill helps you develop robust MQL5 indicators by applying proven display, buffer, and warmup patterns for MT5.

npx playbooks add skill terrylica/cc-skills --skill mql5-indicator-patterns

Review the files below or copy the command above to add this skill to your agents.

Files (6)
SKILL.md
3.7 KB
---
name: mql5-indicator-patterns
description: MQL5 indicator development patterns. TRIGGERS - MQL5 indicator, OnCalculate, indicator buffers, MetaTrader 5.
allowed-tools: Read, Grep, Edit, Write
---

# MQL5 Visual Indicator Patterns

Battle-tested patterns for creating custom MQL5 indicators with proper display, buffer management, and real-time updates.

## 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

## 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); // Hidden
```

**New 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);
```

---

## 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)`

---

## Key Patterns

**For production MQL5 indicators**:

1. Explicit scale for small values (< 1.0 range)
2. Hidden buffers for recalculation tracking
3. New bar detection prevents rolling window drift
4. Static variables maintain state efficiently
5. Proper warmup calculation prevents misalignment
6. Forward indexing for code clarity

These patterns solve the most common indicator development issues encountered in real-world MT5 development.

---

## 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         |

---

## Reference Documentation

For detailed information, see:

- [Display Scale](./references/display-scale.md) - Fix blank indicator windows for small values
- [Buffer Patterns](./references/buffer-patterns.md) - Visible and hidden buffer architecture
- [Recalculation](./references/recalculation.md) - Bar detection and rolling window state management
- [Complete Template](./references/complete-template.md) - Full working example with all patterns
- [Debugging](./references/debugging.md) - Checklist for troubleshooting display issues

Overview

This skill provides battle-tested MQL5 indicator development patterns to build reliable, well-behaved MetaTrader 5 indicators. It focuses on buffer management, display scaling, warmup alignment, and robust OnCalculate behavior so indicators render correctly and avoid common runtime drift. The patterns are practical and targeted at production-ready indicators.

How this skill works

The skill documents concrete code patterns and configuration steps that inspect and control indicator state: explicit display scale settings, visible and hidden buffer architecture, forward-indexed arrays, and new-bar detection using static state. It explains how to compute PLOT_DRAW_BEGIN from underlying and own warmup and how OnCalculate should return rates_total to indicate successful updates. The guidance prevents blank displays, rolling-window drift, misaligned plots, and indexing errors.

When to use it

  • Creating a custom MQL5 indicator for MetaTrader 5 from scratch
  • Fixing a blank or mis-scaled indicator window for small-value plots
  • Stopping value drift caused by rolling-window recalculation
  • Aligning plot start using correct warmup calculation
  • Converting array indexing from series mode to forward-indexed logic

Best practices

  • Set INDICATOR_MINIMUM and INDICATOR_MAXIMUM explicitly for small-value ranges
  • Use separate visible and hidden buffers: INDICATOR_DATA for plots, INDICATOR_CALCULATIONS for internal state
  • Detect new bars with a static last_processed_bar and only reset rolling state on bar change
  • Call ArraySetAsSeries(buffer, false) to use forward indexing consistently
  • Compute StartCalcPosition = underlying_warmup + own_warmup and set PLOT_DRAW_BEGIN accordingly
  • Return rates_total from OnCalculate and only recalc bars from prev_calculated onwards

Example use cases

  • A volatility indicator whose values are <1.0 and currently displays a blank chart
  • An oscillator that slowly drifts because it recalculates all bars on each tick
  • A multi-buffer indicator that needs hidden buffers to track recalculation state
  • Porting an indicator that used series arrays to forward-indexed logic for clarity
  • Creating a template indicator with correct PLOT_DRAW_BEGIN so plots align with price bars

FAQ

Why does my indicator window appear blank?

Often the scale is wrong for small-value plots; explicitly set INDICATOR_MINIMUM and INDICATOR_MAXIMUM to a suitable range.

How do I prevent values from drifting over time?

Use a hidden buffer to track recalculation state and detect new bars with a static last_processed_bar so you only reset rolling-window state on bar changes.

What indexing mode should I use for clarity?

Use forward indexing (ArraySetAsSeries(buffer, false)) to avoid reversed indexing bugs and make loops more intuitive.