From f82a060962b74e7f1d0c3593ee4193bfaa4c35f7 Mon Sep 17 00:00:00 2001 From: Toh4iem9 Date: Fri, 19 Dec 2025 12:06:39 +0100 Subject: [PATCH] refactor: Refactored to use MovingAverage_Engine --- .../MyIndicators/StochasticSlow_Pro.mq5 | 20 ++++++++----------- 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/Indicators/MyIndicators/StochasticSlow_Pro.mq5 b/Indicators/MyIndicators/StochasticSlow_Pro.mq5 index dfaf8ba..fa66e8e 100644 --- a/Indicators/MyIndicators/StochasticSlow_Pro.mq5 +++ b/Indicators/MyIndicators/StochasticSlow_Pro.mq5 @@ -3,7 +3,7 @@ //| Copyright 2025, xxxxxxxx| //+------------------------------------------------------------------+ #property copyright "Copyright 2025, xxxxxxxx" -#property version "2.10" // Optimized for incremental calculation +#property version "3.00" // Refactored to use MovingAverage_Engine #property description "Professional Slow Stochastic with selectable MA types and" #property description "candle source (Standard or Heikin Ashi)." @@ -44,16 +44,18 @@ enum ENUM_CANDLE_SOURCE //--- Input Parameters --- input int InpKPeriod = 5; input int InpSlowingPeriod = 3; -input ENUM_MA_METHOD InpSlowingMAType = MODE_SMA; +// UPDATED: Use ENUM_MA_TYPE +input ENUM_MA_TYPE InpSlowingMAType = SMA; input int InpDPeriod = 3; -input ENUM_MA_METHOD InpDMAType = MODE_SMA; +// UPDATED: Use ENUM_MA_TYPE +input ENUM_MA_TYPE InpDMAType = SMA; input ENUM_CANDLE_SOURCE InpCandleSource = CANDLE_STANDARD; //--- Indicator Buffers --- double BufferK[]; double BufferD[]; -//--- Global calculator object (as a base class pointer) --- +//--- Global calculator object --- CStochasticSlowCalculator *g_calculator; //+------------------------------------------------------------------+ @@ -61,33 +63,29 @@ CStochasticSlowCalculator *g_calculator; //+------------------------------------------------------------------+ int OnInit() { -//--- Map the buffers and set as non-timeseries SetIndexBuffer(0, BufferK, INDICATOR_DATA); SetIndexBuffer(1, BufferD, INDICATOR_DATA); ArraySetAsSeries(BufferK, false); ArraySetAsSeries(BufferD, false); -//--- Dynamically create the appropriate calculator instance switch(InpCandleSource) { case CANDLE_HEIKIN_ASHI: g_calculator = new CStochasticSlowCalculator_HA(); IndicatorSetString(INDICATOR_SHORTNAME, StringFormat("SlowStoch HA(%d,%d,%d)", InpKPeriod, InpSlowingPeriod, InpDPeriod)); break; - default: // CANDLE_STANDARD + default: g_calculator = new CStochasticSlowCalculator(); IndicatorSetString(INDICATOR_SHORTNAME, StringFormat("SlowStoch(%d,%d,%d)", InpKPeriod, InpSlowingPeriod, InpDPeriod)); break; } -//--- Check if creation was successful and initialize if(CheckPointer(g_calculator) == POINTER_INVALID || !g_calculator.Init(InpKPeriod, InpSlowingPeriod, InpSlowingMAType, InpDPeriod, InpDMAType)) { Print("Failed to create or initialize Slow Stochastic Calculator object."); return(INIT_FAILED); } -//--- Set indicator display properties IndicatorSetInteger(INDICATOR_DIGITS, 2); PlotIndexSetInteger(0, PLOT_DRAW_BEGIN, InpKPeriod + InpSlowingPeriod - 2); PlotIndexSetInteger(1, PLOT_DRAW_BEGIN, InpKPeriod + InpSlowingPeriod + InpDPeriod - 3); @@ -100,7 +98,6 @@ int OnInit() //+------------------------------------------------------------------+ void OnDeinit(const int reason) { -//--- Free the calculator object to prevent memory leaks if(CheckPointer(g_calculator) != POINTER_INVALID) delete g_calculator; } @@ -109,7 +106,7 @@ void OnDeinit(const int reason) //| Custom indicator calculation function | //+------------------------------------------------------------------+ int OnCalculate(const int rates_total, - const int prev_calculated, // <--- Now used! + const int prev_calculated, const datetime &time[], const double &open[], const double &high[], @@ -122,7 +119,6 @@ int OnCalculate(const int rates_total, if(CheckPointer(g_calculator) == POINTER_INVALID) return 0; -//--- Delegate calculation with prev_calculated optimization g_calculator.Calculate(rates_total, prev_calculated, open, high, low, close, BufferK, BufferD); return(rates_total);