From 041ac558a399f7ba9eb2a21ee2d5cd72c9ed4583 Mon Sep 17 00:00:00 2001 From: Toh4iem9 Date: Tue, 25 Nov 2025 14:00:36 +0100 Subject: [PATCH] refactor: Added selectable signal line type --- .../MACD_SuperSmoother_Calculator.mqh | 189 +++++++++++++----- 1 file changed, 144 insertions(+), 45 deletions(-) diff --git a/Include/MyIncludes/MACD_SuperSmoother_Calculator.mqh b/Include/MyIncludes/MACD_SuperSmoother_Calculator.mqh index a4273a4..76ea791 100644 --- a/Include/MyIncludes/MACD_SuperSmoother_Calculator.mqh +++ b/Include/MyIncludes/MACD_SuperSmoother_Calculator.mqh @@ -1,38 +1,50 @@ //+------------------------------------------------------------------+ //| MACD_SuperSmoother_Calculator.mqh | -//| VERSION 1.10: Corrected access modifiers and state mgmt. | +//| VERSION 1.21: Corrected method definition placement. | //| Copyright 2025, xxxxxxxx | //+------------------------------------------------------------------+ #property copyright "Copyright 2025, xxxxxxxx" -#include +#include "Ehlers_Smoother_Calculator.mqh" +#include "MovingAverage_Engine.mqh" + +//--- Universal enum for all smoothing types --- +enum ENUM_SMOOTHING_METHOD + { + SMOOTH_SMA, + SMOOTH_EMA, + SMOOTH_SMMA, + SMOOTH_LWMA, + SMOOTH_SuperSmoother + }; //+==================================================================+ class CMACDSuperSmootherCalculator { protected: - //--- Periods int m_fast_period, m_slow_period, m_signal_period; + ENUM_SMOOTHING_METHOD m_signal_ma_type; - //--- Internal calculators (Composition) CEhlersSmootherCalculator *m_fast_smoother; CEhlersSmootherCalculator *m_slow_smoother; - //--- State for the signal line smoother (CRITICAL FIX) double m_sig_f1, m_sig_f2; virtual CEhlersSmootherCalculator *CreateSmootherInstance(void); + void CalculateMA(const double &source_array[], double &dest_array[], int period, ENUM_SMOOTHING_METHOD method, int start_pos); public: CMACDSuperSmootherCalculator(void); virtual ~CMACDSuperSmootherCalculator(void); - bool Init(int fast_p, int slow_p, int signal_p); + bool Init(int fast_p, int slow_p, int signal_p, ENUM_SMOOTHING_METHOD signal_type); void Calculate(int rates_total, const double &open[], const double &high[], const double &low[], const double &close[], ENUM_APPLIED_PRICE price_type, double &macd_line[], double &signal_line[], double &histogram[]); }; -//--- Derived class for Heikin Ashi version --- +//+------------------------------------------------------------------+ +//| | +//+------------------------------------------------------------------+ class CMACDSuperSmootherCalculator_HA : public CMACDSuperSmootherCalculator { protected: @@ -40,7 +52,7 @@ protected: }; //+==================================================================+ -//| METHOD IMPLEMENTATIONS | +//| METHOD IMPLEMENTATIONS: CMACDSuperSmootherCalculator | //+==================================================================+ //+------------------------------------------------------------------+ @@ -68,13 +80,15 @@ CMACDSuperSmootherCalculator::~CMACDSuperSmootherCalculator(void) //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ -CEhlersSmootherCalculator *CMACDSuperSmootherCalculator::CreateSmootherInstance(void) { return new CEhlersSmootherCalculator(); } -CEhlersSmootherCalculator *CMACDSuperSmootherCalculator_HA::CreateSmootherInstance(void) { return new CEhlersSmootherCalculator_HA(); } +CEhlersSmootherCalculator *CMACDSuperSmootherCalculator::CreateSmootherInstance(void) + { + return new CEhlersSmootherCalculator(); + } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ -bool CMACDSuperSmootherCalculator::Init(int fast_p, int slow_p, int signal_p) +bool CMACDSuperSmootherCalculator::Init(int fast_p, int slow_p, int signal_p, ENUM_SMOOTHING_METHOD signal_type) { if(fast_p > slow_p) { @@ -82,13 +96,10 @@ bool CMACDSuperSmootherCalculator::Init(int fast_p, int slow_p, int signal_p) fast_p=slow_p; slow_p=temp; } - -//--- Store periods as member variables m_fast_period = fast_p; m_slow_period = slow_p; - m_signal_period = signal_p; - -//--- Reset signal line state + m_signal_period = (signal_p < 1) ? 1 : signal_p; + m_signal_ma_type = signal_type; m_sig_f1 = 0; m_sig_f2 = 0; @@ -108,46 +119,134 @@ bool CMACDSuperSmootherCalculator::Init(int fast_p, int slow_p, int signal_p) void CMACDSuperSmootherCalculator::Calculate(int rates_total, const double &open[], const double &high[], const double &low[], const double &close[], ENUM_APPLIED_PRICE price_type, double &macd_line[], double &signal_line[], double &histogram[]) { -//--- CORRECTED: Use member variables for period check - if(rates_total <= m_slow_period + m_signal_period) + if(rates_total < m_slow_period + m_signal_period) return; double fast_buffer[], slow_buffer[]; - ArrayResize(fast_buffer, rates_total, 0); - ArrayResize(slow_buffer, rates_total, 0); - + ArrayResize(fast_buffer, rates_total); + ArrayResize(slow_buffer, rates_total); m_fast_smoother.Calculate(rates_total, price_type, open, high, low, close, fast_buffer); m_slow_smoother.Calculate(rates_total, price_type, open, high, low, close, slow_buffer); - for(int i = 0; i < rates_total; i++) macd_line[i] = fast_buffer[i] - slow_buffer[i]; -//--- STEP 4: Calculate Signal Line (SuperSmoother on MACD Line) with proper state management -//--- CORRECTED: Use GetPeriod() and member variables - double a1 = exp(-M_SQRT2 * M_PI / m_signal_period); - double b1 = 2.0 * a1 * cos(M_SQRT2 * M_PI / m_signal_period); - double c2 = b1, c3 = -a1*a1, c1 = 1.0 - c2 - c3; - -//--- Robust initialization - if(ArraySize(signal_line) == 0 || signal_line[0] == 0) - { - if(rates_total > 0) - signal_line[0] = macd_line[0]; - if(rates_total > 1) - signal_line[1] = macd_line[1]; - m_sig_f2 = signal_line[0]; - m_sig_f1 = signal_line[1]; - } - - for(int i=2; i 0) + dest_array[i] = sum/count; + m_sig_f1 = dest_array[i]; + m_sig_f2 = (i > 0 && dest_array[i-1] != EMPTY_VALUE) ? dest_array[i-1] : dest_array[i]; + } + else + { + dest_array[i] = c1 * (source_array[i] + source_array[i-1]) / 2.0 + c2 * m_sig_f1 + c3 * m_sig_f2; + m_sig_f2 = m_sig_f1; + m_sig_f1 = dest_array[i]; + } + break; + } + case SMOOTH_EMA: + case SMOOTH_SMMA: + if(i == start_pos) + { + double sum=0; + int count=0; + for(int j=0; j 0) + dest_array[i]=sum/count; + } + else + { + if(method==SMOOTH_EMA) + { + double pr=2.0/(period+1.0); + dest_array[i]=source_array[i]*pr+dest_array[i-1]*(1.0-pr); + } + else + dest_array[i]=(dest_array[i-1]*(period-1)+source_array[i])/period; + } + break; + case SMOOTH_LWMA: + { + double sum=0, w_sum=0; + for(int j=0; j0) + dest_array[i]=sum/w_sum; + } + break; + default: // SMOOTH_SMA + { + double sum=0; + int count=0; + for(int j=0; j 0) + dest_array[i]=sum/count; + } + break; + } + } + } + +//+==================================================================+ +//| METHOD IMPLEMENTATIONS: CMACDSuperSmootherCalculator_HA | +//+==================================================================+ + +//--- CORRECTED: This method definition now belongs to the _HA class --- +CEhlersSmootherCalculator *CMACDSuperSmootherCalculator_HA::CreateSmootherInstance(void) + { + return new CEhlersSmootherCalculator_HA(); + } //+------------------------------------------------------------------+ //+------------------------------------------------------------------+