refactor: Optimized for incremental calculation

This commit is contained in:
Toh4iem9
2025-11-28 14:11:53 +01:00
parent 10232c7fa1
commit ae3f2bea03
+171 -119
View File
@@ -1,13 +1,13 @@
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
//| MovingAverage_Engine.mqh | //| MovingAverage_Engine.mqh |
//| VERSION 1.30: Added DEMA and TEMA for lag reduction. | //| VERSION 1.40: Optimized for incremental calculation. |
//| Copyright 2025, xxxxxxxx | //| Copyright 2025, xxxxxxxx |
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
#property copyright "Copyright 2025, xxxxxxxx" #property copyright "Copyright 2025, xxxxxxxx"
#include <MyIncludes\HeikinAshi_Tools.mqh> #include <MyIncludes\HeikinAshi_Tools.mqh>
//--- UPDATED: Enum to select the MA type for calculation --- //--- Enum to select the MA type for calculation
enum ENUM_MA_TYPE enum ENUM_MA_TYPE
{ {
SMA, SMA,
@@ -25,17 +25,30 @@ class CMovingAverageCalculator
protected: protected:
int m_period; int m_period;
ENUM_MA_TYPE m_ma_type; ENUM_MA_TYPE m_ma_type;
//--- Persistent Buffers for Incremental Calculation
double m_price[]; double m_price[];
virtual bool PreparePriceSeries(int rates_total, ENUM_APPLIED_PRICE price_type, const double &open[], const double &high[], const double &low[], const double &close[]); //--- Buffers for complex MAs (TMA, DEMA, TEMA)
void CalculateEMA(int rates_total, int period, const double &source[], double &dest[]); double m_temp_buffer1[]; // Used for TMA(sma1), DEMA(ema1), TEMA(ema1)
double m_temp_buffer2[]; // Used for DEMA(ema2), TEMA(ema2)
double m_temp_buffer3[]; // Used for TEMA(ema3)
//--- Updated: Accepts start_index
virtual bool PreparePriceSeries(int rates_total, int start_index, ENUM_APPLIED_PRICE price_type, const double &open[], const double &high[], const double &low[], const double &close[]);
//--- Updated: Accepts start_index
void CalculateEMA(int rates_total, int start_index, int period, const double &source[], double &dest[]);
public: public:
CMovingAverageCalculator(void) {}; CMovingAverageCalculator(void) {};
virtual ~CMovingAverageCalculator(void) {}; virtual ~CMovingAverageCalculator(void) {};
bool Init(int period, ENUM_MA_TYPE ma_type); bool Init(int period, ENUM_MA_TYPE ma_type);
void Calculate(int rates_total, ENUM_APPLIED_PRICE price_type, const double &open[], const double &high[], const double &low[], const double &close[], double &ma_buffer[]);
//--- Updated: Accepts prev_calculated
void Calculate(int rates_total, int prev_calculated, ENUM_APPLIED_PRICE price_type, const double &open[], const double &high[], const double &low[], const double &close[], double &ma_buffer[]);
int GetPeriod(void) const { return m_period; } int GetPeriod(void) const { return m_period; }
}; };
@@ -46,8 +59,11 @@ class CMovingAverageCalculator_HA : public CMovingAverageCalculator
{ {
private: private:
CHeikinAshi_Calculator m_ha_calculator; CHeikinAshi_Calculator m_ha_calculator;
// Internal HA buffers (Persistent)
double m_ha_open[], m_ha_high[], m_ha_low[], m_ha_close[];
protected: protected:
virtual bool PreparePriceSeries(int rates_total, ENUM_APPLIED_PRICE price_type, const double &open[], const double &high[], const double &low[], const double &close[]) override; virtual bool PreparePriceSeries(int rates_total, int start_index, ENUM_APPLIED_PRICE price_type, const double &open[], const double &high[], const double &low[], const double &close[]) override;
}; };
//+==================================================================+ //+==================================================================+
@@ -55,7 +71,7 @@ protected:
//+==================================================================+ //+==================================================================+
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
//| | //| Init |
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
bool CMovingAverageCalculator::Init(int period, ENUM_MA_TYPE ma_type) bool CMovingAverageCalculator::Init(int period, ENUM_MA_TYPE ma_type)
{ {
@@ -65,26 +81,49 @@ bool CMovingAverageCalculator::Init(int period, ENUM_MA_TYPE ma_type)
} }
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
//| | //| Main Calculation (Optimized) |
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
void CMovingAverageCalculator::Calculate(int rates_total, ENUM_APPLIED_PRICE price_type, const double &open[], const double &high[], const double &low[], const double &close[], double &ma_buffer[]) void CMovingAverageCalculator::Calculate(int rates_total, int prev_calculated, ENUM_APPLIED_PRICE price_type, const double &open[], const double &high[], const double &low[], const double &close[], double &ma_buffer[])
{ {
if(rates_total < m_period) if(rates_total < m_period)
return; return;
if(!PreparePriceSeries(rates_total, price_type, 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_price) != rates_total)
{
ArrayResize(m_price, rates_total);
// Resize temp buffers only if needed by type
if(m_ma_type == TMA || m_ma_type == DEMA || m_ma_type == TEMA)
ArrayResize(m_temp_buffer1, rates_total);
if(m_ma_type == DEMA || m_ma_type == TEMA)
ArrayResize(m_temp_buffer2, rates_total);
if(m_ma_type == TEMA)
ArrayResize(m_temp_buffer3, rates_total);
}
//--- 3. Prepare Price (Optimized)
if(!PreparePriceSeries(rates_total, start_index, price_type, open, high, low, close))
return; return;
int start_pos = m_period - 1; int start_pos = m_period - 1;
for(int i = 0; i < rates_total; i++) // Clear all values initially int loop_start = MathMax(start_pos, start_index);
ma_buffer[i] = EMPTY_VALUE;
//--- 4. Calculate MA based on type
switch(m_ma_type) switch(m_ma_type)
{ {
case EMA: case EMA:
CalculateEMA(rates_total, m_period, m_price, ma_buffer); CalculateEMA(rates_total, start_index, m_period, m_price, ma_buffer);
break; break;
case SMMA: case SMMA:
for(int i = start_pos; i < rates_total; i++) for(int i = loop_start; i < rates_total; i++)
{ {
if(i == start_pos) if(i == start_pos)
{ {
@@ -94,11 +133,13 @@ void CMovingAverageCalculator::Calculate(int rates_total, ENUM_APPLIED_PRICE pri
ma_buffer[i]=sum/m_period; ma_buffer[i]=sum/m_period;
} }
else else
// Recursive SMMA works incrementally because ma_buffer[i-1] is preserved
ma_buffer[i]=(ma_buffer[i-1]*(m_period-1)+m_price[i])/m_period; ma_buffer[i]=(ma_buffer[i-1]*(m_period-1)+m_price[i])/m_period;
} }
break; break;
case LWMA: case LWMA:
for(int i = start_pos; i < rates_total; i++) for(int i = loop_start; i < rates_total; i++)
{ {
double sum=0, w_sum=0; double sum=0, w_sum=0;
for(int j=0; j<m_period; j++) for(int j=0; j<m_period; j++)
@@ -111,56 +152,65 @@ void CMovingAverageCalculator::Calculate(int rates_total, ENUM_APPLIED_PRICE pri
ma_buffer[i]=sum/w_sum; ma_buffer[i]=sum/w_sum;
} }
break; break;
case TMA: case TMA:
{ {
double sma1_buffer[];
ArrayResize(sma1_buffer, rates_total);
int period1 = (int)ceil((m_period + 1.0) / 2.0); int period1 = (int)ceil((m_period + 1.0) / 2.0);
int loop_start_tma = MathMax(period1 - 1, start_index);
for(int i = period1 - 1; i < rates_total; i++) // Step 1: Simple MA into temp buffer
for(int i = loop_start_tma; i < rates_total; i++)
{ {
double sum = 0; double sum = 0;
for(int j = 0; j < period1; j++) for(int j = 0; j < period1; j++)
sum += m_price[i-j]; sum += m_price[i-j];
sma1_buffer[i] = sum / period1; m_temp_buffer1[i] = sum / period1;
} }
// Step 2: Simple MA of the first MA
int period2 = m_period - period1 + 1; int period2 = m_period - period1 + 1;
for(int i = period1 + period2 - 2; i < rates_total; i++) int loop_start_final = MathMax(period1 + period2 - 2, start_index);
for(int i = loop_start_final; i < rates_total; i++)
{ {
double sum = 0; double sum = 0;
for(int j = 0; j < period2; j++) for(int j = 0; j < period2; j++)
sum += sma1_buffer[i-j]; sum += m_temp_buffer1[i-j];
ma_buffer[i] = sum / period2; ma_buffer[i] = sum / period2;
} }
} }
break; break;
case DEMA: case DEMA:
{ {
double ema1[], ema2[]; // EMA1 of Price
ArrayResize(ema1, rates_total); CalculateEMA(rates_total, start_index, m_period, m_price, m_temp_buffer1);
ArrayResize(ema2, rates_total); // EMA2 of EMA1
CalculateEMA(rates_total, m_period, m_price, ema1); CalculateEMA(rates_total, start_index, m_period, m_temp_buffer1, m_temp_buffer2);
CalculateEMA(rates_total, m_period, ema1, ema2);
for(int i = (m_period - 1) * 2; i < rates_total; i++) int loop_start_dema = MathMax((m_period - 1) * 2, start_index);
ma_buffer[i] = 2 * ema1[i] - ema2[i]; for(int i = loop_start_dema; i < rates_total; i++)
ma_buffer[i] = 2 * m_temp_buffer1[i] - m_temp_buffer2[i];
break; break;
} }
case TEMA: case TEMA:
{ {
double ema1[], ema2[], ema3[]; // EMA1 of Price
ArrayResize(ema1, rates_total); CalculateEMA(rates_total, start_index, m_period, m_price, m_temp_buffer1);
ArrayResize(ema2, rates_total); // EMA2 of EMA1
ArrayResize(ema3, rates_total); CalculateEMA(rates_total, start_index, m_period, m_temp_buffer1, m_temp_buffer2);
CalculateEMA(rates_total, m_period, m_price, ema1); // EMA3 of EMA2
CalculateEMA(rates_total, m_period, ema1, ema2); CalculateEMA(rates_total, start_index, m_period, m_temp_buffer2, m_temp_buffer3);
CalculateEMA(rates_total, m_period, ema2, ema3);
for(int i = (m_period - 1) * 3; i < rates_total; i++) int loop_start_tema = MathMax((m_period - 1) * 3, start_index);
ma_buffer[i] = 3 * ema1[i] - 3 * ema2[i] + ema3[i]; for(int i = loop_start_tema; i < rates_total; i++)
ma_buffer[i] = 3 * m_temp_buffer1[i] - 3 * m_temp_buffer2[i] + m_temp_buffer3[i];
break; break;
} }
default: // SMA default: // SMA
for(int i = start_pos; i < rates_total; i++) for(int i = loop_start; i < rates_total; i++)
{ {
double sum=0; double sum=0;
for(int j=0; j<m_period; j++) for(int j=0; j<m_period; j++)
@@ -172,27 +222,34 @@ void CMovingAverageCalculator::Calculate(int rates_total, ENUM_APPLIED_PRICE pri
} }
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
//| | //| Calculate EMA (Optimized) |
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
void CMovingAverageCalculator::CalculateEMA(int rates_total, int period, const double &source[], double &dest[]) void CMovingAverageCalculator::CalculateEMA(int rates_total, int start_index, int period, const double &source[], double &dest[])
{ {
if(rates_total < period) if(rates_total < period)
return; return;
int start_pos = period - 1; int start_pos = period - 1;
double pr = 2.0 / (double)(period + 1.0); double pr = 2.0 / (double)(period + 1.0);
for(int i=0; i<start_pos; i++) // Determine where to start loop
dest[i] = EMPTY_VALUE; int i = MathMax(start_pos, start_index);
double sum=0; // If starting from the very beginning (or before valid data), initialize first value
for(int j=0; j<period; j++) if(i == start_pos)
if(source[start_pos-j] != EMPTY_VALUE) {
sum += source[start_pos-j]; double sum=0;
dest[start_pos] = sum / period; for(int j=0; j<period; j++)
if(source[start_pos-j] != EMPTY_VALUE)
sum += source[start_pos-j];
dest[start_pos] = sum / period;
i++; // Move to next
}
for(int i = start_pos + 1; i < rates_total; i++) for(; i < rates_total; i++)
{ {
if(source[i] != EMPTY_VALUE) if(source[i] != EMPTY_VALUE)
// Recursive calculation uses dest[i-1] which is safe due to persistence
dest[i] = source[i] * pr + dest[i-1] * (1.0 - pr); dest[i] = source[i] * pr + dest[i-1] * (1.0 - pr);
else else
dest[i] = dest[i-1]; dest[i] = dest[i-1];
@@ -200,92 +257,87 @@ void CMovingAverageCalculator::CalculateEMA(int rates_total, int period, const d
} }
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
//| | //| Prepare Price (Standard - Optimized) |
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
bool CMovingAverageCalculator::PreparePriceSeries(int rates_total, ENUM_APPLIED_PRICE price_type, const double &open[], const double &high[], const double &low[], const double &close[]) bool CMovingAverageCalculator::PreparePriceSeries(int rates_total, int start_index, ENUM_APPLIED_PRICE price_type, const double &open[], const double &high[], const double &low[], const double &close[])
{ {
if(ArraySize(m_price) != rates_total) // Optimized copy loop
if(ArrayResize(m_price, rates_total) != rates_total) for(int i = start_index; i < rates_total; i++)
return false;
switch(price_type)
{ {
case PRICE_CLOSE: switch(price_type)
ArrayCopy(m_price, close, 0, 0, rates_total); {
break; case PRICE_CLOSE:
case PRICE_OPEN: m_price[i] = close[i];
ArrayCopy(m_price, open, 0, 0, rates_total); break;
break; case PRICE_OPEN:
case PRICE_HIGH: m_price[i] = open[i];
ArrayCopy(m_price, high, 0, 0, rates_total); break;
break; case PRICE_HIGH:
case PRICE_LOW: m_price[i] = high[i];
ArrayCopy(m_price, low, 0, 0, rates_total); break;
break; case PRICE_LOW:
case PRICE_MEDIAN: m_price[i] = low[i];
for(int i=0; i<rates_total; i++) break;
case PRICE_MEDIAN:
m_price[i] = (high[i]+low[i])/2.0; m_price[i] = (high[i]+low[i])/2.0;
break; break;
case PRICE_TYPICAL: case PRICE_TYPICAL:
for(int i=0; i<rates_total; i++)
m_price[i] = (high[i]+low[i]+close[i])/3.0; m_price[i] = (high[i]+low[i]+close[i])/3.0;
break; break;
case PRICE_WEIGHTED: case PRICE_WEIGHTED:
for(int i=0; i<rates_total; i++)
m_price[i] = (high[i]+low[i]+close[i]+close[i])/4.0; m_price[i] = (high[i]+low[i]+close[i]+close[i])/4.0;
break; break;
default: }
return false;
} }
return true; return true;
} }
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
//| | //| Prepare Price (Heikin Ashi - Optimized) |
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
bool CMovingAverageCalculator_HA::PreparePriceSeries(int rates_total, ENUM_APPLIED_PRICE price_type, const double &open[], const double &high[], const double &low[], const double &close[]) bool CMovingAverageCalculator_HA::PreparePriceSeries(int rates_total, int start_index, ENUM_APPLIED_PRICE price_type, const double &open[], const double &high[], const double &low[], const double &close[])
{ {
double ha_open[], ha_high[], ha_low[], ha_close[]; // Resize internal HA buffers
ArrayResize(ha_open, rates_total); if(ArraySize(m_ha_open) != rates_total)
ArrayResize(ha_high, rates_total);
ArrayResize(ha_low, rates_total);
ArrayResize(ha_close, rates_total);
m_ha_calculator.Calculate(rates_total, open, high, low, close, ha_open, ha_high, ha_low, ha_close);
if(ArraySize(m_price) != rates_total)
if(ArrayResize(m_price, rates_total) != rates_total)
return false;
switch(price_type)
{ {
case PRICE_CLOSE: ArrayResize(m_ha_open, rates_total);
ArrayCopy(m_price, ha_close, 0, 0, rates_total); ArrayResize(m_ha_high, rates_total);
break; ArrayResize(m_ha_low, rates_total);
case PRICE_OPEN: ArrayResize(m_ha_close, rates_total);
ArrayCopy(m_price, ha_open, 0, 0, rates_total); }
break;
case PRICE_HIGH: //--- STRICT CALL: Use the optimized 10-param HA calculation
ArrayCopy(m_price, ha_high, 0, 0, rates_total); m_ha_calculator.Calculate(rates_total, start_index, open, high, low, close,
break; m_ha_open, m_ha_high, m_ha_low, m_ha_close);
case PRICE_LOW:
ArrayCopy(m_price, ha_low, 0, 0, rates_total); //--- Copy to m_price (Optimized loop)
break; for(int i = start_index; i < rates_total; i++)
case PRICE_MEDIAN: {
for(int i=0; i<rates_total; i++) switch(price_type)
m_price[i] = (ha_high[i]+ha_low[i])/2.0; {
break; case PRICE_CLOSE:
case PRICE_TYPICAL: m_price[i] = m_ha_close[i];
for(int i=0; i<rates_total; i++) break;
m_price[i] = (ha_high[i]+ha_low[i]+ha_close[i])/3.0; case PRICE_OPEN:
break; m_price[i] = m_ha_open[i];
case PRICE_WEIGHTED: break;
for(int i=0; i<rates_total; i++) case PRICE_HIGH:
m_price[i] = (ha_high[i]+ha_low[i]+ha_close[i]+ha_close[i])/4.0; m_price[i] = m_ha_high[i];
break; break;
default: case PRICE_LOW:
return false; m_price[i] = m_ha_low[i];
break;
case PRICE_MEDIAN:
m_price[i] = (m_ha_high[i]+m_ha_low[i])/2.0;
break;
case PRICE_TYPICAL:
m_price[i] = (m_ha_high[i]+m_ha_low[i]+m_ha_close[i])/3.0;
break;
case PRICE_WEIGHTED:
m_price[i] = (m_ha_high[i]+m_ha_low[i]+m_ha_close[i]+m_ha_close[i])/4.0;
break;
}
} }
return true; return true;
} }
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
//+------------------------------------------------------------------+