mirror of
https://github.com/softwaredevelop/mql5.git
synced 2026-07-27 20:47:44 +00:00
refactor: Optimized for incremental calculation
This commit is contained in:
@@ -1,98 +1,120 @@
|
||||
//+------------------------------------------------------------------+
|
||||
//| Stochastic_Adaptive_Calculator.mqh |
|
||||
//| Engine for Frank Key's Variable-Length Stochastic. |
|
||||
//| VERSION 2.00: Optimized for incremental calculation. |
|
||||
//| Copyright 2025, xxxxxxxx |
|
||||
//+------------------------------------------------------------------+
|
||||
#property copyright "Copyright 2025, xxxxxxxx"
|
||||
|
||||
#include <MyIncludes\MovingAverage_Engine.mqh> // For ENUM_MA_TYPE
|
||||
#include <MyIncludes\MovingAverage_Engine.mqh>
|
||||
#include <MyIncludes\HeikinAshi_Tools.mqh>
|
||||
|
||||
//+==================================================================+
|
||||
//| CLASS 1: CStochasticAdaptiveCalculator |
|
||||
//+==================================================================+
|
||||
class CStochasticAdaptiveCalculator
|
||||
{
|
||||
protected:
|
||||
int m_er_period, m_min_period, m_max_period, m_slowing_period, m_d_period;
|
||||
ENUM_MA_TYPE m_d_ma_type;
|
||||
double m_price[];
|
||||
int m_er_period, m_min_period, m_max_period;
|
||||
|
||||
virtual bool PreparePriceSeries(int rates_total, ENUM_APPLIED_PRICE price_type, const double &open[], const double &high[], const double &low[], const double &close[]);
|
||||
void CalculateMA(const double &source_array[], double &dest_array[], int period, ENUM_MA_TYPE method, int start_pos);
|
||||
//--- Engines for Smoothing
|
||||
CMovingAverageCalculator m_slowing_engine;
|
||||
CMovingAverageCalculator m_signal_engine;
|
||||
|
||||
//--- Persistent Buffers
|
||||
double m_price[];
|
||||
double m_er_buffer[];
|
||||
double m_nsp_buffer[];
|
||||
double m_raw_k[];
|
||||
|
||||
//--- Updated: Accepts start_index
|
||||
virtual bool PreparePriceSeries(int rates_total, int start_index, ENUM_APPLIED_PRICE price_type, const double &open[], const double &high[], const double &low[], const double &close[]);
|
||||
|
||||
public:
|
||||
CStochasticAdaptiveCalculator(void) {};
|
||||
virtual ~CStochasticAdaptiveCalculator(void) {};
|
||||
|
||||
bool Init(int er_p, int min_p, int max_p, int slow_p, int d_p, ENUM_MA_TYPE d_ma);
|
||||
void Calculate(int rates_total, const double &open[], const double &high[], const double &low[], const double &close[], ENUM_APPLIED_PRICE price_type,
|
||||
//--- Init now takes ENUM_MA_TYPE
|
||||
bool Init(int er_p, int min_p, int max_p, int slow_p, ENUM_MA_TYPE slow_ma, int d_p, ENUM_MA_TYPE d_ma);
|
||||
|
||||
//--- Updated: Accepts prev_calculated
|
||||
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 &k_buffer[], double &d_buffer[]);
|
||||
};
|
||||
|
||||
//+------------------------------------------------------------------+
|
||||
//| |
|
||||
//| Init |
|
||||
//+------------------------------------------------------------------+
|
||||
class CStochasticAdaptiveCalculator_HA : public CStochasticAdaptiveCalculator
|
||||
bool CStochasticAdaptiveCalculator::Init(int er_p, int min_p, int max_p, int slow_p, ENUM_MA_TYPE slow_ma, int d_p, ENUM_MA_TYPE d_ma)
|
||||
{
|
||||
private:
|
||||
CHeikinAshi_Calculator m_ha_calculator;
|
||||
protected:
|
||||
virtual bool PreparePriceSeries(int rates_total, ENUM_APPLIED_PRICE price_type, const double &open[], const double &high[], const double &low[], const double &close[]) override;
|
||||
};
|
||||
|
||||
//+==================================================================+
|
||||
//| METHOD IMPLEMENTATIONS |
|
||||
//+==================================================================+
|
||||
|
||||
//+------------------------------------------------------------------+
|
||||
//| |
|
||||
//+------------------------------------------------------------------+
|
||||
bool CStochasticAdaptiveCalculator::Init(int er_p, int min_p, int max_p, int slow_p, int d_p, ENUM_MA_TYPE d_ma)
|
||||
{
|
||||
m_er_period = (er_p < 1) ? 1 : er_p;
|
||||
m_er_period = (er_p < 1) ? 1 : er_p;
|
||||
m_min_period = (min_p < 1) ? 1 : min_p;
|
||||
m_max_period = (max_p <= m_min_period) ? m_min_period + 1 : max_p;
|
||||
m_slowing_period = (slow_p < 1) ? 1 : slow_p;
|
||||
m_d_period = (d_p < 1) ? 1 : d_p;
|
||||
m_d_ma_type = d_ma;
|
||||
|
||||
// Initialize Engines
|
||||
if(!m_slowing_engine.Init(slow_p, slow_ma))
|
||||
return false;
|
||||
if(!m_signal_engine.Init(d_p, d_ma))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
//+------------------------------------------------------------------+
|
||||
//| |
|
||||
//| Main Calculation (Optimized) |
|
||||
//+------------------------------------------------------------------+
|
||||
void CStochasticAdaptiveCalculator::Calculate(int rates_total, const double &open[], const double &high[], const double &low[], const double &close[], ENUM_APPLIED_PRICE price_type,
|
||||
void CStochasticAdaptiveCalculator::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 &k_buffer[], double &d_buffer[])
|
||||
{
|
||||
// Minimum bars check
|
||||
if(rates_total <= m_er_period + m_max_period)
|
||||
return;
|
||||
if(!PreparePriceSeries(rates_total, price_type, open, high, low, close))
|
||||
|
||||
int start_index = (prev_calculated == 0) ? 0 : prev_calculated - 1;
|
||||
|
||||
// Resize Buffers
|
||||
if(ArraySize(m_price) != rates_total)
|
||||
{
|
||||
ArrayResize(m_price, rates_total);
|
||||
ArrayResize(m_er_buffer, rates_total);
|
||||
ArrayResize(m_nsp_buffer, rates_total);
|
||||
ArrayResize(m_raw_k, rates_total);
|
||||
}
|
||||
|
||||
if(!PreparePriceSeries(rates_total, start_index, price_type, open, high, low, close))
|
||||
return;
|
||||
|
||||
double er_buffer[], nsp_buffer[], raw_k[];
|
||||
ArrayResize(er_buffer, rates_total);
|
||||
ArrayResize(nsp_buffer, rates_total);
|
||||
ArrayResize(raw_k, rates_total);
|
||||
//--- 1. Calculate Efficiency Ratio (ER)
|
||||
int loop_start_er = MathMax(m_er_period, start_index);
|
||||
|
||||
for(int i = m_er_period; i < rates_total; i++)
|
||||
for(int i = loop_start_er; i < rates_total; i++)
|
||||
{
|
||||
double direction = MathAbs(m_price[i] - m_price[i - m_er_period]);
|
||||
double volatility = 0;
|
||||
for(int j = 0; j < m_er_period; j++)
|
||||
volatility += MathAbs(m_price[i - j] - m_price[i - j - 1]);
|
||||
er_buffer[i] = (volatility > 0.000001) ? direction / volatility : 0;
|
||||
|
||||
m_er_buffer[i] = (volatility > 0.000001) ? direction / volatility : 0;
|
||||
}
|
||||
|
||||
for(int i = m_er_period; i < rates_total; i++)
|
||||
//--- 2. Calculate Adaptive Period (NSP)
|
||||
for(int i = loop_start_er; i < rates_total; i++)
|
||||
{
|
||||
nsp_buffer[i] = (int)(er_buffer[i] * (m_max_period - m_min_period) + m_min_period);
|
||||
if(nsp_buffer[i] < 1)
|
||||
nsp_buffer[i] = 1;
|
||||
m_nsp_buffer[i] = (int)(m_er_buffer[i] * (m_max_period - m_min_period) + m_min_period);
|
||||
if(m_nsp_buffer[i] < 1)
|
||||
m_nsp_buffer[i] = 1;
|
||||
}
|
||||
|
||||
for(int i = m_er_period + m_max_period - 1; i < rates_total; i++)
|
||||
//--- 3. Calculate Raw %K (Adaptive)
|
||||
int raw_k_start = m_er_period + m_max_period - 1;
|
||||
int loop_start_k = MathMax(raw_k_start, start_index);
|
||||
|
||||
for(int i = loop_start_k; i < rates_total; i++)
|
||||
{
|
||||
int current_nsp = (int)nsp_buffer[i];
|
||||
double highest = m_price[i], lowest = m_price[i];
|
||||
int current_nsp = (int)m_nsp_buffer[i];
|
||||
double highest = m_price[i];
|
||||
double lowest = m_price[i];
|
||||
|
||||
// Lookback based on dynamic period
|
||||
for(int j = 1; j < current_nsp; j++)
|
||||
{
|
||||
if(i-j < 0)
|
||||
@@ -100,178 +122,117 @@ void CStochasticAdaptiveCalculator::Calculate(int rates_total, const double &ope
|
||||
highest = MathMax(highest, m_price[i-j]);
|
||||
lowest = MathMin(lowest, m_price[i-j]);
|
||||
}
|
||||
|
||||
double range = highest - lowest;
|
||||
if(range > 0.000001)
|
||||
raw_k[i] = (m_price[i] - lowest) / range * 100.0;
|
||||
m_raw_k[i] = (m_price[i] - lowest) / 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;
|
||||
}
|
||||
|
||||
int k_slow_start = m_er_period + m_max_period + m_slowing_period - 2;
|
||||
CalculateMA(raw_k, k_buffer, m_slowing_period, SMA, k_slow_start);
|
||||
int d_start = k_slow_start + m_d_period - 1;
|
||||
CalculateMA(k_buffer, d_buffer, m_d_period, m_d_ma_type, d_start);
|
||||
//--- 4. Calculate Slow %K (Main Line) using Slowing Engine
|
||||
// Offset: raw_k_start
|
||||
m_slowing_engine.CalculateOnArray(rates_total, prev_calculated, m_raw_k, k_buffer, raw_k_start);
|
||||
|
||||
//--- 5. Calculate %D (Signal Line) using Signal Engine
|
||||
// Offset: raw_k_start + slowing_period - 1
|
||||
int d_offset = raw_k_start + m_slowing_engine.GetPeriod() - 1;
|
||||
m_signal_engine.CalculateOnArray(rates_total, prev_calculated, k_buffer, d_buffer, d_offset);
|
||||
}
|
||||
|
||||
//+------------------------------------------------------------------+
|
||||
//| |
|
||||
//| Prepare Price (Standard - Optimized) |
|
||||
//+------------------------------------------------------------------+
|
||||
void CStochasticAdaptiveCalculator::CalculateMA(const double &source_array[], double &dest_array[], int period, ENUM_MA_TYPE method, int start_pos)
|
||||
bool CStochasticAdaptiveCalculator::PreparePriceSeries(int rates_total, int start_index, ENUM_APPLIED_PRICE price_type, const double &open[], const double &high[], const double &low[], const double &close[])
|
||||
{
|
||||
for(int i = start_pos; i < ArraySize(source_array); i++)
|
||||
for(int i = start_index; i < rates_total; i++)
|
||||
{
|
||||
switch(method)
|
||||
switch(price_type)
|
||||
{
|
||||
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;
|
||||
}
|
||||
case PRICE_CLOSE:
|
||||
m_price[i] = close[i];
|
||||
break;
|
||||
case PRICE_OPEN:
|
||||
m_price[i] = open[i];
|
||||
break;
|
||||
case PRICE_HIGH:
|
||||
m_price[i] = high[i];
|
||||
break;
|
||||
case PRICE_LOW:
|
||||
m_price[i] = low[i];
|
||||
break;
|
||||
case PRICE_MEDIAN:
|
||||
m_price[i] = (high[i]+low[i])/2.0;
|
||||
break;
|
||||
case PRICE_TYPICAL:
|
||||
m_price[i] = (high[i]+low[i]+close[i])/3.0;
|
||||
break;
|
||||
case PRICE_WEIGHTED:
|
||||
m_price[i] = (high[i]+low[i]+2*close[i])/4.0;
|
||||
break;
|
||||
default:
|
||||
m_price[i] = close[i];
|
||||
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;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
//+==================================================================+
|
||||
//| CLASS 2: CStochasticAdaptiveCalculator_HA |
|
||||
//+==================================================================+
|
||||
class CStochasticAdaptiveCalculator_HA : public CStochasticAdaptiveCalculator
|
||||
{
|
||||
private:
|
||||
CHeikinAshi_Calculator m_ha_calculator;
|
||||
double m_ha_open[], m_ha_high[], m_ha_low[], m_ha_close[];
|
||||
protected:
|
||||
virtual bool PreparePriceSeries(int rates_total, int start_index, ENUM_APPLIED_PRICE price_type, const double &open[], const double &high[], const double &low[], const double &close[]) override;
|
||||
};
|
||||
|
||||
//+------------------------------------------------------------------+
|
||||
//| |
|
||||
//+------------------------------------------------------------------+
|
||||
bool CStochasticAdaptiveCalculator::PreparePriceSeries(int rates_total, ENUM_APPLIED_PRICE price_type, const double &open[], const double &high[], const double &low[], const double &close[])
|
||||
bool CStochasticAdaptiveCalculator_HA::PreparePriceSeries(int rates_total, int start_index, ENUM_APPLIED_PRICE price_type, const double &open[], const double &high[], const double &low[], const double &close[])
|
||||
{
|
||||
if(ArraySize(m_price) != rates_total)
|
||||
if(ArrayResize(m_price, rates_total) != rates_total)
|
||||
return false;
|
||||
|
||||
switch(price_type)
|
||||
if(ArraySize(m_ha_open) != rates_total)
|
||||
{
|
||||
case PRICE_CLOSE:
|
||||
ArrayCopy(m_price, close, 0, 0, rates_total);
|
||||
break;
|
||||
case PRICE_OPEN:
|
||||
ArrayCopy(m_price, open, 0, 0, rates_total);
|
||||
break;
|
||||
case PRICE_HIGH:
|
||||
ArrayCopy(m_price, high, 0, 0, rates_total);
|
||||
break;
|
||||
case PRICE_LOW:
|
||||
ArrayCopy(m_price, low, 0, 0, rates_total);
|
||||
break;
|
||||
case PRICE_MEDIAN:
|
||||
for(int i=0; i<rates_total; i++)
|
||||
m_price[i] = (high[i]+low[i])/2.0;
|
||||
break;
|
||||
case PRICE_TYPICAL:
|
||||
for(int i=0; i<rates_total; i++)
|
||||
m_price[i] = (high[i]+low[i]+close[i])/3.0;
|
||||
break;
|
||||
case PRICE_WEIGHTED:
|
||||
for(int i=0; i<rates_total; i++)
|
||||
m_price[i] = (high[i]+low[i]+close[i]+close[i])/4.0;
|
||||
break;
|
||||
default:
|
||||
return false;
|
||||
ArrayResize(m_ha_open, rates_total);
|
||||
ArrayResize(m_ha_high, rates_total);
|
||||
ArrayResize(m_ha_low, rates_total);
|
||||
ArrayResize(m_ha_close, rates_total);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
//+------------------------------------------------------------------+
|
||||
//| |
|
||||
//+------------------------------------------------------------------+
|
||||
bool CStochasticAdaptiveCalculator_HA::PreparePriceSeries(int rates_total, ENUM_APPLIED_PRICE price_type, const double &open[], const double &high[], const double &low[], const double &close[])
|
||||
{
|
||||
double ha_open[], ha_high[], ha_low[], ha_close[];
|
||||
ArrayResize(ha_open, rates_total);
|
||||
ArrayResize(ha_high, rates_total);
|
||||
ArrayResize(ha_low, rates_total);
|
||||
ArrayResize(ha_close, rates_total);
|
||||
m_ha_calculator.Calculate(rates_total, open, high, low, close, ha_open, ha_high, ha_low, ha_close);
|
||||
|
||||
if(ArraySize(m_price) != rates_total)
|
||||
if(ArrayResize(m_price, rates_total) != rates_total)
|
||||
return false;
|
||||
|
||||
switch(price_type)
|
||||
m_ha_calculator.Calculate(rates_total, start_index, open, high, low, close, m_ha_open, m_ha_high, m_ha_low, m_ha_close);
|
||||
for(int i = start_index; i < rates_total; i++)
|
||||
{
|
||||
case PRICE_CLOSE:
|
||||
ArrayCopy(m_price, ha_close, 0, 0, rates_total);
|
||||
break;
|
||||
case PRICE_OPEN:
|
||||
ArrayCopy(m_price, ha_open, 0, 0, rates_total);
|
||||
break;
|
||||
case PRICE_HIGH:
|
||||
ArrayCopy(m_price, ha_high, 0, 0, rates_total);
|
||||
break;
|
||||
case PRICE_LOW:
|
||||
ArrayCopy(m_price, ha_low, 0, 0, rates_total);
|
||||
break;
|
||||
case PRICE_MEDIAN:
|
||||
for(int i=0; i<rates_total; i++)
|
||||
m_price[i] = (ha_high[i]+ha_low[i])/2.0;
|
||||
break;
|
||||
case PRICE_TYPICAL:
|
||||
for(int i=0; i<rates_total; i++)
|
||||
m_price[i] = (ha_high[i]+ha_low[i]+ha_close[i])/3.0;
|
||||
break;
|
||||
case PRICE_WEIGHTED:
|
||||
for(int i=0; i<rates_total; i++)
|
||||
m_price[i] = (ha_high[i]+ha_low[i]+ha_close[i]+ha_close[i])/4.0;
|
||||
break;
|
||||
default:
|
||||
return false;
|
||||
switch(price_type)
|
||||
{
|
||||
case PRICE_CLOSE:
|
||||
m_price[i] = m_ha_close[i];
|
||||
break;
|
||||
case PRICE_OPEN:
|
||||
m_price[i] = m_ha_open[i];
|
||||
break;
|
||||
case PRICE_HIGH:
|
||||
m_price[i] = m_ha_high[i];
|
||||
break;
|
||||
case PRICE_LOW:
|
||||
m_price[i] = m_ha_low[i];
|
||||
break;
|
||||
case PRICE_MEDIAN:
|
||||
m_price[i] = (m_ha_high[i]+m_ha_low[i])/2.0;
|
||||
break;
|
||||
case PRICE_TYPICAL:
|
||||
m_price[i] = (m_ha_high[i]+m_ha_low[i]+m_ha_close[i])/3.0;
|
||||
break;
|
||||
case PRICE_WEIGHTED:
|
||||
m_price[i] = (m_ha_high[i]+m_ha_low[i]+2*m_ha_close[i])/4.0;
|
||||
break;
|
||||
default:
|
||||
m_price[i] = m_ha_close[i];
|
||||
break;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
//+------------------------------------------------------------------+
|
||||
//+------------------------------------------------------------------+
|
||||
|
||||
Reference in New Issue
Block a user