From 48ca050e1e6c89e86b1ad2b922b8e515f76d2103 Mon Sep 17 00:00:00 2001 From: Toh4iem9 Date: Sun, 4 Jan 2026 10:33:03 +0100 Subject: [PATCH] refactor(indicators): Optimized for incremental calculation --- Include/MyIncludes/ZeroLag_EMA_Calculator.mqh | 281 ++++++++++-------- 1 file changed, 156 insertions(+), 125 deletions(-) diff --git a/Include/MyIncludes/ZeroLag_EMA_Calculator.mqh b/Include/MyIncludes/ZeroLag_EMA_Calculator.mqh index fc3a8b2..5f25053 100644 --- a/Include/MyIncludes/ZeroLag_EMA_Calculator.mqh +++ b/Include/MyIncludes/ZeroLag_EMA_Calculator.mqh @@ -1,7 +1,7 @@ //+------------------------------------------------------------------+ //| ZeroLag_EMA_Calculator.mqh | //| Calculation engine for the John Ehlers' Zero-Lag EMA. | -//| Supports standard (double EMA) and optimized gain modes. | +//| VERSION 3.00: Optimized for incremental calculation. | //| Copyright 2025, xxxxxxxx | //+------------------------------------------------------------------+ #property copyright "Copyright 2025, xxxxxxxx" @@ -9,9 +9,7 @@ #include //+==================================================================+ -//| | -//| CLASS 1: CZeroLagEMACalculator (Base Class) | -//| | +//| CLASS 1: CZeroLagEMACalculator (Base Class) | //+==================================================================+ class CZeroLagEMACalculator { @@ -19,18 +17,33 @@ protected: int m_period; bool m_optimize_gain; double m_gain_limit; + + //--- Persistent Buffers for Incremental Calculation double m_price[]; - virtual bool PreparePriceSeries(int rates_total, ENUM_APPLIED_PRICE price_type, const double &open[], const double &high[], const double &low[], const double &close[]); + //--- State Buffers for Standard Mode + double m_ema1[]; + double m_ema2[]; + + //--- State Buffers for Optimized Gain Mode + double m_ema[]; + double m_ec[]; + + //--- Updated: Accepts start_index + virtual bool PreparePriceSeries(int rates_total, int start_index, ENUM_APPLIED_PRICE price_type, const double &open[], const double &high[], const double &low[], const double &close[]); public: CZeroLagEMACalculator(void) {}; virtual ~CZeroLagEMACalculator(void) {}; bool Init(int period, bool optimize_gain, double gain_limit); - void Calculate(int rates_total, ENUM_APPLIED_PRICE price_type, const double &open[], const double &high[], const double &low[], const double &close[], double &zlema_buffer[]); + + //--- Updated: Accepts prev_calculated + void Calculate(int rates_total, int prev_calculated, ENUM_APPLIED_PRICE price_type, const double &open[], const double &high[], const double &low[], const double &close[], double &zlema_buffer[]); }; +//+------------------------------------------------------------------+ +//| Init | //+------------------------------------------------------------------+ bool CZeroLagEMACalculator::Init(int period, bool optimize_gain, double gain_limit) { @@ -41,11 +54,36 @@ bool CZeroLagEMACalculator::Init(int period, bool optimize_gain, double gain_lim } //+------------------------------------------------------------------+ -void CZeroLagEMACalculator::Calculate(int rates_total, ENUM_APPLIED_PRICE price_type, const double &open[], const double &high[], const double &low[], const double &close[], double &zlema_buffer[]) +//| Main Calculation (Optimized) | +//+------------------------------------------------------------------+ +void CZeroLagEMACalculator::Calculate(int rates_total, int prev_calculated, ENUM_APPLIED_PRICE price_type, const double &open[], const double &high[], const double &low[], const double &close[], double &zlema_buffer[]) { if(rates_total < m_period * 2) return; - if(!PreparePriceSeries(rates_total, price_type, open, high, low, close)) + + int start_index; + if(prev_calculated == 0) + start_index = 0; + else + start_index = prev_calculated - 1; + +// Resize Buffers + if(ArraySize(m_price) != rates_total) + { + ArrayResize(m_price, rates_total); + if(!m_optimize_gain) + { + ArrayResize(m_ema1, rates_total); + ArrayResize(m_ema2, rates_total); + } + else + { + ArrayResize(m_ema, rates_total); + ArrayResize(m_ec, rates_total); + } + } + + if(!PreparePriceSeries(rates_total, start_index, price_type, open, high, low, close)) return; double alpha = 2.0 / (m_period + 1.0); @@ -53,74 +91,59 @@ void CZeroLagEMACalculator::Calculate(int rates_total, ENUM_APPLIED_PRICE price_ if(!m_optimize_gain) { // --- Standard (Double EMA) Zero-Lag EMA Calculation --- - double ema1_buffer[], ema2_buffer[]; - ArrayResize(ema1_buffer, rates_total); - ArrayResize(ema2_buffer, rates_total); - double ema1_prev = 0, ema2_prev = 0; + int loop_start = MathMax(m_period, start_index); - for(int i = 0; i < rates_total; i++) + // Initialization + if(loop_start == m_period) { - if(i == m_period - 1) - { - double sum=0; - for(int j=0; j= m_period) - { - double ema1 = m_price[i] * alpha + (1.0 - alpha) * ema1_prev; - ema1_buffer[i] = ema1; - if(i == m_period * 2 - 2) - { - double sum=0; - for(int j=0; j= m_period * 2 - 1) - { - double ema2 = ema1_buffer[i] * alpha + (1.0 - alpha) * ema2_prev; - zlema_buffer[i] = 2.0 * ema1 - ema2; - ema2_prev = ema2; - } - ema1_prev = ema1; - } + double sum=0; + for(int j=0; j 0) - ema_buffer[i] = m_price[i] * alpha + (1.0 - alpha) * ema_prev; - else - ema_buffer[i] = m_price[i]; - ema_prev = ema_buffer[i]; - - if(i < 1) - { - zlema_buffer[i] = m_price[i]; - ec_prev = m_price[i]; - continue; - } + m_ema[i] = m_price[i] * alpha + (1.0 - alpha) * m_ema[i-1]; // Find the BestGain for the current bar double least_error = 1e10; double best_gain = 0; int gain_steps = (int)(m_gain_limit * 10); + double ec_prev = m_ec[i-1]; for(int j = -gain_steps; j <= gain_steps; j++) { double current_gain = j / 10.0; - double ec_trial = alpha * (ema_buffer[i] + current_gain * (m_price[i] - ec_prev)) + (1.0 - alpha) * ec_prev; + double ec_trial = alpha * (m_ema[i] + current_gain * (m_price[i] - ec_prev)) + (1.0 - alpha) * ec_prev; double error = m_price[i] - ec_trial; if(fabs(error) < least_error) { @@ -130,97 +153,105 @@ void CZeroLagEMACalculator::Calculate(int rates_total, ENUM_APPLIED_PRICE price_ } // Calculate the final ZLEMA (EC) with the BestGain - zlema_buffer[i] = alpha * (ema_buffer[i] + best_gain * (m_price[i] - ec_prev)) + (1.0 - alpha) * ec_prev; - ec_prev = zlema_buffer[i]; + m_ec[i] = alpha * (m_ema[i] + best_gain * (m_price[i] - ec_prev)) + (1.0 - alpha) * ec_prev; + zlema_buffer[i] = m_ec[i]; } } } //+------------------------------------------------------------------+ -bool CZeroLagEMACalculator::PreparePriceSeries(int rates_total, ENUM_APPLIED_PRICE price_type, const double &open[], const double &high[], const double &low[], const double &close[]) +//| Prepare Price (Standard - Optimized) | +//+------------------------------------------------------------------+ +bool CZeroLagEMACalculator::PreparePriceSeries(int rates_total, int start_index, ENUM_APPLIED_PRICE price_type, const double &open[], const double &high[], const double &low[], const double &close[]) { - ArrayResize(m_price, rates_total); - switch(price_type) + for(int i = start_index; i < rates_total; i++) { - case PRICE_CLOSE: - ArrayCopy(m_price, close, 0, 0, rates_total); - break; - case PRICE_OPEN: - ArrayCopy(m_price, open, 0, 0, rates_total); - break; - case PRICE_HIGH: - ArrayCopy(m_price, high, 0, 0, rates_total); - break; - case PRICE_LOW: - ArrayCopy(m_price, low, 0, 0, rates_total); - break; - case PRICE_MEDIAN: - for(int i=0; i