refactor: Optimized for incremental calculation

This commit is contained in:
Toh4iem9
2025-12-16 15:23:40 +01:00
parent dc0b083a80
commit 64f39aeff6
+120 -60
View File
@@ -1,6 +1,6 @@
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
//| Gann_HiLo_Calculator.mqh| //| Gann_HiLo_Calculator.mqh|
//| Calculation engine for Standard and Heikin Ashi Gann HiLo. | //| VERSION 2.00: Optimized for incremental calculation. |
//| Copyright 2025, xxxxxxxx | //| Copyright 2025, xxxxxxxx |
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
#property copyright "Copyright 2025, xxxxxxxx" #property copyright "Copyright 2025, xxxxxxxx"
@@ -8,9 +8,7 @@
#include <MyIncludes\HeikinAshi_Tools.mqh> #include <MyIncludes\HeikinAshi_Tools.mqh>
//+==================================================================+ //+==================================================================+
//| |
//| CLASS 1: CGannHiLoCalculator (Base Class) | //| CLASS 1: CGannHiLoCalculator (Base Class) |
//| |
//+==================================================================+ //+==================================================================+
class CGannHiLoCalculator class CGannHiLoCalculator
{ {
@@ -18,20 +16,25 @@ protected:
int m_period; int m_period;
ENUM_MA_METHOD m_ma_method; ENUM_MA_METHOD m_ma_method;
//--- Persistent Buffers for Incremental Calculation
double m_src_high[], m_src_low[], m_src_close[]; double m_src_high[], m_src_low[], m_src_close[];
double m_hi_avg[], m_lo_avg[], m_trend[];
virtual bool PrepareSourceData(int rates_total, const double &open[], const double &high[], const double &low[], const double &close[]); //--- Updated: Accepts start_index
virtual bool PrepareSourceData(int rates_total, int start_index, const double &open[], const double &high[], const double &low[], const double &close[]);
public: public:
CGannHiLoCalculator(void) {}; CGannHiLoCalculator(void) {};
virtual ~CGannHiLoCalculator(void) {}; virtual ~CGannHiLoCalculator(void) {};
bool Init(int period, ENUM_MA_METHOD ma_method); bool Init(int period, ENUM_MA_METHOD ma_method);
void Calculate(int rates_total, const double &open[], const double &high[], const double &low[], const double &close[], double &hilo_buffer[], double &color_buffer[]);
//--- 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 &hilo_buffer[], double &color_buffer[]);
}; };
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
//| CGannHiLoCalculator: Initialization | //| Init |
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
bool CGannHiLoCalculator::Init(int period, ENUM_MA_METHOD ma_method) bool CGannHiLoCalculator::Init(int period, ENUM_MA_METHOD ma_method)
{ {
@@ -41,30 +44,46 @@ bool CGannHiLoCalculator::Init(int period, ENUM_MA_METHOD ma_method)
} }
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
//| CGannHiLoCalculator: Main Calculation Method (Shared Logic) | //| Main Calculation (Optimized) |
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
void CGannHiLoCalculator::Calculate(int rates_total, const double &open[], const double &high[], const double &low[], const double &close[], double &hilo_buffer[], double &color_buffer[]) void CGannHiLoCalculator::Calculate(int rates_total, int prev_calculated, const double &open[], const double &high[], const double &low[], const double &close[], double &hilo_buffer[], double &color_buffer[])
{ {
if(rates_total <= m_period) if(rates_total <= m_period)
return; return;
if(!PrepareSourceData(rates_total, open, high, low, close))
//--- 1. Determine Start Index
int start_index;
if(prev_calculated == 0)
start_index = 0;
else
start_index = prev_calculated - 1;
//--- 2. Resize Buffers
if(ArraySize(m_src_high) != rates_total)
{
ArrayResize(m_src_high, rates_total);
ArrayResize(m_src_low, rates_total);
ArrayResize(m_src_close, rates_total);
ArrayResize(m_hi_avg, rates_total);
ArrayResize(m_lo_avg, rates_total);
ArrayResize(m_trend, rates_total);
}
//--- 3. Prepare Source Data (Optimized)
if(!PrepareSourceData(rates_total, start_index, open, high, low, close))
return; return;
double hi_avg[], lo_avg[], trend[]; //--- 4. Calculate High/Low Averages (Incremental)
ArrayResize(hi_avg, rates_total); int ma_start_pos = m_period - 1;
ArrayResize(lo_avg, rates_total); int loop_start = MathMax(ma_start_pos, start_index);
ArrayResize(trend, rates_total);
for(int i = 1; i < rates_total; i++) for(int i = loop_start; i < rates_total; i++)
{ {
if(i < m_period - 1)
continue;
switch(m_ma_method) switch(m_ma_method)
{ {
case MODE_EMA: case MODE_EMA:
case MODE_SMMA: case MODE_SMMA:
if(i == m_period - 1) if(i == ma_start_pos)
{ {
double sum_h=0, sum_l=0; double sum_h=0, sum_l=0;
for(int j=0; j<m_period; j++) for(int j=0; j<m_period; j++)
@@ -72,100 +91,141 @@ void CGannHiLoCalculator::Calculate(int rates_total, const double &open[], const
sum_h+=m_src_high[i-j]; sum_h+=m_src_high[i-j];
sum_l+=m_src_low[i-j]; sum_l+=m_src_low[i-j];
} }
hi_avg[i]=sum_h/m_period; m_hi_avg[i]=sum_h/m_period;
lo_avg[i]=sum_l/m_period; m_lo_avg[i]=sum_l/m_period;
} }
else else
{ {
if(m_ma_method==MODE_EMA) if(m_ma_method==MODE_EMA)
{ {
double pr=2.0/(m_period+1.0); double pr=2.0/(m_period+1.0);
hi_avg[i]=m_src_high[i]*pr+hi_avg[i-1]*(1.0-pr); m_hi_avg[i]=m_src_high[i]*pr+m_hi_avg[i-1]*(1.0-pr);
lo_avg[i]=m_src_low[i]*pr+lo_avg[i-1]*(1.0-pr); m_lo_avg[i]=m_src_low[i]*pr+m_lo_avg[i-1]*(1.0-pr);
} }
else else
{ {
hi_avg[i]=(hi_avg[i-1]*(m_period-1)+m_src_high[i])/m_period; m_hi_avg[i]=(m_hi_avg[i-1]*(m_period-1)+m_src_high[i])/m_period;
lo_avg[i]=(lo_avg[i-1]*(m_period-1)+m_src_low[i])/m_period; m_lo_avg[i]=(m_lo_avg[i-1]*(m_period-1)+m_src_low[i])/m_period;
} }
} }
break; break;
case MODE_LWMA: case MODE_LWMA:
{double wh=0,wl=0,ws=0; for(int j=0; j<m_period; j++) {int w=m_period-j; wh+=m_src_high[i-j]*w; wl+=m_src_low[i-j]*w; ws+=w;} if(ws>0) {hi_avg[i]=wh/ws; lo_avg[i]=wl/ws;}} {
double wh=0,wl=0,ws=0;
for(int j=0; j<m_period; j++)
{
int w=m_period-j;
wh+=m_src_high[i-j]*w;
wl+=m_src_low[i-j]*w;
ws+=w;
}
if(ws>0)
{
m_hi_avg[i]=wh/ws;
m_lo_avg[i]=wl/ws;
}
}
break; break;
default: default: // SMA
{double sh=0,sl=0; for(int j=0; j<m_period; j++) {sh+=m_src_high[i-j]; sl+=m_src_low[i-j];} hi_avg[i]=sh/m_period; lo_avg[i]=sl/m_period;} {
double sh=0,sl=0;
for(int j=0; j<m_period; j++)
{
sh+=m_src_high[i-j];
sl+=m_src_low[i-j];
}
m_hi_avg[i]=sh/m_period;
m_lo_avg[i]=sl/m_period;
}
break; break;
} }
//--- 5. Determine Trend (Incremental)
// We need m_trend[i-1] which is persistent
if(i < m_period) if(i < m_period)
continue; continue;
if(m_src_close[i] > hi_avg[i-1]) if(m_src_close[i] > m_hi_avg[i-1])
trend[i] = 1; m_trend[i] = 1;
else else
if(m_src_close[i] < lo_avg[i-1]) if(m_src_close[i] < m_lo_avg[i-1])
trend[i] = -1; m_trend[i] = -1;
else else
trend[i] = trend[i-1]; m_trend[i] = m_trend[i-1]; // Keep previous trend
if(trend[i] == 1) //--- 6. Output to Buffers
if(m_trend[i] == 1)
{ {
hilo_buffer[i] = lo_avg[i]; hilo_buffer[i] = m_lo_avg[i];
color_buffer[i] = 0; color_buffer[i] = 0;
if(trend[i-1] == -1) // Backfill gap if trend changed
hilo_buffer[i-1] = lo_avg[i]; if(m_trend[i-1] == -1)
hilo_buffer[i-1] = m_lo_avg[i];
} }
else else
{ {
hilo_buffer[i] = hi_avg[i]; hilo_buffer[i] = m_hi_avg[i];
color_buffer[i] = 1; color_buffer[i] = 1;
if(trend[i-1] == 1) if(m_trend[i-1] == 1)
hilo_buffer[i-1] = hi_avg[i]; hilo_buffer[i-1] = m_hi_avg[i];
} }
} }
} }
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
//| CGannHiLoCalculator: Prepares the standard source data series. | //| Prepare Source Data (Standard - Optimized) |
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
bool CGannHiLoCalculator::PrepareSourceData(int rates_total, const double &open[], const double &high[], const double &low[], const double &close[]) bool CGannHiLoCalculator::PrepareSourceData(int rates_total, int start_index, const double &open[], const double &high[], const double &low[], const double &close[])
{ {
ArrayResize(m_src_high, rates_total); // Optimized copy loop
ArrayCopy(m_src_high, high, 0, 0, rates_total); for(int i = start_index; i < rates_total; i++)
ArrayResize(m_src_low, rates_total); {
ArrayCopy(m_src_low, low, 0, 0, rates_total); m_src_high[i] = high[i];
ArrayResize(m_src_close, rates_total); m_src_low[i] = low[i];
ArrayCopy(m_src_close, close, 0, 0, rates_total); m_src_close[i] = close[i];
}
return true; return true;
} }
//+==================================================================+ //+==================================================================+
//| | //| CLASS 2: CGannHiLoCalculator_HA (Heikin Ashi) |
//| CLASS 2: CGannHiLoCalculator_HA (Heikin Ashi) |
//| |
//+==================================================================+ //+==================================================================+
class CGannHiLoCalculator_HA : public CGannHiLoCalculator class CGannHiLoCalculator_HA : public CGannHiLoCalculator
{ {
private: private:
CHeikinAshi_Calculator m_ha_calculator; CHeikinAshi_Calculator m_ha_calculator;
// Internal HA buffers
double m_ha_open[], m_ha_high_temp[], m_ha_low_temp[], m_ha_close_temp[];
protected: protected:
virtual bool PrepareSourceData(int rates_total, const double &open[], const double &high[], const double &low[], const double &close[]) override; virtual bool PrepareSourceData(int rates_total, int start_index, const double &open[], const double &high[], const double &low[], const double &close[]) override;
}; };
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
//| CGannHiLoCalculator_HA: Prepares the Heikin Ashi source data. | //| Prepare Source Data (Heikin Ashi - Optimized) |
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
bool CGannHiLoCalculator_HA::PrepareSourceData(int rates_total, const double &open[], const double &high[], const double &low[], const double &close[]) bool CGannHiLoCalculator_HA::PrepareSourceData(int rates_total, int start_index, const double &open[], const double &high[], const double &low[], const double &close[])
{ {
double ha_open[]; // Resize internal HA buffers
ArrayResize(ha_open, rates_total); if(ArraySize(m_ha_open) != rates_total)
ArrayResize(m_src_high, rates_total); {
ArrayResize(m_src_low, rates_total); ArrayResize(m_ha_open, rates_total);
ArrayResize(m_src_close, rates_total); ArrayResize(m_ha_high_temp, rates_total);
ArrayResize(m_ha_low_temp, rates_total);
ArrayResize(m_ha_close_temp, rates_total);
}
m_ha_calculator.Calculate(rates_total, open, high, low, close, ha_open, m_src_high, m_src_low, m_src_close); //--- STRICT CALL: 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_temp, m_ha_low_temp, m_ha_close_temp);
//--- Copy to source buffers (Optimized loop)
for(int i = start_index; i < rates_total; i++)
{
m_src_high[i] = m_ha_high_temp[i];
m_src_low[i] = m_ha_low_temp[i];
m_src_close[i] = m_ha_close_temp[i];
}
return true; return true;
} }
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
//+------------------------------------------------------------------+