# John Ehlers' Zero-Lag EMA Pro Suite (Standard & MTF)

## 1. Summary (Introduction)

The **John Ehlers' Zero-Lag EMA Pro Suite** is an institutional-grade, low-latency trend-following and smoothing suite comprising two advanced indicators:

* `ZeroLag_EMA_Pro` (Standard)
* `ZeroLag_EMA_MTF_Pro` (Multi-Timeframe)

Traditional moving averages (such as SMA or standard EMA) suffer from severe phase lag. When prices change direction rapidly, lagging centerlines generate late trading signals, causing significant slippage and drawdown. The Zero-Lag Exponential Moving Average (ZLEMA) resolves this trade-off.

The suite implements two highly advanced mathematical models:

1. **Standard Mode (Double EMA):** Bypasses lag recursively by calculating a secondary EMA of a primary EMA, and then subtracting the secondary average from twice the primary average, neutralising the fourier phase shift.
2. **Optimized Gain Mode (Ehlers' Error Correcting):** An advanced mathematical formulation by John F. Ehlers. On each bar, the engine runs a highly precise numerical loop to find the optimal gain ($g$) that minimizes the absolute tracking error between the closing price and the smoothed average.

The suite features dynamic Heikin Ashi price integration, customizable gain bounds, and advanced multi-timeframe step-blocking algorithms to prevent real-time drawing warping.

---

## 2. Mathematical Foundations

The calculation logic is split dynamically based on the selected operating mode:

### A. Standard Mode (Double EMA)

Standard mode utilizes Welles Wilder's exponential smoothing factor $\alpha$ based on the period $N$ (`InpPeriod`):

$$\alpha = \frac{2.0}{N + 1.0}$$

First, a primary EMA ($\text{EMA1}$) is calculated recursively over the source price $P_t$. Then, a secondary EMA ($\text{EMA2}$) is calculated on top of the $\text{EMA1}$ series:

$$\text{EMA1}_t = \alpha \times P_t + (1.0 - \alpha) \times \text{EMA1}_{t-1}$$

$$\text{EMA2}_t = \alpha \times \text{EMA1}_t + (1.0 - \alpha) \times \text{EMA2}_{t-1}$$

The final Zero-Lag EMA is calculated by projecting the difference back onto the primary average:

$$\text{ZLEMA}_t = 2.0 \times \text{EMA1}_t - \text{EMA2}_t$$

### B. Optimized Gain Mode (Ehlers' Error Correcting)

This mode implements John Ehlers' dynamic error-correcting low-pass filter. First, a standard primary $\text{EMA}$ is calculated recursively.

Then, on each bar $t$, the calculator runs a numerical search loop to evaluate trial corrected states ($\text{EC}_{\text{Trial}}$) using fractional gain steps ($g$) of $0.1$ within the user-defined gain limit boundary $[-G, G]$ (`InpGainLimit`):

$$\text{EC}_{\text{Trial}, t}(g) = \alpha \times \left( \text{EMA}_t + g \times (P_t - \text{EC}_{t-1}) \right) + (1.0 - \alpha) \times \text{EC}_{t-1}$$

The absolute error is evaluated:

$$\text{Error}(g) = \left| P_t - \text{EC}_{\text{Trial}, t}(g) \right|$$

The gain $g$ that produces the absolute least error is selected as the `best_gain`, and the final error-correcting value is written as the ZLEMA output:

$$\text{ZLEMA}_t = \alpha \times \left( \text{EMA}_t + \text{best\_gain} \times (P_t - \text{EC}_{t-1}) \right) + (1.0 - \alpha) \times \text{EC}_{t-1}$$

---

## 3. High-Performance & Precision Enhancements

The entire suite is optimized to conform with our strict quantitative design guidelines:

* **Strict Chronological Sorting Safeguards:**
  Because both the Double EMA and the Ehlers Optimized Gain calculations rely on a highly state-sensitive recursive history ($t-1$), any reverse-chronological array indexing will completely corrupt the calculations. To prevent this, the suite enforces chronological sorting (`ArraySetAsSeries(..., false)`) on all price inputs inside `OnCalculate()`. This is also applied inside all internal resizes within the calculator classes.

* **Memory Safety Validation (Pointer Guards):**
  To shield the terminal from runtime memory violations (such as access violation fatal crashes), a robust pointer-safety layer validates all dynamic objects via `CheckPointer()` before any calculation sequence is allowed to execute.

---

## 4. Advanced MQL5 MTF Implementation Details

`ZeroLag_EMA_MTF_Pro` resolves standard MTF calculation and display limitations by implementing a synchronized multi-timeframe pipeline:

### A. Forming LTF Block Flat-Force (The Warping Solution)

To prevent real-time step warping and slope distortion on lower timeframe charts, the indicator implements a step-blocking algorithm. On every tick, the indicator isolates the beginning of the active forming HTF block and forces the calculations to rewrite that block completely, keeping the visual lines perfectly flat and historically stable:

```mql5
int first_bar_of_forming_htf = rates_total - 1;
while(first_bar_of_forming_htf > 0 &&
      iBarShift(_Symbol, g_calc_timeframe, time[first_bar_of_forming_htf], false) == 0)
  {
   first_bar_of_forming_htf--;
  }
first_bar_of_forming_htf++; // Dynamic anchor start of current forming block

if(start > first_bar_of_forming_htf)
   start = first_bar_of_forming_htf;
```

### B. State Mocking for IIR State Stability

Since the Double EMA and Error Correcting equations are highly recursive, calling calculations continuously on the live forming bar on every tick would corrupt the feedback states. To avoid this, we perform **State Mocking** by passing `prev_calculated = g_htf_count` during live ticks. This processes the forming index exactly once, protecting closed historical registers from accumulation errors.

### C. Asynchronous Timer Guard

A 1-second `OnTimer` background daemon repeatedly verifies data readiness (`EnsureHTFDataReady`) and instantly triggers a chart redraw (`ChartRedraw()`) as soon as HTF history is ready, preventing blank charts on startup.

---

## 5. Parameters

### A. ZLEMA Settings

* **EMA Period (`InpPeriod`):** The lookback window size ($N$) used to calculate standard deviation and exponential coefficients (Default: `20`, Range: $\ge 1$).
* **Price Source (`InpSourcePrice`):** Selects the price series source, supporting Standard and Heikin Ashi price series (Default: `PRICE_CLOSE_STD`).

### B. Advanced Settings

* **Use Ehlers' Error Correcting (`InpOptimizeGain`):** Toggle to enable/disable Ehlers' Optimized Gain error-correction mode. Default: `false`.
* **Gain Limit (`InpGainLimit`):** The absolute boundary range ($G$) for the dynamic gain search loop (Default: `5.0`, Range: $\ge 0.1$).

### C. MTF Specific Settings (MTF Version Only)

* **Target Timeframe (`InpTimeframe`):** The target higher timeframe to calculate the smoother on (Default: `PERIOD_H1`).

---

## 6. Quantitative Trading Strategies

### A. The Zero-Lag Golden Cross (The Low-Lag Crossover)

By combining a fast-reacting ZLEMA and a medium-term standard EMA, traders can construct a highly sensitive crossover system that filters out choppy sideways markets while entering trend expansions much faster than standard SMA/EMA systems:

1. **Indicator Setup:**
   * **Fast Line:** `ZeroLag_EMA_Pro` configured to standard mode (`InpOptimizeGain = false`), Period set to **`20`** (represented in clrMediumTurquoise).
   * **Slow Line:** Standard Exponential Moving Average (EMA) configured to Period **`50`** (represented in clrCrimson).
2. **The Strategic Advantage:** Because ZLEMA completely eliminates the exponential lag, the Fast Line crosses the slow EMA Anchor much faster than a standard 20 EMA, entering trending waves up to 5-10 bars earlier on daily charts.
3. **Execution Trigger:**
   * **BUY Entry:** Enter Long when the Fast Line crosses above the Slow Line. Place stop-loss strictly below the local swing low.
   * **SELL Entry:** Enter Short when the Fast Line crosses below the Slow Line.

### B. Adaptive S/R Rejections (Ehlers' Error Correcting)

Ehlers' Error Correcting mode tracks price extremely closely during trends, yet slows down during consolidations, making it an excellent trailing pivot line.

1. **Indicator Setup:**
   * Apply `ZeroLag_EMA_Pro` configured to Optimized Gain mode (`InpOptimizeGain = true`) with a period of **`20`** and a gain limit of **`5.0`**.
2. **The Trend Alignment:**
   * **Bullish Trend:** Price trades strictly above the ZLEMA line.
   * **Bearish Trend:** Price trades strictly below the ZLEMA line.
3. **Execution Setup:**
   * In a Bullish Trend, wait for the price to pull back and test the ZLEMA line.
   * **BUY Trigger:** Enter Long as soon as a bullish reversal candlestick closes back above the ZLEMA line. Place stop-loss strictly below the test candle's low.
   * In a Bearish Trend, wait for a rally to test the line. Enter Short once a bearish rejection candle closes back below the line.
