From 598b9ca3e6775b8b025e21ecd8063b377c8d5389 Mon Sep 17 00:00:00 2001 From: Toh4iem9 Date: Sun, 30 Nov 2025 14:03:34 +0100 Subject: [PATCH] refactor: Optimized for incremental calculation --- .../MyIncludes/StochRSI_Slow_Calculator.mqh | 101 +++++++++++------- 1 file changed, 64 insertions(+), 37 deletions(-) diff --git a/Include/MyIncludes/StochRSI_Slow_Calculator.mqh b/Include/MyIncludes/StochRSI_Slow_Calculator.mqh index 44b184f..82a699a 100644 --- a/Include/MyIncludes/StochRSI_Slow_Calculator.mqh +++ b/Include/MyIncludes/StochRSI_Slow_Calculator.mqh @@ -1,16 +1,14 @@ //+------------------------------------------------------------------+ //| StochRSI_Slow_Calculator.mqh| -//| Calculation engine for Standard and Heikin Ashi Slow StochRSI. | +//| VERSION 1.20: Optimized for incremental calculation. | //| Copyright 2025, xxxxxxxx | //+------------------------------------------------------------------+ #property copyright "Copyright 2025, xxxxxxxx" -#include // Re-use the RSI Pro engine +#include //+==================================================================+ -//| | //| CLASS 1: CStochRSI_Slow_Calculator (Base Class) | -//| | //+==================================================================+ class CStochRSI_Slow_Calculator { @@ -19,6 +17,10 @@ protected: ENUM_MA_METHOD m_slowing_ma_type, m_d_ma_type; CRSIProCalculator *m_rsi_calculator; + //--- Persistent Buffers for Incremental Calculation + double m_rsi_buffer[]; + double m_raw_k[]; + double Highest(const double &array[], int period, int current_pos); double Lowest(const double &array[], int period, int current_pos); @@ -27,12 +29,14 @@ public: virtual ~CStochRSI_Slow_Calculator(void); bool Init(int rsi_p, int k_p, int slow_p, ENUM_MA_METHOD slow_ma, int d_p, ENUM_MA_METHOD d_ma); - void Calculate(int rates_total, const double &open[], const double &high[], const double &low[], const double &close[], ENUM_APPLIED_PRICE price_type, + + //--- Updated: Accepts prev_calculated + void Calculate(int rates_total, int prev_calculated, const double &open[], const double &high[], const double &low[], const double &close[], ENUM_APPLIED_PRICE price_type, double &k_buffer[], double &d_buffer[]); }; //+------------------------------------------------------------------+ -//| CStochRSI_Slow_Calculator: Constructor | +//| Constructor | //+------------------------------------------------------------------+ CStochRSI_Slow_Calculator::CStochRSI_Slow_Calculator(void) { @@ -40,7 +44,7 @@ CStochRSI_Slow_Calculator::CStochRSI_Slow_Calculator(void) } //+------------------------------------------------------------------+ -//| CStochRSI_Slow_Calculator: Destructor | +//| Destructor | //+------------------------------------------------------------------+ CStochRSI_Slow_Calculator::~CStochRSI_Slow_Calculator(void) { @@ -49,7 +53,7 @@ CStochRSI_Slow_Calculator::~CStochRSI_Slow_Calculator(void) } //+------------------------------------------------------------------+ -//| CStochRSI_Slow_Calculator: Initialization | +//| Init | //+------------------------------------------------------------------+ bool CStochRSI_Slow_Calculator::Init(int rsi_p, int k_p, int slow_p, ENUM_MA_METHOD slow_ma, int d_p, ENUM_MA_METHOD d_ma) { @@ -66,9 +70,9 @@ bool CStochRSI_Slow_Calculator::Init(int rsi_p, int k_p, int slow_p, ENUM_MA_MET } //+------------------------------------------------------------------+ -//| CStochRSI_Slow_Calculator: Main Calculation Method | +//| Main Calculation (Optimized) | //+------------------------------------------------------------------+ -void CStochRSI_Slow_Calculator::Calculate(int rates_total, const double &open[], const double &high[], const double &low[], const double &close[], ENUM_APPLIED_PRICE price_type, +void CStochRSI_Slow_Calculator::Calculate(int rates_total, int prev_calculated, const double &open[], const double &high[], const double &low[], const double &close[], ENUM_APPLIED_PRICE price_type, double &k_buffer[], double &d_buffer[]) { if(rates_total <= m_rsi_period + m_k_period + m_slowing_period + m_d_period) @@ -76,26 +80,49 @@ void CStochRSI_Slow_Calculator::Calculate(int rates_total, const double &open[], if(CheckPointer(m_rsi_calculator) == POINTER_INVALID) return; - double rsi_buffer[], dummy1[], dummy2[], dummy3[]; - ArrayResize(rsi_buffer, rates_total); - m_rsi_calculator.Calculate(rates_total, price_type, open, high, low, close, rsi_buffer, dummy1, dummy2, dummy3); +//--- 1. Determine Start Index + int start_index; + if(prev_calculated == 0) + start_index = 0; + else + start_index = prev_calculated - 1; - double raw_k[]; - ArrayResize(raw_k, rates_total); - int raw_k_start = m_rsi_period + m_k_period - 2; - for(int i = raw_k_start; i < rates_total; i++) +//--- 2. Resize Internal Buffers + if(ArraySize(m_rsi_buffer) != rates_total) { - double highest_rsi = Highest(rsi_buffer, m_k_period, i); - double lowest_rsi = Lowest(rsi_buffer, m_k_period, i); - double range = highest_rsi - lowest_rsi; - if(range > 0.00001) - raw_k[i] = (rsi_buffer[i] - lowest_rsi) / range * 100.0; - else - raw_k[i] = (i > 0) ? raw_k[i-1] : 50.0; + ArrayResize(m_rsi_buffer, rates_total); + ArrayResize(m_raw_k, rates_total); } +//--- 3. Calculate RSI (Incremental) + double dummy1[], dummy2[], dummy3[]; +// Note: RSI Pro Calculator handles resizing of its internal buffers, but we need to provide output buffers. +// If we provide empty dynamic arrays, RSI Pro will resize them. + + m_rsi_calculator.Calculate(rates_total, prev_calculated, price_type, open, high, low, close, + m_rsi_buffer, dummy1, dummy2, dummy3); + +//--- 4. Calculate Raw %K (Fast %K) + int raw_k_start = m_rsi_period + m_k_period - 2; + int loop_start_k = MathMax(raw_k_start, start_index); + + for(int i = loop_start_k; i < rates_total; i++) + { + double highest_rsi = Highest(m_rsi_buffer, m_k_period, i); + double lowest_rsi = Lowest(m_rsi_buffer, m_k_period, i); + double range = highest_rsi - lowest_rsi; + + if(range > 0.00001) + m_raw_k[i] = (m_rsi_buffer[i] - lowest_rsi) / range * 100.0; + else + m_raw_k[i] = (i > 0) ? m_raw_k[i-1] : 50.0; + } + +//--- 5. Calculate Slow %K (Main Line) int k_slow_start = m_rsi_period + m_k_period + m_slowing_period - 3; - for(int i = k_slow_start; i < rates_total; i++) + int loop_start_slow = MathMax(k_slow_start, start_index); + + for(int i = loop_start_slow; i < rates_total; i++) { switch(m_slowing_ma_type) { @@ -105,7 +132,7 @@ void CStochRSI_Slow_Calculator::Calculate(int rates_total, const double &open[], { double sum=0; for(int j=0; j0) k_buffer[i]=sum/w_sum;} + {double sum=0,w_sum=0; for(int j=0; j0) k_buffer[i]=sum/w_sum;} break; default: - {double sum=0; for(int j=0; j