mirror of
https://github.com/softwaredevelop/mql5.git
synced 2026-08-16 22:08:07 +00:00
refactor(indicator): Unified calculator for ALL MACD indicators
This commit is contained in:
@@ -1,9 +1,9 @@
|
|||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
//| MACD_Calculator.mqh|
|
//| MACD_Calculator.mqh|
|
||||||
//| VERSION 3.00: Uses MovingAverage_Engine for all lines. |
|
//| VERSION 4.00: Unified calculator for ALL MACD indicators. |
|
||||||
//| Copyright 2025, xxxxxxxx |
|
//| Copyright 2026, xxxxxxxx |
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
#property copyright "Copyright 2025, xxxxxxxx"
|
#property copyright "Copyright 2026, xxxxxxxx"
|
||||||
|
|
||||||
#include <MyIncludes\HeikinAshi_Tools.mqh>
|
#include <MyIncludes\HeikinAshi_Tools.mqh>
|
||||||
#include <MyIncludes\MovingAverage_Engine.mqh>
|
#include <MyIncludes\MovingAverage_Engine.mqh>
|
||||||
@@ -25,6 +25,11 @@ protected:
|
|||||||
double m_fast_ma[];
|
double m_fast_ma[];
|
||||||
double m_slow_ma[];
|
double m_slow_ma[];
|
||||||
|
|
||||||
|
//--- Internal Result Buffers
|
||||||
|
double m_macd_internal[];
|
||||||
|
double m_signal_internal[];
|
||||||
|
double m_hist_internal[];
|
||||||
|
|
||||||
//--- Updated: Accepts start_index
|
//--- Updated: Accepts start_index
|
||||||
virtual bool PreparePriceSeries(int rates_total, int start_index, const double &open[], const double &high[], const double &low[], const double &close[], ENUM_APPLIED_PRICE price_type);
|
virtual bool PreparePriceSeries(int rates_total, int start_index, const double &open[], const double &high[], const double &low[], const double &close[], ENUM_APPLIED_PRICE price_type);
|
||||||
|
|
||||||
@@ -32,11 +37,17 @@ public:
|
|||||||
CMACDCalculator(void);
|
CMACDCalculator(void);
|
||||||
virtual ~CMACDCalculator(void);
|
virtual ~CMACDCalculator(void);
|
||||||
|
|
||||||
//--- Init now takes ENUM_MA_TYPE for all MAs
|
|
||||||
bool Init(int fast_p, int slow_p, int signal_p, ENUM_MA_TYPE src_ma, ENUM_MA_TYPE sig_ma);
|
bool Init(int fast_p, int slow_p, int signal_p, ENUM_MA_TYPE src_ma, ENUM_MA_TYPE sig_ma);
|
||||||
|
|
||||||
|
//--- Main Calculation
|
||||||
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,
|
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_line[], double &signal_line[], double &histogram[]);
|
||||||
|
|
||||||
|
//--- Wrappers
|
||||||
|
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[]);
|
||||||
|
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[]);
|
||||||
};
|
};
|
||||||
|
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
@@ -67,7 +78,6 @@ CMACDCalculator::~CMACDCalculator(void)
|
|||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
bool CMACDCalculator::Init(int fast_p, int slow_p, int signal_p, ENUM_MA_TYPE src_ma, ENUM_MA_TYPE sig_ma)
|
bool CMACDCalculator::Init(int fast_p, int slow_p, int signal_p, ENUM_MA_TYPE src_ma, ENUM_MA_TYPE sig_ma)
|
||||||
{
|
{
|
||||||
// Ensure fast < slow
|
|
||||||
int f_p = (fast_p < 1) ? 1 : fast_p;
|
int f_p = (fast_p < 1) ? 1 : fast_p;
|
||||||
int s_p = (slow_p < 1) ? 1 : slow_p;
|
int s_p = (slow_p < 1) ? 1 : slow_p;
|
||||||
if(f_p > s_p)
|
if(f_p > s_p)
|
||||||
@@ -76,10 +86,8 @@ bool CMACDCalculator::Init(int fast_p, int slow_p, int signal_p, ENUM_MA_TYPE sr
|
|||||||
f_p=s_p;
|
f_p=s_p;
|
||||||
s_p=temp;
|
s_p=temp;
|
||||||
}
|
}
|
||||||
|
|
||||||
int sig_p = (signal_p < 1) ? 1 : signal_p;
|
int sig_p = (signal_p < 1) ? 1 : signal_p;
|
||||||
|
|
||||||
// Initialize Engines
|
|
||||||
if(!m_fast_ma_engine.Init(f_p, src_ma))
|
if(!m_fast_ma_engine.Init(f_p, src_ma))
|
||||||
return false;
|
return false;
|
||||||
if(!m_slow_ma_engine.Init(s_p, src_ma))
|
if(!m_slow_ma_engine.Init(s_p, src_ma))
|
||||||
@@ -96,78 +104,91 @@ bool CMACDCalculator::Init(int fast_p, int slow_p, int signal_p, ENUM_MA_TYPE sr
|
|||||||
void CMACDCalculator::Calculate(int rates_total, int prev_calculated, const double &open[], const double &high[], const double &low[], const double &close[], ENUM_APPLIED_PRICE price_type,
|
void CMACDCalculator::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_line[], double &signal_line[], double &histogram[])
|
||||||
{
|
{
|
||||||
// Minimum bars check
|
|
||||||
int min_bars = m_slow_ma_engine.GetPeriod() + m_signal_ma_engine.GetPeriod();
|
int min_bars = m_slow_ma_engine.GetPeriod() + m_signal_ma_engine.GetPeriod();
|
||||||
if(rates_total <= min_bars)
|
if(rates_total <= min_bars)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
//--- 1. Determine Start Index
|
int start_index = (prev_calculated == 0) ? 0 : prev_calculated - 1;
|
||||||
int start_index;
|
|
||||||
if(prev_calculated == 0)
|
|
||||||
start_index = 0;
|
|
||||||
else
|
|
||||||
start_index = prev_calculated - 1;
|
|
||||||
|
|
||||||
//--- 2. Resize Buffers
|
// Resize Buffers
|
||||||
if(ArraySize(m_price) != rates_total)
|
if(ArraySize(m_price) != rates_total)
|
||||||
{
|
{
|
||||||
ArrayResize(m_price, rates_total);
|
ArrayResize(m_price, rates_total);
|
||||||
ArrayResize(m_fast_ma, rates_total);
|
ArrayResize(m_fast_ma, rates_total);
|
||||||
ArrayResize(m_slow_ma, rates_total);
|
ArrayResize(m_slow_ma, rates_total);
|
||||||
|
ArrayResize(m_macd_internal, rates_total);
|
||||||
|
ArrayResize(m_signal_internal, rates_total);
|
||||||
|
ArrayResize(m_hist_internal, rates_total);
|
||||||
}
|
}
|
||||||
|
|
||||||
//--- 3. Prepare Price (Optimized)
|
// Prepare Price
|
||||||
if(!PreparePriceSeries(rates_total, start_index, open, high, low, close, price_type))
|
if(!PreparePriceSeries(rates_total, start_index, open, high, low, close, price_type))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
//--- 4. Calculate Fast & Slow MAs (Delegated to Engine)
|
// Calculate Fast & Slow MAs
|
||||||
m_fast_ma_engine.Calculate(rates_total, prev_calculated, PRICE_CLOSE, m_price, m_price, m_price, m_price, m_fast_ma);
|
m_fast_ma_engine.Calculate(rates_total, prev_calculated, PRICE_CLOSE, m_price, m_price, m_price, m_price, m_fast_ma);
|
||||||
m_slow_ma_engine.Calculate(rates_total, prev_calculated, PRICE_CLOSE, m_price, m_price, m_price, m_price, m_slow_ma);
|
m_slow_ma_engine.Calculate(rates_total, prev_calculated, PRICE_CLOSE, m_price, m_price, m_price, m_price, m_slow_ma);
|
||||||
|
|
||||||
//--- 5. Calculate MACD Line
|
// Calculate MACD Line
|
||||||
int slow_period = m_slow_ma_engine.GetPeriod();
|
int slow_period = m_slow_ma_engine.GetPeriod();
|
||||||
int loop_start_macd = MathMax(slow_period - 1, start_index);
|
int loop_start_macd = MathMax(slow_period - 1, start_index);
|
||||||
|
|
||||||
if(prev_calculated == 0)
|
|
||||||
ArrayInitialize(macd_line, EMPTY_VALUE);
|
|
||||||
|
|
||||||
for(int i = loop_start_macd; i < rates_total; i++)
|
for(int i = loop_start_macd; i < rates_total; i++)
|
||||||
{
|
{
|
||||||
if(m_fast_ma[i] != EMPTY_VALUE && m_slow_ma[i] != EMPTY_VALUE)
|
if(m_fast_ma[i] != EMPTY_VALUE && m_slow_ma[i] != EMPTY_VALUE)
|
||||||
macd_line[i] = m_fast_ma[i] - m_slow_ma[i];
|
m_macd_internal[i] = m_fast_ma[i] - m_slow_ma[i];
|
||||||
else
|
else
|
||||||
macd_line[i] = EMPTY_VALUE;
|
m_macd_internal[i] = EMPTY_VALUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
//--- 6. Calculate Signal Line (Using MA Engine)
|
// Calculate Signal Line
|
||||||
// The MACD line starts being valid at 'slow_period - 1'.
|
|
||||||
// This is the offset we pass to the Signal Engine.
|
|
||||||
int macd_offset = slow_period - 1;
|
int macd_offset = slow_period - 1;
|
||||||
|
m_signal_ma_engine.CalculateOnArray(rates_total, prev_calculated, m_macd_internal, m_signal_internal, macd_offset);
|
||||||
|
|
||||||
if(prev_calculated == 0)
|
// Calculate Histogram & Output
|
||||||
ArrayInitialize(signal_line, EMPTY_VALUE);
|
|
||||||
|
|
||||||
m_signal_ma_engine.CalculateOnArray(rates_total, prev_calculated, macd_line, signal_line, macd_offset);
|
|
||||||
|
|
||||||
//--- 7. Calculate Histogram
|
|
||||||
int signal_period = m_signal_ma_engine.GetPeriod();
|
int signal_period = m_signal_ma_engine.GetPeriod();
|
||||||
int signal_start = macd_offset + signal_period - 1;
|
int signal_start = macd_offset + signal_period - 1;
|
||||||
int loop_start_hist = MathMax(signal_start, start_index);
|
int loop_start_hist = MathMax(signal_start, start_index);
|
||||||
|
|
||||||
if(prev_calculated == 0)
|
|
||||||
ArrayInitialize(histogram, EMPTY_VALUE);
|
|
||||||
|
|
||||||
for(int i = loop_start_hist; i < rates_total; i++)
|
for(int i = loop_start_hist; i < rates_total; i++)
|
||||||
{
|
{
|
||||||
if(macd_line[i] != EMPTY_VALUE && signal_line[i] != EMPTY_VALUE)
|
if(m_macd_internal[i] != EMPTY_VALUE && m_signal_internal[i] != EMPTY_VALUE)
|
||||||
histogram[i] = macd_line[i] - signal_line[i];
|
m_hist_internal[i] = m_macd_internal[i] - m_signal_internal[i];
|
||||||
else
|
else
|
||||||
histogram[i] = EMPTY_VALUE;
|
m_hist_internal[i] = EMPTY_VALUE;
|
||||||
|
|
||||||
|
// Copy to output buffers if valid
|
||||||
|
if(ArraySize(macd_line) == rates_total)
|
||||||
|
macd_line[i] = m_macd_internal[i];
|
||||||
|
if(ArraySize(signal_line) == rates_total)
|
||||||
|
signal_line[i] = m_signal_internal[i];
|
||||||
|
if(ArraySize(histogram) == rates_total)
|
||||||
|
histogram[i] = m_hist_internal[i];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
//| Prepare Price (Standard - Optimized) |
|
//| Wrappers |
|
||||||
|
//+------------------------------------------------------------------+
|
||||||
|
void CMACDCalculator::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[];
|
||||||
|
Calculate(rates_total, prev_calculated, open, high, low, close, price_type, dummy_macd, dummy_signal, hist_out);
|
||||||
|
}
|
||||||
|
|
||||||
|
//+------------------------------------------------------------------+
|
||||||
|
//| |
|
||||||
|
//+------------------------------------------------------------------+
|
||||||
|
void CMACDCalculator::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);
|
||||||
|
}
|
||||||
|
|
||||||
|
//+------------------------------------------------------------------+
|
||||||
|
//| Prepare Price (Standard) |
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
bool CMACDCalculator::PreparePriceSeries(int rates_total, int start_index, const double &open[], const double &high[], const double &low[], const double &close[], ENUM_APPLIED_PRICE price_type)
|
bool CMACDCalculator::PreparePriceSeries(int rates_total, int start_index, const double &open[], const double &high[], const double &low[], const double &close[], ENUM_APPLIED_PRICE price_type)
|
||||||
{
|
{
|
||||||
@@ -217,7 +238,7 @@ protected:
|
|||||||
};
|
};
|
||||||
|
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
//| |
|
//| Prepare Price (Heikin Ashi) |
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
bool CMACDCalculator_HA::PreparePriceSeries(int rates_total, int start_index, const double &open[], const double &high[], const double &low[], const double &close[], ENUM_APPLIED_PRICE price_type)
|
bool CMACDCalculator_HA::PreparePriceSeries(int rates_total, int start_index, const double &open[], const double &high[], const double &low[], const double &close[], ENUM_APPLIED_PRICE price_type)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user