From 9117719f3262f5b4652b5de03f6380c4ca1c148d Mon Sep 17 00:00:00 2001 From: Toh4iem9 Date: Fri, 28 Nov 2025 12:06:43 +0100 Subject: [PATCH] refactor: Refactored to strict 10-param calculation --- Include/MyIncludes/HeikinAshi_Tools.mqh | 54 +++++++++++-------------- 1 file changed, 23 insertions(+), 31 deletions(-) diff --git a/Include/MyIncludes/HeikinAshi_Tools.mqh b/Include/MyIncludes/HeikinAshi_Tools.mqh index 69e14cc..8f2de22 100644 --- a/Include/MyIncludes/HeikinAshi_Tools.mqh +++ b/Include/MyIncludes/HeikinAshi_Tools.mqh @@ -4,19 +4,10 @@ //| Copyright 2025, xxxxxxxx | //+------------------------------------------------------------------+ #property copyright "Copyright 2025, xxxxxxxx" -#property link "" -//+==================================================================+ -//| | -//| GLOBAL ENUM for Standard & Heikin Ashi Prices | -//| | -//+==================================================================+ - -// This enum provides a unified list for input parameters, allowing users -// to select from both standard and Heikin Ashi price sources. +//--- Unified Price Enum enum ENUM_APPLIED_PRICE_HA_ALL { -//--- Heikin Ashi Prices (negative values for easy identification) PRICE_HA_CLOSE = -1, PRICE_HA_OPEN = -2, PRICE_HA_HIGH = -3, @@ -24,7 +15,6 @@ enum ENUM_APPLIED_PRICE_HA_ALL PRICE_HA_MEDIAN = -5, PRICE_HA_TYPICAL = -6, PRICE_HA_WEIGHTED = -7, -//--- Standard Prices (using built-in ENUM_APPLIED_PRICE values) PRICE_CLOSE_STD = PRICE_CLOSE, PRICE_OPEN_STD = PRICE_OPEN, PRICE_HIGH_STD = PRICE_HIGH, @@ -34,24 +24,16 @@ enum ENUM_APPLIED_PRICE_HA_ALL PRICE_WEIGHTED_STD= PRICE_WEIGHTED }; - -//+==================================================================+ -//| | -//| CLASS 1: CHeikinAshi_Calculator | -//| | -//+==================================================================+ - //+------------------------------------------------------------------+ -//| Class CHeikinAshi_Calculator. | -//| Purpose: Encapsulates the logic for calculating Heikin Ashi | -//| candle values from standard OHLC data. This class is | -//| stateless and operates on external buffers. | +//| Class CHeikinAshi_Calculator | +//| Purpose: Stateless calculation engine for Heikin Ashi candles. | //+------------------------------------------------------------------+ class CHeikinAshi_Calculator { public: - //--- Public Interface + //--- STRICT INTERFACE: Always requires start_index for optimization. void Calculate(const int rates_total, + const int start_index, const double &open[], const double &high[], const double &low[], @@ -63,9 +45,10 @@ public: }; //+------------------------------------------------------------------+ -//| Calculates Heikin Ashi values for the entire history. | +//| Implementation | //+------------------------------------------------------------------+ void CHeikinAshi_Calculator::Calculate(const int rates_total, + const int start_index, const double &open[], const double &high[], const double &low[], @@ -75,16 +58,26 @@ void CHeikinAshi_Calculator::Calculate(const int rates_total, double &ha_low[], double &ha_close[]) { - if(rates_total < 1) + if(rates_total < 2) return; - ha_open[0] = (open[0] + close[0]) / 2.0; - ha_close[0] = (open[0] + high[0] + low[0] + close[0]) / 4.0; - ha_high[0] = high[0]; - ha_low[0] = low[0]; + int i = start_index; - for(int i = 1; i < rates_total; i++) +//--- Initialization logic (only if starting from the very beginning) + if(i == 0) { + ha_open[0] = (open[0] + close[0]) / 2.0; + ha_close[0] = (open[0] + high[0] + low[0] + close[0]) / 4.0; + ha_high[0] = high[0]; + ha_low[0] = low[0]; + i = 1; + } + +//--- Main Optimization Loop +//--- Calculates only from start_index to the end + for(; i < rates_total; i++) + { + // HA Open relies on the PREVIOUS calculated HA candle (i-1) ha_open[i] = (ha_open[i - 1] + ha_close[i - 1]) / 2.0; ha_close[i] = (open[i] + high[i] + low[i] + close[i]) / 4.0; ha_high[i] = MathMax(high[i], MathMax(ha_open[i], ha_close[i])); @@ -92,4 +85,3 @@ void CHeikinAshi_Calculator::Calculate(const int rates_total, } } //+------------------------------------------------------------------+ -//+------------------------------------------------------------------+