refactor(indicators): Optimized for incremental calculation

This commit is contained in:
Toh4iem9
2026-01-02 23:37:33 +01:00
parent 2cdf2f0af2
commit dfa1e345c9
+152 -73
View File
@@ -1,64 +1,59 @@
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
//| DPO_Calculator.mqh | //| DPO_Calculator.mqh |
//| Engine for calculating the Detrended Price Oscillator. | //| Engine for calculating the Detrended Price Oscillator. |
//| 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\MovingAverage_Engine.mqh> #include <MyIncludes\MovingAverage_Engine.mqh>
//+==================================================================+
//| CLASS 1: CDPOCalculator (Base Class) |
//+==================================================================+ //+==================================================================+
class CDPOCalculator class CDPOCalculator
{ {
protected: protected:
int m_period; int m_period;
CMovingAverageCalculator *m_ma_calc; CMovingAverageCalculator *m_ma_calc;
double m_price[]; // Internal price buffer
virtual bool PreparePriceSeries(int rates_total, ENUM_APPLIED_PRICE price_type, const double &open[], const double &high[], const double &low[], const double &close[]); //--- Persistent Buffers for Incremental Calculation
double m_price[];
double m_ma_buffer[];
//--- 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[]);
public: public:
CDPOCalculator(void); CDPOCalculator(void);
virtual ~CDPOCalculator(void); virtual ~CDPOCalculator(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[],
//--- 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 &dpo_buffer[]); double &dpo_buffer[]);
}; };
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
//| | //| Constructor |
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
class CDPOCalculator_HA : public CDPOCalculator CDPOCalculator::CDPOCalculator(void)
{ {
public: m_ma_calc = new CMovingAverageCalculator();
CDPOCalculator_HA(void);
};
//+==================================================================+
//| METHOD IMPLEMENTATIONS |
//+==================================================================+
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
CDPOCalculator::CDPOCalculator(void) { m_ma_calc = new CMovingAverageCalculator(); }
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
CDPOCalculator::~CDPOCalculator(void) { if(CheckPointer(m_ma_calc) != POINTER_INVALID) delete m_ma_calc; }
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
CDPOCalculator_HA::CDPOCalculator_HA(void)
{
if(CheckPointer(m_ma_calc) != POINTER_INVALID)
delete m_ma_calc;
m_ma_calc = new CMovingAverageCalculator_HA();
} }
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
//| | //| Destructor |
//+------------------------------------------------------------------+
CDPOCalculator::~CDPOCalculator(void)
{
if(CheckPointer(m_ma_calc) != POINTER_INVALID)
delete m_ma_calc;
}
//+------------------------------------------------------------------+
//| Init |
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
bool CDPOCalculator::Init(int period, ENUM_MA_TYPE ma_type) bool CDPOCalculator::Init(int period, ENUM_MA_TYPE ma_type)
{ {
@@ -69,75 +64,159 @@ bool CDPOCalculator::Init(int period, ENUM_MA_TYPE ma_type)
} }
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
//| | //| Main Calculation (Optimized) |
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
void CDPOCalculator::Calculate(int rates_total, ENUM_APPLIED_PRICE price_type, const double &open[], const double &high[], const double &low[], const double &close[], void CDPOCalculator::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 &dpo_buffer[]) double &dpo_buffer[])
{ {
if(rates_total < m_period) if(rates_total < m_period)
return; return;
if(CheckPointer(m_ma_calc) == POINTER_INVALID) if(CheckPointer(m_ma_calc) == POINTER_INVALID)
return; return;
if(!PreparePriceSeries(rates_total, price_type, open, high, low, close))
int start_index;
if(prev_calculated == 0)
start_index = 0;
else
start_index = prev_calculated - 1;
// Resize Buffers
if(ArraySize(m_price) != rates_total)
{
ArrayResize(m_price, rates_total);
ArrayResize(m_ma_buffer, rates_total);
}
if(!PreparePriceSeries(rates_total, start_index, price_type, open, high, low, close))
return; return;
//--- Step 1: Calculate the standard, lagging MA into an internal buffer --- //--- Step 1: Calculate the standard, lagging MA into an internal buffer (Incremental)
double ma_buffer[]; m_ma_calc.Calculate(rates_total, prev_calculated, price_type, open, high, low, close, m_ma_buffer);
ArrayResize(ma_buffer, rates_total);
m_ma_calc.Calculate(rates_total, price_type, open, high, low, close, ma_buffer);
//--- Step 2: Calculate DPO by subtracting the shifted MA from the price --- //--- Step 2: Calculate DPO (Incremental Loop)
int shift = (m_period / 2) + 1; int shift = (m_period / 2) + 1;
int loop_start = MathMax(shift, start_index);
for(int i = 0; i < rates_total; i++) for(int i = loop_start; i < rates_total; i++)
{ {
int source_index = i - shift; int source_index = i - shift;
if(source_index >= 0 && ma_buffer[source_index] != EMPTY_VALUE) // Ensure we don't access out of bounds or empty values
dpo_buffer[i] = m_price[i] - ma_buffer[source_index]; if(source_index >= 0 && m_ma_buffer[source_index] != EMPTY_VALUE && m_ma_buffer[source_index] != 0.0)
dpo_buffer[i] = m_price[i] - m_ma_buffer[source_index];
else else
dpo_buffer[i] = EMPTY_VALUE; dpo_buffer[i] = EMPTY_VALUE;
} }
} }
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
//| | //| Prepare Price (Standard - Optimized) |
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
bool CDPOCalculator::PreparePriceSeries(int rates_total, ENUM_APPLIED_PRICE price_type, const double &open[], const double &high[], const double &low[], const double &close[]) bool CDPOCalculator::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) for(int i = start_index; i < rates_total; i++)
if(ArrayResize(m_price, rates_total) != rates_total)
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]+2*close[i])/4.0;
m_price[i] = (high[i]+low[i]+close[i]+close[i])/4.0; break;
break; default:
default: m_price[i] = close[i];
return false; break;
}
}
return true;
}
//+==================================================================+
//| CLASS 2: CDPOCalculator_HA (Heikin Ashi) |
//+==================================================================+
class CDPOCalculator_HA : public CDPOCalculator
{
private:
CHeikinAshi_Calculator m_ha_calculator;
double m_ha_open[], m_ha_high[], m_ha_low[], m_ha_close[];
public:
CDPOCalculator_HA(void);
protected:
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;
};
//+------------------------------------------------------------------+
//| Constructor (HA) |
//+------------------------------------------------------------------+
CDPOCalculator_HA::CDPOCalculator_HA(void)
{
if(CheckPointer(m_ma_calc) != POINTER_INVALID)
delete m_ma_calc;
// CRITICAL: Use HA Engine to calculate MA on HA prices
m_ma_calc = new CMovingAverageCalculator_HA();
}
//+------------------------------------------------------------------+
//| Prepare Price (Heikin Ashi - Optimized) |
//+------------------------------------------------------------------+
bool CDPOCalculator_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[])
{
if(ArraySize(m_ha_open) != rates_total)
{
ArrayResize(m_ha_open, rates_total);
ArrayResize(m_ha_high, rates_total);
ArrayResize(m_ha_low, rates_total);
ArrayResize(m_ha_close, 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);
// Fill m_price with the selected HA price type
for(int i = start_index; i < rates_total; i++)
{
switch(price_type)
{
case PRICE_CLOSE:
m_price[i] = m_ha_close[i];
break;
case PRICE_OPEN:
m_price[i] = m_ha_open[i];
break;
case PRICE_HIGH:
m_price[i] = m_ha_high[i];
break;
case PRICE_LOW:
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]+2*m_ha_close[i])/4.0;
break;
default:
m_price[i] = m_ha_close[i];
break;
}
} }
return true; return true;
} }
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
//+------------------------------------------------------------------+