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 and a dedicated calculator engine.
All core calculation logic is encapsulated within a reusable include file. This separates the mathematical complexity from the indicator's user interface and buffer management. All intermediate calculation buffers are managed internally by the class, keeping the main indicator file clean.
* **Object-Oriented Design (Inheritance):**
* A base class, `CADXCalculator`, handles the **entire shared calculation chain** from Step 2 to Step 5 (smoothing, DI, DX, and ADX calculation).
* A derived class, `CADXCalculator_HA`, inherits from the base class and **overrides** only one specific function: the initial calculation of raw +DM, -DM, and TR (Step 1). Its sole responsibility is to use Heikin Ashi candles for this first step and pass the results to the base class's shared calculation pipeline. This is a clean and efficient use of polymorphism.
Unlike basic implementations that recalculate the entire history on every tick, this indicator employs an intelligent incremental algorithm.
* It utilizes the `prev_calculated` state to determine the exact starting point for updates.
* The internal buffers (`m_smoothed_pdm`, etc.) 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, even on charts with extensive history.
* **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.