mirror of
https://github.com/softwaredevelop/mql5.git
synced 2026-08-22 08:48:05 +00:00
refactor(indicators): Optimized for incremental calculation
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
//| Fourier_Series_Calculator.mqh |
|
//| Fourier_Series_Calculator.mqh |
|
||||||
//| Calculation engine for the John Ehlers' Fourier Series. |
|
//| VERSION 2.00: Optimized for incremental calculation. |
|
||||||
//| Copyright 2025, xxxxxxxx |
|
//| Copyright 2025, xxxxxxxx |
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
#property copyright "Copyright 2025, xxxxxxxx"
|
#property copyright "Copyright 2025, xxxxxxxx"
|
||||||
@@ -8,33 +8,40 @@
|
|||||||
#include <MyIncludes\HeikinAshi_Tools.mqh>
|
#include <MyIncludes\HeikinAshi_Tools.mqh>
|
||||||
|
|
||||||
//+==================================================================+
|
//+==================================================================+
|
||||||
//| |
|
//| CLASS 1: CFourierSeriesCalculator |
|
||||||
//| CLASS 1: CFourierSeriesCalculator (Base Class) |
|
|
||||||
//| |
|
|
||||||
//+==================================================================+
|
//+==================================================================+
|
||||||
class CFourierSeriesCalculator
|
class CFourierSeriesCalculator
|
||||||
{
|
{
|
||||||
protected:
|
protected:
|
||||||
int m_period;
|
int m_period;
|
||||||
double m_bandwidth;
|
double m_bandwidth;
|
||||||
|
|
||||||
|
//--- Persistent Buffers
|
||||||
double m_price[];
|
double m_price[];
|
||||||
|
double m_bp1[], m_bp2[], m_bp3[];
|
||||||
|
double m_q1[], m_q2[], m_q3[];
|
||||||
|
|
||||||
// Filter coefficients
|
// Filter coefficients
|
||||||
double L1, G1, S1;
|
double L1, G1, S1;
|
||||||
double L2, G2, S2;
|
double L2, G2, S2;
|
||||||
double L3, G3, S3;
|
double L3, G3, S3;
|
||||||
|
|
||||||
virtual bool PreparePriceSeries(int rates_total, ENUM_APPLIED_PRICE price_type, const double &open[], const double &high[], const double &low[], const double &close[]);
|
//--- 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:
|
public:
|
||||||
CFourierSeriesCalculator(void) {};
|
CFourierSeriesCalculator(void) {};
|
||||||
virtual ~CFourierSeriesCalculator(void) {};
|
virtual ~CFourierSeriesCalculator(void) {};
|
||||||
|
|
||||||
bool Init(int period, double bandwidth);
|
bool Init(int period, double bandwidth);
|
||||||
void Calculate(int rates_total, ENUM_APPLIED_PRICE price_type, const double &open[], const double &high[], const double &low[], const double &close[],
|
|
||||||
|
//--- Updated: Accepts prev_calculated
|
||||||
|
void Calculate(int rates_total, int prev_calculated, ENUM_APPLIED_PRICE price_type, const double &open[], const double &high[], const double &low[], const double &close[],
|
||||||
double &wave_buffer[], double &roc_buffer[]);
|
double &wave_buffer[], double &roc_buffer[]);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
//+------------------------------------------------------------------+
|
||||||
|
//| Init |
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
bool CFourierSeriesCalculator::Init(int period, double bandwidth)
|
bool CFourierSeriesCalculator::Init(int period, double bandwidth)
|
||||||
{
|
{
|
||||||
@@ -58,103 +65,140 @@ bool CFourierSeriesCalculator::Init(int period, double bandwidth)
|
|||||||
}
|
}
|
||||||
|
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
void CFourierSeriesCalculator::Calculate(int rates_total, ENUM_APPLIED_PRICE price_type, const double &open[], const double &high[], const double &low[], const double &close[],
|
//| Main Calculation (Optimized) |
|
||||||
|
//+------------------------------------------------------------------+
|
||||||
|
void CFourierSeriesCalculator::Calculate(int rates_total, int prev_calculated, ENUM_APPLIED_PRICE price_type, const double &open[], const double &high[], const double &low[], const double &close[],
|
||||||
double &wave_buffer[], double &roc_buffer[])
|
double &wave_buffer[], double &roc_buffer[])
|
||||||
{
|
{
|
||||||
if(rates_total < m_period * 2)
|
if(rates_total < m_period * 2)
|
||||||
return;
|
return;
|
||||||
if(!PreparePriceSeries(rates_total, price_type, open, high, low, close))
|
|
||||||
return;
|
|
||||||
|
|
||||||
// Intermediate buffers
|
int start_index;
|
||||||
double bp1[], bp2[], bp3[], q1[], q2[], q3[];
|
if(prev_calculated == 0)
|
||||||
ArrayResize(bp1, rates_total);
|
start_index = 0;
|
||||||
ArrayResize(bp2, rates_total);
|
else
|
||||||
ArrayResize(bp3, rates_total);
|
start_index = prev_calculated - 1;
|
||||||
ArrayResize(q1, rates_total);
|
|
||||||
ArrayResize(q2, rates_total);
|
|
||||||
ArrayResize(q3, rates_total);
|
|
||||||
|
|
||||||
// State variables for recursive filters
|
// Resize Buffers
|
||||||
double bp1_p1=0, bp1_p2=0, bp2_p1=0, bp2_p2=0, bp3_p1=0, bp3_p2=0;
|
if(ArraySize(m_price) != rates_total)
|
||||||
|
|
||||||
for(int i = 2; i < rates_total; i++)
|
|
||||||
{
|
{
|
||||||
// Step 2: Band-Pass Filters
|
ArrayResize(m_price, rates_total);
|
||||||
bp1[i] = 0.5 * (1.0 - S1) * (m_price[i] - m_price[i-2]) + L1 * (1.0 + S1) * bp1_p1 - S1 * bp1_p2;
|
ArrayResize(m_bp1, rates_total);
|
||||||
bp2[i] = 0.5 * (1.0 - S2) * (m_price[i] - m_price[i-2]) + L2 * (1.0 + S2) * bp2_p1 - S2 * bp2_p2;
|
ArrayResize(m_bp2, rates_total);
|
||||||
bp3[i] = 0.5 * (1.0 - S3) * (m_price[i] - m_price[i-2]) + L3 * (1.0 + S3) * bp3_p1 - S3 * bp3_p2;
|
ArrayResize(m_bp3, rates_total);
|
||||||
|
ArrayResize(m_q1, rates_total);
|
||||||
// Step 3: Quadrature Components
|
ArrayResize(m_q2, rates_total);
|
||||||
q1[i] = (m_period / (2.0 * M_PI)) * (bp1[i] - bp1[i-1]);
|
ArrayResize(m_q3, rates_total);
|
||||||
q2[i] = (m_period / (2.0 * M_PI)) * (bp2[i] - bp2[i-1]);
|
|
||||||
q3[i] = (m_period / (2.0 * M_PI)) * (bp3[i] - bp3[i-1]);
|
|
||||||
|
|
||||||
// Update state variables
|
|
||||||
bp1_p2 = bp1_p1;
|
|
||||||
bp1_p1 = bp1[i];
|
|
||||||
bp2_p2 = bp2_p1;
|
|
||||||
bp2_p1 = bp2[i];
|
|
||||||
bp3_p2 = bp3_p1;
|
|
||||||
bp3_p1 = bp3[i];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for(int i = m_period * 2 -1; i < rates_total; i++)
|
if(!PreparePriceSeries(rates_total, start_index, price_type, open, high, low, close))
|
||||||
|
return;
|
||||||
|
|
||||||
|
//--- 1. Calculate Band-Pass Filters and Quadrature (Incremental)
|
||||||
|
int loop_start_bp = MathMax(2, start_index);
|
||||||
|
|
||||||
|
if(loop_start_bp == 2)
|
||||||
|
{
|
||||||
|
// Initialize first few values
|
||||||
|
m_bp1[0]=0;
|
||||||
|
m_bp1[1]=0;
|
||||||
|
m_bp2[0]=0;
|
||||||
|
m_bp2[1]=0;
|
||||||
|
m_bp3[0]=0;
|
||||||
|
m_bp3[1]=0;
|
||||||
|
m_q1[0]=0;
|
||||||
|
m_q1[1]=0;
|
||||||
|
m_q2[0]=0;
|
||||||
|
m_q2[1]=0;
|
||||||
|
m_q3[0]=0;
|
||||||
|
m_q3[1]=0;
|
||||||
|
}
|
||||||
|
|
||||||
|
for(int i = loop_start_bp; i < rates_total; i++)
|
||||||
|
{
|
||||||
|
// Recursive calculation using persistent buffers [i-1], [i-2]
|
||||||
|
m_bp1[i] = 0.5 * (1.0 - S1) * (m_price[i] - m_price[i-2]) + L1 * (1.0 + S1) * m_bp1[i-1] - S1 * m_bp1[i-2];
|
||||||
|
m_bp2[i] = 0.5 * (1.0 - S2) * (m_price[i] - m_price[i-2]) + L2 * (1.0 + S2) * m_bp2[i-1] - S2 * m_bp2[i-2];
|
||||||
|
m_bp3[i] = 0.5 * (1.0 - S3) * (m_price[i] - m_price[i-2]) + L3 * (1.0 + S3) * m_bp3[i-1] - S3 * m_bp3[i-2];
|
||||||
|
|
||||||
|
m_q1[i] = (m_period / (2.0 * M_PI)) * (m_bp1[i] - m_bp1[i-1]);
|
||||||
|
m_q2[i] = (m_period / (2.0 * M_PI)) * (m_bp2[i] - m_bp2[i-1]);
|
||||||
|
m_q3[i] = (m_period / (2.0 * M_PI)) * (m_bp3[i] - m_bp3[i-1]);
|
||||||
|
}
|
||||||
|
|
||||||
|
//--- 2. Calculate Power and Synthesize Wave (Incremental)
|
||||||
|
int loop_start_wave = MathMax(m_period * 2 - 1, start_index);
|
||||||
|
|
||||||
|
for(int i = loop_start_wave; i < rates_total; i++)
|
||||||
{
|
{
|
||||||
// Step 4: Calculate Power
|
|
||||||
double p1=0, p2=0, p3=0;
|
double p1=0, p2=0, p3=0;
|
||||||
|
|
||||||
|
// Sum power over the period
|
||||||
for(int j = 0; j < m_period; j++)
|
for(int j = 0; j < m_period; j++)
|
||||||
{
|
{
|
||||||
p1 += bp1[i-j]*bp1[i-j] + q1[i-j]*q1[i-j];
|
p1 += m_bp1[i-j]*m_bp1[i-j] + m_q1[i-j]*m_q1[i-j];
|
||||||
p2 += bp2[i-j]*bp2[i-j] + q2[i-j]*q2[i-j];
|
p2 += m_bp2[i-j]*m_bp2[i-j] + m_q2[i-j]*m_q2[i-j];
|
||||||
p3 += bp3[i-j]*bp3[i-j] + q3[i-j]*q3[i-j];
|
p3 += m_bp3[i-j]*m_bp3[i-j] + m_q3[i-j]*m_q3[i-j];
|
||||||
}
|
}
|
||||||
|
|
||||||
// Step 5: Synthesize Wave
|
|
||||||
if(p1 > 0)
|
if(p1 > 0)
|
||||||
{
|
{
|
||||||
wave_buffer[i] = bp1[i] + sqrt(p2/p1)*bp2[i] + sqrt(p3/p1)*bp3[i];
|
wave_buffer[i] = m_bp1[i] + sqrt(p2/p1)*m_bp2[i] + sqrt(p3/p1)*m_bp3[i];
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
wave_buffer[i] = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Step 6: Optional ROC
|
// ROC
|
||||||
if(i > 1)
|
if(i > 1)
|
||||||
roc_buffer[i] = (m_period / (4.0 * M_PI)) * (wave_buffer[i] - wave_buffer[i-2]);
|
roc_buffer[i] = (m_period / (4.0 * M_PI)) * (wave_buffer[i] - wave_buffer[i-2]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
bool CFourierSeriesCalculator::PreparePriceSeries(int rates_total, ENUM_APPLIED_PRICE price_type, const double &open[], const double &high[], const double &low[], const double &close[])
|
//| Prepare Price (Standard - Optimized) |
|
||||||
|
//+------------------------------------------------------------------+
|
||||||
|
bool CFourierSeriesCalculator::PreparePriceSeries(int rates_total, int start_index, ENUM_APPLIED_PRICE price_type, const double &open[], const double &high[], const double &low[], const double &close[])
|
||||||
{
|
{
|
||||||
ArrayResize(m_price, rates_total);
|
for(int i = start_index; i < rates_total; i++)
|
||||||
// Ehlers' example uses Median Price
|
{
|
||||||
for(int i=0; i<rates_total; i++)
|
// Ehlers' example uses Median Price (HL/2)
|
||||||
m_price[i] = (high[i]+low[i])/2.0;
|
m_price[i] = (high[i] + low[i]) / 2.0;
|
||||||
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
//+------------------------------------------------------------------+
|
|
||||||
//| |
|
//+==================================================================+
|
||||||
//+------------------------------------------------------------------+
|
//| CLASS 2: CFourierSeriesCalculator_HA |
|
||||||
|
//+==================================================================+
|
||||||
class CFourierSeriesCalculator_HA : public CFourierSeriesCalculator
|
class CFourierSeriesCalculator_HA : public CFourierSeriesCalculator
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
CHeikinAshi_Calculator m_ha_calculator;
|
CHeikinAshi_Calculator m_ha_calculator;
|
||||||
|
double m_ha_open[], m_ha_high[], m_ha_low[], m_ha_close[];
|
||||||
protected:
|
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;
|
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 CFourierSeriesCalculator_HA::PreparePriceSeries(int rates_total, ENUM_APPLIED_PRICE price_type, const double &open[], const double &high[], const double &low[], const double &close[])
|
bool CFourierSeriesCalculator_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[])
|
||||||
{
|
{
|
||||||
double ha_open[], ha_high[], ha_low[], ha_close[];
|
if(ArraySize(m_ha_open) != rates_total)
|
||||||
ArrayResize(ha_open, rates_total);
|
{
|
||||||
ArrayResize(ha_high, rates_total);
|
ArrayResize(m_ha_open, rates_total);
|
||||||
ArrayResize(ha_low, rates_total);
|
ArrayResize(m_ha_high, rates_total);
|
||||||
ArrayResize(ha_close, rates_total);
|
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_price, rates_total);
|
}
|
||||||
for(int i=0; i<rates_total; i++)
|
m_ha_calculator.Calculate(rates_total, start_index, open, high, low, close, m_ha_open, m_ha_high, m_ha_low, m_ha_close);
|
||||||
m_price[i] = (ha_high[i]+ha_low[i])/2.0;
|
|
||||||
|
for(int i = start_index; i < rates_total; i++)
|
||||||
|
{
|
||||||
|
m_price[i] = (m_ha_high[i] + m_ha_low[i]) / 2.0;
|
||||||
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
|
|||||||
Reference in New Issue
Block a user