mirror of
https://github.com/softwaredevelop/mql5.git
synced 2026-08-06 17:17:45 +00:00
210 lines
8.1 KiB
Plaintext
210 lines
8.1 KiB
Plaintext
//+------------------------------------------------------------------+
|
|
//| TDI_CMO_Calculator.mqh |
|
|
//| Calculation engine for TDI based on CMO. |
|
|
//| Copyright 2025, xxxxxxxx |
|
|
//+------------------------------------------------------------------+
|
|
#property copyright "Copyright 2025, xxxxxxxx"
|
|
|
|
#include <MyIncludes\HeikinAshi_Tools.mqh>
|
|
|
|
//+==================================================================+
|
|
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[]);
|
|
|
|
public:
|
|
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[],
|
|
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)
|
|
{
|
|
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;
|
|
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[],
|
|
double &price_line_out[], double &signal_line_out[], double &base_line_out[],
|
|
double &upper_band_out[], double &lower_band_out[])
|
|
{
|
|
if(rates_total <= m_cmo_period + m_base_period)
|
|
return;
|
|
if(!PreparePriceSeries(rates_total, price_type, open, high, low, close))
|
|
return;
|
|
|
|
double cmo_rescaled[];
|
|
ArrayResize(cmo_rescaled, rates_total);
|
|
|
|
//--- STEP 1: Calculate CMO and rescale it to 0-100 range
|
|
for(int i = m_cmo_period; i < rates_total; i++)
|
|
{
|
|
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;
|
|
}
|
|
|
|
//--- STEP 2: Calculate Price Line (SMA on Rescaled CMO)
|
|
for(int i = m_cmo_period + m_price_period - 2; 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;
|
|
}
|
|
|
|
//--- 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++)
|
|
{
|
|
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;
|
|
}
|
|
|
|
//--- 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++)
|
|
{
|
|
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;
|
|
}
|
|
|
|
//--- 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;
|
|
|
|
for(int j = 0; j < m_base_period; j++)
|
|
sum_sq += MathPow(cmo_rescaled[i-j] - base_line_ma_on_cmo, 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;
|
|
}
|
|
}
|
|
|
|
// ... (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[])
|
|
{
|
|
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;
|
|
}
|
|
return true;
|
|
}
|
|
//+------------------------------------------------------------------+
|
|
//| |
|
|
//+------------------------------------------------------------------+
|
|
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;
|
|
};
|
|
//+------------------------------------------------------------------+
|
|
//| |
|
|
//+------------------------------------------------------------------+
|
|
bool CTDICMOCalculator_HA::PreparePriceSeries(int rates_total, 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[];
|
|
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;
|
|
}
|
|
//+------------------------------------------------------------------+
|