From 7a72ec5d02b8b122b27fef47945c5a53f8efdfc3 Mon Sep 17 00:00:00 2001 From: Toh4iem9 Date: Sat, 29 Nov 2025 11:54:36 +0100 Subject: [PATCH] refactor: Optimized for incremental MTF calculation --- .../1_Smoothers/Ehlers_Smoother_MTF_Pro.mq5 | 86 +++++++++++++++---- 1 file changed, 69 insertions(+), 17 deletions(-) diff --git a/Indicators/MyIndicators/Authors/Ehlers/1_Smoothers/Ehlers_Smoother_MTF_Pro.mq5 b/Indicators/MyIndicators/Authors/Ehlers/1_Smoothers/Ehlers_Smoother_MTF_Pro.mq5 index b8f9d73..599c944 100644 --- a/Indicators/MyIndicators/Authors/Ehlers/1_Smoothers/Ehlers_Smoother_MTF_Pro.mq5 +++ b/Indicators/MyIndicators/Authors/Ehlers/1_Smoothers/Ehlers_Smoother_MTF_Pro.mq5 @@ -3,7 +3,7 @@ //| Copyright 2025, xxxxxxxx| //+------------------------------------------------------------------+ #property copyright "Copyright 2025, xxxxxxxx" -#property version "1.00" +#property version "1.10" // Optimized for incremental MTF calculation #property description "Multi-Timeframe (MTF) version of John Ehlers' Smoothers." #property indicator_chart_window @@ -26,6 +26,9 @@ input ENUM_APPLIED_PRICE_HA_ALL InpSourcePrice = PRICE_CLOSE_STD; //--- Indicator Buffers --- double BufferFilterMTF[]; +//--- Internal Buffer for HTF Calculation (Must be global to persist state) +double BufferFilter_HTF_Internal[]; + //--- Global variables --- CEhlersSmootherCalculator *g_calculator; bool g_is_mtf_mode = false; @@ -34,10 +37,12 @@ ENUM_TIMEFRAMES g_calc_timeframe; //+------------------------------------------------------------------+ int OnInit() { +//--- Resolve Timeframe g_calc_timeframe = InpUpperTimeframe; if(g_calc_timeframe == PERIOD_CURRENT) g_calc_timeframe = (ENUM_TIMEFRAMES)Period(); +//--- Validation if(g_calc_timeframe < Period()) { Print("Error: The selected timeframe must be higher than or equal to the current chart timeframe."); @@ -45,10 +50,12 @@ int OnInit() } g_is_mtf_mode = (g_calc_timeframe > Period()); +//--- Buffer Mapping SetIndexBuffer(0, BufferFilterMTF, INDICATOR_DATA); ArraySetAsSeries(BufferFilterMTF, false); PlotIndexSetDouble(0, PLOT_EMPTY_VALUE, EMPTY_VALUE); +//--- Initialize Calculator string name = (InpSmootherType == SUPERSMOOTHER) ? "SuperSmoother" : "UltimateSmoother"; if(InpSourcePrice <= PRICE_HA_CLOSE) g_calculator = new CEhlersSmootherCalculator_HA(); @@ -73,57 +80,102 @@ int OnInit() } //+------------------------------------------------------------------+ -void OnDeinit(const int reason) { if(CheckPointer(g_calculator) != POINTER_INVALID) delete g_calculator; } +void OnDeinit(const int reason) + { + if(CheckPointer(g_calculator) != POINTER_INVALID) + delete g_calculator; + +// Free internal memory + ArrayFree(BufferFilter_HTF_Internal); + } //+------------------------------------------------------------------+ -int OnCalculate(const int rates_total, const int, const datetime &time[], const double &open[], const double &high[], const double &low[], const double &close[], const long &tick_volume[], const long &volume[], const int &spread[]) +int OnCalculate(const int rates_total, + const int prev_calculated, // <--- Now used! + const datetime &time[], + const double &open[], + const double &high[], + const double &low[], + const double &close[], + const long &tick_volume[], + const long &volume[], + const int &spread[]) { if(rates_total < 2 || CheckPointer(g_calculator) == POINTER_INVALID) return 0; ENUM_APPLIED_PRICE price_type = (InpSourcePrice <= PRICE_HA_CLOSE) ? (ENUM_APPLIED_PRICE)(-(int)InpSourcePrice) : (ENUM_APPLIED_PRICE)InpSourcePrice; +//================================================================ +// MTF MODE +//================================================================ if(g_is_mtf_mode) { - // --- MTF Mode --- + //--- 1. Get HTF Bars Count int htf_rates_total = (int)SeriesInfoInteger(_Symbol, g_calc_timeframe, SERIES_BARS_COUNT); if(htf_rates_total < InpPeriod + 3) return 0; + //--- 2. Manage HTF State (Incremental Logic) + static int htf_prev_calculated = 0; + + // Reset if chart was reset + if(prev_calculated == 0) + htf_prev_calculated = 0; + + //--- 3. Fetch HTF Data datetime htf_time[]; double htf_open[], htf_high[], htf_low[], htf_close[]; - if(CopyTime(_Symbol, g_calc_timeframe, 0, htf_rates_total, htf_time) <= 0 || CopyOpen(_Symbol, g_calc_timeframe, 0, htf_rates_total, htf_open) <= 0 || - CopyHigh(_Symbol, g_calc_timeframe, 0, htf_rates_total, htf_high) <= 0 || CopyLow(_Symbol, g_calc_timeframe, 0, htf_rates_total, htf_low) <= 0 || + + if(CopyTime(_Symbol, g_calc_timeframe, 0, htf_rates_total, htf_time) <= 0 || + CopyOpen(_Symbol, g_calc_timeframe, 0, htf_rates_total, htf_open) <= 0 || + CopyHigh(_Symbol, g_calc_timeframe, 0, htf_rates_total, htf_high) <= 0 || + CopyLow(_Symbol, g_calc_timeframe, 0, htf_rates_total, htf_low) <= 0 || CopyClose(_Symbol, g_calc_timeframe, 0, htf_rates_total, htf_close) <= 0) return 0; - double htf_filter_buffer[]; - ArrayResize(htf_filter_buffer, htf_rates_total); - g_calculator.Calculate(htf_rates_total, price_type, htf_open, htf_high, htf_low, htf_close, htf_filter_buffer); + //--- 4. Resize Internal Buffer + if(ArraySize(BufferFilter_HTF_Internal) != htf_rates_total) + ArrayResize(BufferFilter_HTF_Internal, htf_rates_total); - ArraySetAsSeries(htf_filter_buffer, true); + //--- 5. Calculate on HTF (Optimized) + // Pass htf_prev_calculated so the engine skips already calculated bars! + g_calculator.Calculate(htf_rates_total, htf_prev_calculated, price_type, htf_open, htf_high, htf_low, htf_close, BufferFilter_HTF_Internal); + + // Update state + htf_prev_calculated = htf_rates_total; + + //--- 6. Map to Current Timeframe (Optimized Loop) + ArraySetAsSeries(htf_time, true); + ArraySetAsSeries(BufferFilter_HTF_Internal, true); ArraySetAsSeries(time, true); ArraySetAsSeries(BufferFilterMTF, true); - for(int i = 0; i < rates_total; i++) + // Determine where to start mapping + int limit = (prev_calculated > 0) ? rates_total - prev_calculated : rates_total; + + for(int i = 0; i < limit; i++) { - int htf_bar_shift = iBarShift(_Symbol, g_calc_timeframe, time[i]); - if(htf_bar_shift < htf_rates_total && htf_bar_shift >= 0) - BufferFilterMTF[i] = htf_filter_buffer[htf_bar_shift]; + int htf_bar_shift = iBarShift(_Symbol, g_calc_timeframe, time[i], false); + if(htf_bar_shift >= 0 && htf_bar_shift < htf_rates_total) + BufferFilterMTF[i] = BufferFilter_HTF_Internal[htf_bar_shift]; else BufferFilterMTF[i] = EMPTY_VALUE; } ArraySetAsSeries(BufferFilterMTF, false); ArraySetAsSeries(time, false); + ArraySetAsSeries(BufferFilter_HTF_Internal, false); } +//================================================================ +// CURRENT TIMEFRAME MODE +//================================================================ else { - // --- Current Timeframe Mode --- - g_calculator.Calculate(rates_total, price_type, open, high, low, close, BufferFilterMTF); + // Direct calculation with optimization + g_calculator.Calculate(rates_total, prev_calculated, price_type, open, high, low, close, BufferFilterMTF); } return(rates_total); } //+------------------------------------------------------------------+ -//+------------------------------------------------------------------+