mirror of
https://github.com/softwaredevelop/mql5.git
synced 2026-07-27 20:47:44 +00:00
refactor(indicators): Unified calculator for ALL Laguerre MACD inds
This commit is contained in:
@@ -1,9 +1,9 @@
|
||||
//+------------------------------------------------------------------+
|
||||
//| MACD_Laguerre_Calculator.mqh |
|
||||
//| VERSION 2.00: Optimized for incremental calculation. |
|
||||
//| Copyright 2025, xxxxxxxx |
|
||||
//| VERSION 3.00: Unified calculator for ALL Laguerre MACD inds.|
|
||||
//| Copyright 2026, xxxxxxxx |
|
||||
//+------------------------------------------------------------------+
|
||||
#property copyright "Copyright 2025, xxxxxxxx"
|
||||
#property copyright "Copyright 2026, xxxxxxxx"
|
||||
|
||||
#include <MyIncludes\Laguerre_Engine.mqh>
|
||||
#include <MyIncludes\MovingAverage_Engine.mqh>
|
||||
@@ -21,6 +21,8 @@ enum ENUM_SMOOTHING_METHOD_LAGUERRE
|
||||
SMOOTH_TEMA
|
||||
};
|
||||
|
||||
//+==================================================================+
|
||||
//| CLASS 1: CMACDLaguerreCalculator (Base) |
|
||||
//+==================================================================+
|
||||
class CMACDLaguerreCalculator
|
||||
{
|
||||
@@ -29,17 +31,18 @@ protected:
|
||||
int m_signal_period;
|
||||
ENUM_SMOOTHING_METHOD_LAGUERRE m_signal_ma_type;
|
||||
|
||||
//--- Engines for MACD Line
|
||||
//--- Engines
|
||||
CLaguerreEngine *m_fast_engine;
|
||||
CLaguerreEngine *m_slow_engine;
|
||||
|
||||
//--- Engines for Signal Line
|
||||
CLaguerreEngine *m_signal_laguerre_engine;
|
||||
CMovingAverageCalculator *m_signal_ma_engine;
|
||||
|
||||
//--- Persistent Buffers
|
||||
//--- Persistent Internal Buffers
|
||||
double m_fast_filter[];
|
||||
double m_slow_filter[];
|
||||
double m_macd_internal[]; // Stores MACD Line
|
||||
double m_signal_internal[]; // Stores Signal Line
|
||||
double m_hist_internal[]; // Stores Histogram
|
||||
|
||||
virtual CLaguerreEngine *CreateEngineInstance(void);
|
||||
|
||||
@@ -49,13 +52,21 @@ public:
|
||||
|
||||
bool Init(double g1, double g2, double sig_g, int sig_p, ENUM_SMOOTHING_METHOD_LAGUERRE sig_type);
|
||||
|
||||
//--- Updated: Accepts prev_calculated
|
||||
//--- Main Calculation (All outputs)
|
||||
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 &macd_line[], double &signal_line[], double &histogram[]);
|
||||
double &macd_out[], double &signal_out[], double &hist_out[]);
|
||||
|
||||
//--- Wrapper for Histogram Only
|
||||
void CalculateHistogramOnly(int rates_total, int prev_calculated, const double &open[], const double &high[], const double &low[], const double &close[], ENUM_APPLIED_PRICE price_type,
|
||||
double &hist_out[]);
|
||||
|
||||
//--- Wrapper for MACD Line Only
|
||||
void CalculateMACDLineOnly(int rates_total, int prev_calculated, const double &open[], const double &high[], const double &low[], const double &close[], ENUM_APPLIED_PRICE price_type,
|
||||
double &macd_out[]);
|
||||
};
|
||||
|
||||
//+------------------------------------------------------------------+
|
||||
//| |
|
||||
//| CLASS 2: CMACDLaguerreCalculator_HA |
|
||||
//+------------------------------------------------------------------+
|
||||
class CMACDLaguerreCalculator_HA : public CMACDLaguerreCalculator
|
||||
{
|
||||
@@ -110,7 +121,6 @@ bool CMACDLaguerreCalculator::Init(double g1, double g2, double sig_g, int sig_p
|
||||
m_signal_period = (sig_p < 1) ? 1 : sig_p;
|
||||
m_signal_ma_type = sig_type;
|
||||
|
||||
// Create Main Engines
|
||||
m_fast_engine = CreateEngineInstance();
|
||||
m_slow_engine = CreateEngineInstance();
|
||||
|
||||
@@ -118,7 +128,6 @@ bool CMACDLaguerreCalculator::Init(double g1, double g2, double sig_g, int sig_p
|
||||
CheckPointer(m_slow_engine) == POINTER_INVALID || !m_slow_engine.Init(m_slow_gamma, SOURCE_PRICE))
|
||||
return false;
|
||||
|
||||
// Create Signal Engine based on type
|
||||
if(m_signal_ma_type == SMOOTH_Laguerre)
|
||||
{
|
||||
m_signal_laguerre_engine = new CLaguerreEngine();
|
||||
@@ -137,10 +146,10 @@ bool CMACDLaguerreCalculator::Init(double g1, double g2, double sig_g, int sig_p
|
||||
}
|
||||
|
||||
//+------------------------------------------------------------------+
|
||||
//| Main Calculation (Optimized) |
|
||||
//| Main Calculation |
|
||||
//+------------------------------------------------------------------+
|
||||
void CMACDLaguerreCalculator::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 &macd_line[], double &signal_line[], double &histogram[])
|
||||
double &macd_out[], double &signal_out[], double &hist_out[])
|
||||
{
|
||||
if(rates_total < 2)
|
||||
return;
|
||||
@@ -150,38 +159,66 @@ void CMACDLaguerreCalculator::Calculate(int rates_total, int prev_calculated, co
|
||||
{
|
||||
ArrayResize(m_fast_filter, rates_total);
|
||||
ArrayResize(m_slow_filter, rates_total);
|
||||
ArrayResize(m_macd_internal, rates_total);
|
||||
ArrayResize(m_signal_internal, rates_total);
|
||||
ArrayResize(m_hist_internal, rates_total);
|
||||
}
|
||||
|
||||
//--- 1. Calculate Fast and Slow Laguerre Filters (Incremental)
|
||||
// 1. Calculate Fast and Slow Laguerre
|
||||
m_fast_engine.CalculateFilter(rates_total, prev_calculated, price_type, open, high, low, close, m_fast_filter);
|
||||
m_slow_engine.CalculateFilter(rates_total, prev_calculated, price_type, open, high, low, close, m_slow_filter);
|
||||
|
||||
//--- 2. Calculate MACD Line
|
||||
// 2. Calculate MACD Line
|
||||
int start_index = (prev_calculated > 0) ? prev_calculated - 1 : 0;
|
||||
|
||||
for(int i = start_index; i < rates_total; i++)
|
||||
macd_line[i] = m_fast_filter[i] - m_slow_filter[i];
|
||||
|
||||
//--- 3. Calculate Signal Line
|
||||
// Offset: Laguerre filter warms up quickly, but let's say 2 bars to be safe
|
||||
int macd_offset = 2;
|
||||
m_macd_internal[i] = m_fast_filter[i] - m_slow_filter[i];
|
||||
|
||||
// 3. Calculate Signal Line
|
||||
if(m_signal_ma_type == SMOOTH_Laguerre)
|
||||
{
|
||||
// Use Laguerre Engine on the MACD Line
|
||||
m_signal_laguerre_engine.CalculateFilter(rates_total, prev_calculated, PRICE_CLOSE,
|
||||
macd_line, macd_line, macd_line, macd_line,
|
||||
signal_line);
|
||||
m_macd_internal, m_macd_internal, m_macd_internal, m_macd_internal,
|
||||
m_signal_internal);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Use MA Engine on the MACD Line
|
||||
m_signal_ma_engine.CalculateOnArray(rates_total, prev_calculated, macd_line, signal_line, macd_offset);
|
||||
m_signal_ma_engine.CalculateOnArray(rates_total, prev_calculated, m_macd_internal, m_signal_internal, 2);
|
||||
}
|
||||
|
||||
//--- 4. Calculate Histogram
|
||||
// 4. Calculate Histogram & Output
|
||||
for(int i = start_index; i < rates_total; i++)
|
||||
histogram[i] = macd_line[i] - signal_line[i];
|
||||
{
|
||||
m_hist_internal[i] = m_macd_internal[i] - m_signal_internal[i];
|
||||
|
||||
// Copy to output buffers if they are valid (not dummy)
|
||||
// Note: We check array size to avoid writing to dummy arrays if they are small (though we resize them in wrappers)
|
||||
if(ArraySize(macd_out) == rates_total)
|
||||
macd_out[i] = m_macd_internal[i];
|
||||
if(ArraySize(signal_out) == rates_total)
|
||||
signal_out[i] = m_signal_internal[i];
|
||||
if(ArraySize(hist_out) == rates_total)
|
||||
hist_out[i] = m_hist_internal[i];
|
||||
}
|
||||
}
|
||||
|
||||
//+------------------------------------------------------------------+
|
||||
//| Calculate Histogram Only |
|
||||
//+------------------------------------------------------------------+
|
||||
void CMACDLaguerreCalculator::CalculateHistogramOnly(int rates_total, int prev_calculated, const double &open[], const double &high[], const double &low[], const double &close[], ENUM_APPLIED_PRICE price_type,
|
||||
double &hist_out[])
|
||||
{
|
||||
double dummy_macd[], dummy_signal[];
|
||||
// No need to resize dummies, the Main Calculate checks size before writing
|
||||
Calculate(rates_total, prev_calculated, open, high, low, close, price_type, dummy_macd, dummy_signal, hist_out);
|
||||
}
|
||||
|
||||
//+------------------------------------------------------------------+
|
||||
//| Calculate MACD Line Only |
|
||||
//+------------------------------------------------------------------+
|
||||
void CMACDLaguerreCalculator::CalculateMACDLineOnly(int rates_total, int prev_calculated, const double &open[], const double &high[], const double &low[], const double &close[], ENUM_APPLIED_PRICE price_type,
|
||||
double &macd_out[])
|
||||
{
|
||||
double dummy_signal[], dummy_hist[];
|
||||
Calculate(rates_total, prev_calculated, open, high, low, close, price_type, macd_out, dummy_signal, dummy_hist);
|
||||
}
|
||||
//+------------------------------------------------------------------+
|
||||
//+------------------------------------------------------------------+
|
||||
|
||||
Reference in New Issue
Block a user