From 71d3bdefc9acabc299feeb640ba719548206e334 Mon Sep 17 00:00:00 2001 From: Toh4iem9 Date: Sun, 16 Nov 2025 14:04:05 +0100 Subject: [PATCH] refactor: Added DEMA and TEMA for lag reduction --- Include/MyIncludes/MovingAverage_Engine.mqh | 139 +++++++++++++------- 1 file changed, 95 insertions(+), 44 deletions(-) diff --git a/Include/MyIncludes/MovingAverage_Engine.mqh b/Include/MyIncludes/MovingAverage_Engine.mqh index ae64964..3232fb9 100644 --- a/Include/MyIncludes/MovingAverage_Engine.mqh +++ b/Include/MyIncludes/MovingAverage_Engine.mqh @@ -1,6 +1,6 @@ //+------------------------------------------------------------------+ //| MovingAverage_Engine.mqh | -//| VERSION 1.20: Added Triangular Moving Average (TMA). | +//| VERSION 1.30: Added DEMA and TEMA for lag reduction. | //| Copyright 2025, xxxxxxxx | //+------------------------------------------------------------------+ #property copyright "Copyright 2025, xxxxxxxx" @@ -14,7 +14,9 @@ enum ENUM_MA_TYPE EMA, SMMA, LWMA, - TMA // New type added + TMA, + DEMA, + TEMA }; //+==================================================================+ @@ -26,6 +28,7 @@ protected: 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[]); + void CalculateEMA(int rates_total, int period, const double &source[], double &dest[]); public: CMovingAverageCalculator(void) {}; @@ -72,12 +75,17 @@ void CMovingAverageCalculator::Calculate(int rates_total, ENUM_APPLIED_PRICE pri return; int start_pos = m_period - 1; - for(int i = start_pos; i < rates_total; i++) + for(int i = 0; i < rates_total; i++) // Clear all values initially + ma_buffer[i] = EMPTY_VALUE; + + switch(m_ma_type) { - switch(m_ma_type) - { - case EMA: - case SMMA: + case EMA: + CalculateEMA(rates_total, m_period, m_price, ma_buffer); + break; + case SMMA: + for(int i = start_pos; i < rates_total; i++) + { if(i == start_pos) { double sum=0; @@ -86,17 +94,11 @@ void CMovingAverageCalculator::Calculate(int rates_total, ENUM_APPLIED_PRICE pri ma_buffer[i]=sum/m_period; } else - { - if(m_ma_type==EMA) - { - double pr=2.0/(m_period+1.0); - ma_buffer[i]=m_price[i]*pr+ma_buffer[i-1]*(1.0-pr); - } - else - ma_buffer[i]=(ma_buffer[i-1]*(m_period-1)+m_price[i])/m_period; - } - break; - case LWMA: + ma_buffer[i]=(ma_buffer[i-1]*(m_period-1)+m_price[i])/m_period; + } + break; + case LWMA: + for(int i = start_pos; i < rates_total; i++) { double sum=0, w_sum=0; for(int j=0; j