diff --git a/Include/MyIncludes/StochasticSlow_Calculator.mqh b/Include/MyIncludes/StochasticSlow_Calculator.mqh index bfa8db4..3da315e 100644 --- a/Include/MyIncludes/StochasticSlow_Calculator.mqh +++ b/Include/MyIncludes/StochasticSlow_Calculator.mqh @@ -1,38 +1,40 @@ //+------------------------------------------------------------------+ //| StochasticSlow_Calculator.mqh| -//| VERSION 1.20: Optimized for incremental calculation. | +//| VERSION 2.00: Uses MovingAverage_Engine for smoothing. | //| Copyright 2025, xxxxxxxx | //+------------------------------------------------------------------+ #property copyright "Copyright 2025, xxxxxxxx" -#include +#include //+==================================================================+ -//| CLASS 1: CStochasticSlowCalculator (Base Class) | +//| CLASS: CStochasticSlowCalculator | //+==================================================================+ class CStochasticSlowCalculator { protected: - int m_k_period, m_d_period, m_slowing_period; - ENUM_MA_METHOD m_slowing_ma_type, m_d_ma_type; + int m_k_period; - //--- Persistent Buffers for Incremental Calculation + //--- Composition: Two MA Engines + CMovingAverageCalculator m_slowing_engine; // For Slow %K + CMovingAverageCalculator m_signal_engine; // For %D + + //--- Persistent Buffers double m_src_high[], m_src_low[], m_src_close[]; - double m_raw_k[]; // Stores Fast %K + double m_raw_k[]; // Stores Fast %K (intermediate) double Highest(int period, int current_pos); double Lowest(int period, int current_pos); - //--- Updated: Accepts start_index virtual bool PrepareSourceData(int rates_total, int start_index, const double &open[], const double &high[], const double &low[], const double &close[]); public: CStochasticSlowCalculator(void) {}; virtual ~CStochasticSlowCalculator(void) {}; - bool Init(int k_p, int slow_p, ENUM_MA_METHOD slow_ma, int d_p, ENUM_MA_METHOD d_ma); + //--- Init now takes ENUM_MA_TYPE for both smoothings + bool Init(int k_p, int slow_p, ENUM_MA_TYPE slow_ma, int d_p, ENUM_MA_TYPE d_ma); - //--- Updated: Accepts prev_calculated void Calculate(int rates_total, int prev_calculated, const double &open[], const double &high[], const double &low[], const double &close[], double &k_buffer[], double &d_buffer[]); }; @@ -40,33 +42,32 @@ public: //+------------------------------------------------------------------+ //| Init | //+------------------------------------------------------------------+ -bool CStochasticSlowCalculator::Init(int k_p, int slow_p, ENUM_MA_METHOD slow_ma, int d_p, ENUM_MA_METHOD d_ma) +bool CStochasticSlowCalculator::Init(int k_p, int slow_p, ENUM_MA_TYPE slow_ma, int d_p, ENUM_MA_TYPE d_ma) { - m_k_period = (k_p < 1) ? 1 : k_p; - m_slowing_period = (slow_p < 1) ? 1 : slow_p; - m_slowing_ma_type = slow_ma; - m_d_period = (d_p < 1) ? 1 : d_p; - m_d_ma_type = d_ma; + m_k_period = (k_p < 1) ? 1 : k_p; + +// Initialize both engines + if(!m_slowing_engine.Init(slow_p, slow_ma)) + return false; + if(!m_signal_engine.Init(d_p, d_ma)) + return false; + return true; } //+------------------------------------------------------------------+ -//| Main Calculation (Optimized) | +//| Main Calculation | //+------------------------------------------------------------------+ void CStochasticSlowCalculator::Calculate(int rates_total, int prev_calculated, const double &open[], const double &high[], const double &low[], const double &close[], double &k_buffer[], double &d_buffer[]) { - if(rates_total <= m_k_period + m_slowing_period + m_d_period) +// Check minimum bars required + int min_bars = m_k_period + m_slowing_engine.GetPeriod() + m_signal_engine.GetPeriod(); + if(rates_total <= min_bars) return; -//--- 1. Determine Start Index - int start_index; - if(prev_calculated == 0) - start_index = 0; - else - start_index = prev_calculated - 1; + int start_index = (prev_calculated == 0) ? 0 : prev_calculated - 1; -//--- 2. Resize Buffers if(ArraySize(m_src_high) != rates_total) { ArrayResize(m_src_high, rates_total); @@ -75,11 +76,10 @@ void CStochasticSlowCalculator::Calculate(int rates_total, int prev_calculated, ArrayResize(m_raw_k, rates_total); } -//--- 3. Prepare Source Data (Optimized) if(!PrepareSourceData(rates_total, start_index, open, high, low, close)) return; -//--- 4. Calculate Raw %K (Fast %K) +//--- 1. Calculate Raw %K (Fast %K) int loop_start_k = MathMax(m_k_period - 1, start_index); for(int i = loop_start_k; i < rates_total; i++) @@ -94,87 +94,26 @@ void CStochasticSlowCalculator::Calculate(int rates_total, int prev_calculated, m_raw_k[i] = (i > 0) ? m_raw_k[i-1] : 50.0; } -//--- 5. Calculate Slow %K (Main Line) - int k_slow_start = m_k_period + m_slowing_period - 2; - int loop_start_slow = MathMax(k_slow_start, start_index); +//--- 2. Calculate Slow %K (Main Line) using Slowing Engine +// Offset for Raw %K is (K - 1) + int raw_k_offset = m_k_period - 1; - for(int i = loop_start_slow; i < rates_total; i++) - { - switch(m_slowing_ma_type) - { - case MODE_EMA: - case MODE_SMMA: - if(i == k_slow_start) - { - double sum=0; - for(int j=0; j0) k_buffer[i]=sum/w_sum;} - break; - default: - {double sum=0; for(int j=0; j0) d_buffer[i]=sum/w_sum;} - break; - default: - {double sum=0; for(int j=0; j