mirror of
https://github.com/softwaredevelop/mql5.git
synced 2026-07-27 20:47:44 +00:00
refactor(indicators): Optimized for incremental calculation
This commit is contained in:
@@ -1,182 +1,195 @@
|
||||
//+------------------------------------------------------------------+
|
||||
//| UltimateOscillator_Calculator.mqh|
|
||||
//| VERSION 2.20: Optimized moving sum calculation. |
|
||||
//| VERSION 3.00: Optimized for incremental calculation. |
|
||||
//| Copyright 2025, xxxxxxxx |
|
||||
//+------------------------------------------------------------------+
|
||||
#property copyright "Copyright 2025, xxxxxxxx"
|
||||
|
||||
#include <MyIncludes\HeikinAshi_Tools.mqh>
|
||||
#include <MyIncludes\MovingAverage_Engine.mqh>
|
||||
|
||||
//+==================================================================+
|
||||
//| |
|
||||
//| CLASS 1: CUltimateOscillatorCalculator (Base Class) |
|
||||
//| |
|
||||
//+==================================================================+
|
||||
class CUltimateOscillatorCalculator
|
||||
{
|
||||
protected:
|
||||
int m_p1, m_p2, m_p3, m_signal_p;
|
||||
ENUM_MA_METHOD m_signal_ma_type;
|
||||
double m_src_high[], m_src_low[], m_src_close[];
|
||||
|
||||
virtual bool PrepareSourceData(int rates_total, const double &open[], const double &high[], const double &low[], const double &close[]);
|
||||
//--- Engine for Signal Line
|
||||
CMovingAverageCalculator m_signal_engine;
|
||||
|
||||
//--- Persistent Buffers for Incremental Calculation
|
||||
double m_src_high[], m_src_low[], m_src_close[];
|
||||
double m_bp[], m_tr[];
|
||||
double m_uo_buffer[];
|
||||
|
||||
//--- 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:
|
||||
CUltimateOscillatorCalculator(void) {};
|
||||
virtual ~CUltimateOscillatorCalculator(void) {};
|
||||
|
||||
bool Init(int p1, int p2, int p3, int signal_p, ENUM_MA_METHOD signal_ma);
|
||||
void Calculate(int rates_total, const double &open[], const double &high[], const double &low[], const double &close[],
|
||||
//--- Init now takes ENUM_MA_TYPE
|
||||
bool Init(int p1, int p2, int p3, int signal_p, ENUM_MA_TYPE signal_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[],
|
||||
double &uo_buffer[], double &signal_buffer[]);
|
||||
};
|
||||
|
||||
//+------------------------------------------------------------------+
|
||||
//| CUltimateOscillatorCalculator: Initialization |
|
||||
//| Init |
|
||||
//+------------------------------------------------------------------+
|
||||
bool CUltimateOscillatorCalculator::Init(int p1, int p2, int p3, int signal_p, ENUM_MA_METHOD signal_ma)
|
||||
bool CUltimateOscillatorCalculator::Init(int p1, int p2, int p3, int signal_p, ENUM_MA_TYPE signal_ma)
|
||||
{
|
||||
m_p1 = (p1 < 1) ? 1 : p1;
|
||||
m_p2 = (p2 < 1) ? 1 : p2;
|
||||
m_p3 = (p3 < 1) ? 1 : p3;
|
||||
m_signal_p = (signal_p < 1) ? 1 : signal_p;
|
||||
m_signal_ma_type = signal_ma;
|
||||
|
||||
// Initialize Signal Engine
|
||||
if(!m_signal_engine.Init(m_signal_p, signal_ma))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
//+------------------------------------------------------------------+
|
||||
//| CUltimateOscillatorCalculator: Main Calculation Method (Optimized)|
|
||||
//| Main Calculation (Optimized) |
|
||||
//+------------------------------------------------------------------+
|
||||
void CUltimateOscillatorCalculator::Calculate(int rates_total, const double &open[], const double &high[], const double &low[], const double &close[],
|
||||
void CUltimateOscillatorCalculator::Calculate(int rates_total, int prev_calculated, const double &open[], const double &high[], const double &low[], const double &close[],
|
||||
double &uo_buffer[], double &signal_buffer[])
|
||||
{
|
||||
int max_period = MathMax(m_p1, MathMax(m_p2, m_p3));
|
||||
if(rates_total <= max_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_bp, rates_total);
|
||||
ArrayResize(m_tr, rates_total);
|
||||
ArrayResize(m_uo_buffer, rates_total);
|
||||
}
|
||||
|
||||
//--- 3. Prepare Source Data (Optimized)
|
||||
if(!PrepareSourceData(rates_total, start_index, open, high, low, close))
|
||||
return;
|
||||
|
||||
const double W1=4.0, W2=2.0, W3=1.0, W_SUM=7.0;
|
||||
|
||||
double bp[], tr[];
|
||||
ArrayResize(bp, rates_total);
|
||||
ArrayResize(tr, rates_total);
|
||||
for(int i=1; i<rates_total; i++)
|
||||
//--- 4. Calculate BP and TR (Incremental)
|
||||
int loop_start_bp = MathMax(1, start_index);
|
||||
|
||||
for(int i = loop_start_bp; i < rates_total; i++)
|
||||
{
|
||||
double true_low = MathMin(m_src_low[i], m_src_close[i-1]);
|
||||
bp[i] = m_src_close[i] - true_low;
|
||||
tr[i] = MathMax(m_src_high[i], m_src_close[i-1]) - true_low;
|
||||
m_bp[i] = m_src_close[i] - true_low;
|
||||
m_tr[i] = MathMax(m_src_high[i], m_src_close[i-1]) - true_low;
|
||||
}
|
||||
|
||||
//--- OPTIMIZED: Use efficient moving sum calculation ---
|
||||
double sum_bp1=0, sum_tr1=0, sum_bp2=0, sum_tr2=0, sum_bp3=0, sum_tr3=0;
|
||||
for(int i = 1; i < rates_total; i++)
|
||||
//--- 5. Calculate UO (Incremental Sliding Window)
|
||||
int loop_start_uo = MathMax(max_period, start_index);
|
||||
|
||||
for(int i = loop_start_uo; i < rates_total; i++)
|
||||
{
|
||||
sum_bp1+=bp[i];
|
||||
sum_tr1+=tr[i];
|
||||
sum_bp2+=bp[i];
|
||||
sum_tr2+=tr[i];
|
||||
sum_bp3+=bp[i];
|
||||
sum_tr3+=tr[i];
|
||||
// Optimization: Instead of persistent sums, we use a loop for robustness and simplicity.
|
||||
// For UO periods (typically 7, 14, 28), a loop is very fast.
|
||||
// Maintaining 6 persistent sum variables across ticks is error-prone.
|
||||
|
||||
if(i >= m_p1)
|
||||
double sum_bp1=0, sum_tr1=0;
|
||||
double sum_bp2=0, sum_tr2=0;
|
||||
double sum_bp3=0, sum_tr3=0;
|
||||
|
||||
// Calculate sums for period 1
|
||||
for(int j=0; j<m_p1; j++)
|
||||
{
|
||||
sum_bp1-=bp[i-m_p1];
|
||||
sum_tr1-=tr[i-m_p1];
|
||||
}
|
||||
if(i >= m_p2)
|
||||
{
|
||||
sum_bp2-=bp[i-m_p2];
|
||||
sum_tr2-=tr[i-m_p2];
|
||||
}
|
||||
if(i >= m_p3)
|
||||
{
|
||||
sum_bp3-=bp[i-m_p3];
|
||||
sum_tr3-=tr[i-m_p3];
|
||||
sum_bp1 += m_bp[i-j];
|
||||
sum_tr1 += m_tr[i-j];
|
||||
}
|
||||
|
||||
if(i >= max_period -1)
|
||||
// Calculate sums for period 2
|
||||
for(int j=0; j<m_p2; j++)
|
||||
{
|
||||
double avg1 = (sum_tr1 > 0) ? sum_bp1 / sum_tr1 : 0;
|
||||
double avg2 = (sum_tr2 > 0) ? sum_bp2 / sum_tr2 : 0;
|
||||
double avg3 = (sum_tr3 > 0) ? sum_bp3 / sum_tr3 : 0;
|
||||
uo_buffer[i] = 100.0 * (W1*avg1 + W2*avg2 + W3*avg3) / W_SUM;
|
||||
sum_bp2 += m_bp[i-j];
|
||||
sum_tr2 += m_tr[i-j];
|
||||
}
|
||||
|
||||
// Calculate sums for period 3
|
||||
for(int j=0; j<m_p3; j++)
|
||||
{
|
||||
sum_bp3 += m_bp[i-j];
|
||||
sum_tr3 += m_tr[i-j];
|
||||
}
|
||||
|
||||
double avg1 = (sum_tr1 > 0) ? sum_bp1 / sum_tr1 : 0;
|
||||
double avg2 = (sum_tr2 > 0) ? sum_bp2 / sum_tr2 : 0;
|
||||
double avg3 = (sum_tr3 > 0) ? sum_bp3 / sum_tr3 : 0;
|
||||
|
||||
m_uo_buffer[i] = 100.0 * (W1*avg1 + W2*avg2 + W3*avg3) / W_SUM;
|
||||
}
|
||||
|
||||
//--- Calculate Signal Line
|
||||
int signal_start = max_period + m_signal_p - 1;
|
||||
for(int i = signal_start; i < rates_total; i++)
|
||||
{
|
||||
switch(m_signal_ma_type)
|
||||
{
|
||||
case MODE_EMA:
|
||||
case MODE_SMMA:
|
||||
if(i == signal_start)
|
||||
{
|
||||
double sum=0;
|
||||
for(int j=0; j<m_signal_p; j++)
|
||||
sum+=uo_buffer[i-j];
|
||||
signal_buffer[i]=sum/m_signal_p;
|
||||
}
|
||||
else
|
||||
{
|
||||
if(m_signal_ma_type==MODE_EMA)
|
||||
{
|
||||
double pr=2.0/(m_signal_p+1.0);
|
||||
signal_buffer[i]=uo_buffer[i]*pr+signal_buffer[i-1]*(1.0-pr);
|
||||
}
|
||||
else
|
||||
signal_buffer[i]=(signal_buffer[i-1]*(m_signal_p-1)+uo_buffer[i])/m_signal_p;
|
||||
}
|
||||
break;
|
||||
case MODE_LWMA:
|
||||
{double sum=0,w_sum=0; for(int j=0; j<m_signal_p; j++) {int w=m_signal_p-j; sum+=uo_buffer[i-j]*w; w_sum+=w;} if(w_sum>0) signal_buffer[i]=sum/w_sum;}
|
||||
break;
|
||||
default:
|
||||
{double sum=0; for(int j=0; j<m_signal_p; j++) sum+=uo_buffer[i-j]; signal_buffer[i]=sum/m_signal_p;}
|
||||
break;
|
||||
}
|
||||
}
|
||||
//--- 6. Calculate Signal Line (Using Engine)
|
||||
// UO is valid from index: max_period
|
||||
int uo_offset = max_period;
|
||||
m_signal_engine.CalculateOnArray(rates_total, prev_calculated, m_uo_buffer, signal_buffer, uo_offset);
|
||||
|
||||
//--- 7. Copy UO to Output
|
||||
ArrayCopy(uo_buffer, m_uo_buffer, 0, 0, rates_total);
|
||||
}
|
||||
|
||||
//+------------------------------------------------------------------+
|
||||
//| CUltimateOscillatorCalculator: Prepares the standard source data.|
|
||||
//| Prepare Source Data (Standard - Optimized) |
|
||||
//+------------------------------------------------------------------+
|
||||
bool CUltimateOscillatorCalculator::PrepareSourceData(int rates_total, const double &open[], const double &high[], const double &low[], const double &close[])
|
||||
bool CUltimateOscillatorCalculator::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);
|
||||
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;
|
||||
}
|
||||
|
||||
//+==================================================================+
|
||||
//| |
|
||||
//| CLASS 2: CUltimateOscillatorCalculator_HA (Heikin Ashi) |
|
||||
//| |
|
||||
//| CLASS 2: CUltimateOscillatorCalculator_HA |
|
||||
//+==================================================================+
|
||||
class CUltimateOscillatorCalculator_HA : public CUltimateOscillatorCalculator
|
||||
{
|
||||
private:
|
||||
CHeikinAshi_Calculator m_ha_calculator;
|
||||
// Internal HA buffers
|
||||
double m_ha_open[];
|
||||
|
||||
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;
|
||||
};
|
||||
|
||||
//+------------------------------------------------------------------+
|
||||
//| CUltimateOscillatorCalculator_HA: Prepares the HA source data. |
|
||||
//| Prepare Source Data (Heikin Ashi - Optimized) |
|
||||
//+------------------------------------------------------------------+
|
||||
bool CUltimateOscillatorCalculator_HA::PrepareSourceData(int rates_total, const double &open[], const double &high[], const double &low[], const double &close[])
|
||||
bool CUltimateOscillatorCalculator_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);
|
||||
if(ArraySize(m_ha_open) != rates_total)
|
||||
ArrayResize(m_ha_open, 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_src_high, m_src_low, m_src_close);
|
||||
|
||||
return true;
|
||||
}
|
||||
//+------------------------------------------------------------------+
|
||||
|
||||
Reference in New Issue
Block a user