mirror of
https://github.com/softwaredevelop/mql5.git
synced 2026-08-02 15:17:43 +00:00
refactor: Added selectable signal line type
This commit is contained in:
@@ -1,35 +1,51 @@
|
||||
//+------------------------------------------------------------------+
|
||||
//| MACD_Laguerre_Calculator.mqh |
|
||||
//| Engine for the full MACD calculated from Laguerre. |
|
||||
//| VERSION 1.12: Corrected variable name in Laguerre signal calc.|
|
||||
//| Copyright 2025, xxxxxxxx |
|
||||
//+------------------------------------------------------------------+
|
||||
#property copyright "Copyright 2025, xxxxxxxx"
|
||||
|
||||
#include <MyIncludes\Laguerre_Engine.mqh>
|
||||
#include "Laguerre_Engine.mqh"
|
||||
#include "MovingAverage_Engine.mqh"
|
||||
|
||||
//--- Universal enum for smoothing types, now including Laguerre ---
|
||||
enum ENUM_SMOOTHING_METHOD_LAGUERRE
|
||||
{
|
||||
SMOOTH_Laguerre,
|
||||
SMOOTH_SMA,
|
||||
SMOOTH_EMA,
|
||||
SMOOTH_SMMA,
|
||||
SMOOTH_LWMA
|
||||
};
|
||||
|
||||
//+==================================================================+
|
||||
class CMACDLaguerreCalculator
|
||||
{
|
||||
protected:
|
||||
double m_fast_gamma, m_slow_gamma, m_signal_gamma;
|
||||
int m_signal_period;
|
||||
ENUM_SMOOTHING_METHOD_LAGUERRE m_signal_ma_type;
|
||||
|
||||
CLaguerreEngine *m_fast_engine;
|
||||
CLaguerreEngine *m_slow_engine;
|
||||
|
||||
//--- State variables for the signal line's Laguerre filter
|
||||
double m_sig_L0_prev, m_sig_L1_prev, m_sig_L2_prev, m_sig_L3_prev;
|
||||
|
||||
virtual CLaguerreEngine *CreateEngineInstance(void);
|
||||
void CalculateMA(const double &source_array[], double &dest_array[], int period, ENUM_MA_TYPE method, int start_pos);
|
||||
|
||||
public:
|
||||
CMACDLaguerreCalculator(void);
|
||||
virtual ~CMACDLaguerreCalculator(void);
|
||||
|
||||
bool Init(double gamma1, double gamma2, double signal_g);
|
||||
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,
|
||||
double &macd_line[], double &signal_line[], double &histogram[]);
|
||||
};
|
||||
|
||||
//--- Derived class for Heikin Ashi version ---
|
||||
//+------------------------------------------------------------------+
|
||||
//| |
|
||||
//+------------------------------------------------------------------+
|
||||
class CMACDLaguerreCalculator_HA : public CMACDLaguerreCalculator
|
||||
{
|
||||
protected:
|
||||
@@ -73,17 +89,18 @@ CLaguerreEngine *CMACDLaguerreCalculator_HA::CreateEngineInstance(void) { return
|
||||
//+------------------------------------------------------------------+
|
||||
//| |
|
||||
//+------------------------------------------------------------------+
|
||||
bool CMACDLaguerreCalculator::Init(double gamma1, double gamma2, double signal_g)
|
||||
bool CMACDLaguerreCalculator::Init(double g1, double g2, double sig_g, int sig_p, ENUM_SMOOTHING_METHOD_LAGUERRE sig_type)
|
||||
{
|
||||
m_fast_gamma = MathMin(gamma1, gamma2);
|
||||
m_slow_gamma = MathMax(gamma1, gamma2);
|
||||
m_signal_gamma = fmax(0.0, fmin(1.0, signal_g));
|
||||
m_fast_gamma = MathMin(g1, g2);
|
||||
m_slow_gamma = MathMax(g1, g2);
|
||||
m_signal_gamma = fmax(0.0, fmin(1.0, sig_g));
|
||||
m_signal_period = (sig_p < 1) ? 1 : sig_p;
|
||||
m_signal_ma_type = sig_type;
|
||||
|
||||
// Reset state
|
||||
m_sig_L0_prev = 0;
|
||||
m_sig_L1_prev = 0;
|
||||
m_sig_L2_prev = 0;
|
||||
m_sig_L3_prev = 0;
|
||||
m_sig_L0_prev=0;
|
||||
m_sig_L1_prev=0;
|
||||
m_sig_L2_prev=0;
|
||||
m_sig_L3_prev=0;
|
||||
|
||||
m_fast_engine = CreateEngineInstance();
|
||||
m_slow_engine = CreateEngineInstance();
|
||||
@@ -106,42 +123,118 @@ void CMACDLaguerreCalculator::Calculate(int rates_total, const double &open[], c
|
||||
|
||||
double fast_filter[], slow_filter[];
|
||||
double L0_dummy[], L1_dummy[], L2_dummy[], L3_dummy[];
|
||||
|
||||
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);
|
||||
|
||||
for(int i = 0; i < rates_total; i++)
|
||||
macd_line[i] = fast_filter[i] - slow_filter[i];
|
||||
|
||||
//--- STEP 4: Calculate Signal Line (Laguerre Filter on MACD Line)
|
||||
// Robust initialization
|
||||
if(rates_total > 0)
|
||||
switch(m_signal_ma_type)
|
||||
{
|
||||
signal_line[0] = macd_line[0];
|
||||
m_sig_L0_prev = macd_line[0];
|
||||
m_sig_L1_prev = macd_line[0];
|
||||
m_sig_L2_prev = macd_line[0];
|
||||
m_sig_L3_prev = macd_line[0];
|
||||
case SMOOTH_Laguerre:
|
||||
if(rates_total > 0)
|
||||
{
|
||||
signal_line[0] = macd_line[0];
|
||||
m_sig_L0_prev = macd_line[0];
|
||||
m_sig_L1_prev = macd_line[0];
|
||||
m_sig_L2_prev = macd_line[0];
|
||||
m_sig_L3_prev = macd_line[0];
|
||||
}
|
||||
for(int i = 1; i < rates_total; i++)
|
||||
{
|
||||
double L0 = (1.0 - m_signal_gamma) * macd_line[i] + m_signal_gamma * m_sig_L0_prev;
|
||||
//--- CORRECTED: Use m_signal_gamma for all calculations in this block ---
|
||||
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 = 1; i < rates_total; i++)
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
//--- STEP 5: Calculate Histogram
|
||||
for(int i = 0; i < rates_total; i++)
|
||||
histogram[i] = macd_line[i] - signal_line[i];
|
||||
}
|
||||
|
||||
//+------------------------------------------------------------------+
|
||||
//| |
|
||||
//+------------------------------------------------------------------+
|
||||
void CMACDLaguerreCalculator::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