mirror of
https://github.com/softwaredevelop/mql5.git
synced 2026-08-20 07:48:06 +00:00
refactor(indicators): Optimized for incremental calculation
This commit is contained in:
@@ -1,174 +1,203 @@
|
|||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
//| MFI_Calculator.mqh |
|
//| MFI_Calculator.mqh |
|
||||||
//| Calculation engine for Standard and Heikin Ashi MFI. |
|
//| Calculation engine for Standard and Heikin Ashi MFI. |
|
||||||
|
//| VERSION 2.00: Optimized for incremental calculation. |
|
||||||
//| 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: CMFICalculator (Base Class) |
|
//| CLASS 1: CMFICalculator (Base Class) |
|
||||||
//| |
|
|
||||||
//+==================================================================+
|
//+==================================================================+
|
||||||
class CMFICalculator
|
class CMFICalculator
|
||||||
{
|
{
|
||||||
protected:
|
protected:
|
||||||
int m_mfi_period, m_ma_period;
|
int m_mfi_period;
|
||||||
ENUM_MA_METHOD m_ma_method;
|
|
||||||
ENUM_APPLIED_VOLUME m_volume_type;
|
ENUM_APPLIED_VOLUME m_volume_type;
|
||||||
double m_typical_price[];
|
|
||||||
|
|
||||||
//--- CORRECTED: Added 'open' to signature
|
//--- Engine for Signal Line
|
||||||
virtual bool PreparePriceSeries(int rates_total, const double &open[], const double &high[], const double &low[], const double &close[]);
|
CMovingAverageCalculator m_signal_engine;
|
||||||
|
|
||||||
|
//--- Persistent Buffers for Incremental Calculation
|
||||||
|
double m_typical_price[];
|
||||||
|
double m_pos_mf[]; // Positive Money Flow
|
||||||
|
double m_neg_mf[]; // Negative Money Flow
|
||||||
|
double m_mfi_buffer[];
|
||||||
|
|
||||||
|
//--- 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[]);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
CMFICalculator(void) {};
|
CMFICalculator(void) {};
|
||||||
virtual ~CMFICalculator(void) {};
|
virtual ~CMFICalculator(void) {};
|
||||||
|
|
||||||
bool Init(int mfi_p, int ma_p, ENUM_MA_METHOD ma_m, ENUM_APPLIED_VOLUME vol_t);
|
//--- Init now takes ENUM_MA_TYPE
|
||||||
//--- CORRECTED: Added 'open' to signature
|
bool Init(int mfi_p, int ma_p, ENUM_MA_TYPE ma_m, ENUM_APPLIED_VOLUME vol_t);
|
||||||
void Calculate(int rates_total, const double &open[], const double &high[], const double &low[], const double &close[], const long &tick_volume[], const long &volume[],
|
|
||||||
|
//--- Updated: Accepts prev_calculated
|
||||||
|
void Calculate(int rates_total, int prev_calculated, const double &open[], const double &high[], const double &low[], const double &close[], const long &tick_volume[], const long &volume[],
|
||||||
double &mfi_buffer[], double &signal_buffer[]);
|
double &mfi_buffer[], double &signal_buffer[]);
|
||||||
};
|
};
|
||||||
|
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
//| CMFICalculator: Initialization |
|
//| Init |
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
bool CMFICalculator::Init(int mfi_p, int ma_p, ENUM_MA_METHOD ma_m, ENUM_APPLIED_VOLUME vol_t)
|
bool CMFICalculator::Init(int mfi_p, int ma_p, ENUM_MA_TYPE ma_m, ENUM_APPLIED_VOLUME vol_t)
|
||||||
{
|
{
|
||||||
m_mfi_period = (mfi_p < 1) ? 1 : mfi_p;
|
m_mfi_period = (mfi_p < 1) ? 1 : mfi_p;
|
||||||
m_ma_period = (ma_p < 1) ? 1 : ma_p;
|
|
||||||
m_ma_method = ma_m;
|
|
||||||
m_volume_type = vol_t;
|
m_volume_type = vol_t;
|
||||||
|
|
||||||
|
// Initialize Signal Engine
|
||||||
|
if(!m_signal_engine.Init(ma_p, ma_m))
|
||||||
|
return false;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
//| CMFICalculator: Main Calculation Method (Shared Logic) |
|
//| Main Calculation (Optimized) |
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
void CMFICalculator::Calculate(int rates_total, const double &open[], const double &high[], const double &low[], const double &close[], const long &tick_volume[], const long &volume[],
|
void CMFICalculator::Calculate(int rates_total, int prev_calculated, const double &open[], const double &high[], const double &low[], const double &close[], const long &tick_volume[], const long &volume[],
|
||||||
double &mfi_buffer[], double &signal_buffer[])
|
double &mfi_buffer[], double &signal_buffer[])
|
||||||
{
|
{
|
||||||
if(rates_total <= m_mfi_period + m_ma_period)
|
if(rates_total <= m_mfi_period)
|
||||||
return;
|
|
||||||
//--- CORRECTED: Pass 'open' to PreparePriceSeries
|
|
||||||
if(!PreparePriceSeries(rates_total, open, high, low, close))
|
|
||||||
return;
|
return;
|
||||||
|
|
||||||
double pos_mf[], neg_mf[];
|
//--- 1. Determine Start Index
|
||||||
ArrayResize(pos_mf, rates_total);
|
int start_index;
|
||||||
ArrayResize(neg_mf, rates_total);
|
if(prev_calculated == 0)
|
||||||
|
start_index = 0;
|
||||||
|
else
|
||||||
|
start_index = prev_calculated - 1;
|
||||||
|
|
||||||
for(int i=1; i<rates_total; i++)
|
//--- 2. Resize Buffers
|
||||||
|
if(ArraySize(m_typical_price) != rates_total)
|
||||||
|
{
|
||||||
|
ArrayResize(m_typical_price, rates_total);
|
||||||
|
ArrayResize(m_pos_mf, rates_total);
|
||||||
|
ArrayResize(m_neg_mf, rates_total);
|
||||||
|
ArrayResize(m_mfi_buffer, rates_total);
|
||||||
|
}
|
||||||
|
|
||||||
|
//--- 3. Prepare Price (Optimized)
|
||||||
|
if(!PreparePriceSeries(rates_total, start_index, open, high, low, close))
|
||||||
|
return;
|
||||||
|
|
||||||
|
//--- 4. Calculate Money Flow (Incremental)
|
||||||
|
int loop_start_mf = MathMax(1, start_index);
|
||||||
|
|
||||||
|
for(int i = loop_start_mf; i < rates_total; i++)
|
||||||
{
|
{
|
||||||
double raw_mf = m_typical_price[i] * ((m_volume_type == VOLUME_TICK) ? tick_volume[i] : volume[i]);
|
double raw_mf = m_typical_price[i] * ((m_volume_type == VOLUME_TICK) ? tick_volume[i] : volume[i]);
|
||||||
|
|
||||||
if(m_typical_price[i] > m_typical_price[i-1])
|
if(m_typical_price[i] > m_typical_price[i-1])
|
||||||
pos_mf[i] = raw_mf;
|
{
|
||||||
|
m_pos_mf[i] = raw_mf;
|
||||||
|
m_neg_mf[i] = 0;
|
||||||
|
}
|
||||||
else
|
else
|
||||||
if(m_typical_price[i] < m_typical_price[i-1])
|
if(m_typical_price[i] < m_typical_price[i-1])
|
||||||
neg_mf[i] = raw_mf;
|
|
||||||
}
|
|
||||||
|
|
||||||
double sum_pos = 0, sum_neg = 0;
|
|
||||||
for(int i = 1; i < rates_total; i++)
|
|
||||||
{
|
|
||||||
sum_pos += pos_mf[i];
|
|
||||||
sum_neg += neg_mf[i];
|
|
||||||
if(i > m_mfi_period)
|
|
||||||
{
|
|
||||||
sum_pos -= pos_mf[i - m_mfi_period];
|
|
||||||
sum_neg -= neg_mf[i - m_mfi_period];
|
|
||||||
}
|
|
||||||
if(i >= m_mfi_period)
|
|
||||||
{
|
|
||||||
if(sum_neg > 0)
|
|
||||||
{
|
{
|
||||||
double ratio = sum_pos / sum_neg;
|
m_pos_mf[i] = 0;
|
||||||
mfi_buffer[i] = 100.0 - (100.0 / (1.0 + ratio));
|
m_neg_mf[i] = raw_mf;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
mfi_buffer[i] = 100.0;
|
{
|
||||||
}
|
m_pos_mf[i] = 0;
|
||||||
|
m_neg_mf[i] = 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int ma_start_pos = m_mfi_period + m_ma_period - 1;
|
//--- 5. Calculate MFI (Incremental Sliding Window)
|
||||||
for(int i = ma_start_pos; i < rates_total; i++)
|
int loop_start_mfi = MathMax(m_mfi_period, start_index);
|
||||||
|
|
||||||
|
// If full recalc, we need to handle the first value specially or just loop
|
||||||
|
if(prev_calculated == 0)
|
||||||
{
|
{
|
||||||
switch(m_ma_method)
|
// Initialize first few values
|
||||||
{
|
for(int i=0; i<m_mfi_period; i++)
|
||||||
case MODE_EMA:
|
m_mfi_buffer[i] = 50.0;
|
||||||
case MODE_SMMA:
|
|
||||||
if(i == ma_start_pos)
|
|
||||||
{
|
|
||||||
double sum=0;
|
|
||||||
for(int j=0; j<m_ma_period; j++)
|
|
||||||
sum+=mfi_buffer[i-j];
|
|
||||||
signal_buffer[i] = sum/m_ma_period;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if(m_ma_method == MODE_EMA)
|
|
||||||
{
|
|
||||||
double pr=2.0/(m_ma_period+1.0);
|
|
||||||
signal_buffer[i] = mfi_buffer[i]*pr + signal_buffer[i-1]*(1.0-pr);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
signal_buffer[i] = (signal_buffer[i-1]*(m_ma_period-1)+mfi_buffer[i])/m_ma_period;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case MODE_LWMA:
|
|
||||||
{double lwma_sum=0, weight_sum=0; for(int j=0; j<m_ma_period; j++) {int weight=m_ma_period-j; lwma_sum+=mfi_buffer[i-j]*weight; weight_sum+=weight;} if(weight_sum>0) signal_buffer[i]=lwma_sum/weight_sum;}
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
{double sum=0; for(int j=0; j<m_ma_period; j++) sum+=mfi_buffer[i-j]; signal_buffer[i] = sum/m_ma_period;}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for(int i = loop_start_mfi; i < rates_total; i++)
|
||||||
|
{
|
||||||
|
double sum_pos = 0;
|
||||||
|
double sum_neg = 0;
|
||||||
|
|
||||||
|
// Sum over the lookback period
|
||||||
|
// Optimization: We could use a running sum, but for MFI period (usually 14), a loop is fast enough and safer.
|
||||||
|
for(int j = 0; j < m_mfi_period; j++)
|
||||||
|
{
|
||||||
|
sum_pos += m_pos_mf[i-j];
|
||||||
|
sum_neg += m_neg_mf[i-j];
|
||||||
|
}
|
||||||
|
|
||||||
|
if(sum_neg > 0)
|
||||||
|
{
|
||||||
|
double ratio = sum_pos / sum_neg;
|
||||||
|
m_mfi_buffer[i] = 100.0 - (100.0 / (1.0 + ratio));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
m_mfi_buffer[i] = 100.0;
|
||||||
|
}
|
||||||
|
|
||||||
|
//--- 6. Calculate Signal Line (Using Engine)
|
||||||
|
// MFI is valid from index: m_mfi_period
|
||||||
|
int mfi_offset = m_mfi_period;
|
||||||
|
m_signal_engine.CalculateOnArray(rates_total, prev_calculated, m_mfi_buffer, signal_buffer, mfi_offset);
|
||||||
|
|
||||||
|
//--- 7. Copy MFI to Output
|
||||||
|
ArrayCopy(mfi_buffer, m_mfi_buffer, 0, 0, rates_total);
|
||||||
}
|
}
|
||||||
|
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
//| CMFICalculator: Prepares the standard source price series. |
|
//| Prepare Price (Standard - Optimized) |
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
bool CMFICalculator::PreparePriceSeries(int rates_total, const double &open[], const double &high[], const double &low[], const double &close[])
|
bool CMFICalculator::PreparePriceSeries(int rates_total, int start_index, const double &open[], const double &high[], const double &low[], const double &close[])
|
||||||
{
|
{
|
||||||
ArrayResize(m_typical_price, rates_total);
|
for(int i = start_index; i < rates_total; i++)
|
||||||
for(int i=0; i<rates_total; i++)
|
|
||||||
m_typical_price[i] = (high[i] + low[i] + close[i]) / 3.0;
|
m_typical_price[i] = (high[i] + low[i] + close[i]) / 3.0;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
//+==================================================================+
|
//+==================================================================+
|
||||||
//| |
|
//| CLASS 2: CMFICalculator_HA (Heikin Ashi) |
|
||||||
//| CLASS 2: CMFICalculator_HA (Heikin Ashi) |
|
|
||||||
//| |
|
|
||||||
//+==================================================================+
|
//+==================================================================+
|
||||||
class CMFICalculator_HA : public CMFICalculator
|
class CMFICalculator_HA : public CMFICalculator
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
CHeikinAshi_Calculator m_ha_calculator;
|
CHeikinAshi_Calculator m_ha_calculator;
|
||||||
|
// Internal HA buffers
|
||||||
|
double m_ha_open[], m_ha_high[], m_ha_low[], m_ha_close[];
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
//--- CORRECTED: Added 'open' to signature
|
virtual bool PreparePriceSeries(int rates_total, int start_index, const double &open[], const double &high[], const double &low[], const double &close[]) override;
|
||||||
virtual bool PreparePriceSeries(int rates_total, const double &open[], const double &high[], const double &low[], const double &close[]) override;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
//| CMFICalculator_HA: Prepares the Heikin Ashi source price. |
|
//| Prepare Price (Heikin Ashi - Optimized) |
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
bool CMFICalculator_HA::PreparePriceSeries(int rates_total, const double &open[], const double &high[], const double &low[], const double &close[])
|
bool CMFICalculator_HA::PreparePriceSeries(int rates_total, int start_index, const double &open[], const double &high[], const double &low[], const double &close[])
|
||||||
{
|
{
|
||||||
double ha_open[], ha_high[], ha_low[], ha_close[];
|
// Resize internal HA buffers
|
||||||
ArrayResize(ha_open, rates_total);
|
if(ArraySize(m_ha_open) != rates_total)
|
||||||
ArrayResize(ha_high, rates_total);
|
{
|
||||||
ArrayResize(ha_low, rates_total);
|
ArrayResize(m_ha_open, rates_total);
|
||||||
ArrayResize(ha_close, rates_total);
|
ArrayResize(m_ha_high, rates_total);
|
||||||
//--- CORRECTED: Pass 'open' to the HA calculator
|
ArrayResize(m_ha_low, rates_total);
|
||||||
m_ha_calculator.Calculate(rates_total, open, high, low, close, ha_open, ha_high, ha_low, ha_close);
|
ArrayResize(m_ha_close, rates_total);
|
||||||
|
}
|
||||||
|
|
||||||
ArrayResize(m_typical_price, rates_total);
|
//--- STRICT CALL: Use the optimized 10-param HA calculation
|
||||||
for(int i=0; i<rates_total; i++)
|
m_ha_calculator.Calculate(rates_total, start_index, open, high, low, close,
|
||||||
m_typical_price[i] = (ha_high[i] + ha_low[i] + ha_close[i]) / 3.0;
|
m_ha_open, m_ha_high, m_ha_low, m_ha_close);
|
||||||
|
|
||||||
|
//--- Copy to typical price (Optimized loop)
|
||||||
|
for(int i = start_index; i < rates_total; i++)
|
||||||
|
m_typical_price[i] = (m_ha_high[i] + m_ha_low[i] + m_ha_close[i]) / 3.0;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
|
|||||||
Reference in New Issue
Block a user