mirror of
https://github.com/softwaredevelop/mql5.git
synced 2026-08-24 01:38:06 +00:00
refactor(indicators): Optimized for incremental calculation
This commit is contained in:
@@ -1,209 +1,211 @@
|
|||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
//| TDI_CMO_Calculator.mqh |
|
//| TDI_CMO_Calculator.mqh |
|
||||||
//| Calculation engine for TDI based on CMO. |
|
//| Calculation engine for TDI based on CMO. |
|
||||||
|
//| 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\CMO_Calculator.mqh>
|
||||||
|
#include <MyIncludes\MovingAverage_Engine.mqh>
|
||||||
|
|
||||||
|
//+==================================================================+
|
||||||
|
//| CLASS 1: CTDICMOCalculator (Base Class) |
|
||||||
//+==================================================================+
|
//+==================================================================+
|
||||||
class CTDICMOCalculator
|
class CTDICMOCalculator
|
||||||
{
|
{
|
||||||
protected:
|
protected:
|
||||||
int m_cmo_period, m_price_period, m_signal_period, m_base_period;
|
int m_cmo_period, m_price_period, m_signal_period, m_base_period;
|
||||||
double m_std_dev;
|
double m_std_dev;
|
||||||
double m_price[];
|
|
||||||
|
|
||||||
virtual bool PreparePriceSeries(int rates_total, ENUM_APPLIED_PRICE price_type, const double &open[], const double &high[], const double &low[], const double &close[]);
|
//--- Engines
|
||||||
|
CCMOCalculator *m_cmo_calculator;
|
||||||
|
CMovingAverageCalculator m_price_line_engine;
|
||||||
|
CMovingAverageCalculator m_signal_line_engine;
|
||||||
|
CMovingAverageCalculator m_base_line_engine;
|
||||||
|
|
||||||
|
//--- Persistent Buffers
|
||||||
|
double m_cmo_buffer[];
|
||||||
|
double m_cmo_rescaled[];
|
||||||
|
double m_price_line[];
|
||||||
|
double m_base_line[];
|
||||||
|
|
||||||
|
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:
|
||||||
CTDICMOCalculator(void) {};
|
CTDICMOCalculator(void);
|
||||||
virtual ~CTDICMOCalculator(void) {};
|
virtual ~CTDICMOCalculator(void);
|
||||||
|
|
||||||
bool Init(int cmo_p, int price_p, int signal_p, int base_p, double dev);
|
//--- Init now takes MA types (optional, default to SMA for classic TDI)
|
||||||
void Calculate(int rates_total, ENUM_APPLIED_PRICE price_type, const double &open[], const double &high[], const double &low[], const double &close[],
|
bool Init(int cmo_p, int price_p, int signal_p, int base_p, double dev, ENUM_MA_TYPE ma_type = SMA);
|
||||||
|
|
||||||
|
//--- 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 &price_line_out[], double &signal_line_out[], double &base_line_out[],
|
double &price_line_out[], double &signal_line_out[], double &base_line_out[],
|
||||||
double &upper_band_out[], double &lower_band_out[]);
|
double &upper_band_out[], double &lower_band_out[]);
|
||||||
};
|
};
|
||||||
|
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
bool CTDICMOCalculator::Init(int cmo_p, int price_p, int signal_p, int base_p, double dev)
|
//| Constructor |
|
||||||
|
//+------------------------------------------------------------------+
|
||||||
|
CTDICMOCalculator::CTDICMOCalculator(void)
|
||||||
|
{
|
||||||
|
m_cmo_calculator = new CCMOCalculator();
|
||||||
|
}
|
||||||
|
|
||||||
|
//+------------------------------------------------------------------+
|
||||||
|
//| Destructor |
|
||||||
|
//+------------------------------------------------------------------+
|
||||||
|
CTDICMOCalculator::~CTDICMOCalculator(void)
|
||||||
|
{
|
||||||
|
if(CheckPointer(m_cmo_calculator) != POINTER_INVALID)
|
||||||
|
delete m_cmo_calculator;
|
||||||
|
}
|
||||||
|
|
||||||
|
//+------------------------------------------------------------------+
|
||||||
|
//| Init |
|
||||||
|
//+------------------------------------------------------------------+
|
||||||
|
bool CTDICMOCalculator::Init(int cmo_p, int price_p, int signal_p, int base_p, double dev, ENUM_MA_TYPE ma_type)
|
||||||
{
|
{
|
||||||
m_cmo_period = (cmo_p < 1) ? 1 : cmo_p;
|
m_cmo_period = (cmo_p < 1) ? 1 : cmo_p;
|
||||||
m_price_period = (price_p < 1) ? 1 : price_p;
|
m_price_period = (price_p < 1) ? 1 : price_p;
|
||||||
m_signal_period = (signal_p < 1) ? 1 : signal_p;
|
m_signal_period = (signal_p < 1) ? 1 : signal_p;
|
||||||
m_base_period = (base_p < 1) ? 1 : base_p;
|
m_base_period = (base_p < 1) ? 1 : base_p;
|
||||||
m_std_dev = (dev <= 0) ? 1.618 : dev;
|
m_std_dev = (dev <= 0) ? 1.618 : dev;
|
||||||
|
|
||||||
|
if(CheckPointer(m_cmo_calculator) == POINTER_INVALID)
|
||||||
|
return false;
|
||||||
|
if(!m_cmo_calculator.Init(m_cmo_period))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
// Initialize MA Engines (Classic TDI uses SMA, but we allow override)
|
||||||
|
if(!m_price_line_engine.Init(m_price_period, ma_type))
|
||||||
|
return false;
|
||||||
|
if(!m_signal_line_engine.Init(m_signal_period, ma_type))
|
||||||
|
return false;
|
||||||
|
if(!m_base_line_engine.Init(m_base_period, ma_type))
|
||||||
|
return false;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
void CTDICMOCalculator::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 CTDICMOCalculator::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 &price_line_out[], double &signal_line_out[], double &base_line_out[],
|
double &price_line_out[], double &signal_line_out[], double &base_line_out[],
|
||||||
double &upper_band_out[], double &lower_band_out[])
|
double &upper_band_out[], double &lower_band_out[])
|
||||||
{
|
{
|
||||||
|
// Minimum bars check
|
||||||
if(rates_total <= m_cmo_period + m_base_period)
|
if(rates_total <= m_cmo_period + m_base_period)
|
||||||
return;
|
return;
|
||||||
if(!PreparePriceSeries(rates_total, price_type, open, high, low, close))
|
if(CheckPointer(m_cmo_calculator) == POINTER_INVALID)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
double cmo_rescaled[];
|
int start_index = (prev_calculated == 0) ? 0 : prev_calculated - 1;
|
||||||
ArrayResize(cmo_rescaled, rates_total);
|
|
||||||
|
|
||||||
//--- STEP 1: Calculate CMO and rescale it to 0-100 range
|
// Resize Buffers
|
||||||
for(int i = m_cmo_period; i < rates_total; i++)
|
if(ArraySize(m_cmo_buffer) != rates_total)
|
||||||
{
|
{
|
||||||
double sum_up = 0.0, sum_down = 0.0;
|
ArrayResize(m_cmo_buffer, rates_total);
|
||||||
for(int j = 0; j < m_cmo_period; j++)
|
ArrayResize(m_cmo_rescaled, rates_total);
|
||||||
{
|
ArrayResize(m_price_line, rates_total);
|
||||||
double diff = m_price[i - j] - m_price[i - j - 1];
|
ArrayResize(m_base_line, rates_total);
|
||||||
if(diff > 0.0)
|
|
||||||
sum_up += diff;
|
|
||||||
else
|
|
||||||
sum_down += (-diff);
|
|
||||||
}
|
|
||||||
double total_sum = sum_up + sum_down;
|
|
||||||
double raw_cmo = (total_sum == 0.0) ? 0.0 : 100.0 * (sum_up - sum_down) / total_sum;
|
|
||||||
|
|
||||||
//--- CRITICAL: Rescale CMO from [-100, 100] to [0, 100]
|
|
||||||
cmo_rescaled[i] = (raw_cmo + 100.0) / 2.0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//--- STEP 2: Calculate Price Line (SMA on Rescaled CMO)
|
if(!PreparePriceSeries(rates_total, start_index, price_type, open, high, low, close))
|
||||||
for(int i = m_cmo_period + m_price_period - 2; i < rates_total; i++)
|
return;
|
||||||
|
|
||||||
|
//--- 1. Calculate CMO (Incremental)
|
||||||
|
m_cmo_calculator.Calculate(rates_total, prev_calculated, price_type, open, high, low, close, m_cmo_buffer);
|
||||||
|
|
||||||
|
//--- 2. Rescale CMO to 0-100 range
|
||||||
|
int loop_start_cmo = MathMax(m_cmo_period, start_index);
|
||||||
|
for(int i = loop_start_cmo; i < rates_total; i++)
|
||||||
{
|
{
|
||||||
double sum=0;
|
// CMO is -100 to 100. Rescale to 0 to 100.
|
||||||
for(int j=0; j<m_price_period; j++)
|
m_cmo_rescaled[i] = (m_cmo_buffer[i] + 100.0) / 2.0;
|
||||||
sum+=cmo_rescaled[i-j];
|
|
||||||
price_line_out[i]=sum/m_price_period;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//--- STEP 3: Calculate Signal Line (SMA on Price Line)
|
//--- 3. Calculate Price Line (MA on Rescaled CMO)
|
||||||
for(int i = m_cmo_period + m_price_period + m_signal_period - 3; i < rates_total; i++)
|
// Offset: m_cmo_period
|
||||||
|
m_price_line_engine.CalculateOnArray(rates_total, prev_calculated, m_cmo_rescaled, m_price_line, m_cmo_period);
|
||||||
|
ArrayCopy(price_line_out, m_price_line, 0, 0, rates_total);
|
||||||
|
|
||||||
|
//--- 4. Calculate Signal Line (MA on Price Line)
|
||||||
|
// Offset: m_cmo_period + m_price_period - 1
|
||||||
|
int signal_offset = m_cmo_period + m_price_period - 1;
|
||||||
|
m_signal_line_engine.CalculateOnArray(rates_total, prev_calculated, m_price_line, signal_line_out, signal_offset);
|
||||||
|
|
||||||
|
//--- 5. Calculate Base Line (MA on Price Line)
|
||||||
|
// Offset: same as signal line start (based on Price Line)
|
||||||
|
// But Base Line usually has longer period, so it starts later validly.
|
||||||
|
// The engine handles validity based on period.
|
||||||
|
m_base_line_engine.CalculateOnArray(rates_total, prev_calculated, m_price_line, m_base_line, signal_offset);
|
||||||
|
ArrayCopy(base_line_out, m_base_line, 0, 0, rates_total);
|
||||||
|
|
||||||
|
//--- 6. Calculate Volatility Bands (Bollinger Bands on Base Line)
|
||||||
|
// Bands are calculated using StdDev of Price Line around Base Line?
|
||||||
|
// Or StdDev of Rescaled CMO around Base Line?
|
||||||
|
// Original code used: StdDev of Rescaled CMO around Base Line (where Base Line is MA of Price Line).
|
||||||
|
// Wait, original code:
|
||||||
|
// base_line_ma_on_cmo = sum_cmo / m_base_period; (This IS the Base Line value at i)
|
||||||
|
// sum_sq += MathPow(cmo_rescaled[i-j] - base_line_ma_on_cmo, 2);
|
||||||
|
// So it calculates StdDev of CMO around the Base Line.
|
||||||
|
|
||||||
|
int bands_start = m_cmo_period + m_base_period - 1; // Approx start
|
||||||
|
int loop_start_bands = MathMax(bands_start, start_index);
|
||||||
|
|
||||||
|
if(prev_calculated == 0)
|
||||||
{
|
{
|
||||||
double sum=0;
|
ArrayInitialize(upper_band_out, EMPTY_VALUE);
|
||||||
for(int j=0; j<m_signal_period; j++)
|
ArrayInitialize(lower_band_out, EMPTY_VALUE);
|
||||||
sum+=price_line_out[i-j];
|
|
||||||
signal_line_out[i]=sum/m_signal_period;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//--- STEP 4: Calculate Base Line (SMA on Price Line)
|
for(int i = loop_start_bands; i < rates_total; i++)
|
||||||
for(int i = m_cmo_period + m_price_period + m_base_period - 3; i < rates_total; i++)
|
|
||||||
{
|
{
|
||||||
double sum=0;
|
if(m_base_line[i] == EMPTY_VALUE)
|
||||||
for(int j=0; j<m_base_period; j++)
|
continue;
|
||||||
sum+=price_line_out[i-j];
|
|
||||||
base_line_out[i]=sum/m_base_period;
|
|
||||||
}
|
|
||||||
|
|
||||||
//--- STEP 5: Calculate Volatility Bands (Bollinger Bands on Base Line, using Rescaled CMO data for StdDev)
|
|
||||||
int bands_start = m_cmo_period + m_base_period - 2;
|
|
||||||
for(int i = bands_start; i < rates_total; i++)
|
|
||||||
{
|
|
||||||
double std_dev = 0, sum_sq = 0;
|
double std_dev = 0, sum_sq = 0;
|
||||||
double base_line_ma_on_cmo = 0;
|
// Standard Deviation of Rescaled CMO around the Base Line
|
||||||
double sum_cmo = 0;
|
|
||||||
for(int j=0; j<m_base_period; j++)
|
|
||||||
sum_cmo += cmo_rescaled[i-j];
|
|
||||||
base_line_ma_on_cmo = sum_cmo / m_base_period;
|
|
||||||
|
|
||||||
for(int j = 0; j < m_base_period; j++)
|
for(int j = 0; j < m_base_period; j++)
|
||||||
sum_sq += MathPow(cmo_rescaled[i-j] - base_line_ma_on_cmo, 2);
|
sum_sq += MathPow(m_cmo_rescaled[i-j] - m_base_line[i], 2);
|
||||||
|
|
||||||
std_dev = MathSqrt(sum_sq / m_base_period);
|
std_dev = MathSqrt(sum_sq / m_base_period);
|
||||||
|
|
||||||
upper_band_out[i] = base_line_out[i] + m_std_dev * std_dev;
|
upper_band_out[i] = m_base_line[i] + m_std_dev * std_dev;
|
||||||
lower_band_out[i] = base_line_out[i] - m_std_dev * std_dev;
|
lower_band_out[i] = m_base_line[i] - m_std_dev * std_dev;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ... (A PreparePriceSeries és a _HA osztály PONTOSAN UGYANAZ MARAD, mint a TDI_Calculator-ban) ...
|
//+------------------------------------------------------------------+
|
||||||
bool CTDICMOCalculator::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 CTDICMOCalculator::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);
|
// This method is just a placeholder for the base class.
|
||||||
switch(price_type)
|
// The CMO calculator handles its own data preparation internally.
|
||||||
{
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
//+------------------------------------------------------------------+
|
|
||||||
//| |
|
//+==================================================================+
|
||||||
//+------------------------------------------------------------------+
|
//| CLASS 2: CTDICMOCalculator_HA (Heikin Ashi) |
|
||||||
|
//+==================================================================+
|
||||||
class CTDICMOCalculator_HA : public CTDICMOCalculator
|
class CTDICMOCalculator_HA : public CTDICMOCalculator
|
||||||
{
|
{
|
||||||
private:
|
public:
|
||||||
CHeikinAshi_Calculator m_ha_calculator;
|
CTDICMOCalculator_HA(void);
|
||||||
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;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
//| |
|
//| |
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
bool CTDICMOCalculator_HA::PreparePriceSeries(int rates_total, ENUM_APPLIED_PRICE price_type, const double &open[], const double &high[], const double &low[], const double &close[])
|
CTDICMOCalculator_HA::CTDICMOCalculator_HA(void)
|
||||||
{
|
{
|
||||||
double ha_open[], ha_high[], ha_low[], ha_close[];
|
if(CheckPointer(m_cmo_calculator) != POINTER_INVALID)
|
||||||
ArrayResize(ha_open, rates_total);
|
delete m_cmo_calculator;
|
||||||
ArrayResize(ha_high, rates_total);
|
// Use HA version of CMO calculator
|
||||||
ArrayResize(ha_low, rates_total);
|
m_cmo_calculator = new CCMOCalculator_HA();
|
||||||
ArrayResize(ha_close, rates_total);
|
|
||||||
m_ha_calculator.Calculate(rates_total, open, high, low, close, ha_open, ha_high, ha_low, ha_close);
|
|
||||||
|
|
||||||
ArrayResize(m_price, rates_total);
|
|
||||||
switch(price_type)
|
|
||||||
{
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
|
|||||||
Reference in New Issue
Block a user