From ced4003988dc4002a50234f2aad710ebe8f28563 Mon Sep 17 00:00:00 2001 From: Toh4iem9 Date: Fri, 26 Jun 2026 13:43:06 +0200 Subject: [PATCH] refactor: Refactored with overloaded Calculate to support VWMA slowing/signals --- .../MyIncludes/DMIStochastic_Calculator.mqh | 105 ++++++++++++++++-- 1 file changed, 94 insertions(+), 11 deletions(-) diff --git a/Include/MyIncludes/DMIStochastic_Calculator.mqh b/Include/MyIncludes/DMIStochastic_Calculator.mqh index 4c0e52f..68fa307 100644 --- a/Include/MyIncludes/DMIStochastic_Calculator.mqh +++ b/Include/MyIncludes/DMIStochastic_Calculator.mqh @@ -1,9 +1,13 @@ //+------------------------------------------------------------------+ //| DMIStochastic_Calculator.mqh | -//| VERSION 3.00: Refactored to use DMI_Engine. | -//| Copyright 2025, xxxxxxxx | +//| VERSION 3.10: Dynamic Volume-Weighted MA Support (VWMA) | +//| Copyright 2026, xxxxxxxx | //+------------------------------------------------------------------+ -#property copyright "Copyright 2025, xxxxxxxx" +#property copyright "Copyright 2026, xxxxxxxx" +#property version "3.10" // Refactored with overloaded Calculate to support VWMA slowing/signals + +#ifndef DMISTOCHASTIC_CALCULATOR_MQH +#define DMISTOCHASTIC_CALCULATOR_MQH #include #include @@ -11,9 +15,9 @@ enum ENUM_CANDLE_SOURCE { CANDLE_STANDARD, CANDLE_HEIKIN_ASHI }; enum ENUM_DMI_OSC_TYPE { OSC_PDI_MINUS_NDI, OSC_NDI_MINUS_PDI }; -//+------------------------------------------------------------------+ -//| | -//+------------------------------------------------------------------+ +//+==================================================================+ +//| CLASS: CDMIStochasticCalculator | +//+==================================================================+ class CDMIStochasticCalculator { protected: @@ -36,20 +40,37 @@ public: bool Init(int dmi_p, int fast_k, int slow_k, int smooth_p, ENUM_MA_TYPE k_method, ENUM_MA_TYPE d_method, ENUM_DMI_OSC_TYPE osc_type); + //--- Standard Calculate (Without volume) void Calculate(int rates_total, int prev_calculated, const double &open[], const double &high[], const double &low[], const double &close[], double &k_buffer[], double &d_buffer[]); + + //--- NEW: Overloaded Calculate (With volume to support VWMA Slowing/Signal) + void Calculate(int rates_total, int prev_calculated, const double &open[], const double &high[], const double &low[], const double &close[], + const long &volume[], + double &k_buffer[], double &d_buffer[]); }; //+------------------------------------------------------------------+ -//| | +//| Constructor | //+------------------------------------------------------------------+ CDMIStochasticCalculator::CDMIStochasticCalculator(void) { m_dmi_engine = NULL; } -CDMIStochasticCalculator::~CDMIStochasticCalculator(void) { if(CheckPointer(m_dmi_engine) != POINTER_INVALID) delete m_dmi_engine; } +//+------------------------------------------------------------------+ +//| Destructor | +//+------------------------------------------------------------------+ +CDMIStochasticCalculator::~CDMIStochasticCalculator(void) + { + if(CheckPointer(m_dmi_engine) != POINTER_INVALID) + delete m_dmi_engine; + } + +//+------------------------------------------------------------------+ +//| Factory Method | +//+------------------------------------------------------------------+ void CDMIStochasticCalculator::CreateEngine(void) { m_dmi_engine = new CDMIEngine(); } //+------------------------------------------------------------------+ -//| | +//| Init | //+------------------------------------------------------------------+ bool CDMIStochasticCalculator::Init(int dmi_p, int fast_k, int slow_k, int smooth_p, ENUM_MA_TYPE k_method, ENUM_MA_TYPE d_method, ENUM_DMI_OSC_TYPE osc_type) { @@ -69,7 +90,7 @@ bool CDMIStochasticCalculator::Init(int dmi_p, int fast_k, int slow_k, int smoot } //+------------------------------------------------------------------+ -//| | +//| Calculate (Standard - No Volume) | //+------------------------------------------------------------------+ void CDMIStochasticCalculator::Calculate(int rates_total, int prev_calculated, const double &open[], const double &high[], const double &low[], const double &close[], double &k_buffer[], double &d_buffer[]) @@ -116,16 +137,78 @@ void CDMIStochasticCalculator::Calculate(int rates_total, int prev_calculated, c m_fastK[i] = (range == 0.0) ? 50.0 : ((m_dmiOsc[i] - lowest) / range) * 100.0; } -// 3. Smooth K and D +// 3. Smooth K and D (Without Volume) m_slow_k_engine.CalculateOnArray(rates_total, prev_calculated, m_fastK, k_buffer, fast_k_start); int d_start = fast_k_start + m_slow_k_period - 1; m_smooth_d_engine.CalculateOnArray(rates_total, prev_calculated, k_buffer, d_buffer, d_start); } +//+------------------------------------------------------------------+ +//| Calculate (Overloaded - With Volume for VWMA) | +//+------------------------------------------------------------------+ +void CDMIStochasticCalculator::Calculate(int rates_total, int prev_calculated, const double &open[], const double &high[], const double &low[], const double &close[], + const long &volume[], + double &k_buffer[], double &d_buffer[]) + { + if(rates_total < m_dmi_period + m_fast_k_period) + return; + + if(ArraySize(m_pDI) != rates_total) + { + ArrayResize(m_pDI, rates_total); + ArrayResize(m_nDI, rates_total); + ArrayResize(m_dmiOsc, rates_total); + ArrayResize(m_fastK, rates_total); + } + +// 1. Calculate DI values + m_dmi_engine.Calculate(rates_total, prev_calculated, open, high, low, close, m_pDI, m_nDI); + +// 2. Calculate DMI Oscillator & Fast %K + int start_index = (prev_calculated > 0) ? prev_calculated - 1 : 0; + int loop_start = MathMax(m_dmi_period, start_index); + + for(int i = loop_start; i < rates_total; i++) + { + if(m_osc_type == OSC_PDI_MINUS_NDI) + m_dmiOsc[i] = m_pDI[i] - m_nDI[i]; + else + m_dmiOsc[i] = m_nDI[i] - m_pDI[i]; + } + + int fast_k_start = m_dmi_period + m_fast_k_period - 1; + int loop_start_k = MathMax(fast_k_start, start_index); + + for(int i = loop_start_k; i < rates_total; i++) + { + double highest = m_dmiOsc[i]; + double lowest = m_dmiOsc[i]; + for(int j = 1; j < m_fast_k_period; j++) + { + highest = MathMax(highest, m_dmiOsc[i-j]); + lowest = MathMin(lowest, m_dmiOsc[i-j]); + } + double range = highest - lowest; + m_fastK[i] = (range == 0.0) ? 50.0 : ((m_dmiOsc[i] - lowest) / range) * 100.0; + } + +// 3. Convert long volume to double to support VWMA Slowing & Signal + double vol_double[]; + ArrayResize(vol_double, rates_total); + for(int j = start_index; j < rates_total; j++) + vol_double[j] = (double)volume[j]; + +// 4. Smooth K and D (With Volume) + m_slow_k_engine.CalculateOnArray(rates_total, prev_calculated, m_fastK, vol_double, k_buffer, fast_k_start); + int d_start = fast_k_start + m_slow_k_period - 1; + m_smooth_d_engine.CalculateOnArray(rates_total, prev_calculated, k_buffer, vol_double, d_buffer, d_start); + } + //--- HA Subclass class CDMIStochasticCalculator_HA : public CDMIStochasticCalculator { protected: virtual void CreateEngine(void) override { m_dmi_engine = new CDMIEngine_HA(); } }; +#endif // DMISTOCHASTIC_CALCULATOR_MQH //+------------------------------------------------------------------+