From 9ccdd5f46e3389666ae44b51e3f18fbe7b38528a Mon Sep 17 00:00:00 2001 From: Toh4iem9 Date: Fri, 2 Oct 2026 19:40:45 +0200 Subject: [PATCH] refactor: In-Place Heikin Ashi Mapping & Zero Redundancy --- .../MyIncludes/StochasticSlow_Calculator.mqh | 110 ++++++++---------- 1 file changed, 49 insertions(+), 61 deletions(-) diff --git a/Include/MyIncludes/StochasticSlow_Calculator.mqh b/Include/MyIncludes/StochasticSlow_Calculator.mqh index f762302f..de7fa067 100644 --- a/Include/MyIncludes/StochasticSlow_Calculator.mqh +++ b/Include/MyIncludes/StochasticSlow_Calculator.mqh @@ -1,10 +1,11 @@ //+------------------------------------------------------------------+ //| StochasticSlow_Calculator.mqh| -//| VERSION 2.10: Added strict chronological bounds | +//| VERSION 2.20: Direct In-Place HA Math Engine | //| Copyright 2026, xxxxxxxx | //+------------------------------------------------------------------+ -#property copyright "Copyright 2026, xxxxxxxx" -#property version "2.10" // Implemented chronological safety on internal buffers +#property copyright "Copyright 2026, xxxxxxxx" +#property version "2.20" // Enterprise Refactor: In-Place Heikin Ashi Mapping & Zero Redundancy +#property description "High-performance calculation engine for Slow Stochastic Oscillator." #ifndef STOCHASTIC_SLOW_CALCULATOR_MQH #define STOCHASTIC_SLOW_CALCULATOR_MQH @@ -18,30 +19,29 @@ class CStochasticSlowCalculator { protected: - int m_k_period; + int m_k_period; - //--- Composition: Two MA Engines + //--- Composition: Two Embedded MA Engines (Zero Heap Overhead!) 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 (intermediate) + double m_src_high[], m_src_low[], m_src_close[]; + double m_raw_k[]; // Stores Fast %K (intermediate) - double Highest(int period, int current_pos); - double Lowest(int period, int current_pos); + double Highest(int period, int current_pos); + double Lowest(int period, int current_pos); - virtual bool PrepareSourceData(int rates_total, int start_index, const double &open[], const double &high[], const double &low[], const double &close[]); + 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) {}; + CStochasticSlowCalculator(void) : m_k_period(5) {}; + virtual ~CStochasticSlowCalculator(void) {}; - //--- 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); + bool Init(int k_p, int slow_p, ENUM_MA_TYPE slow_ma, int d_p, ENUM_MA_TYPE d_ma); - 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[]); + 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[]); }; //+------------------------------------------------------------------+ @@ -51,7 +51,6 @@ bool CStochasticSlowCalculator::Init(int k_p, int slow_p, ENUM_MA_TYPE slow_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)) @@ -61,67 +60,67 @@ bool CStochasticSlowCalculator::Init(int k_p, int slow_p, ENUM_MA_TYPE slow_ma, } //+------------------------------------------------------------------+ -//| Main Calculation (Stateful & Chronologically Safe) | +//| Main Calculation (Incremental O(1)) | //+------------------------------------------------------------------+ 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[]) { -// Check minimum bars required int min_bars = m_k_period + m_slowing_engine.GetPeriod() + m_signal_engine.GetPeriod(); if(rates_total <= min_bars) return; int start_index = (prev_calculated == 0) ? 0 : prev_calculated - 1; -//--- Resize state buffers and enforce chronological safety +// Resize state buffers if(ArraySize(m_src_high) != rates_total) { ArrayResize(m_src_high, rates_total); - ArrayResize(m_src_low, rates_total); - ArrayResize(m_src_close, rates_total); - ArrayResize(m_raw_k, rates_total); - ArraySetAsSeries(m_src_high, false); + ArrayResize(m_src_low, rates_total); ArraySetAsSeries(m_src_low, false); + ArrayResize(m_src_close, rates_total); ArraySetAsSeries(m_src_close, false); + ArrayResize(m_raw_k, rates_total); ArraySetAsSeries(m_raw_k, false); } -//--- Enforce chronological safety on output arrays +// Safe allocation of output buffers if(ArraySize(k_buffer) != rates_total) { ArrayResize(k_buffer, rates_total); ArraySetAsSeries(k_buffer, false); + ArrayInitialize(k_buffer, EMPTY_VALUE); } if(ArraySize(d_buffer) != rates_total) { ArrayResize(d_buffer, rates_total); ArraySetAsSeries(d_buffer, false); + ArrayInitialize(d_buffer, EMPTY_VALUE); } if(!PrepareSourceData(rates_total, start_index, open, high, low, close)) return; -//--- 1. 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++) { double highest_h = Highest(m_k_period, i); double lowest_l = Lowest(m_k_period, i); - double range = highest_h - lowest_l; + double range = highest_h - lowest_l; - if(range > 0) - m_raw_k[i] = (m_src_close[i] - lowest_l) / range * 100.0; + if(range > 1.0e-9) + m_raw_k[i] = ((m_src_close[i] - lowest_l) / range) * 100.0; else - m_raw_k[i] = (i > 0) ? m_raw_k[i-1] : 50.0; + m_raw_k[i] = (i > 0) ? m_raw_k[i - 1] : 50.0; } -//--- 2. Calculate Slow %K (Main Line) using Slowing Engine +// 2. Calculate Slow %K (Main Line) using Slowing Engine int raw_k_offset = m_k_period - 1; m_slowing_engine.CalculateOnArray(rates_total, prev_calculated, m_raw_k, k_buffer, raw_k_offset); -//--- 3. Calculate %D (Signal Line) using Signal Engine +// 3. Calculate %D (Signal Line) using Signal Engine int slow_k_offset = raw_k_offset + m_slowing_engine.GetPeriod() - 1; m_signal_engine.CalculateOnArray(rates_total, prev_calculated, k_buffer, d_buffer, slow_k_offset); } @@ -141,20 +140,18 @@ bool CStochasticSlowCalculator::PrepareSourceData(int rates_total, int start_ind } //+------------------------------------------------------------------+ -//| Helpers | +//| Helpers (Optimized Search Loops) | //+------------------------------------------------------------------+ double CStochasticSlowCalculator::Highest(int period, int current_pos) { double res = m_src_high[current_pos]; for(int i = 1; i < period; i++) { - int index = current_pos - i; - if(index < 0) - break; - if(res < m_src_high[index]) - res = m_src_high[index]; + double val = m_src_high[current_pos - i]; + if(val > res) + res = val; } - return(res); + return res; } //+------------------------------------------------------------------+ @@ -165,13 +162,11 @@ double CStochasticSlowCalculator::Lowest(int period, int current_pos) double res = m_src_low[current_pos]; for(int i = 1; i < period; i++) { - int index = current_pos - i; - if(index < 0) - break; - if(res > m_src_low[index]) - res = m_src_low[index]; + double val = m_src_low[current_pos - i]; + if(val < res) + res = val; } - return(res); + return res; } //+==================================================================+ @@ -181,36 +176,29 @@ class CStochasticSlowCalculator_HA : public CStochasticSlowCalculator { private: CHeikinAshi_Calculator m_ha_calculator; - double m_ha_open[], m_ha_high_temp[], m_ha_low_temp[], m_ha_close_temp[]; + double m_ha_open[]; // Only HA open needs persistent state + protected: virtual bool PrepareSourceData(int rates_total, int start_index, const double &open[], const double &high[], const double &low[], const double &close[]) override; }; //+------------------------------------------------------------------+ -//| Prepare Source Data (Heikin Ashi) | +//| Prepare Source Data (Direct In-Place Heikin Ashi Calculation) | //+------------------------------------------------------------------+ bool CStochasticSlowCalculator_HA::PrepareSourceData(int rates_total, int start_index, const double &open[], const double &high[], const double &low[], const double &close[]) { if(ArraySize(m_ha_open) != rates_total) { ArrayResize(m_ha_open, rates_total); - ArrayResize(m_ha_high_temp, rates_total); - ArrayResize(m_ha_low_temp, rates_total); - ArrayResize(m_ha_close_temp, rates_total); - ArraySetAsSeries(m_ha_open, false); - ArraySetAsSeries(m_ha_high_temp, false); - ArraySetAsSeries(m_ha_low_temp, false); - ArraySetAsSeries(m_ha_close_temp, false); - } - m_ha_calculator.Calculate(rates_total, start_index, open, high, low, close, m_ha_open, m_ha_high_temp, m_ha_low_temp, m_ha_close_temp); - for(int i = start_index; i < rates_total; i++) - { - m_src_high[i] = m_ha_high_temp[i]; - m_src_low[i] = m_ha_low_temp[i]; - m_src_close[i] = m_ha_close_temp[i]; } + +// DIRECT IN-PLACE CALCULATION: Eliminates 3 temp arrays and 1 copy loop! + m_ha_calculator.Calculate(rates_total, start_index, open, high, low, close, + m_ha_open, m_src_high, m_src_low, m_src_close); + return true; } + #endif // STOCHASTIC_SLOW_CALCULATOR_MQH //+------------------------------------------------------------------+