mirror of
https://github.com/softwaredevelop/mql5.git
synced 2026-07-28 04:57:44 +00:00
refactor: VIDYA_Color_Pro
This commit is contained in:
@@ -1,10 +1,15 @@
|
||||
# Variable Index Dynamic Average (VIDYA) Professional
|
||||
# Variable Index Dynamic Average (VIDYA) Professional Family
|
||||
|
||||
## 1. Summary (Introduction)
|
||||
|
||||
The Variable Index Dynamic Average (VIDYA), developed by Tushar Chande, is an adaptive moving average that automatically adjusts its speed based on market momentum. It uses the Chande Momentum Oscillator (CMO) to dynamically alter its smoothing factor. When momentum is high, VIDYA speeds up; when momentum wanes, it slows down.
|
||||
The Variable Index Dynamic Average (VIDYA), developed by Tushar Chande, is a sophisticated adaptive moving average that automatically adjusts its speed based on market momentum. It uses the Chande Momentum Oscillator (CMO) to dynamically alter its smoothing factor. When momentum is high, VIDYA becomes more sensitive and follows prices closely. When momentum wanes in a consolidating market, it slows down and smooths out price action.
|
||||
|
||||
Our `VIDYA_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.
|
||||
Our professional implementation is an **indicator family** consisting of two versions, both powered by a single, universal calculation engine:
|
||||
|
||||
* **`VIDYA_Pro`:** The classic, single-color implementation that acts as a pure adaptive trend line.
|
||||
* **`VIDYA_Color_Pro`:** An enhanced version that changes color based on the direction of the underlying momentum (CMO positive or negative), providing an additional layer of visual information.
|
||||
|
||||
Both indicators support calculations based on either **standard** or **Heikin Ashi** price data.
|
||||
|
||||
## 2. Mathematical Foundations and Calculation Logic
|
||||
|
||||
@@ -18,28 +23,38 @@ VIDYA is a modified Exponential Moving Average where the smoothing factor is mul
|
||||
|
||||
### Calculation Steps (Algorithm)
|
||||
|
||||
1. **Calculate the Chande Momentum Oscillator (CMO):** The CMO measures momentum over a period `M`.
|
||||
$\text{CMO}_i = \frac{\text{Sum Up}_i - \text{Sum Down}_i}{\text{Sum Up}_i + \text{Sum Down}_i}$
|
||||
1. **Calculate the Chande Momentum Oscillator (CMO):** The CMO measures momentum over a period `M`, oscillating between -100 and +100.
|
||||
* $\text{Sum Up}_i = \text{Sum of positive price changes over M periods}$
|
||||
* $\text{Sum Down}_i = \text{Sum of absolute negative price changes over M periods}$
|
||||
* $\text{CMO}_i = 100 \times \frac{\text{Sum Up}_i - \text{Sum Down}_i}{\text{Sum Up}_i + \text{Sum Down}_i}$
|
||||
|
||||
2. **Calculate the VIDYA:** The VIDYA is calculated recursively.
|
||||
* First, define the standard EMA smoothing factor, `alpha`:
|
||||
$\alpha = \frac{2}{N + 1}$
|
||||
* Then, calculate the VIDYA for each bar:
|
||||
$\text{VIDYA}_i = (P_i \times \alpha \times \text{Abs}(\text{CMO}_i)) + (\text{VIDYA}_{i-1} \times (1 - \alpha \times \text{Abs}(\text{CMO}_i)))$
|
||||
* Then, calculate the VIDYA for each bar. Note that the formula uses the **absolute value** of the CMO (normalized to a 0-1 range) to adjust the speed, not the direction.
|
||||
$\text{VIDYA}_i = (P_i \times \alpha \times \text{Abs}(\frac{\text{CMO}_i}{100})) + (\text{VIDYA}_{i-1} \times (1 - \alpha \times \text{Abs}(\frac{\text{CMO}_i}{100})))$
|
||||
|
||||
## 3. MQL5 Implementation Details
|
||||
|
||||
Our MQL5 implementation follows a modern, object-oriented design to ensure stability, reusability, and maintainability.
|
||||
Our MQL5 implementation is built on a highly efficient and reusable object-oriented architecture.
|
||||
|
||||
* **Modular Calculation Engine (`VIDYA_Calculator.mqh`):**
|
||||
The entire calculation logic is encapsulated within a reusable include file.
|
||||
* **`CVIDYACalculator`**: The base class that performs the full VIDYA calculation on a given source price.
|
||||
* **`CVIDYACalculator_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.
|
||||
* **Universal Calculation Engine (`VIDYA_Calculator.mqh`):**
|
||||
A single, powerful engine file contains all the core calculation logic. This eliminates code duplication and ensures that both `VIDYA_Pro` and `VIDYA_Color_Pro` produce identical average values.
|
||||
|
||||
* **Stability via Full Recalculation:** We employ a "brute-force" full recalculation within `OnCalculate`. For a recursive indicator like VIDYA, this is the most reliable method.
|
||||
* **Method Overloading:** The `CVIDYACalculator` class features two versions of the `Calculate` method:
|
||||
1. `Calculate(..., double &vidya_buffer[])`: A version that accepts a single output buffer. This is automatically called by `VIDYA_Pro`.
|
||||
2. `Calculate(..., double &vidya_up_buffer[], double &vidya_down_buffer[])`: A version that accepts two output buffers. This is automatically called by `VIDYA_Color_Pro`.
|
||||
The MQL5 compiler intelligently selects the correct method based on the number of buffer arguments passed from the main indicator file.
|
||||
|
||||
* **Robust Initialization:** The recursive VIDYA calculation is carefully initialized with a manual Simple Moving Average (SMA) to provide a stable starting point.
|
||||
* **Advanced Multi-Color Drawing (`VIDYA_Color_Pro`):**
|
||||
The color-changing version uses the professional "double buffer" technique. To ensure a perfectly continuous line without visual gaps at color change points, a "gap bridging" logic is implemented. When the CMO's sign changes, the previous bar's VIDYA value is written to *both* color buffers, creating a seamless connection point.
|
||||
|
||||
## 4. Parameters
|
||||
* **Object-Oriented Design (Inheritance):**
|
||||
The engine uses a `CVIDYACalculator` base class and a `CVIDYACalculator_HA` derived class. The child class only overrides the `PreparePriceSeries` method to supply Heikin Ashi data, inheriting the entire complex calculation logic from its parent.
|
||||
|
||||
## 4. Parameters (`VIDYA_Pro` & `VIDYA_Color_Pro`)
|
||||
|
||||
The input parameters are identical for both indicators.
|
||||
|
||||
* **CMO Period (`InpPeriodCMO`):** The lookback period for the Chande Momentum Oscillator. Default is `9`.
|
||||
* **EMA Period (`InpPeriodEMA`):** The base period for the EMA smoothing. Default is `12`.
|
||||
@@ -47,7 +62,17 @@ Our MQL5 implementation follows a modern, object-oriented design to ensure stabi
|
||||
|
||||
## 5. Usage and Interpretation
|
||||
|
||||
* **Trend Identification:** VIDYA is used as an adaptive trend line. When the price is above the VIDYA and the line is rising, the trend is bullish.
|
||||
* **Crossover Signals:** Crossovers of the price and the VIDYA line can be used as trade signals.
|
||||
* **Trend Filter:** The key advantage of VIDYA is its ability to flatten out during periods of low momentum. A flat VIDYA line is a clear signal to avoid trend-following strategies.
|
||||
* **Caution:** While adaptive, VIDYA is still a lagging indicator. It is a tool for trend confirmation and filtering, not for precise market timing.
|
||||
### `VIDYA_Pro` (Single Color)
|
||||
|
||||
* **Adaptive Trend Line:** Use it as a more intelligent, responsive trend line. It hugs the price during strong trends and flattens out during consolidation, helping to reduce whipsaws.
|
||||
* **Trend Filter:** A flat or sideways VIDYA line is a strong indication of a ranging market, suggesting that trend-following strategies should be paused.
|
||||
* **Dynamic Support/Resistance:** In a trending market, the VIDYA line can act as a dynamic level of support (in an uptrend) or resistance (in a downtrend).
|
||||
|
||||
### `VIDYA_Color_Pro` (Multi-Color)
|
||||
|
||||
This version includes all the benefits of the standard `VIDYA_Pro` but adds an immediate visual cue for momentum direction.
|
||||
|
||||
* **Trend Direction at a Glance:**
|
||||
* **Green Line:** Indicates that the underlying momentum is bullish (CMO > 0).
|
||||
* **Red Line:** Indicates that the underlying momentum is bearish (CMO < 0).
|
||||
* **Confirmation of Trend Change:** A color change from red to green can act as an early confirmation that bullish momentum is taking over, and vice-versa. This can be particularly useful for timing entries after a pullback. For example, in a larger uptrend, a brief switch to red followed by a return to green can signal a good entry point.
|
||||
|
||||
Reference in New Issue
Block a user