refactor: CMcGinleyFilter class

This commit is contained in:
Toh4iem9
2025-10-11 18:54:45 +02:00
parent b7eb35ce97
commit 8181b73d61
+15 -16
View File
@@ -2,15 +2,13 @@
## 1. Summary (Introduction)
The McGinley Dynamic indicator, developed by John R. McGinley, is a more responsive and reliable alternative to traditional moving averages. Unlike averages with a fixed period, the McGinley Dynamic automatically adjusts its speed based on the speed of the market itself.
Its primary purpose is to hug prices more closely, minimizing whipsaws. It speeds up in down markets to protect capital and slows down in up markets to let profits run.
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.
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
The core of the McGinley Dynamic is its unique, self-adjusting smoothing factor. The formula is recursive, with each new value depending on the previous one.
The core of the McGinley Dynamic is its unique, self-adjusting smoothing factor.
### Required Components
@@ -19,35 +17,36 @@ The core of the McGinley Dynamic is its unique, self-adjusting smoothing factor.
### Calculation Steps (Algorithm)
1. **Initialization:** The first value of the McGinley Dynamic line is the first available source price.
$\text{MD}_0 = P_0$
2. **Recursive Calculation:** All subsequent values are calculated using the following formula:
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:
$\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
Our MQL5 implementation follows a modern, object-oriented design to ensure stability, reusability, and maintainability.
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.
* **Modular Calculation Engine (`McGinleyDynamic_Calculator.mqh`):**
The entire calculation logic is encapsulated within a reusable include file.
* **`CMcGinleyDynamicCalculator`**: The base class that performs the full recursive calculation on a given source price.
* **`CMcGinleyDynamicCalculator_HA`**: A child class that inherits all the complex logic and only overrides the initial data preparation step to use smoothed Heikin Ashi prices as its input. This object-oriented approach eliminates code duplication.
* **`CMcGinleyDynamicCalculator`**: The base class that handles price preparation.
* **`CMcGinleyDynamicCalculator_HA`**: A child class that overrides the data preparation step to use smoothed Heikin Ashi prices.
* **`CMcGinleyFilter`**: A dedicated internal class that manages the stateful, recursive calculation, ensuring stability.
* **Stability via Full Recalculation:** We employ a "brute-force" full recalculation within `OnCalculate`. For a recursive indicator like the McGinley Dynamic, this is the most reliable method to prevent calculation errors.
* **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.
* **Robust Initialization and Defensive Coding:** The recursive calculation is carefully initialized with the first available price. The calculation loop includes explicit checks to prevent division by zero, enhancing the indicator's robustness.
* **Stability via Full Recalculation:** We employ a "brute-force" full recalculation within `OnCalculate` for maximum stability.
## 4. Parameters
* **Length (`InpLength`):** The base period for the indicator. McGinley suggested this value should be approximately 60% of the period of a corresponding SMA. Default is `14`.
* **Length (`InpLength`):** The base period for the indicator. Default is `14`.
* **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.
* **Dynamic Support and Resistance:** The line itself can act as a more reliable level of dynamic support or resistance compared to traditional moving averages, as it reacts more quickly to changes in market speed.
* **Dynamic Support and Resistance:** The line itself can act as a more reliable level of dynamic support or resistance compared to traditional moving averages.
* **Crossovers:** Crossovers of the price and the McGinley Dynamic line can be used as trade signals.
* **Caution:** While it reduces whipsaws, it is still a lagging indicator (though less so than others) and should be used in conjunction with other forms of analysis for confirmation.
* **Caution:** While it reduces whipsaws, it is still a lagging indicator. It should be used in conjunction with other forms of analysis for confirmation.