refactor: Optimized for incremental calculation & Consolidated Engine

This commit is contained in:
Toh4iem9
2025-11-28 18:03:22 +01:00
parent 3213507516
commit 652dbeb9db
@@ -2,65 +2,57 @@
## 1. Summary (Introduction)
The `MovingAverage_Ribbon_MTF_Pro` is an exceptionally powerful and flexible multi-timeframe (MTF) analysis tool. It elevates the concept of a moving average ribbon by allowing the user to configure not just the period and type, but also the **source timeframe for each of the four lines independently**.
The `MovingAverage_Ribbon_MTF_Pro` is a specialized multi-timeframe (MTF) analysis tool designed to project a complete Moving Average Ribbon from a higher timeframe onto your current chart.
This unique capability allows traders to construct a "multi-dimensional" view of the market on a single chart. For example, one can display a long-term trend line from the Daily timeframe, a medium-term line from H4, and two short-term lines from H1, all overlaid on an M15 chart for precise entry timing.
Unlike standard ribbons that only show trends on the current chart, this indicator allows you to visualize the trend structure of a **higher, user-selected timeframe** (e.g., seeing the H4 ribbon structure while trading on M15). This provides invaluable context for trend alignment and dynamic support/resistance zones.
The indicator provides complete control over:
The indicator calculates four moving average lines on the chosen higher timeframe and projects them onto the current chart as "step-like" lines.
* **Timeframe** for each of the 4 lines.
* **Period** for each of the 4 lines.
* **MA Type** (SMA, EMA, SMMA, LWMA, TMA, DEMA, TEMA) for each of the 4 lines.
* **Price Source** (Standard or Heikin Ashi).
The indicator provides control over:
* **Central Timeframe:** One single higher timeframe that applies to the entire ribbon.
* **Period:** Independent period settings for each of the 4 lines.
* **MA Type:** Independent type settings (SMA, EMA, SMMA, LWMA, TMA, DEMA, TEMA) for each of the 4 lines.
* **Price Source:** Standard or Heikin Ashi.
## 2. Mathematical Foundations and Calculation Logic
The indicator is a "meta-indicator" that performs four independent MTF moving average calculations and displays them together. The logic for each line follows the standard MTF process:
The indicator performs four independent MTF moving average calculations based on a shared higher timeframe.
1. **Fetch Higher Timeframe Data:** For each of the four lines, the indicator retrieves the OHLC price data for its user-selected timeframe.
2. **Calculate the Moving Average:** The chosen moving average type is calculated on its corresponding timeframe's price data.
3. **Project to Current Chart:** All four calculated MTF moving average series are then mapped to the current chart, creating four distinct "step-like" lines.
The power of the indicator comes from the visual relationship between these lines originating from different dimensions of market time.
1. **Fetch Higher Timeframe Data:** The indicator retrieves the OHLC price data for the single user-selected higher timeframe (`InpUpperTimeframe`).
2. **Calculate the Moving Averages:** Four separate moving averages are calculated on this higher timeframe data, using their respective periods and types.
3. **Project to Current Chart:** All four calculated series are mapped to the current chart. Each value from the higher timeframe is held constant for the duration of its corresponding bars on the lower timeframe, creating the visual "step" effect.
## 3. MQL5 Implementation Details
This indicator is built on a robust, `iCustom()`-free, and highly modular "multi-engine" architecture to handle its complexity.
This indicator is built on a robust, `iCustom()`-free, and highly modular architecture.
* **Modular "Manager" Calculator (`MovingAverage_Ribbon_Calculator.mqh`):** The main engine for this ribbon indicator uses the composition design pattern. It **contains four independent instances** of the `CMovingAverageCalculator`.
* The main engine acts as a "manager," delegating the complete calculation of each line to one of the specialized single-line calculators.
* This architecture perfectly separates concerns, ensuring the code is clean, stable, and easy to maintain.
* **Modular "Manager" Calculator (`MovingAverage_Ribbon_MTF_Calculator.mqh`):** The main engine uses the composition design pattern. It contains four independent instances of the `CSingleMAMTFCalculator` helper class.
* Even though they share the same source timeframe, each line is calculated independently to allow for different MA types (e.g., mixing EMA and SMA) and periods.
* **Optimized Incremental Calculation:**
Despite managing four separate MTF calculations simultaneously, the indicator remains extremely efficient.
* Each of the four internal calculators tracks its own state and utilizes the `prev_calculated` optimization.
* **Persistent State:** The internal buffers for recursive calculations (EMA, SMMA, DEMA, TEMA) persist between ticks for each line independently.
* This ensures that the indicator runs with **O(1) complexity** per tick, updating all four lines instantly without re-processing history.
The indicator employs a sophisticated incremental algorithm to ensure high performance.
* **HTF State Tracking:** It tracks the calculation state of the higher timeframe separately.
* **Persistent Buffers:** The internal buffers for recursive calculations (EMA, SMMA, DEMA, TEMA) persist between ticks.
* **Efficient Mapping:** The projection loop only updates the bars corresponding to the new data.
* This results in **O(1) complexity** per tick, ensuring the indicator remains lightweight even when running on multiple charts.
* **No External Dependencies:** The entire calculation is self-contained. It directly fetches the required higher-timeframe price data using built-in `Copy...` functions, making the indicator fully portable and robust.
* **No External Dependencies:** The entire calculation is self-contained. It directly fetches the required higher-timeframe price data using built-in `Copy...` functions.
## 4. Parameters
The indicator's inputs are organized into four groups, one for each moving average line, providing full control.
* **Timeframe & Price Source:**
* **`InpUpperTimeframe`:** The single higher timeframe on which the entire ribbon will be calculated. If set to `PERIOD_CURRENT`, it behaves like a standard ribbon.
* **`InpSourcePrice`:** The source price for all calculations (Standard or Heikin Ashi).
* **MA 1-4 Settings:**
* **`InpTimeframe1` - `InpTimeframe4`:** The source timeframe for each line. `PERIOD_CURRENT` will use the chart's active timeframe.
* **`InpPeriod1` - `InpPeriod4`:** The lookback period for each line.
* **`InpMAType1` - `InpMAType4`:** The MA type (SMA, EMA, SMMA, LWMA, TMA, DEMA, TEMA) for each line.
* **Price Source:**
* **`InpSourcePrice`:** The source price for all calculations (Standard or Heikin Ashi).
## 5. Usage and Interpretation
The MTF Ribbon is an advanced tool for building a comprehensive view of the market's trend structure.
The MTF Ribbon provides crucial context for lower-timeframe price action.
* **Trend Confirmation and Confluence:** The primary use is to identify strong trends where multiple timeframes are aligned. A high-probability uptrend is in place when the price is above all four ribbon lines, and the lines themselves are ordered logically (e.g., H4 MA below H1 MA, which is below M15 MA).
* **Dynamic Support/Resistance Zones:** The space between two moving averages from different timeframes creates a powerful dynamic support or resistance "zone." For example, in an uptrend, the area between the H1 MA and the H4 MA can act as a major pullback zone for high-probability long entries.
* **Building a Complete Strategy (Example):** A trader could configure the ribbon as follows:
* **MA 4 (Slowest):** `EMA(50)` on `D1` (Daily) - The overall market bias.
* **MA 3:** `EMA(50)` on `H4` - The major trend direction.
* **MA 2:** `EMA(21)` on `H1` - The medium-term trend and primary pullback zone.
* **MA 1 (Fastest):** `EMA(8)` on `M15` - The short-term trend and entry trigger.
* **Trade Logic:** Look for long entries on the M15 chart only when the price is above the D1, H4, and H1 MAs. An entry could be triggered when the price pulls back to the H1 EMA zone and then bounces, or when the M15 EMA crosses back up through a slightly slower MA.
* **Trend Alignment:** Use the MTF Ribbon to ensure you are trading in the direction of the higher timeframe's momentum. If the H4 ribbon (displayed on your M15 chart) is expanding and sloping up, look primarily for buy setups.
* **Dynamic Support/Resistance Zones:** The space between the MTF lines acts as a strong support/resistance zone. When price pulls back to these "steps," it often presents a high-probability entry point.
* **Range Detection:** When the MTF ribbon lines become flat and intertwined, it indicates that the higher timeframe is in a consolidation phase.