mirror of
https://github.com/softwaredevelop/mql5.git
synced 2026-08-26 02:38:05 +00:00
refactor(indicators): Optimized for incremental calculation
This commit is contained in:
@@ -1,63 +1,66 @@
|
|||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
//| CHO_Calculator.mqh |
|
//| CHO_Calculator.mqh |
|
||||||
//| Calculation engine for Standard and Heikin Ashi CHO. |
|
//| Calculation engine for Standard and Heikin Ashi CHO. |
|
||||||
|
//| VERSION 3.00: Optimized for incremental calculation. |
|
||||||
//| Copyright 2025, xxxxxxxx |
|
//| Copyright 2025, xxxxxxxx |
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
#property copyright "Copyright 2025, xxxxxxxx"
|
#property copyright "Copyright 2025, xxxxxxxx"
|
||||||
|
|
||||||
//--- Re-use the AD calculator we built previously ---
|
|
||||||
#include <MyIncludes\AD_Calculator.mqh>
|
#include <MyIncludes\AD_Calculator.mqh>
|
||||||
|
#include <MyIncludes\MovingAverage_Engine.mqh>
|
||||||
|
|
||||||
//+==================================================================+
|
//+==================================================================+
|
||||||
//| |
|
|
||||||
//| CLASS 1: CCHOCalculator (Base Class) |
|
//| CLASS 1: CCHOCalculator (Base Class) |
|
||||||
//| |
|
|
||||||
//+==================================================================+
|
//+==================================================================+
|
||||||
class CCHOCalculator
|
class CCHOCalculator
|
||||||
{
|
{
|
||||||
protected:
|
protected:
|
||||||
int m_fast_period;
|
int m_fast_period;
|
||||||
int m_slow_period;
|
int m_slow_period;
|
||||||
ENUM_MA_METHOD m_ma_method;
|
|
||||||
ENUM_APPLIED_VOLUME m_volume_type;
|
|
||||||
|
|
||||||
//--- It contains a pointer to a base AD calculator
|
//--- Composition: AD Calculator + 2 MA Engines
|
||||||
CADCalculator *m_ad_calculator;
|
CADCalculator *m_ad_calculator;
|
||||||
|
CMovingAverageCalculator m_fast_ma_engine;
|
||||||
|
CMovingAverageCalculator m_slow_ma_engine;
|
||||||
|
|
||||||
|
//--- Persistent Buffers for Incremental Calculation
|
||||||
|
double m_adl_buffer[];
|
||||||
|
double m_fast_ma_buffer[];
|
||||||
|
double m_slow_ma_buffer[];
|
||||||
|
|
||||||
public:
|
public:
|
||||||
CCHOCalculator(void);
|
CCHOCalculator(void);
|
||||||
virtual ~CCHOCalculator(void);
|
virtual ~CCHOCalculator(void);
|
||||||
|
|
||||||
//--- Public methods
|
//--- Init now takes ENUM_MA_TYPE
|
||||||
bool Init(int fast_p, int slow_p, ENUM_MA_METHOD ma_m, ENUM_APPLIED_VOLUME vol_t);
|
bool Init(int fast_p, int slow_p, ENUM_MA_TYPE ma_m, ENUM_APPLIED_VOLUME vol_t);
|
||||||
int GetSlowPeriod(void) const { return m_slow_period; }
|
int GetSlowPeriod(void) const { return m_slow_period; }
|
||||||
//--- CORRECTED: Added 'open' array to the signature
|
|
||||||
void Calculate(int rates_total, const double &open[], const double &high[], const double &low[], const double &close[], const long &tick_volume[], const long &volume[], double &cho_buffer[]);
|
//--- Updated: Accepts prev_calculated
|
||||||
|
void 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 &cho_buffer[]);
|
||||||
};
|
};
|
||||||
|
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
//| CCHOCalculator: Constructor |
|
//| Constructor |
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
CCHOCalculator::CCHOCalculator(void)
|
CCHOCalculator::CCHOCalculator(void)
|
||||||
{
|
{
|
||||||
//--- The base class creates a standard AD calculator instance
|
|
||||||
m_ad_calculator = new CADCalculator();
|
m_ad_calculator = new CADCalculator();
|
||||||
}
|
}
|
||||||
|
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
//| CCHOCalculator: Destructor |
|
//| Destructor |
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
CCHOCalculator::~CCHOCalculator(void)
|
CCHOCalculator::~CCHOCalculator(void)
|
||||||
{
|
{
|
||||||
//--- Clean up the contained object
|
|
||||||
if(CheckPointer(m_ad_calculator) != POINTER_INVALID)
|
if(CheckPointer(m_ad_calculator) != POINTER_INVALID)
|
||||||
delete m_ad_calculator;
|
delete m_ad_calculator;
|
||||||
}
|
}
|
||||||
|
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
//| CCHOCalculator: Initialization |
|
//| Init |
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
bool CCHOCalculator::Init(int fast_p, int slow_p, ENUM_MA_METHOD ma_m, ENUM_APPLIED_VOLUME vol_t)
|
bool CCHOCalculator::Init(int fast_p, int slow_p, ENUM_MA_TYPE ma_m, ENUM_APPLIED_VOLUME vol_t)
|
||||||
{
|
{
|
||||||
m_fast_period = (fast_p < 1) ? 1 : fast_p;
|
m_fast_period = (fast_p < 1) ? 1 : fast_p;
|
||||||
m_slow_period = (slow_p < 1) ? 1 : slow_p;
|
m_slow_period = (slow_p < 1) ? 1 : slow_p;
|
||||||
@@ -69,109 +72,60 @@ bool CCHOCalculator::Init(int fast_p, int slow_p, ENUM_MA_METHOD ma_m, ENUM_APPL
|
|||||||
m_slow_period = temp;
|
m_slow_period = temp;
|
||||||
}
|
}
|
||||||
|
|
||||||
m_ma_method = ma_m;
|
// Initialize MA Engines
|
||||||
m_volume_type = vol_t;
|
if(!m_fast_ma_engine.Init(m_fast_period, ma_m))
|
||||||
|
return false;
|
||||||
|
if(!m_slow_ma_engine.Init(m_slow_period, ma_m))
|
||||||
|
return false;
|
||||||
|
|
||||||
return (CheckPointer(m_ad_calculator) != POINTER_INVALID);
|
return (CheckPointer(m_ad_calculator) != POINTER_INVALID);
|
||||||
}
|
}
|
||||||
|
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
//| CCHOCalculator: Main Calculation Method |
|
//| Main Calculation (Optimized) |
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
void CCHOCalculator::Calculate(int rates_total, const double &open[], const double &high[], const double &low[], const double &close[], const long &tick_volume[], const long &volume[], double &cho_buffer[])
|
void CCHOCalculator::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 &cho_buffer[])
|
||||||
{
|
{
|
||||||
if(rates_total < m_slow_period || CheckPointer(m_ad_calculator) == POINTER_INVALID)
|
if(rates_total < m_slow_period || CheckPointer(m_ad_calculator) == POINTER_INVALID)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
//--- Internal buffers
|
// Resize internal buffers
|
||||||
double adl[], fast_ma[], slow_ma[];
|
if(ArraySize(m_adl_buffer) != rates_total)
|
||||||
ArrayResize(adl, rates_total);
|
|
||||||
ArrayResize(fast_ma, rates_total);
|
|
||||||
ArrayResize(slow_ma, rates_total);
|
|
||||||
|
|
||||||
//--- STEP 1: Calculate ADL using the contained calculator
|
|
||||||
//--- CORRECTED: Pass the 'open' array to the AD calculator
|
|
||||||
m_ad_calculator.Calculate(rates_total, open, high, low, close, tick_volume, volume, m_volume_type, adl);
|
|
||||||
|
|
||||||
//--- STEP 2: Calculate Fast MA on ADL
|
|
||||||
for(int i = m_fast_period - 1; i < rates_total; i++)
|
|
||||||
{
|
{
|
||||||
switch(m_ma_method)
|
ArrayResize(m_adl_buffer, rates_total);
|
||||||
{
|
ArrayResize(m_fast_ma_buffer, rates_total);
|
||||||
case MODE_EMA:
|
ArrayResize(m_slow_ma_buffer, rates_total);
|
||||||
case MODE_SMMA:
|
|
||||||
if(i == m_fast_period - 1)
|
|
||||||
{
|
|
||||||
double sum=0;
|
|
||||||
for(int j=0; j<m_fast_period; j++)
|
|
||||||
sum+=adl[i-j];
|
|
||||||
fast_ma[i] = sum/m_fast_period;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if(m_ma_method == MODE_EMA)
|
|
||||||
{
|
|
||||||
double pr=2.0/(m_fast_period+1.0);
|
|
||||||
fast_ma[i] = adl[i]*pr + fast_ma[i-1]*(1.0-pr);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
fast_ma[i] = (fast_ma[i-1]*(m_fast_period-1)+adl[i])/m_fast_period;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case MODE_LWMA:
|
|
||||||
{double lwma_sum=0, weight_sum=0; for(int j=0; j<m_fast_period; j++) {int weight=m_fast_period-j; lwma_sum+=adl[i-j]*weight; weight_sum+=weight;} if(weight_sum>0) fast_ma[i]=lwma_sum/weight_sum;}
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
{double sum=0; for(int j=0; j<m_fast_period; j++) sum+=adl[i-j]; fast_ma[i] = sum/m_fast_period;}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//--- STEP 3: Calculate Slow MA on ADL
|
//--- 1. Calculate ADL (Incremental)
|
||||||
for(int i = m_slow_period - 1; i < rates_total; i++)
|
// The AD calculator handles its own incremental logic
|
||||||
{
|
m_ad_calculator.Calculate(rates_total, prev_calculated, open, high, low, close, tick_volume, volume, volume_type, m_adl_buffer);
|
||||||
switch(m_ma_method)
|
|
||||||
{
|
|
||||||
case MODE_EMA:
|
|
||||||
case MODE_SMMA:
|
|
||||||
if(i == m_slow_period - 1)
|
|
||||||
{
|
|
||||||
double sum=0;
|
|
||||||
for(int j=0; j<m_slow_period; j++)
|
|
||||||
sum+=adl[i-j];
|
|
||||||
slow_ma[i] = sum/m_slow_period;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if(m_ma_method == MODE_EMA)
|
|
||||||
{
|
|
||||||
double pr=2.0/(m_slow_period+1.0);
|
|
||||||
slow_ma[i] = adl[i]*pr + slow_ma[i-1]*(1.0-pr);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
slow_ma[i] = (slow_ma[i-1]*(m_slow_period-1)+adl[i])/m_slow_period;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case MODE_LWMA:
|
|
||||||
{double lwma_sum=0, weight_sum=0; for(int j=0; j<m_slow_period; j++) {int weight=m_slow_period-j; lwma_sum+=adl[i-j]*weight; weight_sum+=weight;} if(weight_sum>0) slow_ma[i]=lwma_sum/weight_sum;}
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
{double sum=0; for(int j=0; j<m_slow_period; j++) sum+=adl[i-j]; slow_ma[i] = sum/m_slow_period;}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//--- STEP 4: Calculate final Chaikin Oscillator value
|
//--- 2. Calculate Fast MA on ADL (Using Engine)
|
||||||
for(int i = m_slow_period - 1; i < rates_total; i++)
|
// ADL is valid from index 0
|
||||||
|
m_fast_ma_engine.CalculateOnArray(rates_total, prev_calculated, m_adl_buffer, m_fast_ma_buffer, 0);
|
||||||
|
|
||||||
|
//--- 3. Calculate Slow MA on ADL (Using Engine)
|
||||||
|
m_slow_ma_engine.CalculateOnArray(rates_total, prev_calculated, m_adl_buffer, m_slow_ma_buffer, 0);
|
||||||
|
|
||||||
|
//--- 4. Calculate CHO (Fast - Slow)
|
||||||
|
int start_index = (prev_calculated > 0) ? prev_calculated - 1 : 0;
|
||||||
|
int loop_start = MathMax(m_slow_period - 1, start_index);
|
||||||
|
|
||||||
|
if(prev_calculated == 0)
|
||||||
|
ArrayInitialize(cho_buffer, EMPTY_VALUE);
|
||||||
|
|
||||||
|
for(int i = loop_start; i < rates_total; i++)
|
||||||
{
|
{
|
||||||
cho_buffer[i] = fast_ma[i] - slow_ma[i];
|
if(m_fast_ma_buffer[i] != EMPTY_VALUE && m_slow_ma_buffer[i] != EMPTY_VALUE)
|
||||||
|
cho_buffer[i] = m_fast_ma_buffer[i] - m_slow_ma_buffer[i];
|
||||||
|
else
|
||||||
|
cho_buffer[i] = EMPTY_VALUE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//+==================================================================+
|
//+==================================================================+
|
||||||
//| |
|
//| CLASS 2: CCHOCalculator_HA (Heikin Ashi) |
|
||||||
//| CLASS 2: CCHOCalculator_HA (Heikin Ashi) |
|
|
||||||
//| |
|
|
||||||
//+==================================================================+
|
//+==================================================================+
|
||||||
class CCHOCalculator_HA : public CCHOCalculator
|
class CCHOCalculator_HA : public CCHOCalculator
|
||||||
{
|
{
|
||||||
@@ -180,26 +134,20 @@ public:
|
|||||||
};
|
};
|
||||||
|
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
//| CCHOCalculator_HA: Constructor |
|
//| |
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
CCHOCalculator_HA::CCHOCalculator_HA(void)
|
CCHOCalculator_HA::CCHOCalculator_HA(void)
|
||||||
{
|
{
|
||||||
//--- Clean up the base class's standard calculator first
|
|
||||||
if(CheckPointer(m_ad_calculator) != POINTER_INVALID)
|
if(CheckPointer(m_ad_calculator) != POINTER_INVALID)
|
||||||
delete m_ad_calculator;
|
delete m_ad_calculator;
|
||||||
|
// Use HA version of AD calculator
|
||||||
//--- The derived class creates a Heikin Ashi AD calculator instance instead
|
|
||||||
m_ad_calculator = new CADCalculator_HA();
|
m_ad_calculator = new CADCalculator_HA();
|
||||||
}
|
}
|
||||||
|
|
||||||
//+==================================================================+
|
//+==================================================================+
|
||||||
//| |
|
//| CLASS 3: CCHOCalculator_Std (Standard) |
|
||||||
//| CLASS 3: CCHOCalculator_Std (Standard) |
|
|
||||||
//| |
|
|
||||||
//+==================================================================+
|
//+==================================================================+
|
||||||
// This is just for consistency in naming, it inherits everything from the base.
|
|
||||||
class CCHOCalculator_Std : public CCHOCalculator
|
class CCHOCalculator_Std : public CCHOCalculator
|
||||||
{
|
{
|
||||||
};
|
};
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
//+------------------------------------------------------------------+
|
|
||||||
|
|||||||
Reference in New Issue
Block a user