refactor: Optimized for incremental calculation

This commit is contained in:
Toh4iem9
2025-11-28 12:37:59 +01:00
parent e2ea754dba
commit d75642694e
+103 -82
View File
@@ -8,34 +8,36 @@
#include <MyIncludes\HeikinAshi_Tools.mqh> #include <MyIncludes\HeikinAshi_Tools.mqh>
//+==================================================================+ //+==================================================================+
//| |
//| CLASS 1: CADXCalculator (Base Class) | //| CLASS 1: CADXCalculator (Base Class) |
//| |
//+==================================================================+ //+==================================================================+
class CADXCalculator class CADXCalculator
{ {
protected: protected:
int m_adx_period; int m_adx_period;
//--- Virtual method for preparing the raw directional movement values. //--- Internal buffers for intermediate calculations
//--- CORRECTED: Added 'open' to the signature for the derived class. //--- We keep them as class members to preserve state between ticks
virtual void PrepareDirectionalMovement(int rates_total, const double &open[], const double &high[], const double &low[], const double &close[], double m_pDM[], m_nDM[], m_TR[];
double m_smoothed_pdm[], m_smoothed_ndm[], m_smoothed_tr[], m_dx[];
//--- Updated: Accepts start_index for optimization
virtual void PrepareDirectionalMovement(int rates_total, int start_index, const double &open[], const double &high[], const double &low[], const double &close[],
double &pDM[], double &nDM[], double &TR[]); double &pDM[], double &nDM[], double &TR[]);
public: public:
CADXCalculator(void) {}; CADXCalculator(void) {};
virtual ~CADXCalculator(void) {}; virtual ~CADXCalculator(void) {};
//--- Public methods
bool Init(int period); bool Init(int period);
int GetPeriod(void) const { return m_adx_period; } int GetPeriod(void) const { return m_adx_period; }
//--- CORRECTED: Added 'open' to the signature.
void Calculate(int rates_total, const double &open[], const double &high[], const double &low[], const double &close[], //--- Updated: Accepts prev_calculated
void Calculate(int rates_total, int prev_calculated, const double &open[], const double &high[], const double &low[], const double &close[],
double &adx_buffer[], double &pdi_buffer[], double &ndi_buffer[]); double &adx_buffer[], double &pdi_buffer[], double &ndi_buffer[]);
}; };
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
//| CADXCalculator: Initialization | //| Init |
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
bool CADXCalculator::Init(int period) bool CADXCalculator::Init(int period)
{ {
@@ -44,93 +46,115 @@ bool CADXCalculator::Init(int period)
} }
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
//| CADXCalculator: Main Calculation Method (Shared Logic) | //| Main Calculation Method (Optimized) |
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
void CADXCalculator::Calculate(int rates_total, const double &open[], const double &high[], const double &low[], const double &close[], void CADXCalculator::Calculate(int rates_total, int prev_calculated, const double &open[], const double &high[], const double &low[], const double &close[],
double &adx_buffer[], double &pdi_buffer[], double &ndi_buffer[]) double &adx_buffer[], double &pdi_buffer[], double &ndi_buffer[])
{ {
if(rates_total < m_adx_period * 2) if(rates_total < m_adx_period * 2)
return; return;
//--- STEP 1: Calculate raw +DM, -DM, and TR (delegated to virtual method) //--- 1. Determine Start Index
double pDM[], nDM[], TR[]; int start_index;
PrepareDirectionalMovement(rates_total, open, high, low, close, pDM, nDM, TR); if(prev_calculated == 0)
start_index = 0;
else
start_index = prev_calculated - 1;
//--- Intermediate calculation buffers //--- 2. Resize internal buffers if needed
double smoothed_pdm[], smoothed_ndm[], smoothed_tr[], dx[]; if(ArraySize(m_pDM) != rates_total)
ArrayResize(smoothed_pdm, rates_total);
ArrayResize(smoothed_ndm, rates_total);
ArrayResize(smoothed_tr, rates_total);
ArrayResize(dx, rates_total);
//--- STEP 2: Calculate Smoothed PDM, NDM, and TR
for(int i = m_adx_period; i < rates_total; i++)
{ {
if(i == m_adx_period) // First calculation is a simple sum ArrayResize(m_pDM, rates_total);
ArrayResize(m_nDM, rates_total);
ArrayResize(m_TR, rates_total);
ArrayResize(m_smoothed_pdm, rates_total);
ArrayResize(m_smoothed_ndm, rates_total);
ArrayResize(m_smoothed_tr, rates_total);
ArrayResize(m_dx, rates_total);
}
//--- 3. Prepare Raw DM and TR (Optimized)
PrepareDirectionalMovement(rates_total, start_index, open, high, low, close, m_pDM, m_nDM, m_TR);
//--- 4. Calculate Smoothed PDM, NDM, and TR
//--- Ensure we don't start before the period
int loop_start = MathMax(m_adx_period, start_index);
for(int i = loop_start; i < rates_total; i++)
{
if(i == m_adx_period) // First calculation: Simple Sum
{ {
double sum_pdm=0, sum_ndm=0, sum_tr=0; double sum_pdm=0, sum_ndm=0, sum_tr=0;
for(int j=1; j<=m_adx_period; j++) for(int j=1; j<=m_adx_period; j++)
{ {
sum_pdm += pDM[j]; sum_pdm += m_pDM[j];
sum_ndm += nDM[j]; sum_ndm += m_nDM[j];
sum_tr += TR[j]; sum_tr += m_TR[j];
} }
smoothed_pdm[i] = sum_pdm; m_smoothed_pdm[i] = sum_pdm;
smoothed_ndm[i] = sum_ndm; m_smoothed_ndm[i] = sum_ndm;
smoothed_tr[i] = sum_tr; m_smoothed_tr[i] = sum_tr;
} }
else // Subsequent calculations use Wilder's smoothing else // Subsequent: Wilder's Smoothing
{ {
smoothed_pdm[i] = smoothed_pdm[i-1] - (smoothed_pdm[i-1] / m_adx_period) + pDM[i]; // This works incrementally because m_smoothed_...[i-1] preserves its value from the previous tick
smoothed_ndm[i] = smoothed_ndm[i-1] - (smoothed_ndm[i-1] / m_adx_period) + nDM[i]; m_smoothed_pdm[i] = m_smoothed_pdm[i-1] - (m_smoothed_pdm[i-1] / m_adx_period) + m_pDM[i];
smoothed_tr[i] = smoothed_tr[i-1] - (smoothed_tr[i-1] / m_adx_period) + TR[i]; m_smoothed_ndm[i] = m_smoothed_ndm[i-1] - (m_smoothed_ndm[i-1] / m_adx_period) + m_nDM[i];
m_smoothed_tr[i] = m_smoothed_tr[i-1] - (m_smoothed_tr[i-1] / m_adx_period) + m_TR[i];
} }
} }
//--- STEP 3: Calculate +DI, -DI, and DX //--- 5. Calculate +DI, -DI, and DX
for(int i = m_adx_period; i < rates_total; i++) for(int i = loop_start; i < rates_total; i++)
{ {
if(smoothed_tr[i] != 0.0) if(m_smoothed_tr[i] != 0.0)
{ {
pdi_buffer[i] = (smoothed_pdm[i] / smoothed_tr[i]) * 100.0; pdi_buffer[i] = (m_smoothed_pdm[i] / m_smoothed_tr[i]) * 100.0;
ndi_buffer[i] = (smoothed_ndm[i] / smoothed_tr[i]) * 100.0; ndi_buffer[i] = (m_smoothed_ndm[i] / m_smoothed_tr[i]) * 100.0;
}
else
{
pdi_buffer[i] = 0.0;
ndi_buffer[i] = 0.0;
} }
double di_sum = pdi_buffer[i] + ndi_buffer[i]; double di_sum = pdi_buffer[i] + ndi_buffer[i];
if(di_sum != 0.0) if(di_sum != 0.0)
dx[i] = MathAbs(pdi_buffer[i] - ndi_buffer[i]) / di_sum * 100.0; m_dx[i] = MathAbs(pdi_buffer[i] - ndi_buffer[i]) / di_sum * 100.0;
else else
dx[i] = 0.0; m_dx[i] = 0.0;
} }
//--- STEP 4: Smooth DX to get the final ADX value //--- 6. Calculate Final ADX
for(int i = m_adx_period * 2 - 1; i < rates_total; i++) int adx_start = m_adx_period * 2 - 1;
int loop_start_adx = MathMax(adx_start, start_index);
for(int i = loop_start_adx; i < rates_total; i++)
{ {
if(i == m_adx_period * 2 - 1) // First ADX value is a simple average if(i == adx_start) // First ADX: Simple Average of DX
{ {
double sum_dx = 0; double sum_dx = 0;
for(int j=i-m_adx_period+1; j<=i; j++) for(int j=i-m_adx_period+1; j<=i; j++)
sum_dx += dx[j]; sum_dx += m_dx[j];
adx_buffer[i] = sum_dx / m_adx_period; adx_buffer[i] = sum_dx / m_adx_period;
} }
else // Subsequent ADX values are smoothed else // Subsequent: Wilder's Smoothing on ADX
{ {
adx_buffer[i] = (adx_buffer[i-1] * (m_adx_period - 1) + dx[i]) / m_adx_period; adx_buffer[i] = (adx_buffer[i-1] * (m_adx_period - 1) + m_dx[i]) / m_adx_period;
} }
} }
} }
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
//| CADXCalculator: Prepares raw DM and TR from standard prices. | //| Prepare Raw DM (Standard - Optimized) |
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
void CADXCalculator::PrepareDirectionalMovement(int rates_total, const double &open[], const double &high[], const double &low[], const double &close[], void CADXCalculator::PrepareDirectionalMovement(int rates_total, int start_index, const double &open[], const double &high[], const double &low[], const double &close[],
double &pDM[], double &nDM[], double &TR[]) double &pDM[], double &nDM[], double &TR[])
{ {
ArrayResize(pDM, rates_total); // Ensure we start at least from index 1
ArrayResize(nDM, rates_total); int i = (start_index < 1) ? 1 : start_index;
ArrayResize(TR, rates_total);
for(int i = 1; i < rates_total; i++) for(; i < rates_total; i++)
{ {
pDM[i] = high[i] - high[i-1]; pDM[i] = high[i] - high[i-1];
nDM[i] = low[i-1] - low[i]; nDM[i] = low[i-1] - low[i];
@@ -145,56 +169,53 @@ void CADXCalculator::PrepareDirectionalMovement(int rates_total, const double &o
} }
//+==================================================================+ //+==================================================================+
//| |
//| CLASS 2: CADXCalculator_HA (Heikin Ashi) | //| CLASS 2: CADXCalculator_HA (Heikin Ashi) |
//| |
//+==================================================================+ //+==================================================================+
class CADXCalculator_HA : public CADXCalculator class CADXCalculator_HA : public CADXCalculator
{ {
private: private:
CHeikinAshi_Calculator m_ha_calculator; // Instance of the HA calculator tool CHeikinAshi_Calculator m_ha_calculator;
// Internal HA buffers
double m_ha_open[], m_ha_high[], m_ha_low[], m_ha_close[];
protected: protected:
//--- Overridden method to prepare Heikin Ashi based DM and TR virtual void PrepareDirectionalMovement(int rates_total, int start_index, const double &open[], const double &high[], const double &low[], const double &close[],
//--- CORRECTED: Signature now matches the base class.
virtual void PrepareDirectionalMovement(int rates_total, const double &open[], const double &high[], const double &low[], const double &close[],
double &pDM[], double &nDM[], double &TR[]) override; double &pDM[], double &nDM[], double &TR[]) override;
}; };
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
//| CADXCalculator_HA: Prepares raw DM and TR from HA prices. | //| Prepare Raw DM (Heikin Ashi - Optimized) |
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
void CADXCalculator_HA::PrepareDirectionalMovement(int rates_total, const double &open[], const double &high[], const double &low[], const double &close[], void CADXCalculator_HA::PrepareDirectionalMovement(int rates_total, int start_index, const double &open[], const double &high[], const double &low[], const double &close[],
double &pDM[], double &nDM[], double &TR[]) double &pDM[], double &nDM[], double &TR[])
{ {
//--- Intermediate Heikin Ashi Buffers // Resize internal HA buffers
double ha_open[], ha_high[], ha_low[], ha_close[]; if(ArraySize(m_ha_open) != rates_total)
ArrayResize(ha_open, rates_total);
ArrayResize(ha_high, rates_total);
ArrayResize(ha_low, rates_total);
ArrayResize(ha_close, rates_total);
//--- Calculate the HA candles first
//--- CORRECTED: Removed invalid GetPointer() call and now passing 'open' correctly.
m_ha_calculator.Calculate(rates_total, open, high, low, close, ha_open, ha_high, ha_low, ha_close);
//--- Now, calculate DM and TR using the HA candles
ArrayResize(pDM, rates_total);
ArrayResize(nDM, rates_total);
ArrayResize(TR, rates_total);
for(int i = 1; i < rates_total; i++)
{ {
pDM[i] = ha_high[i] - ha_high[i-1]; ArrayResize(m_ha_open, rates_total);
nDM[i] = ha_low[i-1] - ha_low[i]; ArrayResize(m_ha_high, rates_total);
ArrayResize(m_ha_low, rates_total);
ArrayResize(m_ha_close, rates_total);
}
//--- CRITICAL: Use the optimized 10-param HA calculation
m_ha_calculator.Calculate(rates_total, start_index, open, high, low, close,
m_ha_open, m_ha_high, m_ha_low, m_ha_close);
//--- Calculate DM/TR based on HA candles
int i = (start_index < 1) ? 1 : start_index;
for(; i < rates_total; i++)
{
pDM[i] = m_ha_high[i] - m_ha_high[i-1];
nDM[i] = m_ha_low[i-1] - m_ha_low[i];
if(pDM[i] < 0 || pDM[i] < nDM[i]) if(pDM[i] < 0 || pDM[i] < nDM[i])
pDM[i] = 0; pDM[i] = 0;
if(nDM[i] < 0 || nDM[i] < pDM[i]) if(nDM[i] < 0 || nDM[i] < pDM[i])
nDM[i] = 0; nDM[i] = 0;
TR[i] = MathMax(ha_high[i], ha_close[i-1]) - MathMin(ha_low[i], ha_close[i-1]); TR[i] = MathMax(m_ha_high[i], m_ha_close[i-1]) - MathMin(m_ha_low[i], m_ha_close[i-1]);
} }
} }
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
//+------------------------------------------------------------------+