From 9fec0ee8f525170eb70e03072681138a4213a204 Mon Sep 17 00:00:00 2001 From: Toh4iem9 Date: Thu, 8 Jan 2026 10:53:52 +0100 Subject: [PATCH] feat(indicators): Safety checks refined --- .../MyIncludes/RSI_Adaptive_Calculator.mqh | 325 +++++++++++------- 1 file changed, 207 insertions(+), 118 deletions(-) diff --git a/Include/MyIncludes/RSI_Adaptive_Calculator.mqh b/Include/MyIncludes/RSI_Adaptive_Calculator.mqh index 0aabd11..320861e 100644 --- a/Include/MyIncludes/RSI_Adaptive_Calculator.mqh +++ b/Include/MyIncludes/RSI_Adaptive_Calculator.mqh @@ -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 +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