refactor(indicators): Optimized for incremental calculation

This commit is contained in:
Toh4iem9
2025-12-31 17:28:25 +01:00
parent db5c86b57e
commit 3f50b02443
+46 -45
View File
@@ -1,6 +1,7 @@
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
//| AD_Calculator.mqh | //| AD_Calculator.mqh |
//| Calculation engine for Standard and Heikin Ashi A/D. | //| Calculation engine for Standard and Heikin Ashi A/D. |
//| VERSION 2.00: Optimized for incremental calculation. |
//| Copyright 2025, xxxxxxxx | //| Copyright 2025, xxxxxxxx |
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
#property copyright "Copyright 2025, xxxxxxxx" #property copyright "Copyright 2025, xxxxxxxx"
@@ -8,47 +9,58 @@
#include <MyIncludes\HeikinAshi_Tools.mqh> #include <MyIncludes\HeikinAshi_Tools.mqh>
//+==================================================================+ //+==================================================================+
//| |
//| CLASS 1: CADCalculator (Base Class) | //| CLASS 1: CADCalculator (Base Class) |
//| |
//+==================================================================+ //+==================================================================+
class CADCalculator class CADCalculator
{ {
protected: protected:
//--- Internal buffers for the selected candle data //--- Persistent Buffers for Incremental Calculation
double m_high[]; double m_high[];
double m_low[]; double m_low[];
double m_close[]; double m_close[];
//--- Virtual method for preparing the candle data. Base class handles standard candles. //--- Updated: Accepts start_index
//--- CORRECTED: Added 'open' array to the signature for consistency with derived class. virtual bool PrepareCandleData(int rates_total, int start_index, const double &open[], const double &high[], const double &low[], const double &close[]);
virtual bool PrepareCandleData(int rates_total, const double &open[], const double &high[], const double &low[], const double &close[]);
public: public:
CADCalculator(void) {}; CADCalculator(void) {};
virtual ~CADCalculator(void) {}; virtual ~CADCalculator(void) {};
//--- Public calculation method //--- Updated: Accepts prev_calculated
//--- CORRECTED: Added 'open' array to the signature. void Calculate(int rates_total, int prev_calculated, const double &open[], const double &high[], const double &low[], const double &close[],
void Calculate(int rates_total, const double &open[], const double &high[], const double &low[], const double &close[],
const long &tick_volume[], const long &volume[], ENUM_APPLIED_VOLUME volume_type, double &ad_buffer[]); const long &tick_volume[], const long &volume[], ENUM_APPLIED_VOLUME volume_type, double &ad_buffer[]);
}; };
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
//| CADCalculator: Main Calculation Method (Shared Logic) | //| Main Calculation (Optimized) |
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
void CADCalculator::Calculate(int rates_total, const double &open[], const double &high[], const double &low[], const double &close[], void CADCalculator::Calculate(int rates_total, int prev_calculated, const double &open[], const double &high[], const double &low[], const double &close[],
const long &tick_volume[], const long &volume[], ENUM_APPLIED_VOLUME volume_type, double &ad_buffer[]) const long &tick_volume[], const long &volume[], ENUM_APPLIED_VOLUME volume_type, double &ad_buffer[])
{ {
if(rates_total < 1) if(rates_total < 1)
return; return;
//--- STEP 1: Prepare the source candle arrays (delegated to virtual method) //--- 1. Determine Start Index
if(!PrepareCandleData(rates_total, open, high, low, close)) int start_index;
if(prev_calculated == 0)
start_index = 0;
else
start_index = prev_calculated - 1;
//--- 2. Resize Internal Buffers
if(ArraySize(m_high) != rates_total)
{
ArrayResize(m_high, rates_total);
ArrayResize(m_low, rates_total);
ArrayResize(m_close, rates_total);
}
//--- 3. Prepare Candle Data (Optimized)
if(!PrepareCandleData(rates_total, start_index, open, high, low, close))
return; return;
//--- STEP 2: Core A/D calculation using the prepared m_high[], m_low[], m_close[] arrays //--- 4. Calculate A/D (Incremental Loop)
for(int i = 0; i < rates_total; i++) for(int i = start_index; i < rates_total; i++)
{ {
double mfm = 0; // Money Flow Multiplier double mfm = 0; // Money Flow Multiplier
double range = m_high[i] - m_low[i]; double range = m_high[i] - m_low[i];
@@ -69,55 +81,44 @@ void CADCalculator::Calculate(int rates_total, const double &open[], const doubl
} }
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
//| CADCalculator: Prepares the standard candle data series. | //| Prepare Candle Data (Standard - Optimized) |
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
bool CADCalculator::PrepareCandleData(int rates_total, const double &open[], const double &high[], const double &low[], const double &close[]) bool CADCalculator::PrepareCandleData(int rates_total, int start_index, const double &open[], const double &high[], const double &low[], const double &close[])
{ {
//--- CORRECTED: Use ArrayCopy for robust data handling instead of invalid pointer assignment. for(int i = start_index; i < rates_total; i++)
ArrayResize(m_high, rates_total); {
ArrayResize(m_low, rates_total); m_high[i] = high[i];
ArrayResize(m_close, rates_total); m_low[i] = low[i];
m_close[i] = close[i];
ArrayCopy(m_high, high, 0, 0, rates_total); }
ArrayCopy(m_low, low, 0, 0, rates_total);
ArrayCopy(m_close, close, 0, 0, rates_total);
return true; return true;
} }
//+==================================================================+ //+==================================================================+
//| |
//| CLASS 2: CADCalculator_HA (Heikin Ashi) | //| CLASS 2: CADCalculator_HA (Heikin Ashi) |
//| |
//+==================================================================+ //+==================================================================+
class CADCalculator_HA : public CADCalculator class CADCalculator_HA : public CADCalculator
{ {
private: private:
CHeikinAshi_Calculator m_ha_calculator; // Instance of the HA calculator tool CHeikinAshi_Calculator m_ha_calculator;
// Internal HA buffers
double m_ha_open[]; // Temp buffer for HA Open
protected: protected:
//--- Overridden method to prepare Heikin Ashi candle data virtual bool PrepareCandleData(int rates_total, int start_index, const double &open[], const double &high[], const double &low[], const double &close[]) override;
//--- CORRECTED: Signature now matches the base class, including 'open'.
virtual bool PrepareCandleData(int rates_total, const double &open[], const double &high[], const double &low[], const double &close[]) override;
}; };
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
//| CADCalculator_HA: Prepares the Heikin Ashi candle data series. | //| Prepare Candle Data (Heikin Ashi - Optimized) |
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
bool CADCalculator_HA::PrepareCandleData(int rates_total, const double &open[], const double &high[], const double &low[], const double &close[]) bool CADCalculator_HA::PrepareCandleData(int rates_total, int start_index, const double &open[], const double &high[], const double &low[], const double &close[])
{ {
//--- For the HA calculator, we must calculate and store the HA values. if(ArraySize(m_ha_open) != rates_total)
ArrayResize(m_high, rates_total); ArrayResize(m_ha_open, rates_total);
ArrayResize(m_low, rates_total);
ArrayResize(m_close, rates_total);
//--- We need a temporary ha_open buffer for the calculation //--- STRICT CALL: Use the optimized 10-param HA calculation
double ha_open[]; m_ha_calculator.Calculate(rates_total, start_index, open, high, low, close,
ArrayResize(ha_open, rates_total); m_ha_open, m_high, m_low, m_close);
//--- Calculate the HA candles into our member arrays
//--- CORRECTED: Removed invalid GetPointer() calls.
m_ha_calculator.Calculate(rates_total, open, high, low, close, ha_open, m_high, m_low, m_close);
return true; return true;
} }