mirror of
https://github.com/softwaredevelop/mql5.git
synced 2026-08-08 01:57:44 +00:00
refactor(indicators): Optimized for incremental calculation
This commit is contained in:
@@ -1,209 +1,211 @@
|
||||
//+------------------------------------------------------------------+
|
||||
//| TDI_CMO_Calculator.mqh |
|
||||
//| Calculation engine for TDI based on CMO. |
|
||||
//| VERSION 2.00: Optimized for incremental calculation. |
|
||||
//| 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
|
||||
{
|
||||
protected:
|
||||
int m_cmo_period, m_price_period, m_signal_period, m_base_period;
|
||||
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:
|
||||
CTDICMOCalculator(void) {};
|
||||
virtual ~CTDICMOCalculator(void) {};
|
||||
CTDICMOCalculator(void);
|
||||
virtual ~CTDICMOCalculator(void);
|
||||
|
||||
bool Init(int cmo_p, int price_p, int signal_p, int base_p, double dev);
|
||||
void Calculate(int rates_total, ENUM_APPLIED_PRICE price_type, const double &open[], const double &high[], const double &low[], const double &close[],
|
||||
//--- Init now takes MA types (optional, default to SMA for classic TDI)
|
||||
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 &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_price_period = (price_p < 1) ? 1 : price_p;
|
||||
m_signal_period = (signal_p < 1) ? 1 : signal_p;
|
||||
m_base_period = (base_p < 1) ? 1 : base_p;
|
||||
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;
|
||||
}
|
||||
|
||||
//+------------------------------------------------------------------+
|
||||
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 &upper_band_out[], double &lower_band_out[])
|
||||
{
|
||||
// Minimum bars check
|
||||
if(rates_total <= m_cmo_period + m_base_period)
|
||||
return;
|
||||
if(!PreparePriceSeries(rates_total, price_type, open, high, low, close))
|
||||
if(CheckPointer(m_cmo_calculator) == POINTER_INVALID)
|
||||
return;
|
||||
|
||||
double cmo_rescaled[];
|
||||
ArrayResize(cmo_rescaled, rates_total);
|
||||
int start_index = (prev_calculated == 0) ? 0 : prev_calculated - 1;
|
||||
|
||||
//--- STEP 1: Calculate CMO and rescale it to 0-100 range
|
||||
for(int i = m_cmo_period; i < rates_total; i++)
|
||||
// Resize Buffers
|
||||
if(ArraySize(m_cmo_buffer) != rates_total)
|
||||
{
|
||||
double sum_up = 0.0, sum_down = 0.0;
|
||||
for(int j = 0; j < m_cmo_period; j++)
|
||||
{
|
||||
double diff = m_price[i - j] - m_price[i - j - 1];
|
||||
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;
|
||||
ArrayResize(m_cmo_buffer, rates_total);
|
||||
ArrayResize(m_cmo_rescaled, rates_total);
|
||||
ArrayResize(m_price_line, rates_total);
|
||||
ArrayResize(m_base_line, rates_total);
|
||||
}
|
||||
|
||||
//--- STEP 2: Calculate Price Line (SMA on Rescaled CMO)
|
||||
for(int i = m_cmo_period + m_price_period - 2; i < rates_total; i++)
|
||||
if(!PreparePriceSeries(rates_total, start_index, price_type, open, high, low, close))
|
||||
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;
|
||||
for(int j=0; j<m_price_period; j++)
|
||||
sum+=cmo_rescaled[i-j];
|
||||
price_line_out[i]=sum/m_price_period;
|
||||
// CMO is -100 to 100. Rescale to 0 to 100.
|
||||
m_cmo_rescaled[i] = (m_cmo_buffer[i] + 100.0) / 2.0;
|
||||
}
|
||||
|
||||
//--- STEP 3: Calculate Signal Line (SMA on Price Line)
|
||||
for(int i = m_cmo_period + m_price_period + m_signal_period - 3; i < rates_total; i++)
|
||||
//--- 3. Calculate Price Line (MA on Rescaled CMO)
|
||||
// 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;
|
||||
for(int j=0; j<m_signal_period; j++)
|
||||
sum+=price_line_out[i-j];
|
||||
signal_line_out[i]=sum/m_signal_period;
|
||||
ArrayInitialize(upper_band_out, EMPTY_VALUE);
|
||||
ArrayInitialize(lower_band_out, EMPTY_VALUE);
|
||||
}
|
||||
|
||||
//--- STEP 4: Calculate Base Line (SMA on Price Line)
|
||||
for(int i = m_cmo_period + m_price_period + m_base_period - 3; i < rates_total; i++)
|
||||
for(int i = loop_start_bands; i < rates_total; i++)
|
||||
{
|
||||
double sum=0;
|
||||
for(int j=0; j<m_base_period; j++)
|
||||
sum+=price_line_out[i-j];
|
||||
base_line_out[i]=sum/m_base_period;
|
||||
}
|
||||
if(m_base_line[i] == EMPTY_VALUE)
|
||||
continue;
|
||||
|
||||
//--- 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 base_line_ma_on_cmo = 0;
|
||||
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;
|
||||
|
||||
// Standard Deviation of Rescaled CMO around the Base Line
|
||||
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);
|
||||
|
||||
upper_band_out[i] = base_line_out[i] + m_std_dev * std_dev;
|
||||
lower_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] = 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);
|
||||
switch(price_type)
|
||||
{
|
||||
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;
|
||||
}
|
||||
// This method is just a placeholder for the base class.
|
||||
// The CMO calculator handles its own data preparation internally.
|
||||
return true;
|
||||
}
|
||||
//+------------------------------------------------------------------+
|
||||
//| |
|
||||
//+------------------------------------------------------------------+
|
||||
|
||||
//+==================================================================+
|
||||
//| CLASS 2: CTDICMOCalculator_HA (Heikin Ashi) |
|
||||
//+==================================================================+
|
||||
class CTDICMOCalculator_HA : public CTDICMOCalculator
|
||||
{
|
||||
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;
|
||||
public:
|
||||
CTDICMOCalculator_HA(void);
|
||||
};
|
||||
|
||||
//+------------------------------------------------------------------+
|
||||
//| |
|
||||
//+------------------------------------------------------------------+
|
||||
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[];
|
||||
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);
|
||||
|
||||
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;
|
||||
if(CheckPointer(m_cmo_calculator) != POINTER_INVALID)
|
||||
delete m_cmo_calculator;
|
||||
// Use HA version of CMO calculator
|
||||
m_cmo_calculator = new CCMOCalculator_HA();
|
||||
}
|
||||
//+------------------------------------------------------------------+
|
||||
|
||||
Reference in New Issue
Block a user