The Average Directional Index (ADX), developed by J. Welles Wilder, is a widely used technical indicator designed to measure the **strength of a trend**, regardless of its direction. It does not indicate whether the trend is bullish or bearish, but only quantifies its momentum.
The ADX system consists of three lines:
* **ADX Line:** The main line that indicates trend strength.
* **+DI (Positive Directional Indicator):** A line that measures the strength of the upward price movement.
* **-DI (Negative Directional Indicator):** A line that measures the strength of the downward price movement.
Our `ADX_Pro` implementation is a unified, professional version that allows the calculation to be based on either **standard** or **Heikin Ashi** candles.
## 2. Mathematical Foundations and Calculation Logic
The ADX calculation is a complex, multi-stage process that relies heavily on Wilder's smoothing technique (a specific type of Smoothed or Running Moving Average - SMMA/RMA).
### Required Components
* **ADX Period (N):** The lookback period for all calculations (e.g., 14).
* **Directional Movement (+DM, -DM):** Measures the portion of the current bar's range that is outside the previous bar's range.
* **True Range (TR):** The standard measure of a single bar's volatility.
### Calculation Steps (Algorithm)
1.**Calculate Directional Movement and True Range:** For each period, calculate:
Our MQL5 implementation follows a modern, object-oriented design pattern to ensure stability, reusability, and maintainability. The logic is separated into a main indicator file, a dedicated calculator engine, and a shared core engine.
* **Shared Core Engine (`DMI_Engine.mqh`):**
The fundamental calculation of Directional Movement (+DI, -DI) is outsourced to a shared engine. This ensures that both the ADX and the DMI Stochastic indicators use the exact same, validated mathematical core, eliminating code duplication and potential inconsistencies.
The ADX-specific logic (calculating the DX and smoothing it to get the ADX) is encapsulated here. This calculator orchestrates the `DMI_Engine` to get the raw data and then performs the final steps.
* The `CADXCalculator` uses **Composition** to include the `CDMIEngine`.
* The Heikin Ashi support is implemented via a **Factory Pattern** in the `CADXCalculator_HA` subclass, which instantiates a specialized `CDMIEngine_HA`. This keeps the main logic clean and agnostic of the data source.
* The internal buffers persist their state between ticks, allowing the recursive Wilder's Smoothing algorithm to continue seamlessly from the last known value.
* This results in **O(1) complexity** per tick, ensuring instant updates and zero lag.
* **ADX Period (`InpPeriodADX`):** The lookback period used for all internal calculations. Wilder's original recommendation and the most common value is `14`.
* **Candle Source (`InpCandleSource`):** Allows the user to select the candle type for the initial calculation of directional movement.
*`CANDLE_STANDARD`: Uses the standard chart's OHLC data.
*`CANDLE_HEIKIN_ASHI`: Uses smoothed Heikin Ashi data.
## 5. Usage and Interpretation
* **Trend Strength:** The primary signal is the ADX line itself.
* **ADX < 25:** Weak or non-existent trend (ranging market). Trend-following strategies should be avoided.
* **ADX > 25:** Strong trend. The higher the ADX, the stronger the trend.
* **Rising ADX:** The trend is gaining strength.
* **Falling ADX:** The trend is losing strength.
* **Trend Direction (+DI and -DI Crossover):**
* When the **+DI line (green) crosses above the -DI line (red)**, it suggests the start of a bullish trend.
* When the **-DI line (red) crosses above the +DI line (green)**, it suggests the start of a bearish trend.
* **Trade Confirmation:** A common strategy is to wait for a +DI/-DI crossover and then confirm that the ADX line is above 25 (or rising) before entering a trade. This helps to filter out signals that occur in weak or non-trending markets.