mirror of
https://github.com/softwaredevelop/mql5.git
synced 2026-08-17 14:28:07 +00:00
refactor: Optimized for incremental calculation
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
//| MACD_Laguerre_Histogram_Calculator.mqh |
|
//| MACD_Laguerre_Histogram_Calculator.mqh |
|
||||||
//| VERSION 1.10: Added selectable signal line. |
|
//| VERSION 1.20: Optimized for incremental calculation. |
|
||||||
//| Copyright 2025, xxxxxxxx |
|
//| Copyright 2025, xxxxxxxx |
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
#property copyright "Copyright 2025, xxxxxxxx"
|
#property copyright "Copyright 2025, xxxxxxxx"
|
||||||
@@ -8,6 +8,7 @@
|
|||||||
#include <MyIncludes\Laguerre_Engine.mqh>
|
#include <MyIncludes\Laguerre_Engine.mqh>
|
||||||
#include <MyIncludes\MovingAverage_Engine.mqh>
|
#include <MyIncludes\MovingAverage_Engine.mqh>
|
||||||
|
|
||||||
|
//--- Universal enum for smoothing types
|
||||||
enum ENUM_SMOOTHING_METHOD_LAGUERRE
|
enum ENUM_SMOOTHING_METHOD_LAGUERRE
|
||||||
{
|
{
|
||||||
SMOOTH_Laguerre,
|
SMOOTH_Laguerre,
|
||||||
@@ -25,18 +26,24 @@ protected:
|
|||||||
int m_signal_period;
|
int m_signal_period;
|
||||||
ENUM_SMOOTHING_METHOD_LAGUERRE m_signal_ma_type;
|
ENUM_SMOOTHING_METHOD_LAGUERRE m_signal_ma_type;
|
||||||
|
|
||||||
CLaguerreEngine *m_fast_engine, *m_slow_engine;
|
//--- Engines for MACD Line
|
||||||
double m_sig_L0_prev, m_sig_L1_prev, m_sig_L2_prev, m_sig_L3_prev;
|
CLaguerreEngine *m_fast_engine;
|
||||||
|
CLaguerreEngine *m_slow_engine;
|
||||||
|
|
||||||
|
//--- Engines for Signal Line (Optimization)
|
||||||
|
CLaguerreEngine *m_signal_laguerre_engine;
|
||||||
|
CMovingAverageCalculator *m_signal_ma_engine;
|
||||||
|
|
||||||
virtual CLaguerreEngine *CreateEngineInstance(void);
|
virtual CLaguerreEngine *CreateEngineInstance(void);
|
||||||
void CalculateMA(const double &source_array[], double &dest_array[], int period, ENUM_MA_TYPE method, int start_pos);
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
CMACDLaguerreHistogramCalculator(void);
|
CMACDLaguerreHistogramCalculator(void);
|
||||||
virtual ~CMACDLaguerreHistogramCalculator(void);
|
virtual ~CMACDLaguerreHistogramCalculator(void);
|
||||||
|
|
||||||
bool Init(double g1, double g2, double sig_g, int sig_p, ENUM_SMOOTHING_METHOD_LAGUERRE sig_type);
|
bool Init(double g1, double g2, double sig_g, int sig_p, ENUM_SMOOTHING_METHOD_LAGUERRE sig_type);
|
||||||
void Calculate(int rates_total, const double &open[], const double &high[], const double &low[], const double &close[], ENUM_APPLIED_PRICE price_type,
|
|
||||||
|
//--- Updated: Accepts prev_calculated
|
||||||
|
void Calculate(int rates_total, int prev_calculated, const double &open[], const double &high[], const double &low[], const double &close[], ENUM_APPLIED_PRICE price_type,
|
||||||
double &histogram[]);
|
double &histogram[]);
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -54,20 +61,18 @@ protected:
|
|||||||
//+==================================================================+
|
//+==================================================================+
|
||||||
|
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
//| |
|
//| Constructor |
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
CMACDLaguerreHistogramCalculator::CMACDLaguerreHistogramCalculator(void)
|
CMACDLaguerreHistogramCalculator::CMACDLaguerreHistogramCalculator(void)
|
||||||
{
|
{
|
||||||
m_fast_engine = NULL;
|
m_fast_engine = NULL;
|
||||||
m_slow_engine = NULL;
|
m_slow_engine = NULL;
|
||||||
m_sig_L0_prev = 0;
|
m_signal_laguerre_engine = NULL;
|
||||||
m_sig_L1_prev = 0;
|
m_signal_ma_engine = NULL;
|
||||||
m_sig_L2_prev = 0;
|
|
||||||
m_sig_L3_prev = 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
//| |
|
//| Destructor |
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
CMACDLaguerreHistogramCalculator::~CMACDLaguerreHistogramCalculator(void)
|
CMACDLaguerreHistogramCalculator::~CMACDLaguerreHistogramCalculator(void)
|
||||||
{
|
{
|
||||||
@@ -75,16 +80,20 @@ CMACDLaguerreHistogramCalculator::~CMACDLaguerreHistogramCalculator(void)
|
|||||||
delete m_fast_engine;
|
delete m_fast_engine;
|
||||||
if(CheckPointer(m_slow_engine) != POINTER_INVALID)
|
if(CheckPointer(m_slow_engine) != POINTER_INVALID)
|
||||||
delete m_slow_engine;
|
delete m_slow_engine;
|
||||||
|
if(CheckPointer(m_signal_laguerre_engine) != POINTER_INVALID)
|
||||||
|
delete m_signal_laguerre_engine;
|
||||||
|
if(CheckPointer(m_signal_ma_engine) != POINTER_INVALID)
|
||||||
|
delete m_signal_ma_engine;
|
||||||
}
|
}
|
||||||
|
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
//| |
|
//| Factory Method |
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
CLaguerreEngine *CMACDLaguerreHistogramCalculator::CreateEngineInstance(void) { return new CLaguerreEngine(); }
|
CLaguerreEngine *CMACDLaguerreHistogramCalculator::CreateEngineInstance(void) { return new CLaguerreEngine(); }
|
||||||
CLaguerreEngine *CMACDLaguerreHistogramCalculator_HA::CreateEngineInstance(void) { return new CLaguerreEngine_HA(); }
|
CLaguerreEngine *CMACDLaguerreHistogramCalculator_HA::CreateEngineInstance(void) { return new CLaguerreEngine_HA(); }
|
||||||
|
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
//| |
|
//| Init |
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
bool CMACDLaguerreHistogramCalculator::Init(double g1, double g2, double sig_g, int sig_p, ENUM_SMOOTHING_METHOD_LAGUERRE sig_type)
|
bool CMACDLaguerreHistogramCalculator::Init(double g1, double g2, double sig_g, int sig_p, ENUM_SMOOTHING_METHOD_LAGUERRE sig_type)
|
||||||
{
|
{
|
||||||
@@ -94,11 +103,7 @@ bool CMACDLaguerreHistogramCalculator::Init(double g1, double g2, double sig_g,
|
|||||||
m_signal_period = (sig_p < 1) ? 1 : sig_p;
|
m_signal_period = (sig_p < 1) ? 1 : sig_p;
|
||||||
m_signal_ma_type = sig_type;
|
m_signal_ma_type = sig_type;
|
||||||
|
|
||||||
m_sig_L0_prev=0;
|
// Create Main Engines
|
||||||
m_sig_L1_prev=0;
|
|
||||||
m_sig_L2_prev=0;
|
|
||||||
m_sig_L3_prev=0;
|
|
||||||
|
|
||||||
m_fast_engine = CreateEngineInstance();
|
m_fast_engine = CreateEngineInstance();
|
||||||
m_slow_engine = CreateEngineInstance();
|
m_slow_engine = CreateEngineInstance();
|
||||||
|
|
||||||
@@ -106,135 +111,70 @@ bool CMACDLaguerreHistogramCalculator::Init(double g1, double g2, double sig_g,
|
|||||||
CheckPointer(m_slow_engine) == POINTER_INVALID || !m_slow_engine.Init(m_slow_gamma, SOURCE_PRICE))
|
CheckPointer(m_slow_engine) == POINTER_INVALID || !m_slow_engine.Init(m_slow_gamma, SOURCE_PRICE))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
|
// Create Signal Engine based on type
|
||||||
|
if(m_signal_ma_type == SMOOTH_Laguerre)
|
||||||
|
{
|
||||||
|
m_signal_laguerre_engine = new CLaguerreEngine();
|
||||||
|
if(!m_signal_laguerre_engine.Init(m_signal_gamma, SOURCE_PRICE))
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
m_signal_ma_engine = new CMovingAverageCalculator();
|
||||||
|
ENUM_MA_TYPE ma_type = (ENUM_MA_TYPE)(m_signal_ma_type - 1);
|
||||||
|
if(!m_signal_ma_engine.Init(m_signal_period, ma_type))
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
//| |
|
//| Main Calculation (Optimized) |
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
void CMACDLaguerreHistogramCalculator::Calculate(int rates_total, const double &open[], const double &high[], const double &low[], const double &close[], ENUM_APPLIED_PRICE price_type,
|
void CMACDLaguerreHistogramCalculator::Calculate(int rates_total, int prev_calculated, const double &open[], const double &high[], const double &low[], const double &close[], ENUM_APPLIED_PRICE price_type,
|
||||||
double &histogram[])
|
double &histogram[])
|
||||||
{
|
{
|
||||||
if(rates_total < 2)
|
if(rates_total < 2)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
double macd_line[], signal_line[];
|
//--- 1. Calculate Fast and Slow Laguerre Filters (Incremental)
|
||||||
ArrayResize(macd_line, rates_total);
|
|
||||||
ArrayResize(signal_line, rates_total);
|
|
||||||
|
|
||||||
double fast_filter[], slow_filter[];
|
double fast_filter[], slow_filter[];
|
||||||
double L0_dummy[], L1_dummy[], L2_dummy[], L3_dummy[];
|
// Note: The engine resizes them.
|
||||||
m_fast_engine.CalculateFilter(rates_total, price_type, open, high, low, close, L0_dummy, L1_dummy, L2_dummy, L3_dummy, fast_filter);
|
|
||||||
m_slow_engine.CalculateFilter(rates_total, price_type, open, high, low, close, L0_dummy, L1_dummy, L2_dummy, L3_dummy, slow_filter);
|
m_fast_engine.CalculateFilter(rates_total, prev_calculated, price_type, open, high, low, close, fast_filter);
|
||||||
for(int i = 0; i < rates_total; i++)
|
m_slow_engine.CalculateFilter(rates_total, prev_calculated, price_type, open, high, low, close, slow_filter);
|
||||||
|
|
||||||
|
//--- 2. Calculate MACD Line
|
||||||
|
double macd_line[];
|
||||||
|
ArrayResize(macd_line, rates_total);
|
||||||
|
|
||||||
|
int start_index = (prev_calculated > 0) ? prev_calculated - 1 : 0;
|
||||||
|
|
||||||
|
for(int i = start_index; i < rates_total; i++)
|
||||||
macd_line[i] = fast_filter[i] - slow_filter[i];
|
macd_line[i] = fast_filter[i] - slow_filter[i];
|
||||||
|
|
||||||
switch(m_signal_ma_type)
|
//--- 3. Calculate Signal Line
|
||||||
|
double signal_line[];
|
||||||
|
ArrayResize(signal_line, rates_total);
|
||||||
|
|
||||||
|
if(m_signal_ma_type == SMOOTH_Laguerre)
|
||||||
{
|
{
|
||||||
case SMOOTH_Laguerre:
|
// Use Laguerre Engine on the MACD Line
|
||||||
if(rates_total > 0)
|
m_signal_laguerre_engine.CalculateFilter(rates_total, prev_calculated, PRICE_CLOSE,
|
||||||
{
|
macd_line, macd_line, macd_line, macd_line,
|
||||||
signal_line[0] = macd_line[0];
|
signal_line);
|
||||||
m_sig_L0_prev = macd_line[0];
|
}
|
||||||
m_sig_L1_prev = macd_line[0];
|
else
|
||||||
m_sig_L2_prev = macd_line[0];
|
{
|
||||||
m_sig_L3_prev = macd_line[0];
|
// Use MA Engine on the MACD Line
|
||||||
}
|
m_signal_ma_engine.Calculate(rates_total, prev_calculated, PRICE_CLOSE,
|
||||||
for(int i = 1; i < rates_total; i++)
|
macd_line, macd_line, macd_line, macd_line,
|
||||||
{
|
signal_line);
|
||||||
double L0 = (1.0 - m_signal_gamma) * macd_line[i] + m_signal_gamma * m_sig_L0_prev;
|
|
||||||
double L1 = -m_signal_gamma * L0 + m_sig_L0_prev + m_signal_gamma * m_sig_L1_prev;
|
|
||||||
double L2 = -m_signal_gamma * L1 + m_sig_L1_prev + m_signal_gamma * m_sig_L2_prev;
|
|
||||||
double L3 = -m_signal_gamma * L2 + m_sig_L2_prev + m_signal_gamma * m_sig_L3_prev;
|
|
||||||
signal_line[i] = (L0 + 2.0 * L1 + 2.0 * L2 + L3) / 6.0;
|
|
||||||
m_sig_L0_prev = L0;
|
|
||||||
m_sig_L1_prev = L1;
|
|
||||||
m_sig_L2_prev = L2;
|
|
||||||
m_sig_L3_prev = L3;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
{
|
|
||||||
ENUM_MA_TYPE ma_type = (ENUM_MA_TYPE)(m_signal_ma_type - 1);
|
|
||||||
CalculateMA(macd_line, signal_line, m_signal_period, ma_type, m_signal_period + 1);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for(int i = 0; i < rates_total; i++)
|
//--- 4. Calculate Histogram
|
||||||
|
for(int i = start_index; i < rates_total; i++)
|
||||||
histogram[i] = macd_line[i] - signal_line[i];
|
histogram[i] = macd_line[i] - signal_line[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
//+------------------------------------------------------------------+
|
|
||||||
//| |
|
|
||||||
//+------------------------------------------------------------------+
|
|
||||||
void CMACDLaguerreHistogramCalculator::CalculateMA(const double &source_array[], double &dest_array[], int period, ENUM_MA_TYPE method, int start_pos)
|
|
||||||
{
|
|
||||||
for(int i = start_pos; i < ArraySize(source_array); i++)
|
|
||||||
{
|
|
||||||
switch(method)
|
|
||||||
{
|
|
||||||
case EMA:
|
|
||||||
case SMMA:
|
|
||||||
if(i == start_pos)
|
|
||||||
{
|
|
||||||
double sum=0;
|
|
||||||
int count=0;
|
|
||||||
for(int j=0; j<period; j++)
|
|
||||||
{
|
|
||||||
if(source_array[i-j] != EMPTY_VALUE)
|
|
||||||
{
|
|
||||||
sum+=source_array[i-j];
|
|
||||||
count++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if(count > 0)
|
|
||||||
dest_array[i]=sum/count;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if(method==EMA)
|
|
||||||
{
|
|
||||||
double pr=2.0/(period+1.0);
|
|
||||||
dest_array[i]=source_array[i]*pr+dest_array[i-1]*(1.0-pr);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
dest_array[i]=(dest_array[i-1]*(period-1)+source_array[i])/period;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case LWMA:
|
|
||||||
{
|
|
||||||
double sum=0, w_sum=0;
|
|
||||||
for(int j=0; j<period; j++)
|
|
||||||
{
|
|
||||||
if(source_array[i-j] == EMPTY_VALUE)
|
|
||||||
continue;
|
|
||||||
int w=period-j;
|
|
||||||
sum+=source_array[i-j]*w;
|
|
||||||
w_sum+=w;
|
|
||||||
}
|
|
||||||
if(w_sum>0)
|
|
||||||
dest_array[i]=sum/w_sum;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
default: // SMA
|
|
||||||
{
|
|
||||||
double sum=0;
|
|
||||||
int count=0;
|
|
||||||
for(int j=0; j<period; j++)
|
|
||||||
{
|
|
||||||
if(source_array[i-j] != EMPTY_VALUE)
|
|
||||||
{
|
|
||||||
sum+=source_array[i-j];
|
|
||||||
count++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if(count > 0)
|
|
||||||
dest_array[i]=sum/count;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
//+------------------------------------------------------------------+
|
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
|
|||||||
Reference in New Issue
Block a user