feat(indicators): Safety checks refined

This commit is contained in:
Toh4iem9
2026-01-08 10:53:52 +01:00
parent d75e62f1d4
commit 9fec0ee8f5
+207 -118
View File
@@ -1,102 +1,139 @@
//+------------------------------------------------------------------+
//| RSI_Adaptive_Calculator.mqh |
//| Engine for a variable-length RSI (Dynamic Momentum Index). |
//| VERSION 3.01: Safety checks refined. |
//| Copyright 2025, xxxxxxxx |
//+------------------------------------------------------------------+
#property copyright "Copyright 2025, xxxxxxxx"
#include <MyIncludes\HeikinAshi_Tools.mqh>
enum ENUM_ADAPTIVE_SOURCE_RSI
{
ADAPTIVE_SOURCE_RSI_STANDARD, // Calculate Volatility on Standard Price
ADAPTIVE_SOURCE_RSI_HEIKIN_ASHI // Calculate Volatility on Heikin Ashi Price
};
//+==================================================================+
//| CLASS 1: CAdaptiveRSICalculator (Base Class) |
//+==================================================================+
class CAdaptiveRSICalculator
{
protected:
int m_pivotal_period, m_vola_short, m_vola_long;
double m_price[];
ENUM_ADAPTIVE_SOURCE_RSI m_adaptive_source;
virtual bool PreparePriceSeries(int rates_total, ENUM_APPLIED_PRICE price_type, const double &open[], const double &high[], const double &low[], const double &close[]);
//--- Persistent Buffers
double m_price[]; // Used for Volatility calculation
double m_rsi_source[]; // Used for RSI calculation
double m_vola_sum[];
double m_vola_avg[];
double m_nsp_buffer[];
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:
CAdaptiveRSICalculator(void) {};
virtual ~CAdaptiveRSICalculator(void) {};
bool Init(int pivotal_p, int vola_s, int vola_l);
void Calculate(int rates_total, const double &open[], const double &high[], const double &low[], const double &close[], ENUM_APPLIED_PRICE price_type,
bool Init(int pivotal_p, int vola_s, int vola_l, ENUM_ADAPTIVE_SOURCE_RSI adapt_src);
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 &rsi_buffer[]);
};
//+------------------------------------------------------------------+
//| |
//| Init |
//+------------------------------------------------------------------+
class CAdaptiveRSICalculator_HA : public CAdaptiveRSICalculator
{
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;
};
//+==================================================================+
//| METHOD IMPLEMENTATIONS |
//+==================================================================+
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool CAdaptiveRSICalculator::Init(int pivotal_p, int vola_s, int vola_l)
bool CAdaptiveRSICalculator::Init(int pivotal_p, int vola_s, int vola_l, ENUM_ADAPTIVE_SOURCE_RSI adapt_src)
{
m_pivotal_period = (pivotal_p < 2) ? 2 : pivotal_p;
m_vola_short = (vola_s < 1) ? 1 : vola_s;
m_vola_long = (vola_l <= m_vola_short) ? m_vola_short + 1 : vola_l;
m_adaptive_source = adapt_src;
return true;
}
//+------------------------------------------------------------------+
//| |
//| Main Calculation (Optimized) |
//+------------------------------------------------------------------+
void CAdaptiveRSICalculator::Calculate(int rates_total, const double &open[], const double &high[], const double &low[], const double &close[], ENUM_APPLIED_PRICE price_type,
void CAdaptiveRSICalculator::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 &rsi_buffer[])
{
if(rates_total <= m_vola_long + m_pivotal_period)
return;
if(!PreparePriceSeries(rates_total, price_type, open, high, low, close))
// Safety Check: Ensure we have enough bars for the longest possible lookback
// Max lookback = VolaLong + Max possible RSI Period (approx 2 * Pivotal)
if(rates_total <= m_vola_long + m_pivotal_period * 2)
return;
double vola_sum[], vola_avg[], nsp_buffer[];
ArrayResize(vola_sum, rates_total);
ArrayResize(vola_avg, rates_total);
ArrayResize(nsp_buffer, rates_total);
int start_index;
if(prev_calculated == 0)
start_index = 0;
else
start_index = prev_calculated - 1;
//--- Step 1: Calculate Volatility Ratio and Adaptive Period (NSP)
for(int i = m_vola_short; i < rates_total; i++)
if(ArraySize(m_price) != rates_total)
{
for(int j = 0; j < m_vola_short; j++)
vola_sum[i] += MathAbs(m_price[i-j] - m_price[i-j-1]);
ArrayResize(m_price, rates_total);
ArrayResize(m_rsi_source, rates_total);
ArrayResize(m_vola_sum, rates_total);
ArrayResize(m_vola_avg, rates_total);
ArrayResize(m_nsp_buffer, rates_total);
}
for(int i = m_vola_short + m_vola_long - 1; i < rates_total; i++)
if(!PreparePriceSeries(rates_total, start_index, price_type, open, high, low, close))
return;
//--- 4. Calculate Volatility Sum (Incremental)
int loop_start_vola = MathMax(m_vola_short, start_index);
for(int i = loop_start_vola; i < rates_total; i++)
{
double sum = 0;
for(int j = 0; j < m_vola_short; j++)
sum += MathAbs(m_price[i-j] - m_price[i-j-1]);
m_vola_sum[i] = sum;
}
//--- 5. Calculate Volatility Avg and Adaptive Period (NSP)
int loop_start_nsp = MathMax(m_vola_short + m_vola_long - 1, start_index);
for(int i = loop_start_nsp; i < rates_total; i++)
{
double sum_of_sums = 0;
for(int j = 0; j < m_vola_long; j++)
sum_of_sums += vola_sum[i-j];
vola_avg[i] = sum_of_sums / m_vola_long;
sum_of_sums += m_vola_sum[i-j];
m_vola_avg[i] = sum_of_sums / m_vola_long;
double vola_ratio = (vola_avg[i] > 0.000001) ? vola_sum[i] / vola_avg[i] : 1.0;
double vola_ratio = (m_vola_avg[i] > 0.000001) ? m_vola_sum[i] / m_vola_avg[i] : 1.0;
// Calculate adaptive period
int period = (int)round(m_pivotal_period / vola_ratio);
nsp_buffer[i] = fmax(2, fmin(m_pivotal_period * 2, period)); // Clamp period to a reasonable range
// Clamp period between 2 and 2*Pivotal to prevent extreme noise or flatness
m_nsp_buffer[i] = fmax(2, fmin(m_pivotal_period * 2, period));
}
//--- Step 2: Calculate Simple RSI using the adaptive period
for(int i = m_vola_long + m_pivotal_period; i < rates_total; i++)
//--- 6. Calculate Simple RSI using m_rsi_source
// Start where we have valid NSP data
int loop_start_rsi = MathMax(m_vola_short + m_vola_long, start_index);
for(int i = loop_start_rsi; i < rates_total; i++)
{
int current_nsp = (int)nsp_buffer[i];
if(i < current_nsp)
int current_nsp = (int)m_nsp_buffer[i];
// Safety check: Ensure we don't look back before the start of the array
if(i <= current_nsp)
{
rsi_buffer[i] = 50.0;
continue;
}
double sum_pos = 0, sum_neg = 0;
// Brute force loop is required here because 'current_nsp' changes per bar
for(int j = 0; j < current_nsp; j++)
{
double diff = m_price[i-j] - m_price[i-j-1];
double diff = m_rsi_source[i-j] - m_rsi_source[i-j-1];
if(diff > 0)
sum_pos += diff;
else
@@ -111,90 +148,142 @@ void CAdaptiveRSICalculator::Calculate(int rates_total, const double &open[], co
}
//+------------------------------------------------------------------+
//| |
//| Prepare Price (Standard) |
//+------------------------------------------------------------------+
bool CAdaptiveRSICalculator::PreparePriceSeries(int rates_total, ENUM_APPLIED_PRICE price_type, const double &open[], const double &high[], const double &low[], const double &close[])
bool CAdaptiveRSICalculator::PreparePriceSeries(int rates_total, int start_index, ENUM_APPLIED_PRICE price_type, const double &open[], const double &high[], const double &low[], const double &close[])
{
if(ArraySize(m_price) != rates_total)
if(ArrayResize(m_price, rates_total) != rates_total)
return false;
switch(price_type)
for(int i = start_index; i < rates_total; i++)
{
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;
double p;
switch(price_type)
{
case PRICE_CLOSE:
p = close[i];
break;
case PRICE_OPEN:
p = open[i];
break;
case PRICE_HIGH:
p = high[i];
break;
case PRICE_LOW:
p = low[i];
break;
case PRICE_MEDIAN:
p = (high[i]+low[i])/2.0;
break;
case PRICE_TYPICAL:
p = (high[i]+low[i]+close[i])/3.0;
break;
case PRICE_WEIGHTED:
p = (high[i]+low[i]+2*close[i])/4.0;
break;
default:
p = close[i];
break;
}
m_price[i] = p; // Volatility Source
m_rsi_source[i] = p; // RSI Source
}
return true;
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool CAdaptiveRSICalculator_HA::PreparePriceSeries(int rates_total, ENUM_APPLIED_PRICE price_type, const double &open[], const double &high[], const double &low[], const double &close[])
//+==================================================================+
//| CLASS 2: CAdaptiveRSICalculator_HA |
//+==================================================================+
class CAdaptiveRSICalculator_HA : public CAdaptiveRSICalculator
{
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);
private:
CHeikinAshi_Calculator m_ha_calculator;
double m_ha_open[], m_ha_high[], m_ha_low[], m_ha_close[];
protected:
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;
};
if(ArraySize(m_price) != rates_total)
if(ArrayResize(m_price, rates_total) != rates_total)
return false;
switch(price_type)
//+------------------------------------------------------------------+
//| Prepare Price (Heikin Ashi) |
//+------------------------------------------------------------------+
bool CAdaptiveRSICalculator_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[])
{
if(ArraySize(m_ha_open) != rates_total)
{
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;
ArrayResize(m_ha_open, rates_total);
ArrayResize(m_ha_high, rates_total);
ArrayResize(m_ha_low, rates_total);
ArrayResize(m_ha_close, rates_total);
}
m_ha_calculator.Calculate(rates_total, start_index, open, high, low, close, m_ha_open, m_ha_high, m_ha_low, m_ha_close);
for(int i = start_index; i < rates_total; i++)
{
double ha_p;
switch(price_type)
{
case PRICE_CLOSE:
ha_p = m_ha_close[i];
break;
case PRICE_OPEN:
ha_p = m_ha_open[i];
break;
case PRICE_HIGH:
ha_p = m_ha_high[i];
break;
case PRICE_LOW:
ha_p = m_ha_low[i];
break;
case PRICE_MEDIAN:
ha_p = (m_ha_high[i]+m_ha_low[i])/2.0;
break;
case PRICE_TYPICAL:
ha_p = (m_ha_high[i]+m_ha_low[i]+m_ha_close[i])/3.0;
break;
case PRICE_WEIGHTED:
ha_p = (m_ha_high[i]+m_ha_low[i]+2*m_ha_close[i])/4.0;
break;
default:
ha_p = m_ha_close[i];
break;
}
m_rsi_source[i] = ha_p; // RSI uses HA
if(m_adaptive_source == ADAPTIVE_SOURCE_RSI_HEIKIN_ASHI)
{
m_price[i] = ha_p;
}
else
{
// Recalculate standard price for volatility
double std_p;
switch(price_type)
{
case PRICE_CLOSE:
std_p = close[i];
break;
case PRICE_OPEN:
std_p = open[i];
break;
case PRICE_HIGH:
std_p = high[i];
break;
case PRICE_LOW:
std_p = low[i];
break;
case PRICE_MEDIAN:
std_p = (high[i]+low[i])/2.0;
break;
case PRICE_TYPICAL:
std_p = (high[i]+low[i]+close[i])/3.0;
break;
case PRICE_WEIGHTED:
std_p = (high[i]+low[i]+2*close[i])/4.0;
break;
default:
std_p = close[i];
break;
}
m_price[i] = std_p;
}
}
return true;
}