mirror of
https://github.com/softwaredevelop/mql5.git
synced 2026-08-06 09:07:45 +00:00
refactor:
This commit is contained in:
@@ -1,73 +1,65 @@
|
||||
# Commodity Channel Index (CCI) Professional
|
||||
# Commodity Channel Index (CCI) Professional Suite
|
||||
|
||||
## 1. Summary (Introduction)
|
||||
|
||||
The Commodity Channel Index (CCI) is a versatile momentum oscillator developed by Donald Lambert. Despite its name, it is used effectively in any market, including stocks, forex, and futures.
|
||||
The Commodity Channel Index (CCI), developed by Donald Lambert, is a versatile momentum oscillator that measures the current price level relative to an average price level. It is widely used to identify overbought/oversold conditions and cyclical turns.
|
||||
|
||||
The CCI measures the current price level relative to an average price level over a specified period. It is designed to identify cyclical turns but is widely used to detect overbought and oversold conditions.
|
||||
Our professional suite provides a complete, unified family of indicators based on the CCI, all built upon our robust, modular framework:
|
||||
|
||||
Our professional toolkit provides a unified implementation of this indicator family:
|
||||
- **`CCI_Pro.mq5`**: The main indicator. It plots the CCI line, a configurable moving average signal line, and optional **Bollinger Bands** calculated on the CCI itself.
|
||||
- **`CCI_Oscillator_Pro.mq5`**: Displays the difference between the CCI and its signal line as a histogram, providing a clear visual of momentum acceleration/deceleration.
|
||||
- **`CCI_PercentB_Pro.mq5`**: A %B oscillator that shows the CCI's position relative to its Bollinger Bands, scaled from 0 to 100.
|
||||
|
||||
* **`CCI_Pro.mq5`**: Plots the main CCI line and a configurable moving average signal line.
|
||||
* **`CCI_Oscillator_Pro.mq5`**: Displays the difference between the CCI and its signal line as a histogram, providing a clearer visual of momentum.
|
||||
|
||||
Both indicators can be calculated using either **standard** or **Heikin Ashi** price data, selectable from a single input parameter.
|
||||
All indicators in the suite can be calculated using either **standard** or **Heikin Ashi** price data.
|
||||
|
||||
## 2. Mathematical Foundations and Calculation Logic
|
||||
|
||||
The CCI is based on the relationship between the price, its moving average, and the average deviation from that moving average.
|
||||
|
||||
### Required Components
|
||||
|
||||
* **Period (N):** The lookback period for all calculations (e.g., 20).
|
||||
* **Source Price (P):** The price series used for the calculation. The classic definition uses the **Typical Price** `(High + Low + Close) / 3`.
|
||||
* **Constant:** A statistical constant of `0.015` used to scale the result.
|
||||
The suite is constructed in a layered process, starting with the base CCI.
|
||||
|
||||
### Calculation Steps (Algorithm)
|
||||
|
||||
1. **Calculate the Source Price:** For each bar, calculate the source price (e.g., Typical Price).
|
||||
$\text{P}_i = \frac{\text{High}_i + \text{Low}_i + \text{Close}_i}{3}$
|
||||
|
||||
2. **Calculate the Simple Moving Average (SMA):** Compute an `N`-period SMA of the source price.
|
||||
$\text{SMA}_i = \text{SMA}(P, N)_i$
|
||||
|
||||
3. **Calculate the Mean Absolute Deviation (MAD):** For each bar, calculate the average absolute difference between the source price and its SMA over the `N` period.
|
||||
$\text{MAD}_i = \frac{1}{N} \sum_{k=i-N+1}^{i} \text{Abs}(P_k - \text{SMA}_i)$
|
||||
|
||||
4. **Calculate the CCI Value:** Apply the final formula.
|
||||
$\text{CCI}_i = \frac{P_i - \text{SMA}_i}{0.015 \times \text{MAD}_i}$
|
||||
|
||||
5. **Calculate the Signal Line & Oscillator:** The signal line is a moving average of the CCI line, and the oscillator is the difference between the two.
|
||||
1. **Calculate the CCI Value:** The standard CCI is calculated based on the Typical Price, its SMA, and the Mean Absolute Deviation (MAD).
|
||||
$\text{CCI}_i = \frac{\text{Typical Price}_i - \text{SMA}_i}{0.015 \times \text{MAD}_i}$
|
||||
2. **Calculate the Signal Line:** A moving average of the CCI line is calculated.
|
||||
3. **Calculate the Bollinger Bands:** Standard Bollinger Bands are calculated on the CCI line, centered around the Signal Line.
|
||||
- $\text{StdDev}_t = \text{StandardDeviation}(\text{CCI}, \text{Bands Period})_t$
|
||||
- $\text{Upper Band}_t = \text{Signal Line}_t + (\text{Bands Deviation} \times \text{StdDev}_t)$
|
||||
- $\text{Lower Band}_t = \text{Signal Line}_t - (\text{Bands Deviation} \times \text{StdDev}_t)$
|
||||
4. **Calculate the Oscillators:**
|
||||
- **CCI Oscillator:** $\text{Oscillator}_i = \text{CCI}_i - \text{Signal Line}_i$
|
||||
- **Percent B (%B):** $\text{\%B}_i = 100 \times \frac{\text{CCI}_i - \text{Lower Band}_i}{\text{Upper Band}_i - \text{Lower Band}_i}$
|
||||
|
||||
## 3. MQL5 Implementation Details
|
||||
|
||||
Our MQL5 implementation follows a highly modular, object-oriented design to ensure accuracy, reusability, and maintainability across the entire indicator family.
|
||||
Our MQL5 implementation follows a modern, object-oriented design to ensure stability and maintainability.
|
||||
|
||||
* **Centralized Calculation Engine (`CCI_Engine.mqh`):**
|
||||
The core of our implementation is a single, powerful calculation engine. This include file contains the complete, mathematically precise logic for calculating both the CCI and its signal line. It supports both standard and Heikin Ashi data sources through class inheritance (`CCCI_Engine` and `CCCI_Engine_HA`). This approach eliminates code duplication entirely.
|
||||
- **Self-Contained Calculators:** Each indicator in the suite (`CCI_Pro`, `CCI_Oscillator_Pro`, `CCI_PercentB_Pro`) uses its own dedicated, self-contained calculator. Each calculator contains the full, multi-stage logic required for its specific output. This approach prioritizes stability and clarity for each individual component.
|
||||
|
||||
* **Specialized Wrappers (`CCI_Calculator.mqh`, `CCI_Oscillator_Calculator.mqh`):**
|
||||
The final indicators use thin "wrapper" classes that utilize the central engine.
|
||||
* `CCI_Calculator` calls the engine and directly outputs the CCI and signal lines.
|
||||
* `CCI_Oscillator_Calculator` calls the same engine, receives the CCI and signal lines, and then performs one additional step: calculating the difference to produce the histogram.
|
||||
- **Object-Oriented Inheritance:** Within each calculator, an elegant inheritance model (`...Calculator` and `...Calculator_HA`) is used. The Heikin Ashi child class inherits all the complex logic from its base class and only overrides the initial data preparation step.
|
||||
|
||||
* **Mathematically Precise Implementation:** Our engine adheres strictly to the mathematical definition of CCI. It uses a nested loop structure to calculate the Mean Absolute Deviation (MAD) precisely for each bar, ensuring maximum accuracy rather than relying on a faster but less accurate sliding-window approximation.
|
||||
- **Stability via Full Recalculation:** All indicators employ a "brute-force" full recalculation within `OnCalculate` for maximum stability.
|
||||
|
||||
* **Stability via Full Recalculation:** All versions employ a "brute-force" full recalculation within `OnCalculate` to ensure maximum stability and prevent calculation errors.
|
||||
- **Mathematically Precise Implementation:** The engine adheres strictly to the mathematical definition of CCI, using a nested loop structure to calculate the Mean Absolute Deviation (MAD) precisely for each bar.
|
||||
|
||||
## 4. Parameters
|
||||
|
||||
* **CCI Period (`InpCCIPeriod`):** The lookback period for the SMA and MAD calculations. Common values are 14 or 20.
|
||||
* **Applied Price (`InpSourcePrice`):** The source price for the calculation. This unified dropdown menu allows you to select from all standard price types (e.g., `PRICE_TYPICAL_STD`) and all Heikin Ashi price types (e.g., `PRICE_HA_TYPICAL`). The classic default is `PRICE_TYPICAL`.
|
||||
* **Signal Line Settings:**
|
||||
* `InpMAPeriod`: The lookback period for the signal line.
|
||||
* `InpMAMethod`: The type of moving average for the signal line.
|
||||
- **CCI Period (`InpCCIPeriod`):** The lookback period for the base CCI calculation.
|
||||
- **Applied Price (`InpSourcePrice`):** The source price for the base CCI. This unified dropdown allows selection from all standard and Heikin Ashi price types.
|
||||
- **Overlay Settings:**
|
||||
- **Display Mode (`InpDisplayMode`):** (Only for `CCI_Pro`) Toggles between `CCI_Only`, `CCI_and_MA`, and `CCI_and_Bands`.
|
||||
- **MA Period (`InpMAPeriod`):** The period for the signal line, which also serves as the center for the Bollinger Bands.
|
||||
- **MA Method (`InpMAMethod`):** The type of moving average for the signal line.
|
||||
- **Bands Period (`InpBandsPeriod`):** The lookback period for the standard deviation calculation of the Bollinger Bands.
|
||||
- **Bands Deviation (`InpBandsDev`):** The standard deviation multiplier for the Bollinger Bands.
|
||||
|
||||
## 5. Usage and Interpretation
|
||||
|
||||
* **Overbought/Oversold Levels:** The primary use of the CCI is to identify extreme conditions.
|
||||
* **Overbought:** Readings above **+100**.
|
||||
* **Oversold:** Readings below **-100**.
|
||||
* **Zero Line Crossovers:** A crossover of the CCI line above the zero line is a bullish signal; a crossover below zero is a bearish signal.
|
||||
* **Divergence:** A powerful signal where price and the CCI move in opposite directions, often foreshadowing a reversal.
|
||||
* **Oscillator (Histogram):** The histogram provides a clear visual of the relationship between the CCI and its signal line, highlighting the acceleration and deceleration of momentum.
|
||||
- **`CCI_Pro`:**
|
||||
- **Overbought/Oversold:** Readings above +100 and below -100.
|
||||
- **Crossovers:** CCI line crossing its signal line.
|
||||
- **Bollinger Bands:** The bands show the volatility *of the CCI itself*. A "squeeze" on the CCI bands can foreshadow a significant breakout in price momentum.
|
||||
- **`CCI_Oscillator_Pro`:**
|
||||
- Visualizes the momentum of the CCI relative to its signal line. A zero-line cross confirms a signal line crossover on the `CCI_Pro`. Growing bars indicate accelerating momentum, while shrinking bars indicate decelerating momentum.
|
||||
- **`CCI_PercentB_Pro`:**
|
||||
- **Overbought/Oversold:** Provides a normalized (0-100) view of overbought/oversold conditions. Readings above 80-100 or below 0-20 are significant.
|
||||
- **"Headroom":** Shows how much "room" the CCI has to move before it becomes extreme relative to its own recent volatility.
|
||||
|
||||
Reference in New Issue
Block a user