mirror of
https://github.com/softwaredevelop/mql5.git
synced 2026-07-27 20:47:44 +00:00
refactor: Unified MTF Engine Pattern
This commit is contained in:
@@ -4,62 +4,45 @@
|
||||
|
||||
The `MovingAverage_MTF_Pro` is a versatile multi-timeframe (MTF) version of our universal moving average indicator. It allows a trader to calculate one of the four fundamental moving average types (SMA, EMA, SMMA, LWMA) on a **higher, user-selected timeframe** and project it onto the current, lower-timeframe chart.
|
||||
|
||||
This provides a clear, smoothed-out view of the underlying trend from a broader perspective. The higher-timeframe moving average acts as a powerful, dynamic benchmark for identifying major trend direction, support, and resistance, all without needing to switch between different charts.
|
||||
|
||||
The indicator is highly flexible:
|
||||
|
||||
* The user can select the moving average type from a simple dropdown menu.
|
||||
* If the current chart's timeframe is selected, it functions identically to the standard `MovingAverage_Pro` indicator.
|
||||
* It fully supports both **standard** and **Heikin Ashi** price data.
|
||||
This provides a clear, smoothed-out view of the underlying trend from a broader perspective without needing to switch charts.
|
||||
|
||||
## 2. Mathematical Foundations and Calculation Logic
|
||||
|
||||
The underlying calculations for each moving average type are standard. The key concept is the multi-timeframe application.
|
||||
|
||||
### Core MA Types
|
||||
|
||||
* **SMA (Simple Moving Average):** An unweighted average of the last N prices.
|
||||
* **EMA (Exponential Moving Average):** A weighted average that gives more importance to recent prices.
|
||||
* **SMMA (Smoothed Moving Average):** A smoothed average, often used in Wilder's indicators, with a longer "memory" than an EMA.
|
||||
* **LWMA (Linear Weighted Moving Average):** A weighted average that gives linearly more weight to recent prices.
|
||||
* **SMA:** Simple Moving Average.
|
||||
* **EMA:** Exponential Moving Average.
|
||||
* **SMMA:** Smoothed Moving Average (Wilder's).
|
||||
* **LWMA:** Linear Weighted Moving Average.
|
||||
|
||||
### MTF Calculation Steps (Algorithm)
|
||||
### MTF Calculation Steps
|
||||
|
||||
1. **Fetch Higher Timeframe Data:** The indicator first retrieves the OHLC price data for the user-selected higher timeframe (`htf`).
|
||||
2. **Calculate the Moving Average on the Higher Timeframe:** The chosen moving average type (e.g., EMA) is calculated recursively or directly using the `htf` price data over the specified period.
|
||||
3. **Project to Current Chart:** The calculated `htf` moving average values are then mapped to the current chart. This creates a characteristic "step-like" line, where each value from the higher timeframe is held constant for the duration of its corresponding bars on the lower timeframe.
|
||||
1. **Fetch Data:** Retrieves OHLC data for the selected higher timeframe.
|
||||
2. **Calculate:** Performs the recursive or standard calculation on the higher timeframe data.
|
||||
3. **Map:** Projects the values to the current chart, creating a "step-like" visual representation.
|
||||
|
||||
## 3. MQL5 Implementation Details
|
||||
|
||||
* **Self-Contained and Robust:** This indicator is fully self-contained and does not depend on any external indicator files (`iCustom`). It directly fetches the required higher-timeframe price data using built-in `Copy...` functions for maximum stability and performance.
|
||||
* **Self-Contained:** No `iCustom` dependencies. Uses direct `Copy...` functions for stability.
|
||||
* **Universal Engine (`MovingAverage_Engine.mqh`):** Reuses the core logic from the standard suite for consistency.
|
||||
|
||||
* **Universal Calculation Engine (`MovingAverage_Engine.mqh`):** The indicator reuses the exact same, proven calculation engine as the standard `MovingAverage_Pro`. This engine can calculate all four MA types, ensuring mathematical consistency and leveraging our modular design principles.
|
||||
* **Optimized Incremental Calculation (O(1)):**
|
||||
This indicator is engineered for performance:
|
||||
* **HTF State Tracking:** Tracks `htf_prev_calculated` to avoid redundant math on the higher timeframe.
|
||||
* **Persistent State:** Preserves the recursive state of EMAs and SMMAs between ticks.
|
||||
* **Smart Mapping:** The projection loop handles index alignment precisely and only updates changed bars.
|
||||
|
||||
* **Optimized Incremental Calculation:**
|
||||
Unlike basic MTF indicators that download and recalculate the entire higher-timeframe history on every tick, this indicator employs a sophisticated incremental algorithm.
|
||||
* **HTF State Tracking:** It tracks the calculation state of the higher timeframe separately (`htf_prev_calculated`).
|
||||
* **Persistent Buffers:** The internal buffer for the higher timeframe (`BufferMA_HTF_Internal`) is maintained globally, preserving the recursive state of EMAs and SMMAs between ticks.
|
||||
* **Efficient Mapping:** The projection loop only updates the bars corresponding to the new data, drastically reducing CPU usage.
|
||||
* This results in **O(1) complexity** per tick, ensuring the indicator remains lightweight even when running on multiple charts simultaneously.
|
||||
|
||||
* **Dual-Mode Logic:** The `OnCalculate` function contains a smart branching logic.
|
||||
* If a higher timeframe is selected, it performs the optimized MTF data fetching and projection process.
|
||||
* If the current timeframe is selected, it bypasses the MTF logic and functions identically to the standard `MovingAverage_Pro`, calculating directly on the current chart's data for maximum efficiency.
|
||||
* **Dual-Mode Logic:** Automatically switches between MTF mode (projection) and Standard mode (direct calculation) based on the timeframe selection.
|
||||
|
||||
## 4. Parameters
|
||||
|
||||
* **Upper Timeframe (`InpUpperTimeframe`):** The higher timeframe on which the moving average will be calculated. If set to `PERIOD_CURRENT`, the indicator will run on the current chart's timeframe.
|
||||
* **Period (`InpPeriod`):** The lookback period for the moving average.
|
||||
* **MA Type (`InpMAType`):** A dropdown menu to select the desired moving average type (SMA, EMA, SMMA, LWMA).
|
||||
* **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.
|
||||
* **Upper Timeframe (`InpUpperTimeframe`):** The calculation timeframe.
|
||||
* **Period (`InpPeriod`):** The lookback period.
|
||||
* **MA Type (`InpMAType`):** SMA, EMA, SMMA, or LWMA.
|
||||
* **Applied Price (`InpSourcePrice`):** Standard or Heikin Ashi price sources.
|
||||
|
||||
## 5. Usage and Interpretation
|
||||
|
||||
The MTF version of a moving average is a cornerstone of multi-timeframe analysis, providing crucial context for lower-timeframe price action.
|
||||
|
||||
* **Dynamic Support and Resistance:** The primary use of the MTF MA is as a dynamic, high-level area of support and resistance. When the price on the lower timeframe pulls back to the higher-timeframe MA line, it can present a high-probability entry point in the direction of the larger trend.
|
||||
* **Major Trend Filter:** The slope and position of the MTF MA line provide a clear view of the dominant trend.
|
||||
* If the price is consistently above a rising MTF MA, the market is in a strong uptrend. Traders should focus on buying opportunities.
|
||||
* If the price is consistently below a falling MTF MA, the market is in a strong downtrend. Traders should focus on selling opportunities.
|
||||
* **Confirmation of Breakouts:** A breakout on the lower timeframe that is also supported by the direction of the MTF MA line is a much stronger and more reliable signal.
|
||||
* **Range Detection:** A flat MTF MA line indicates that the higher timeframe is consolidating, signaling that range-bound strategies might be more appropriate on the lower timeframe.
|
||||
* **Trend Direction:** A rising MTF MA indicates a bullish higher-timeframe trend; falling indicates bearish.
|
||||
* **Pullbacks:** In an uptrend, a pullback to the MTF MA often represents a high-value buying opportunity (dynamic support).
|
||||
* **Crossovers:** Price crossing the MTF MA can signal a potential reversal in the major trend.
|
||||
|
||||
Reference in New Issue
Block a user