RobotFX curates the best open-source MetaTrader code to inspire your trading automation.
Overview

The Adaptive MACD is a custom technical indicator for MetaTrader 5 that enhances the classic MACD (Moving Average Convergence Divergence) by dynamically adjusting its smoothing coefficients based on market conditions. Instead of using fixed fast and slow EMA lengths, the indicator computes a Pearson correlation coefficient between the closing price and the bar index over a user‑defined lookback period. The resulting R² value is then used to blend two different MACD coefficient sets, making the oscillator more responsive during strong trends and smoother during sideways or noisy markets.
The indicator is displayed in a separate window and consists of:
-
Acolor‑coded histogram
Classic MACD strategy made fully automatic – the MACD Expert Advisor for MT4 handles it all. Check it out.
-
TheAdaptive MACD line
-
Thesignal line, which is an EMA of the MACD line.
The histogram uses four colors to indicate both direction and momentum strength:
| Color Index | Meaning |
|---|---|
| 0 | Strong bullish (rising positive histogram) |
| 1 | Weak bullish (falling positive histogram) |
| 2 | Strong bearish (falling negative histogram) |
| 3 | Weak bearish (rising negative histogram) |
The MACD line color can be automatically chosen to contrast with the chart background (light/dark theme), or set manually.

Interpretation
The Adaptive MACD can be interpreted in ways similar to the standard MACD:
-
Signal line crossovers
-
Histogram behavior
-
Zero line crossings
Because the smoothing is adaptive, the indicator tends to filter out noise during ranging periods and react more quickly when a genuine trend develops, potentially reducing false signals compared to a fixed‑period MACD.
Input Parameters
The following inputs are available for customization:
| Group | Parameter | Default | Description |
|---|---|---|---|
| Settings | InpR2Length | 20 | Lookback period used to calculate the Pearson correlation (R²). |
| Settings | InpFast | 10 | Fast EMA length for the adaptive calculation. |
| Settings | InpSlow | 20 | Slow EMA length for the adaptive calculation. |
| Settings | InpSignal | 9 | Signal line EMA length. |
| Style | InpBullColor | Blue (RGB) | Color used for bullish histogram bars (strong/weak). |
| Style | InpBearColor | Red (RGB) | Color used for bearish histogram bars (strong/weak). |
| Style | InpAutoFg | true | If true, the MACD line color automatically adapts to the chart background. |
| Style | InpMacdColor | White | Manual color for the MACD line (used when InpAutoFg = false ). |
| Style | InpSignalColor | Orange | Color of the signal line. |

Recommended Symbols and Timeframes
Because the indicator is a general‑purpose oscillator, it can be applied to any financial instrument (forex, commodities, indices, stocks) and any timeframe. However, its adaptive nature is particularly useful on:
-
Liquid pairs
-
Timeframes from M15 to H4, where trends are frequent enough to benefit from the adaptive smoothing, and noise is still present.
For scalping or very short timeframes, a smaller InpR2Length
Build better strategies with RobotFX professional tools – check them out.
76679