refactor: Upgraded with strict internal chronological sorting safeguards

This commit is contained in:
Toh4iem9
2026-07-01 12:41:27 +02:00
parent 312b1d322e
commit ffc3f61603
+26 -19
View File
@@ -1,9 +1,12 @@
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
//| Ehlers_Bands_Calculator.mqh | //| Ehlers_Bands_Calculator.mqh |
//| VERSION 1.20: Optimized for incremental calculation. | //| Copyright 2026, xxxxxxxx|
//| Copyright 2025, xxxxxxxx |
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
#property copyright "Copyright 2025, xxxxxxxx" #property copyright "Copyright 2026, xxxxxxxx"
#property version "1.30" // Upgraded with strict internal chronological sorting safeguards
#ifndef EHLERS_BANDS_CALCULATOR_MQH
#define EHLERS_BANDS_CALCULATOR_MQH
#include <MyIncludes\Ehlers_Smoother_Calculator.mqh> #include <MyIncludes\Ehlers_Smoother_Calculator.mqh>
@@ -18,7 +21,6 @@ protected:
//--- Persistent Buffer for Price //--- Persistent Buffer for Price
double m_price[]; double m_price[];
//--- 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[]); 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:
@@ -27,7 +29,6 @@ public:
bool Init(int period, double multiplier, ENUM_SMOOTHER_TYPE smoother_type); bool Init(int period, double multiplier, ENUM_SMOOTHER_TYPE smoother_type);
//--- 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[], 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 &upper_buffer[], double &lower_buffer[], double &middle_buffer[]); double &upper_buffer[], double &lower_buffer[], double &middle_buffer[]);
}; };
@@ -37,12 +38,14 @@ CEhlersBandsCalculator::CEhlersBandsCalculator(void)
{ {
m_calc_center = NULL; m_calc_center = NULL;
} }
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
CEhlersBandsCalculator::~CEhlersBandsCalculator(void) CEhlersBandsCalculator::~CEhlersBandsCalculator(void)
{ {
if(CheckPointer(m_calc_center) != POINTER_INVALID) if(CheckPointer(m_calc_center) != POINTER_INVALID)
delete m_calc_center; delete m_calc_center;
} }
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
bool CEhlersBandsCalculator::Init(int period, double multiplier, ENUM_SMOOTHER_TYPE smoother_type) bool CEhlersBandsCalculator::Init(int period, double multiplier, ENUM_SMOOTHER_TYPE smoother_type)
{ {
@@ -65,16 +68,18 @@ void CEhlersBandsCalculator::Calculate(int rates_total, int prev_calculated, ENU
if(rates_total < m_period) if(rates_total < m_period)
return; return;
//--- 1. Determine Start Index if(CheckPointer(m_calc_center) == POINTER_INVALID)
int start_index; return;
if(prev_calculated == 0)
start_index = 0;
else
start_index = prev_calculated - 1;
//--- 2. Resize Internal Buffer //--- 1. Determine Start Index
int start_index = (prev_calculated == 0) ? 0 : prev_calculated - 1;
//--- 2. Resize Internal Buffer and force chronological indexing
if(ArraySize(m_price) != rates_total) if(ArraySize(m_price) != rates_total)
{
ArrayResize(m_price, rates_total); ArrayResize(m_price, rates_total);
ArraySetAsSeries(m_price, false); // Fixed: strict chronological safety on internal buffers
}
//--- 3. Prepare Price (Optimized) //--- 3. Prepare Price (Optimized)
if(!PreparePriceSeries(rates_total, start_index, price_type, open, high, low, close)) if(!PreparePriceSeries(rates_total, start_index, price_type, open, high, low, close))
@@ -108,7 +113,6 @@ void CEhlersBandsCalculator::Calculate(int rates_total, int prev_calculated, ENU
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
bool CEhlersBandsCalculator::PreparePriceSeries(int rates_total, int start_index, ENUM_APPLIED_PRICE price_type, const double &open[], const double &high[], const double &low[], const double &close[]) bool CEhlersBandsCalculator::PreparePriceSeries(int rates_total, int start_index, ENUM_APPLIED_PRICE price_type, const double &open[], const double &high[], const double &low[], const double &close[])
{ {
// Optimized copy loop
for(int i = start_index; i < rates_total; i++) for(int i = start_index; i < rates_total; i++)
{ {
switch(price_type) switch(price_type)
@@ -132,7 +136,7 @@ bool CEhlersBandsCalculator::PreparePriceSeries(int rates_total, int start_index
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:
m_price[i] = (high[i]+low[i]+2*close[i])/4.0; m_price[i] = (high[i]+low[i]+2.0*close[i])/4.0;
break; break;
default: default:
m_price[i] = close[i]; m_price[i] = close[i];
@@ -147,7 +151,6 @@ class CEhlersBandsCalculator_HA : public CEhlersBandsCalculator
{ {
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[]; double m_ha_open[], m_ha_high[], m_ha_low[], m_ha_close[];
public: public:
@@ -164,20 +167,23 @@ protected:
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
bool CEhlersBandsCalculator_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[]) bool CEhlersBandsCalculator_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[])
{ {
// Resize internal HA buffers // Resize internal HA buffers and force chronological indexing
if(ArraySize(m_ha_open) != rates_total) if(ArraySize(m_ha_open) != rates_total)
{ {
ArrayResize(m_ha_open, rates_total); ArrayResize(m_ha_open, rates_total);
ArrayResize(m_ha_high, rates_total); ArrayResize(m_ha_high, rates_total);
ArrayResize(m_ha_low, rates_total); ArrayResize(m_ha_low, rates_total);
ArrayResize(m_ha_close, rates_total); ArrayResize(m_ha_close, rates_total);
ArraySetAsSeries(m_ha_open, false);
ArraySetAsSeries(m_ha_high, false);
ArraySetAsSeries(m_ha_low, false);
ArraySetAsSeries(m_ha_close, false);
} }
//--- STRICT CALL: Use the optimized 10-param HA calculation
m_ha_calculator.Calculate(rates_total, start_index, open, high, low, close, m_ha_calculator.Calculate(rates_total, start_index, open, high, low, close,
m_ha_open, m_ha_high, m_ha_low, m_ha_close); m_ha_open, m_ha_high, m_ha_low, m_ha_close);
//--- Copy to m_price (Optimized loop)
for(int i = start_index; i < rates_total; i++) for(int i = start_index; i < rates_total; i++)
{ {
switch(price_type) switch(price_type)
@@ -201,7 +207,7 @@ bool CEhlersBandsCalculator_HA::PreparePriceSeries(int rates_total, int start_in
m_price[i] = (m_ha_high[i]+m_ha_low[i]+m_ha_close[i])/3.0; m_price[i] = (m_ha_high[i]+m_ha_low[i]+m_ha_close[i])/3.0;
break; break;
case PRICE_WEIGHTED: case PRICE_WEIGHTED:
m_price[i] = (m_ha_high[i]+m_ha_low[i]+2*m_ha_close[i])/4.0; m_price[i] = (m_ha_high[i]+m_ha_low[i]+2.0*m_ha_close[i])/4.0;
break; break;
default: default:
m_price[i] = m_ha_close[i]; m_price[i] = m_ha_close[i];
@@ -210,4 +216,5 @@ bool CEhlersBandsCalculator_HA::PreparePriceSeries(int rates_total, int start_in
} }
return true; return true;
} }
#endif // EHLERS_BANDS_CALCULATOR_MQH
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+