mirror of
https://github.com/softwaredevelop/mql5.git
synced 2026-08-23 01:08:06 +00:00
refactor(indicators): Refactored to use MovingAverage_Engine
This commit is contained in:
@@ -1,11 +1,12 @@
|
|||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
//| Gann_HiLo_Calculator.mqh|
|
//| Gann_HiLo_Calculator.mqh|
|
||||||
//| VERSION 2.00: Optimized for incremental calculation. |
|
//| VERSION 3.00: Refactored to use MovingAverage_Engine. |
|
||||||
//| 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: CGannHiLoCalculator (Base Class) |
|
//| CLASS 1: CGannHiLoCalculator (Base Class) |
|
||||||
@@ -14,7 +15,10 @@ class CGannHiLoCalculator
|
|||||||
{
|
{
|
||||||
protected:
|
protected:
|
||||||
int m_period;
|
int m_period;
|
||||||
ENUM_MA_METHOD m_ma_method;
|
|
||||||
|
//--- Engines for High and Low MA
|
||||||
|
CMovingAverageCalculator m_ma_high_engine;
|
||||||
|
CMovingAverageCalculator m_ma_low_engine;
|
||||||
|
|
||||||
//--- Persistent Buffers for Incremental Calculation
|
//--- Persistent Buffers for Incremental Calculation
|
||||||
double m_src_high[], m_src_low[], m_src_close[];
|
double m_src_high[], m_src_low[], m_src_close[];
|
||||||
@@ -27,7 +31,8 @@ public:
|
|||||||
CGannHiLoCalculator(void) {};
|
CGannHiLoCalculator(void) {};
|
||||||
virtual ~CGannHiLoCalculator(void) {};
|
virtual ~CGannHiLoCalculator(void) {};
|
||||||
|
|
||||||
bool Init(int period, ENUM_MA_METHOD ma_method);
|
//--- Init now takes ENUM_MA_TYPE instead of ENUM_MA_METHOD
|
||||||
|
bool Init(int period, ENUM_MA_TYPE ma_type);
|
||||||
|
|
||||||
//--- Updated: Accepts prev_calculated
|
//--- Updated: Accepts prev_calculated
|
||||||
void Calculate(int rates_total, int prev_calculated, const double &open[], const double &high[], const double &low[], const double &close[], double &hilo_buffer[], double &color_buffer[]);
|
void Calculate(int rates_total, int prev_calculated, const double &open[], const double &high[], const double &low[], const double &close[], double &hilo_buffer[], double &color_buffer[]);
|
||||||
@@ -36,10 +41,16 @@ public:
|
|||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
//| Init |
|
//| Init |
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
bool CGannHiLoCalculator::Init(int period, ENUM_MA_METHOD ma_method)
|
bool CGannHiLoCalculator::Init(int period, ENUM_MA_TYPE ma_type)
|
||||||
{
|
{
|
||||||
m_period = (period < 1) ? 1 : period;
|
m_period = (period < 1) ? 1 : period;
|
||||||
m_ma_method = ma_method;
|
|
||||||
|
// Initialize MA Engines
|
||||||
|
if(!m_ma_high_engine.Init(m_period, ma_type))
|
||||||
|
return false;
|
||||||
|
if(!m_ma_low_engine.Init(m_period, ma_type))
|
||||||
|
return false;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -73,78 +84,19 @@ void CGannHiLoCalculator::Calculate(int rates_total, int prev_calculated, const
|
|||||||
if(!PrepareSourceData(rates_total, start_index, open, high, low, close))
|
if(!PrepareSourceData(rates_total, start_index, open, high, low, close))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
//--- 4. Calculate High/Low Averages (Incremental)
|
//--- 4. Calculate High/Low Averages (Using Engines)
|
||||||
int ma_start_pos = m_period - 1;
|
// Note: Engines handle their own incremental logic internally
|
||||||
int loop_start = MathMax(ma_start_pos, start_index);
|
m_ma_high_engine.CalculateOnArray(rates_total, prev_calculated, m_src_high, m_hi_avg, 0);
|
||||||
|
m_ma_low_engine.CalculateOnArray(rates_total, prev_calculated, m_src_low, m_lo_avg, 0);
|
||||||
|
|
||||||
|
//--- 5. Determine Trend & Output (Incremental Loop)
|
||||||
|
// MA is valid from index: m_period - 1 (for SMA/LWMA) or 0 (for EMA)
|
||||||
|
// But Gann logic needs previous bar's MA, so we start at m_period
|
||||||
|
int loop_start = MathMax(m_period, start_index);
|
||||||
|
|
||||||
for(int i = loop_start; i < rates_total; i++)
|
for(int i = loop_start; i < rates_total; i++)
|
||||||
{
|
{
|
||||||
switch(m_ma_method)
|
// Determine Trend
|
||||||
{
|
|
||||||
case MODE_EMA:
|
|
||||||
case MODE_SMMA:
|
|
||||||
if(i == ma_start_pos)
|
|
||||||
{
|
|
||||||
double sum_h=0, sum_l=0;
|
|
||||||
for(int j=0; j<m_period; j++)
|
|
||||||
{
|
|
||||||
sum_h+=m_src_high[i-j];
|
|
||||||
sum_l+=m_src_low[i-j];
|
|
||||||
}
|
|
||||||
m_hi_avg[i]=sum_h/m_period;
|
|
||||||
m_lo_avg[i]=sum_l/m_period;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if(m_ma_method==MODE_EMA)
|
|
||||||
{
|
|
||||||
double pr=2.0/(m_period+1.0);
|
|
||||||
m_hi_avg[i]=m_src_high[i]*pr+m_hi_avg[i-1]*(1.0-pr);
|
|
||||||
m_lo_avg[i]=m_src_low[i]*pr+m_lo_avg[i-1]*(1.0-pr);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
m_hi_avg[i]=(m_hi_avg[i-1]*(m_period-1)+m_src_high[i])/m_period;
|
|
||||||
m_lo_avg[i]=(m_lo_avg[i-1]*(m_period-1)+m_src_low[i])/m_period;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case MODE_LWMA:
|
|
||||||
{
|
|
||||||
double wh=0,wl=0,ws=0;
|
|
||||||
for(int j=0; j<m_period; j++)
|
|
||||||
{
|
|
||||||
int w=m_period-j;
|
|
||||||
wh+=m_src_high[i-j]*w;
|
|
||||||
wl+=m_src_low[i-j]*w;
|
|
||||||
ws+=w;
|
|
||||||
}
|
|
||||||
if(ws>0)
|
|
||||||
{
|
|
||||||
m_hi_avg[i]=wh/ws;
|
|
||||||
m_lo_avg[i]=wl/ws;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
default: // SMA
|
|
||||||
{
|
|
||||||
double sh=0,sl=0;
|
|
||||||
for(int j=0; j<m_period; j++)
|
|
||||||
{
|
|
||||||
sh+=m_src_high[i-j];
|
|
||||||
sl+=m_src_low[i-j];
|
|
||||||
}
|
|
||||||
m_hi_avg[i]=sh/m_period;
|
|
||||||
m_lo_avg[i]=sl/m_period;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
//--- 5. Determine Trend (Incremental)
|
|
||||||
// We need m_trend[i-1] which is persistent
|
|
||||||
if(i < m_period)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
if(m_src_close[i] > m_hi_avg[i-1])
|
if(m_src_close[i] > m_hi_avg[i-1])
|
||||||
m_trend[i] = 1;
|
m_trend[i] = 1;
|
||||||
else
|
else
|
||||||
@@ -153,19 +105,22 @@ void CGannHiLoCalculator::Calculate(int rates_total, int prev_calculated, const
|
|||||||
else
|
else
|
||||||
m_trend[i] = m_trend[i-1]; // Keep previous trend
|
m_trend[i] = m_trend[i-1]; // Keep previous trend
|
||||||
|
|
||||||
//--- 6. Output to Buffers
|
// Output to Buffers
|
||||||
if(m_trend[i] == 1)
|
if(m_trend[i] == 1)
|
||||||
{
|
{
|
||||||
hilo_buffer[i] = m_lo_avg[i];
|
hilo_buffer[i] = m_lo_avg[i];
|
||||||
color_buffer[i] = 0;
|
color_buffer[i] = 0; // Bullish Color
|
||||||
// Backfill gap if trend changed
|
|
||||||
|
// Backfill gap if trend changed from Bearish to Bullish
|
||||||
if(m_trend[i-1] == -1)
|
if(m_trend[i-1] == -1)
|
||||||
hilo_buffer[i-1] = m_lo_avg[i];
|
hilo_buffer[i-1] = m_lo_avg[i];
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
hilo_buffer[i] = m_hi_avg[i];
|
hilo_buffer[i] = m_hi_avg[i];
|
||||||
color_buffer[i] = 1;
|
color_buffer[i] = 1; // Bearish Color
|
||||||
|
|
||||||
|
// Backfill gap if trend changed from Bullish to Bearish
|
||||||
if(m_trend[i-1] == 1)
|
if(m_trend[i-1] == 1)
|
||||||
hilo_buffer[i-1] = m_hi_avg[i];
|
hilo_buffer[i-1] = m_hi_avg[i];
|
||||||
}
|
}
|
||||||
@@ -177,7 +132,6 @@ void CGannHiLoCalculator::Calculate(int rates_total, int prev_calculated, const
|
|||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
bool CGannHiLoCalculator::PrepareSourceData(int rates_total, int start_index, const double &open[], const double &high[], const double &low[], const double &close[])
|
bool CGannHiLoCalculator::PrepareSourceData(int rates_total, int start_index, const double &open[], const double &high[], const double &low[], const double &close[])
|
||||||
{
|
{
|
||||||
// Optimized copy loop
|
|
||||||
for(int i = start_index; i < rates_total; i++)
|
for(int i = start_index; i < rates_total; i++)
|
||||||
{
|
{
|
||||||
m_src_high[i] = high[i];
|
m_src_high[i] = high[i];
|
||||||
@@ -194,7 +148,6 @@ class CGannHiLoCalculator_HA : public CGannHiLoCalculator
|
|||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
CHeikinAshi_Calculator m_ha_calculator;
|
CHeikinAshi_Calculator m_ha_calculator;
|
||||||
// Internal HA buffers
|
|
||||||
double m_ha_open[], m_ha_high_temp[], m_ha_low_temp[], m_ha_close_temp[];
|
double m_ha_open[], m_ha_high_temp[], m_ha_low_temp[], m_ha_close_temp[];
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
@@ -206,7 +159,6 @@ protected:
|
|||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
bool CGannHiLoCalculator_HA::PrepareSourceData(int rates_total, int start_index, const double &open[], const double &high[], const double &low[], const double &close[])
|
bool CGannHiLoCalculator_HA::PrepareSourceData(int rates_total, int start_index, const double &open[], const double &high[], const double &low[], const double &close[])
|
||||||
{
|
{
|
||||||
// Resize internal HA buffers
|
|
||||||
if(ArraySize(m_ha_open) != rates_total)
|
if(ArraySize(m_ha_open) != rates_total)
|
||||||
{
|
{
|
||||||
ArrayResize(m_ha_open, rates_total);
|
ArrayResize(m_ha_open, rates_total);
|
||||||
@@ -215,11 +167,9 @@ bool CGannHiLoCalculator_HA::PrepareSourceData(int rates_total, int start_index,
|
|||||||
ArrayResize(m_ha_close_temp, rates_total);
|
ArrayResize(m_ha_close_temp, rates_total);
|
||||||
}
|
}
|
||||||
|
|
||||||
//--- STRICT CALL: Use the optimized 10-param HA calculation
|
|
||||||
m_ha_calculator.Calculate(rates_total, start_index, open, high, low, close,
|
m_ha_calculator.Calculate(rates_total, start_index, open, high, low, close,
|
||||||
m_ha_open, m_ha_high_temp, m_ha_low_temp, m_ha_close_temp);
|
m_ha_open, m_ha_high_temp, m_ha_low_temp, m_ha_close_temp);
|
||||||
|
|
||||||
//--- Copy to source buffers (Optimized loop)
|
|
||||||
for(int i = start_index; i < rates_total; i++)
|
for(int i = start_index; i < rates_total; i++)
|
||||||
{
|
{
|
||||||
m_src_high[i] = m_ha_high_temp[i];
|
m_src_high[i] = m_ha_high_temp[i];
|
||||||
@@ -229,3 +179,4 @@ bool CGannHiLoCalculator_HA::PrepareSourceData(int rates_total, int start_index,
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
|
//+------------------------------------------------------------------+
|
||||||
|
|||||||
Reference in New Issue
Block a user