mirror of
https://github.com/softwaredevelop/mql5.git
synced 2026-08-06 00:57:45 +00:00
refactor: Optimized for incremental calculation
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
//+------------------------------------------------------------------+
|
||||
//| StochasticSlow_Calculator.mqh|
|
||||
//| Calculation engine for Standard and Heikin Ashi Slow Stochastic. |
|
||||
//| VERSION 1.20: Optimized for incremental calculation. |
|
||||
//| Copyright 2025, xxxxxxxx |
|
||||
//+------------------------------------------------------------------+
|
||||
#property copyright "Copyright 2025, xxxxxxxx"
|
||||
@@ -8,33 +8,37 @@
|
||||
#include <MyIncludes\HeikinAshi_Tools.mqh>
|
||||
|
||||
//+==================================================================+
|
||||
//| |
|
||||
//| CLASS 1: CStochasticSlowCalculator (Base Class) |
|
||||
//| |
|
||||
//+==================================================================+
|
||||
class CStochasticSlowCalculator
|
||||
{
|
||||
protected:
|
||||
int m_k_period, m_d_period, m_slowing_period;
|
||||
ENUM_MA_METHOD m_slowing_ma_type, m_d_ma_type;
|
||||
|
||||
//--- Persistent Buffers for Incremental Calculation
|
||||
double m_src_high[], m_src_low[], m_src_close[];
|
||||
double m_raw_k[]; // Stores Fast %K
|
||||
|
||||
double Highest(int period, int current_pos);
|
||||
double Lowest(int period, int current_pos);
|
||||
|
||||
virtual bool PrepareSourceData(int rates_total, const double &open[], const double &high[], const double &low[], const double &close[]);
|
||||
//--- Updated: Accepts start_index
|
||||
virtual bool PrepareSourceData(int rates_total, int start_index, const double &open[], const double &high[], const double &low[], const double &close[]);
|
||||
|
||||
public:
|
||||
CStochasticSlowCalculator(void) {};
|
||||
virtual ~CStochasticSlowCalculator(void) {};
|
||||
|
||||
bool Init(int k_p, int slow_p, ENUM_MA_METHOD slow_ma, int d_p, ENUM_MA_METHOD d_ma);
|
||||
void Calculate(int rates_total, const double &open[], const double &high[], const double &low[], const double &close[],
|
||||
|
||||
//--- 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 &k_buffer[], double &d_buffer[]);
|
||||
};
|
||||
|
||||
//+------------------------------------------------------------------+
|
||||
//| CStochasticSlowCalculator: Initialization |
|
||||
//| Init |
|
||||
//+------------------------------------------------------------------+
|
||||
bool CStochasticSlowCalculator::Init(int k_p, int slow_p, ENUM_MA_METHOD slow_ma, int d_p, ENUM_MA_METHOD d_ma)
|
||||
{
|
||||
@@ -47,34 +51,54 @@ bool CStochasticSlowCalculator::Init(int k_p, int slow_p, ENUM_MA_METHOD slow_ma
|
||||
}
|
||||
|
||||
//+------------------------------------------------------------------+
|
||||
//| CStochasticSlowCalculator: Main Calculation Method (Shared Logic)|
|
||||
//| Main Calculation (Optimized) |
|
||||
//+------------------------------------------------------------------+
|
||||
void CStochasticSlowCalculator::Calculate(int rates_total, const double &open[], const double &high[], const double &low[], const double &close[],
|
||||
void CStochasticSlowCalculator::Calculate(int rates_total, int prev_calculated, const double &open[], const double &high[], const double &low[], const double &close[],
|
||||
double &k_buffer[], double &d_buffer[])
|
||||
{
|
||||
if(rates_total <= m_k_period + m_slowing_period + m_d_period)
|
||||
return;
|
||||
if(!PrepareSourceData(rates_total, open, high, low, close))
|
||||
|
||||
//--- 1. Determine Start Index
|
||||
int start_index;
|
||||
if(prev_calculated == 0)
|
||||
start_index = 0;
|
||||
else
|
||||
start_index = prev_calculated - 1;
|
||||
|
||||
//--- 2. Resize Buffers
|
||||
if(ArraySize(m_src_high) != rates_total)
|
||||
{
|
||||
ArrayResize(m_src_high, rates_total);
|
||||
ArrayResize(m_src_low, rates_total);
|
||||
ArrayResize(m_src_close, rates_total);
|
||||
ArrayResize(m_raw_k, rates_total);
|
||||
}
|
||||
|
||||
//--- 3. Prepare Source Data (Optimized)
|
||||
if(!PrepareSourceData(rates_total, start_index, open, high, low, close))
|
||||
return;
|
||||
|
||||
double raw_k[];
|
||||
ArrayResize(raw_k, rates_total);
|
||||
//--- 4. Calculate Raw %K (Fast %K)
|
||||
int loop_start_k = MathMax(m_k_period - 1, start_index);
|
||||
|
||||
//--- STEP 1: Calculate Raw %K (Fast %K)
|
||||
for(int i = m_k_period - 1; i < rates_total; i++)
|
||||
for(int i = loop_start_k; i < rates_total; i++)
|
||||
{
|
||||
double highest_h = Highest(m_k_period, i);
|
||||
double lowest_l = Lowest(m_k_period, i);
|
||||
double range = highest_h - lowest_l;
|
||||
|
||||
if(range > 0)
|
||||
raw_k[i] = (m_src_close[i] - lowest_l) / range * 100.0;
|
||||
m_raw_k[i] = (m_src_close[i] - lowest_l) / range * 100.0;
|
||||
else
|
||||
raw_k[i] = (i > 0) ? raw_k[i-1] : 50.0;
|
||||
m_raw_k[i] = (i > 0) ? m_raw_k[i-1] : 50.0;
|
||||
}
|
||||
|
||||
//--- STEP 2: Calculate Slow %K (Main Line) by smoothing Raw %K
|
||||
//--- 5. Calculate Slow %K (Main Line)
|
||||
int k_slow_start = m_k_period + m_slowing_period - 2;
|
||||
for(int i = k_slow_start; i < rates_total; i++)
|
||||
int loop_start_slow = MathMax(k_slow_start, start_index);
|
||||
|
||||
for(int i = loop_start_slow; i < rates_total; i++)
|
||||
{
|
||||
switch(m_slowing_ma_type)
|
||||
{
|
||||
@@ -84,7 +108,7 @@ void CStochasticSlowCalculator::Calculate(int rates_total, const double &open[],
|
||||
{
|
||||
double sum=0;
|
||||
for(int j=0; j<m_slowing_period; j++)
|
||||
sum+=raw_k[i-j];
|
||||
sum+=m_raw_k[i-j];
|
||||
k_buffer[i]=sum/m_slowing_period;
|
||||
}
|
||||
else
|
||||
@@ -92,24 +116,26 @@ void CStochasticSlowCalculator::Calculate(int rates_total, const double &open[],
|
||||
if(m_slowing_ma_type==MODE_EMA)
|
||||
{
|
||||
double pr=2.0/(m_slowing_period+1.0);
|
||||
k_buffer[i]=raw_k[i]*pr+k_buffer[i-1]*(1.0-pr);
|
||||
k_buffer[i]=m_raw_k[i]*pr+k_buffer[i-1]*(1.0-pr);
|
||||
}
|
||||
else
|
||||
k_buffer[i]=(k_buffer[i-1]*(m_slowing_period-1)+raw_k[i])/m_slowing_period;
|
||||
k_buffer[i]=(k_buffer[i-1]*(m_slowing_period-1)+m_raw_k[i])/m_slowing_period;
|
||||
}
|
||||
break;
|
||||
case MODE_LWMA:
|
||||
{double sum=0,w_sum=0; for(int j=0; j<m_slowing_period; j++) {int w=m_slowing_period-j; sum+=raw_k[i-j]*w; w_sum+=w;} if(w_sum>0) k_buffer[i]=sum/w_sum;}
|
||||
{double sum=0,w_sum=0; for(int j=0; j<m_slowing_period; j++) {int w=m_slowing_period-j; sum+=m_raw_k[i-j]*w; w_sum+=w;} if(w_sum>0) k_buffer[i]=sum/w_sum;}
|
||||
break;
|
||||
default:
|
||||
{double sum=0; for(int j=0; j<m_slowing_period; j++) sum+=raw_k[i-j]; k_buffer[i]=sum/m_slowing_period;}
|
||||
{double sum=0; for(int j=0; j<m_slowing_period; j++) sum+=m_raw_k[i-j]; k_buffer[i]=sum/m_slowing_period;}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
//--- STEP 3: Calculate %D (Signal Line) by smoothing Slow %K
|
||||
//--- 6. Calculate %D (Signal Line)
|
||||
int d_start = m_k_period + m_slowing_period + m_d_period - 3;
|
||||
for(int i = d_start; i < rates_total; i++)
|
||||
int loop_start_d = MathMax(d_start, start_index);
|
||||
|
||||
for(int i = loop_start_d; i < rates_total; i++)
|
||||
{
|
||||
switch(m_d_ma_type)
|
||||
{
|
||||
@@ -144,21 +170,22 @@ void CStochasticSlowCalculator::Calculate(int rates_total, const double &open[],
|
||||
}
|
||||
|
||||
//+------------------------------------------------------------------+
|
||||
//| CStochasticSlowCalculator: Prepares the standard source data. |
|
||||
//| Prepare Source Data (Standard - Optimized) |
|
||||
//+------------------------------------------------------------------+
|
||||
bool CStochasticSlowCalculator::PrepareSourceData(int rates_total, const double &open[], const double &high[], const double &low[], const double &close[])
|
||||
bool CStochasticSlowCalculator::PrepareSourceData(int rates_total, int start_index, const double &open[], const double &high[], const double &low[], const double &close[])
|
||||
{
|
||||
ArrayResize(m_src_high, rates_total);
|
||||
ArrayCopy(m_src_high, high, 0, 0, rates_total);
|
||||
ArrayResize(m_src_low, rates_total);
|
||||
ArrayCopy(m_src_low, low, 0, 0, rates_total);
|
||||
ArrayResize(m_src_close, rates_total);
|
||||
ArrayCopy(m_src_close, close, 0, 0, rates_total);
|
||||
// Optimized copy loop
|
||||
for(int i = start_index; i < rates_total; i++)
|
||||
{
|
||||
m_src_high[i] = high[i];
|
||||
m_src_low[i] = low[i];
|
||||
m_src_close[i] = close[i];
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
//+------------------------------------------------------------------+
|
||||
//| Finds the highest value in the internal price buffer. |
|
||||
//| Highest |
|
||||
//+------------------------------------------------------------------+
|
||||
double CStochasticSlowCalculator::Highest(int period, int current_pos)
|
||||
{
|
||||
@@ -175,7 +202,7 @@ double CStochasticSlowCalculator::Highest(int period, int current_pos)
|
||||
}
|
||||
|
||||
//+------------------------------------------------------------------+
|
||||
//| Finds the lowest value in the internal price buffer. |
|
||||
//| Lowest |
|
||||
//+------------------------------------------------------------------+
|
||||
double CStochasticSlowCalculator::Lowest(int period, int current_pos)
|
||||
{
|
||||
@@ -192,30 +219,44 @@ double CStochasticSlowCalculator::Lowest(int period, int current_pos)
|
||||
}
|
||||
|
||||
//+==================================================================+
|
||||
//| |
|
||||
//| CLASS 2: CStochasticSlowCalculator_HA (Heikin Ashi) |
|
||||
//| |
|
||||
//+==================================================================+
|
||||
class CStochasticSlowCalculator_HA : public CStochasticSlowCalculator
|
||||
{
|
||||
private:
|
||||
CHeikinAshi_Calculator m_ha_calculator;
|
||||
// Internal HA buffers
|
||||
double m_ha_open[], m_ha_high_temp[], m_ha_low_temp[], m_ha_close_temp[];
|
||||
|
||||
protected:
|
||||
virtual bool PrepareSourceData(int rates_total, const double &open[], const double &high[], const double &low[], const double &close[]) override;
|
||||
virtual bool PrepareSourceData(int rates_total, int start_index, const double &open[], const double &high[], const double &low[], const double &close[]) override;
|
||||
};
|
||||
|
||||
//+------------------------------------------------------------------+
|
||||
//| CStochasticSlowCalculator_HA: Prepares the HA source data. |
|
||||
//| Prepare Source Data (Heikin Ashi - Optimized) |
|
||||
//+------------------------------------------------------------------+
|
||||
bool CStochasticSlowCalculator_HA::PrepareSourceData(int rates_total, const double &open[], const double &high[], const double &low[], const double &close[])
|
||||
bool CStochasticSlowCalculator_HA::PrepareSourceData(int rates_total, int start_index, const double &open[], const double &high[], const double &low[], const double &close[])
|
||||
{
|
||||
double ha_open[];
|
||||
ArrayResize(ha_open, rates_total);
|
||||
ArrayResize(m_src_high, rates_total);
|
||||
ArrayResize(m_src_low, rates_total);
|
||||
ArrayResize(m_src_close, rates_total);
|
||||
m_ha_calculator.Calculate(rates_total, open, high, low, close, ha_open, m_src_high, m_src_low, m_src_close);
|
||||
// Resize internal HA buffers
|
||||
if(ArraySize(m_ha_open) != rates_total)
|
||||
{
|
||||
ArrayResize(m_ha_open, rates_total);
|
||||
ArrayResize(m_ha_high_temp, rates_total);
|
||||
ArrayResize(m_ha_low_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_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++)
|
||||
{
|
||||
m_src_high[i] = m_ha_high_temp[i];
|
||||
m_src_low[i] = m_ha_low_temp[i];
|
||||
m_src_close[i] = m_ha_close_temp[i];
|
||||
}
|
||||
return true;
|
||||
}
|
||||
//+------------------------------------------------------------------+
|
||||
//+------------------------------------------------------------------+
|
||||
|
||||
Reference in New Issue
Block a user