refactor(indicators): Optimized for incremental calculation

This commit is contained in:
Toh4iem9
2026-01-05 08:54:20 +01:00
parent 4e4aebb71a
commit 2fd752774d
+117 -74
View File
@@ -1,182 +1,225 @@
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
//| Windowed_MA_Calculator.mqh | //| Windowed_MA_Calculator.mqh |
//| Calculation engine for Windowed FIR filters (SMA, HWMA). | //| Calculation engine for Hann Windowed FIR filter. |
//| VERSION 2.00: 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>
enum ENUM_WINDOW_TYPE { W_SMA, W_TRIANGULAR, W_HANN };
enum ENUM_INPUT_SOURCE { SOURCE_PRICE, SOURCE_MOMENTUM }; // Price or (Close-Open) enum ENUM_INPUT_SOURCE { SOURCE_PRICE, SOURCE_MOMENTUM }; // Price or (Close-Open)
//+==================================================================+
//| CLASS 1: CWindowedMACalculator (Base Class) |
//+==================================================================+ //+==================================================================+
class CWindowedMACalculator class CWindowedMACalculator
{ {
protected: protected:
int m_period; int m_period;
ENUM_WINDOW_TYPE m_window_type;
ENUM_INPUT_SOURCE m_source_type; ENUM_INPUT_SOURCE m_source_type;
//--- Persistent Buffer for Incremental Calculation
double m_source_data[]; double m_source_data[];
virtual bool PrepareSourceData(int rates_total, ENUM_APPLIED_PRICE price_type, const double &open[], const double &high[], const double &low[], const double &close[]); //--- Pre-calculated Weights
double m_weights[];
double m_weight_sum;
//--- Updated: Accepts start_index
virtual bool PrepareSourceData(int rates_total, int start_index, ENUM_APPLIED_PRICE price_type, const double &open[], const double &high[], const double &low[], const double &close[]);
public: public:
CWindowedMACalculator(void) {}; CWindowedMACalculator(void) {};
virtual ~CWindowedMACalculator(void) {}; virtual ~CWindowedMACalculator(void) {};
bool Init(int period, ENUM_WINDOW_TYPE window_type, ENUM_INPUT_SOURCE source_type); bool Init(int period, ENUM_INPUT_SOURCE source_type);
void Calculate(int rates_total, ENUM_APPLIED_PRICE price_type, const double &open[], const double &high[], const double &low[], const double &close[], double &output_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 &output_buffer[]);
}; };
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
bool CWindowedMACalculator::Init(int period, ENUM_WINDOW_TYPE window_type, ENUM_INPUT_SOURCE source_type) //| Init |
{
m_period = (period < 1) ? 1 : period;
m_window_type = window_type;
m_source_type = source_type;
return true;
}
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
void CWindowedMACalculator::Calculate(int rates_total, ENUM_APPLIED_PRICE price_type, const double &open[], const double &high[], const double &low[], const double &close[], double &output_buffer[]) bool CWindowedMACalculator::Init(int period, ENUM_INPUT_SOURCE source_type)
{ {
if(rates_total < m_period) m_period = (period < 2) ? 2 : period;
return; m_source_type = source_type;
if(!PrepareSourceData(rates_total, price_type, open, high, low, close))
return;
for(int i = m_period - 1; i < rates_total; i++) // Pre-calculate Hann Weights
{ ArrayResize(m_weights, m_period);
double sum = 0; m_weight_sum = 0;
double weight_sum = 0;
for(int j = 0; j < m_period; j++) for(int j = 0; j < m_period; j++)
{ {
double weight = 1.0; // Hann Window Formula: 0.5 * (1 - cos(2*pi*j / (N-1)))
switch(m_window_type) // Note: j goes from 0 to N-1.
// j=0 -> weight=0
// j=N-1 -> weight=0
// Peak is at j=(N-1)/2
double weight = 0.5 * (1.0 - cos(2.0 * M_PI * j / (m_period - 1.0)));
m_weights[j] = weight;
m_weight_sum += weight;
}
return (m_weight_sum > 0);
}
//+------------------------------------------------------------------+
//| Main Calculation (Optimized) |
//+------------------------------------------------------------------+
void CWindowedMACalculator::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 &output_buffer[])
{ {
case W_TRIANGULAR: if(rates_total < m_period)
weight = (m_period/2.0) - fabs(j - (m_period-1.0)/2.0); return;
break;
case W_HANN: //--- 1. Determine Start Index
if(m_period > 1) int start_index;
weight = 0.5 * (1.0 - cos(2.0 * M_PI * j / (m_period - 1.0))); if(prev_calculated == 0)
start_index = 0;
else else
weight = 1.0; start_index = prev_calculated - 1;
break;
//--- 2. Resize Buffer
if(ArraySize(m_source_data) != rates_total)
ArrayResize(m_source_data, rates_total);
//--- 3. Prepare Source Data (Optimized)
if(!PrepareSourceData(rates_total, start_index, price_type, open, high, low, close))
return;
//--- 4. Calculate Windowed MA (Incremental Loop)
int loop_start = MathMax(m_period - 1, start_index);
for(int i = loop_start; i < rates_total; i++)
{
double sum = 0;
// Convolution: Sum(Price[i-j] * Weight[j])
// Optimization: Weights are pre-calculated
for(int j = 0; j < m_period; j++)
{
sum += m_source_data[i-j] * m_weights[j];
} }
sum += m_source_data[i-j] * weight; output_buffer[i] = sum / m_weight_sum;
weight_sum += weight;
}
if(weight_sum > 0)
output_buffer[i] = sum / weight_sum;
} }
} }
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
bool CWindowedMACalculator::PrepareSourceData(int rates_total, ENUM_APPLIED_PRICE price_type, const double &open[], const double &high[], const double &low[], const double &close[]) //| Prepare Source Data (Standard - Optimized) |
//+------------------------------------------------------------------+
bool CWindowedMACalculator::PrepareSourceData(int rates_total, int start_index, ENUM_APPLIED_PRICE price_type, const double &open[], const double &high[], const double &low[], const double &close[])
{
for(int i = start_index; i < rates_total; i++)
{ {
ArrayResize(m_source_data, rates_total);
if(m_source_type == SOURCE_PRICE) if(m_source_type == SOURCE_PRICE)
{ {
// Use the selected price type for the calculation
switch(price_type) switch(price_type)
{ {
case PRICE_CLOSE:
m_source_data[i] = close[i];
break;
case PRICE_OPEN: case PRICE_OPEN:
ArrayCopy(m_source_data, open, 0, 0, rates_total); m_source_data[i] = open[i];
break; break;
case PRICE_HIGH: case PRICE_HIGH:
ArrayCopy(m_source_data, high, 0, 0, rates_total); m_source_data[i] = high[i];
break; break;
case PRICE_LOW: case PRICE_LOW:
ArrayCopy(m_source_data, low, 0, 0, rates_total); m_source_data[i] = low[i];
break; break;
case PRICE_MEDIAN: case PRICE_MEDIAN:
for(int i=0; i<rates_total; i++)
m_source_data[i] = (high[i]+low[i])/2.0; m_source_data[i] = (high[i]+low[i])/2.0;
break; break;
case PRICE_TYPICAL: case PRICE_TYPICAL:
for(int i=0; i<rates_total; i++)
m_source_data[i] = (high[i]+low[i]+close[i])/3.0; m_source_data[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_source_data[i] = (high[i]+low[i]+2*close[i])/4.0; m_source_data[i] = (high[i]+low[i]+2*close[i])/4.0;
break; break;
default: default:
ArrayCopy(m_source_data, close, 0, 0, rates_total); m_source_data[i] = close[i];
break; break;
} }
} }
else // SOURCE_MOMENTUM else // SOURCE_MOMENTUM
{ {
for(int i=0; i<rates_total; i++)
m_source_data[i] = close[i] - open[i]; m_source_data[i] = close[i] - open[i];
} }
}
return true; return true;
} }
//+==================================================================+
//| CLASS 2: CWindowedMACalculator_HA (Heikin Ashi) |
//+==================================================================+ //+==================================================================+
class CWindowedMACalculator_HA : public CWindowedMACalculator class CWindowedMACalculator_HA : public CWindowedMACalculator
{ {
private: private:
CHeikinAshi_Calculator m_ha_calculator; CHeikinAshi_Calculator m_ha_calculator;
// Internal HA buffers
double m_ha_open[], m_ha_high[], m_ha_low[], m_ha_close[];
protected: protected:
// CORRECTED: Function name typo fixed virtual bool PrepareSourceData(int rates_total, int start_index, ENUM_APPLIED_PRICE price_type, const double &open[], const double &high[], const double &low[], const double &close[]) override;
virtual bool PrepareSourceData(int rates_total, ENUM_APPLIED_PRICE price_type, const double &open[], const double &high[], const double &low[], const double &close[]) override;
}; };
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
// CORRECTED: Function name typo fixed //| Prepare Source Data (Heikin Ashi - Optimized) |
bool CWindowedMACalculator_HA::PrepareSourceData(int rates_total, ENUM_APPLIED_PRICE price_type, const double &open[], const double &high[], const double &low[], const double &close[]) //+------------------------------------------------------------------+
bool CWindowedMACalculator_HA::PrepareSourceData(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[]; if(ArraySize(m_ha_open) != rates_total)
ArrayResize(ha_open, rates_total); {
ArrayResize(ha_high, rates_total); ArrayResize(m_ha_open, rates_total);
ArrayResize(ha_low, rates_total); ArrayResize(m_ha_high, rates_total);
ArrayResize(ha_close, rates_total); ArrayResize(m_ha_low, rates_total);
m_ha_calculator.Calculate(rates_total, open, high, low, close, ha_open, ha_high, ha_low, ha_close); ArrayResize(m_ha_close, rates_total);
}
ArrayResize(m_source_data, rates_total); m_ha_calculator.Calculate(rates_total, start_index, open, high, low, close,
m_ha_open, m_ha_high, m_ha_low, m_ha_close);
for(int i = start_index; i < rates_total; i++)
{
if(m_source_type == SOURCE_PRICE) if(m_source_type == SOURCE_PRICE)
{ {
switch(price_type) switch(price_type)
{ {
case PRICE_CLOSE:
m_source_data[i] = m_ha_close[i];
break;
case PRICE_OPEN: case PRICE_OPEN:
ArrayCopy(m_source_data, ha_open, 0, 0, rates_total); m_source_data[i] = m_ha_open[i];
break; break;
case PRICE_HIGH: case PRICE_HIGH:
ArrayCopy(m_source_data, ha_high, 0, 0, rates_total); m_source_data[i] = m_ha_high[i];
break; break;
case PRICE_LOW: case PRICE_LOW:
ArrayCopy(m_source_data, ha_low, 0, 0, rates_total); m_source_data[i] = m_ha_low[i];
break; break;
case PRICE_MEDIAN: case PRICE_MEDIAN:
for(int i=0; i<rates_total; i++) m_source_data[i] = (m_ha_high[i]+m_ha_low[i])/2.0;
m_source_data[i] = (ha_high[i]+ha_low[i])/2.0;
break; break;
case PRICE_TYPICAL: case PRICE_TYPICAL:
for(int i=0; i<rates_total; i++) m_source_data[i] = (m_ha_high[i]+m_ha_low[i]+m_ha_close[i])/3.0;
m_source_data[i] = (ha_high[i]+ha_low[i]+ha_close[i])/3.0;
break; break;
case PRICE_WEIGHTED: case PRICE_WEIGHTED:
for(int i=0; i<rates_total; i++) m_source_data[i] = (m_ha_high[i]+m_ha_low[i]+2*m_ha_close[i])/4.0;
m_source_data[i] = (ha_high[i]+ha_low[i]+2*ha_close[i])/4.0;
break; break;
default: default:
ArrayCopy(m_source_data, ha_close, 0, 0, rates_total); m_source_data[i] = m_ha_close[i];
break; break;
} }
} }
else // SOURCE_MOMENTUM else // SOURCE_MOMENTUM
{ {
for(int i=0; i<rates_total; i++) m_source_data[i] = m_ha_close[i] - m_ha_open[i];
m_source_data[i] = ha_close[i] - ha_open[i]; }
} }
return true; return true;
} }
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
//+------------------------------------------------------------------+