mirror of
https://github.com/softwaredevelop/mql5.git
synced 2026-08-25 18:28:04 +00:00
refactor(indicators): Optimized for incremental calculation
This commit is contained in:
@@ -1,132 +1,151 @@
|
|||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
//| CCI_Calculator.mqh |
|
//| CCI_Calculator.mqh |
|
||||||
//| Calculation engine for CCI Pro (CCI, Signal Line, BBands). |
|
//| 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>
|
||||||
|
#include <MyIncludes\MovingAverage_Engine.mqh>
|
||||||
|
|
||||||
//+==================================================================+
|
//+==================================================================+
|
||||||
//| |
|
|
||||||
//| CLASS 1: CCCI_Calculator (Base Class) |
|
//| CLASS 1: CCCI_Calculator (Base Class) |
|
||||||
//| |
|
|
||||||
//+==================================================================+
|
//+==================================================================+
|
||||||
class CCCI_Calculator
|
class CCCI_Calculator
|
||||||
{
|
{
|
||||||
protected:
|
protected:
|
||||||
int m_cci_period, m_ma_period, m_bands_period;
|
int m_cci_period, m_bands_period;
|
||||||
ENUM_MA_METHOD m_ma_method;
|
|
||||||
double m_bands_dev;
|
double m_bands_dev;
|
||||||
double m_price[];
|
|
||||||
|
|
||||||
virtual bool PreparePriceSeries(int rates_total, const double &open[], const double &high[], const double &low[], const double &close[], ENUM_APPLIED_PRICE price_type);
|
//--- Engine for Signal Line
|
||||||
|
CMovingAverageCalculator m_signal_engine;
|
||||||
|
|
||||||
|
//--- Persistent Buffers
|
||||||
|
double m_price[];
|
||||||
|
double m_sma_buffer[]; // Simple Moving Average of Price
|
||||||
|
double m_mad_buffer[]; // Mean Absolute Deviation
|
||||||
|
|
||||||
|
//--- 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:
|
||||||
CCCI_Calculator(void) {};
|
CCCI_Calculator(void) {};
|
||||||
virtual ~CCCI_Calculator(void) {};
|
virtual ~CCCI_Calculator(void) {};
|
||||||
|
|
||||||
bool Init(int cci_p, int ma_p, ENUM_MA_METHOD ma_m, int bands_p, double bands_dev);
|
//--- Init now takes ENUM_MA_TYPE
|
||||||
void Calculate(int rates_total, const double &open[], const double &high[], const double &low[], const double &close[], ENUM_APPLIED_PRICE price_type,
|
bool Init(int cci_p, int ma_p, ENUM_MA_TYPE ma_m, int bands_p, double bands_dev);
|
||||||
|
|
||||||
|
//--- 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 &cci_out[], double &signal_out[], double &upper_out[], double &lower_out[]);
|
double &cci_out[], double &signal_out[], double &upper_out[], double &lower_out[]);
|
||||||
};
|
};
|
||||||
|
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
//| CCCI_Calculator: Initialization |
|
//| Init |
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
bool CCCI_Calculator::Init(int cci_p, int ma_p, ENUM_MA_METHOD ma_m, int bands_p, double bands_dev)
|
bool CCCI_Calculator::Init(int cci_p, int ma_p, ENUM_MA_TYPE ma_m, int bands_p, double bands_dev)
|
||||||
{
|
{
|
||||||
m_cci_period = (cci_p < 1) ? 1 : cci_p;
|
m_cci_period = (cci_p < 1) ? 1 : cci_p;
|
||||||
m_ma_period = (ma_p < 1) ? 1 : ma_p;
|
|
||||||
m_ma_method = ma_m;
|
|
||||||
m_bands_period = (bands_p < 1) ? 1 : bands_p;
|
m_bands_period = (bands_p < 1) ? 1 : bands_p;
|
||||||
m_bands_dev = (bands_dev <= 0) ? 2.0 : bands_dev;
|
m_bands_dev = (bands_dev <= 0) ? 2.0 : bands_dev;
|
||||||
|
|
||||||
|
// Initialize Signal Engine
|
||||||
|
if(!m_signal_engine.Init(ma_p, ma_m))
|
||||||
|
return false;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
//| CCCI_Calculator: Main Calculation Method (Shared Logic) |
|
//| Main Calculation (Optimized) |
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
void CCCI_Calculator::Calculate(int rates_total, const double &open[], const double &high[], const double &low[], const double &close[], ENUM_APPLIED_PRICE price_type,
|
void CCCI_Calculator::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 &cci_out[], double &signal_out[], double &upper_out[], double &lower_out[])
|
double &cci_out[], double &signal_out[], double &upper_out[], double &lower_out[])
|
||||||
{
|
{
|
||||||
|
// Minimum bars check
|
||||||
if(rates_total <= m_cci_period + m_bands_period)
|
if(rates_total <= m_cci_period + m_bands_period)
|
||||||
return;
|
return;
|
||||||
if(!PreparePriceSeries(rates_total, open, high, low, close, price_type))
|
|
||||||
|
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_sma_buffer, rates_total);
|
||||||
|
ArrayResize(m_mad_buffer, rates_total);
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!PreparePriceSeries(rates_total, start_index, price_type, open, high, low, close))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
double buffer_sma[], buffer_mad[];
|
|
||||||
ArrayResize(buffer_sma, rates_total);
|
|
||||||
ArrayResize(buffer_mad, rates_total);
|
|
||||||
const double CCI_CONSTANT = 0.015;
|
const double CCI_CONSTANT = 0.015;
|
||||||
|
|
||||||
//--- Calculate CCI ---
|
//--- 1. Calculate SMA of Price (Incremental)
|
||||||
double sma_sum = 0;
|
// We can use a sliding window sum for O(1) SMA calculation, but standard loop is safer for now.
|
||||||
for(int i = 0; i < rates_total; i++)
|
// Optimization: Only calculate for new bars.
|
||||||
|
int loop_start_sma = MathMax(m_cci_period - 1, start_index);
|
||||||
|
|
||||||
|
for(int i = loop_start_sma; i < rates_total; i++)
|
||||||
{
|
{
|
||||||
sma_sum += m_price[i];
|
double sum = 0;
|
||||||
if(i >= m_cci_period)
|
for(int j = 0; j < m_cci_period; j++)
|
||||||
sma_sum -= m_price[i - m_cci_period];
|
sum += m_price[i-j];
|
||||||
if(i >= m_cci_period - 1)
|
m_sma_buffer[i] = sum / m_cci_period;
|
||||||
buffer_sma[i] = sma_sum / m_cci_period;
|
|
||||||
}
|
}
|
||||||
for(int i = m_cci_period - 1; i < rates_total; i++)
|
|
||||||
|
//--- 2. Calculate Mean Absolute Deviation (MAD)
|
||||||
|
for(int i = loop_start_sma; i < rates_total; i++)
|
||||||
{
|
{
|
||||||
double deviation_sum = 0;
|
double deviation_sum = 0;
|
||||||
for(int j = 0; j < m_cci_period; j++)
|
for(int j = 0; j < m_cci_period; j++)
|
||||||
deviation_sum += MathAbs(m_price[i - j] - buffer_sma[i]);
|
deviation_sum += MathAbs(m_price[i - j] - m_sma_buffer[i]);
|
||||||
buffer_mad[i] = deviation_sum / m_cci_period;
|
m_mad_buffer[i] = deviation_sum / m_cci_period;
|
||||||
}
|
|
||||||
for(int i = m_cci_period - 1; i < rates_total; i++)
|
|
||||||
{
|
|
||||||
if(buffer_mad[i] > 0)
|
|
||||||
cci_out[i] = (m_price[i] - buffer_sma[i]) / (CCI_CONSTANT * buffer_mad[i]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//--- Calculate Signal Line (MA of CCI) ---
|
//--- 3. Calculate CCI
|
||||||
int ma_start_pos = m_cci_period + m_ma_period - 2;
|
if(prev_calculated == 0)
|
||||||
for(int i = ma_start_pos; i < rates_total; i++)
|
ArrayInitialize(cci_out, EMPTY_VALUE);
|
||||||
|
|
||||||
|
for(int i = loop_start_sma; i < rates_total; i++)
|
||||||
{
|
{
|
||||||
switch(m_ma_method)
|
if(m_mad_buffer[i] > 0)
|
||||||
{
|
cci_out[i] = (m_price[i] - m_sma_buffer[i]) / (CCI_CONSTANT * m_mad_buffer[i]);
|
||||||
case MODE_EMA:
|
else
|
||||||
case MODE_SMMA:
|
cci_out[i] = 0;
|
||||||
if(i == ma_start_pos)
|
|
||||||
{
|
|
||||||
double sum=0;
|
|
||||||
for(int j=0; j<m_ma_period; j++)
|
|
||||||
sum+=cci_out[i-j];
|
|
||||||
signal_out[i]=sum/m_ma_period;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if(m_ma_method==MODE_EMA)
|
|
||||||
{
|
|
||||||
double pr=2.0/(m_ma_period+1.0);
|
|
||||||
signal_out[i]=cci_out[i]*pr+signal_out[i-1]*(1.0-pr);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
signal_out[i]=(signal_out[i-1]*(m_ma_period-1)+cci_out[i])/m_ma_period;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case MODE_LWMA:
|
|
||||||
{double sum=0,w_sum=0; for(int j=0; j<m_ma_period; j++) {int w=m_ma_period-j; sum+=cci_out[i-j]*w; w_sum+=w;} if(w_sum>0) signal_out[i]=sum/w_sum;}
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
{double sum=0; for(int j=0; j<m_ma_period; j++) sum+=cci_out[i-j]; signal_out[i]=sum/m_ma_period;}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//--- Calculate Bollinger Bands (on CCI, centered on Signal Line) ---
|
//--- 4. Calculate Signal Line (Using Engine)
|
||||||
int bands_start_pos = m_cci_period + m_bands_period - 2;
|
// CCI is valid from index: m_cci_period - 1
|
||||||
for(int i = bands_start_pos; i < rates_total; i++)
|
int cci_offset = m_cci_period - 1;
|
||||||
|
m_signal_engine.CalculateOnArray(rates_total, prev_calculated, cci_out, signal_out, cci_offset);
|
||||||
|
|
||||||
|
//--- 5. Calculate Bollinger Bands (Optimized)
|
||||||
|
// Bands are based on CCI and centered on Signal Line
|
||||||
|
int ma_period = m_signal_engine.GetPeriod();
|
||||||
|
int bands_start_pos = cci_offset + ma_period - 1; // Where signal line starts
|
||||||
|
int loop_start_bands = MathMax(bands_start_pos, start_index);
|
||||||
|
|
||||||
|
if(prev_calculated == 0)
|
||||||
|
{
|
||||||
|
ArrayInitialize(upper_out, EMPTY_VALUE);
|
||||||
|
ArrayInitialize(lower_out, EMPTY_VALUE);
|
||||||
|
}
|
||||||
|
|
||||||
|
for(int i = loop_start_bands; i < rates_total; i++)
|
||||||
{
|
{
|
||||||
if(signal_out[i] == EMPTY_VALUE)
|
if(signal_out[i] == EMPTY_VALUE)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
double std_dev = 0, sum_sq = 0;
|
double std_dev = 0, sum_sq = 0;
|
||||||
|
// Standard Deviation of CCI around the Signal Line
|
||||||
for(int j = 0; j < m_bands_period; j++)
|
for(int j = 0; j < m_bands_period; j++)
|
||||||
sum_sq += MathPow(cci_out[i-j] - signal_out[i], 2);
|
sum_sq += MathPow(cci_out[i-j] - signal_out[i], 2);
|
||||||
|
|
||||||
std_dev = MathSqrt(sum_sq / m_bands_period);
|
std_dev = MathSqrt(sum_sq / m_bands_period);
|
||||||
upper_out[i] = signal_out[i] + m_bands_dev * std_dev;
|
upper_out[i] = signal_out[i] + m_bands_dev * std_dev;
|
||||||
lower_out[i] = signal_out[i] - m_bands_dev * std_dev;
|
lower_out[i] = signal_out[i] - m_bands_dev * std_dev;
|
||||||
@@ -134,95 +153,98 @@ void CCCI_Calculator::Calculate(int rates_total, const double &open[], const dou
|
|||||||
}
|
}
|
||||||
|
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
//| CCCI_Calculator: Prepares the standard source price series. |
|
//| Prepare Price (Standard - Optimized) |
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
bool CCCI_Calculator::PreparePriceSeries(int rates_total, const double &open[], const double &high[], const double &low[], const double &close[], ENUM_APPLIED_PRICE price_type)
|
bool CCCI_Calculator::PreparePriceSeries(int rates_total, int start_index, ENUM_APPLIED_PRICE price_type, const double &open[], const double &high[], const double &low[], const double &close[])
|
||||||
{
|
{
|
||||||
ArrayResize(m_price, rates_total);
|
for(int i = start_index; i < rates_total; i++)
|
||||||
switch(price_type)
|
|
||||||
{
|
{
|
||||||
case PRICE_OPEN:
|
switch(price_type)
|
||||||
ArrayCopy(m_price, open, 0, 0, rates_total);
|
{
|
||||||
break;
|
case PRICE_CLOSE:
|
||||||
case PRICE_HIGH:
|
m_price[i] = close[i];
|
||||||
ArrayCopy(m_price, high, 0, 0, rates_total);
|
break;
|
||||||
break;
|
case PRICE_OPEN:
|
||||||
case PRICE_LOW:
|
m_price[i] = open[i];
|
||||||
ArrayCopy(m_price, low, 0, 0, rates_total);
|
break;
|
||||||
break;
|
case PRICE_HIGH:
|
||||||
case PRICE_MEDIAN:
|
m_price[i] = high[i];
|
||||||
for(int i=0; i<rates_total; i++)
|
break;
|
||||||
|
case PRICE_LOW:
|
||||||
|
m_price[i] = low[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]+2*close[i])/4.0;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
ArrayCopy(m_price, close, 0, 0, rates_total);
|
m_price[i] = close[i];
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
//+==================================================================+
|
//+==================================================================+
|
||||||
//| |
|
//| CLASS 2: CCCI_Calculator_HA (Heikin Ashi) |
|
||||||
//| CLASS 2: CCCI_Calculator_HA (Heikin Ashi) |
|
|
||||||
//| |
|
|
||||||
//+==================================================================+
|
//+==================================================================+
|
||||||
class CCCI_Calculator_HA : public CCCI_Calculator
|
class CCCI_Calculator_HA : public CCCI_Calculator
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
CHeikinAshi_Calculator m_ha_calculator;
|
CHeikinAshi_Calculator m_ha_calculator;
|
||||||
|
double m_ha_open[], m_ha_high[], m_ha_low[], m_ha_close[];
|
||||||
protected:
|
protected:
|
||||||
virtual bool PreparePriceSeries(int rates_total, const double &open[], const double &high[], const double &low[], const double &close[], ENUM_APPLIED_PRICE price_type) override;
|
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;
|
||||||
};
|
};
|
||||||
|
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
//| CCCI_Calculator_HA: Prepares the Heikin Ashi source price. |
|
//| |
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
bool CCCI_Calculator_HA::PreparePriceSeries(int rates_total, const double &open[], const double &high[], const double &low[], const double &close[], ENUM_APPLIED_PRICE price_type)
|
bool CCCI_Calculator_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[])
|
||||||
{
|
{
|
||||||
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(ha_low, rates_total);
|
|
||||||
ArrayResize(ha_close, rates_total);
|
|
||||||
m_ha_calculator.Calculate(rates_total, open, high, low, close, ha_open, ha_high, ha_low, ha_close);
|
|
||||||
|
|
||||||
ArrayResize(m_price, rates_total);
|
|
||||||
switch(price_type)
|
|
||||||
{
|
{
|
||||||
case PRICE_OPEN:
|
ArrayResize(m_ha_open, rates_total);
|
||||||
ArrayCopy(m_price, ha_open, 0, 0, rates_total);
|
ArrayResize(m_ha_high, rates_total);
|
||||||
break;
|
ArrayResize(m_ha_low, rates_total);
|
||||||
case PRICE_HIGH:
|
ArrayResize(m_ha_close, rates_total);
|
||||||
ArrayCopy(m_price, ha_high, 0, 0, rates_total);
|
}
|
||||||
break;
|
m_ha_calculator.Calculate(rates_total, start_index, open, high, low, close, m_ha_open, m_ha_high, m_ha_low, m_ha_close);
|
||||||
case PRICE_LOW:
|
for(int i = start_index; i < rates_total; i++)
|
||||||
ArrayCopy(m_price, ha_low, 0, 0, rates_total);
|
{
|
||||||
break;
|
switch(price_type)
|
||||||
case PRICE_MEDIAN:
|
{
|
||||||
for(int i=0; i<rates_total; i++)
|
case PRICE_CLOSE:
|
||||||
m_price[i] = (ha_high[i]+ha_low[i])/2.0;
|
m_price[i] = m_ha_close[i];
|
||||||
break;
|
break;
|
||||||
case PRICE_TYPICAL:
|
case PRICE_OPEN:
|
||||||
for(int i=0; i<rates_total; i++)
|
m_price[i] = m_ha_open[i];
|
||||||
m_price[i] = (ha_high[i]+ha_low[i]+ha_close[i])/3.0;
|
break;
|
||||||
break;
|
case PRICE_HIGH:
|
||||||
case PRICE_WEIGHTED:
|
m_price[i] = m_ha_high[i];
|
||||||
for(int i=0; i<rates_total; i++)
|
break;
|
||||||
m_price[i] = (ha_high[i]+ha_low[i]+2*ha_close[i])/4.0;
|
case PRICE_LOW:
|
||||||
break;
|
m_price[i] = m_ha_low[i];
|
||||||
default:
|
break;
|
||||||
ArrayCopy(m_price, ha_close, 0, 0, rates_total);
|
case PRICE_MEDIAN:
|
||||||
break;
|
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;
|
||||||
}
|
}
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
//+------------------------------------------------------------------+
|
|
||||||
|
|||||||
Reference in New Issue
Block a user