Files
mql5/Indicators/MyIndicators/McGinleyDynamic_Pro.md
T

56 lines
4.0 KiB
Markdown
Raw Normal View History

# McGinley Dynamic Pro
2025-09-30 14:31:24 +02:00
## 1. Summary (Introduction)
2025-10-11 18:54:45 +02:00
The McGinley Dynamic indicator, developed by John R. McGinley, is a more responsive and reliable alternative to traditional moving averages. It automatically adjusts its speed based on the speed of the market itself, hugging prices more closely and minimizing whipsaws.
2025-09-30 14:31:24 +02:00
Our `McGinleyDynamic_Pro` implementation is a unified, professional version that allows the calculation to be based on either **standard** or **Heikin Ashi** price data, selectable from a single input parameter.
## 2. Mathematical Foundations and Calculation Logic
2025-10-11 18:54:45 +02:00
The core of the McGinley Dynamic is its unique, self-adjusting smoothing factor.
2025-09-30 14:31:24 +02:00
### Required Components
* **Length (N):** The base period for the indicator.
* **Source Price (P):** The price series used for the calculation.
### Calculation Steps (Algorithm)
2025-10-11 18:54:45 +02:00
1. **Initialization:** The first value is typically an `N`-period moving average of the price.
2. **Recursive Calculation:** All subsequent values are calculated using the formula:
2025-09-30 14:31:24 +02:00
$\text{MD}_i = \text{MD}_{i-1} + \frac{P_i - \text{MD}_{i-1}}{N \times (\frac{P_i}{\text{MD}_{i-1}})^4}$
The key component is the denominator, which contains the ratio $(\frac{P_i}{\text{MD}_{i-1}})$ that measures the speed of the market and adjusts the indicator's responsiveness.
## 3. MQL5 Implementation Details
2025-10-11 18:54:45 +02:00
Our MQL5 implementation is a highly robust and definition-true representation, specifically engineered to handle the mathematical sensitivity of the McGinley formula, especially on volatile instruments.
2025-09-30 14:31:24 +02:00
* **Modular Calculation Engine (`McGinleyDynamic_Calculator.mqh`):**
The entire calculation logic is encapsulated within a reusable include file.
* **`CMcGinleyDynamicCalculator`**: The base class that handles price preparation and the core algorithm.
2025-10-11 18:54:45 +02:00
* **`CMcGinleyDynamicCalculator_HA`**: A child class that overrides the data preparation step to use smoothed Heikin Ashi prices.
* **Optimized Incremental Calculation:**
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.
* **Persistent State:** The internal buffers (`m_price`, `mcginley_buffer`) persist their state between ticks. This allows the recursive calculation to continue seamlessly from the last known value without re-processing the entire history.
* This results in **O(1) complexity** per tick, ensuring instant updates and zero lag.
2025-09-30 14:31:24 +02:00
2025-10-11 18:54:45 +02:00
* **Robust Initialization and Overflow Protection:**
* **SMA Initialization:** The recursive calculation is properly "primed" by using an `N`-period Simple Moving Average for its first value, as suggested by modern, robust implementations.
* **Overflow Protection:** To prevent floating-point overflows on highly volatile instruments (like cryptocurrencies), the `(Price / Previous_Value)` ratio is "clamped" within a reasonable range before the `^4` power is applied. This makes the indicator stable under all market conditions.
2025-09-30 14:31:24 +02:00
## 4. Parameters
2025-10-11 18:54:45 +02:00
* **Length (`InpLength`):** The base period for the indicator. Default is `14`.
2025-09-30 14:31:24 +02:00
* **Applied Price (`InpSourcePrice`):** The source price for the calculation. This unified dropdown menu allows you to select from all standard and Heikin Ashi price types.
## 5. Usage and Interpretation
* **Trend Identification:** The McGinley Dynamic is primarily used as a dynamic trend line. When the price is above the line, the trend is considered bullish. When the price is below the line, the trend is considered bearish.
2025-10-11 18:54:45 +02:00
* **Dynamic Support and Resistance:** The line itself can act as a more reliable level of dynamic support or resistance compared to traditional moving averages.
2025-09-30 14:31:24 +02:00
* **Crossovers:** Crossovers of the price and the McGinley Dynamic line can be used as trade signals.
2025-10-11 18:54:45 +02:00
* **Caution:** While it reduces whipsaws, it is still a lagging indicator. It should be used in conjunction with other forms of analysis for confirmation.