From 691f8c2e118c5ee8b96ed202e2c2c5a76069b728 Mon Sep 17 00:00:00 2001 From: Toh4iem9 Date: Thu, 18 Dec 2025 13:45:53 +0100 Subject: [PATCH] refactor: Fixed MTF Indexing Bug --- .../Authors/Kaufman/KAMA_MTF_Pro.md | 40 +++++++++---------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/Indicators/MyIndicators/Authors/Kaufman/KAMA_MTF_Pro.md b/Indicators/MyIndicators/Authors/Kaufman/KAMA_MTF_Pro.md index 10db5af..e6816b6 100644 --- a/Indicators/MyIndicators/Authors/Kaufman/KAMA_MTF_Pro.md +++ b/Indicators/MyIndicators/Authors/Kaufman/KAMA_MTF_Pro.md @@ -14,39 +14,39 @@ The underlying calculation is identical to the standard KAMA, which uses the **E ### Calculation Steps (Algorithm) -1. **Fetch Higher Timeframe Data:** The indicator first retrieves the OHLC price data for the user-selected higher timeframe (`htf`). +1. **Fetch Higher Timeframe Data:** The indicator retrieves the OHLC price data for the user-selected higher timeframe (`htf`). 2. **Calculate KAMA on the Higher Timeframe:** The complete KAMA calculation (including ER, dynamic smoothing constant, and the recursive formula) is performed using the `htf` price data. 3. **Project to Current Chart:** The calculated `htf` KAMA values are then mapped to the current chart, creating a characteristic "step-like" line that represents the higher timeframe's adaptive trend. ## 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. +* **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. -* **Modular Calculation Engine (`KAMA_Calculator.mqh`):** The indicator reuses the exact same, proven calculation engine as the standard `KAMA_Pro`. This engine, with its robust state management for the recursive KAMA formula, ensures mathematical consistency and leverages our modular design principles. +* **Modular Calculation Engine (`KAMA_Calculator.mqh`):** The indicator reuses the exact same calculation engine as the standard `KAMA_Pro`. This ensures mathematical consistency across all versions. + +* **Optimized Incremental Calculation (O(1)):** + Unlike basic MTF indicators that recalculate the entire history on every tick, this indicator employs a sophisticated incremental algorithm: + * **HTF State Tracking:** It tracks the calculation state of the higher timeframe separately (`g_htf_prev_calculated`). + * **Persistent Buffers:** The internal buffer for the higher timeframe is maintained globally, preserving the recursive state of the KAMA between ticks. + * **Efficient Mapping:** The projection loop only updates the bars corresponding to the new data. + * This ensures the indicator remains lightweight and high-performance even on heavy load. * **Dual-Mode Logic:** The `OnCalculate` function contains a smart branching logic. - * If a higher timeframe is selected, it performs the full MTF data fetching and projection process. + * 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 `KAMA_Pro`. -* **Stability via Full Recalculation:** We employ a full recalculation for both modes, which is the most reliable method for a state-dependent, recursive filter like KAMA. - ## 4. Parameters -* **Upper Timeframe (`InpUpperTimeframe`):** The higher timeframe on which the KAMA will be calculated. If set to `PERIOD_CURRENT`, the indicator will run on the current chart's timeframe. -* **ER Period (`InpErPeriod`):** The lookback period for the Efficiency Ratio calculation. Kaufman's standard value is `10`. -* **Fast EMA Period (`InpFastEmaPeriod`):** The period for the fastest EMA speed. Kaufman's standard value is `2`. -* **Slow EMA Period (`InpSlowEmaPeriod`):** The period for the slowest EMA speed. Kaufman's standard value is `30`. +* **Upper Timeframe (`InpUpperTimeframe`):** The higher timeframe on which the KAMA will be calculated. +* **ER Period (`InpErPeriod`):** The lookback period for the Efficiency Ratio calculation. (Default: `10`). +* **Fast EMA Period (`InpFastEmaPeriod`):** The period for the fastest EMA speed. (Default: `2`). +* **Slow EMA Period (`InpSlowEmaPeriod`):** The period for the slowest EMA speed. (Default: `30`). * **Applied Price (`InpSourcePrice`):** The source price for the calculation (Standard or Heikin Ashi). ## 5. Usage and Interpretation -The MTF version of KAMA is an exceptionally powerful tool for multi-timeframe trend analysis. - -* **Superior Dynamic Support and Resistance:** The primary use of the MTF KAMA is as a dynamic, high-level area of support and resistance. Because KAMA adapts to the higher timeframe's volatility, the levels it provides are often more respected by price than a standard MTF moving average. - -* **Major Trend and Range Filter:** The state of the MTF KAMA line provides a clear, smoothed-out view of the dominant market condition. - * If the price is consistently above a **rising** MTF KAMA, the market is in a strong uptrend. - * If the price is consistently below a **falling** MTF KAMA, the market is in a strong downtrend. - * If the MTF KAMA line **flattens out**, it is a very strong signal that the higher timeframe has entered a consolidation or ranging phase. This is a crucial piece of information, suggesting that breakout strategies on the lower timeframe are likely to fail and range-bound strategies may be more appropriate. - -* **Confirmation of Breakouts:** A breakout on the lower timeframe that is supported by a steepening slope of the MTF KAMA line is a much stronger and more reliable signal. +* **Dynamic Support/Resistance:** Use the MTF KAMA as a dynamic floor or ceiling. Price respecting the MTF KAMA suggests the higher timeframe trend is intact. +* **Trend Filter:** + * **Rising:** Uptrend. + * **Falling:** Downtrend. + * **Flat:** The higher timeframe is ranging/consolidating. Avoid trend-following entries on the lower timeframe.