mirror of
https://github.com/softwaredevelop/mql5.git
synced 2026-07-27 20:47:44 +00:00
refactor: Refactored with overloaded Calculate to support VWMA slowing/signals
This commit is contained in:
@@ -1,9 +1,13 @@
|
|||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
//| DMIStochastic_Calculator.mqh |
|
//| DMIStochastic_Calculator.mqh |
|
||||||
//| VERSION 3.00: Refactored to use DMI_Engine. |
|
//| VERSION 3.10: Dynamic Volume-Weighted MA Support (VWMA) |
|
||||||
//| Copyright 2025, xxxxxxxx |
|
//| 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 <MyIncludes\DMI_Engine.mqh>
|
#include <MyIncludes\DMI_Engine.mqh>
|
||||||
#include <MyIncludes\MovingAverage_Engine.mqh>
|
#include <MyIncludes\MovingAverage_Engine.mqh>
|
||||||
@@ -11,9 +15,9 @@
|
|||||||
enum ENUM_CANDLE_SOURCE { CANDLE_STANDARD, CANDLE_HEIKIN_ASHI };
|
enum ENUM_CANDLE_SOURCE { CANDLE_STANDARD, CANDLE_HEIKIN_ASHI };
|
||||||
enum ENUM_DMI_OSC_TYPE { OSC_PDI_MINUS_NDI, OSC_NDI_MINUS_PDI };
|
enum ENUM_DMI_OSC_TYPE { OSC_PDI_MINUS_NDI, OSC_NDI_MINUS_PDI };
|
||||||
|
|
||||||
//+------------------------------------------------------------------+
|
//+==================================================================+
|
||||||
//| |
|
//| CLASS: CDMIStochasticCalculator |
|
||||||
//+------------------------------------------------------------------+
|
//+==================================================================+
|
||||||
class CDMIStochasticCalculator
|
class CDMIStochasticCalculator
|
||||||
{
|
{
|
||||||
protected:
|
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);
|
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[],
|
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[]);
|
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) { 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(); }
|
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)
|
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[],
|
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[])
|
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;
|
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);
|
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;
|
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);
|
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
|
//--- HA Subclass
|
||||||
class CDMIStochasticCalculator_HA : public CDMIStochasticCalculator
|
class CDMIStochasticCalculator_HA : public CDMIStochasticCalculator
|
||||||
{
|
{
|
||||||
protected:
|
protected:
|
||||||
virtual void CreateEngine(void) override { m_dmi_engine = new CDMIEngine_HA(); }
|
virtual void CreateEngine(void) override { m_dmi_engine = new CDMIEngine_HA(); }
|
||||||
};
|
};
|
||||||
|
#endif // DMISTOCHASTIC_CALCULATOR_MQH
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
|
|||||||
Reference in New Issue
Block a user