refactor: Refactored to use MovingAverage_Engine

This commit is contained in:
Toh4iem9
2025-12-19 12:06:39 +01:00
parent 1c8a8b939d
commit f82a060962
+8 -12
View File
@@ -3,7 +3,7 @@
//| Copyright 2025, xxxxxxxx| //| Copyright 2025, xxxxxxxx|
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
#property copyright "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 "Professional Slow Stochastic with selectable MA types and"
#property description "candle source (Standard or Heikin Ashi)." #property description "candle source (Standard or Heikin Ashi)."
@@ -44,16 +44,18 @@ enum ENUM_CANDLE_SOURCE
//--- Input Parameters --- //--- Input Parameters ---
input int InpKPeriod = 5; input int InpKPeriod = 5;
input int InpSlowingPeriod = 3; 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 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; input ENUM_CANDLE_SOURCE InpCandleSource = CANDLE_STANDARD;
//--- Indicator Buffers --- //--- Indicator Buffers ---
double BufferK[]; double BufferK[];
double BufferD[]; double BufferD[];
//--- Global calculator object (as a base class pointer) --- //--- Global calculator object ---
CStochasticSlowCalculator *g_calculator; CStochasticSlowCalculator *g_calculator;
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
@@ -61,33 +63,29 @@ CStochasticSlowCalculator *g_calculator;
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
int OnInit() int OnInit()
{ {
//--- Map the buffers and set as non-timeseries
SetIndexBuffer(0, BufferK, INDICATOR_DATA); SetIndexBuffer(0, BufferK, INDICATOR_DATA);
SetIndexBuffer(1, BufferD, INDICATOR_DATA); SetIndexBuffer(1, BufferD, INDICATOR_DATA);
ArraySetAsSeries(BufferK, false); ArraySetAsSeries(BufferK, false);
ArraySetAsSeries(BufferD, false); ArraySetAsSeries(BufferD, false);
//--- Dynamically create the appropriate calculator instance
switch(InpCandleSource) switch(InpCandleSource)
{ {
case CANDLE_HEIKIN_ASHI: case CANDLE_HEIKIN_ASHI:
g_calculator = new CStochasticSlowCalculator_HA(); g_calculator = new CStochasticSlowCalculator_HA();
IndicatorSetString(INDICATOR_SHORTNAME, StringFormat("SlowStoch HA(%d,%d,%d)", InpKPeriod, InpSlowingPeriod, InpDPeriod)); IndicatorSetString(INDICATOR_SHORTNAME, StringFormat("SlowStoch HA(%d,%d,%d)", InpKPeriod, InpSlowingPeriod, InpDPeriod));
break; break;
default: // CANDLE_STANDARD default:
g_calculator = new CStochasticSlowCalculator(); g_calculator = new CStochasticSlowCalculator();
IndicatorSetString(INDICATOR_SHORTNAME, StringFormat("SlowStoch(%d,%d,%d)", InpKPeriod, InpSlowingPeriod, InpDPeriod)); IndicatorSetString(INDICATOR_SHORTNAME, StringFormat("SlowStoch(%d,%d,%d)", InpKPeriod, InpSlowingPeriod, InpDPeriod));
break; break;
} }
//--- Check if creation was successful and initialize
if(CheckPointer(g_calculator) == POINTER_INVALID || !g_calculator.Init(InpKPeriod, InpSlowingPeriod, InpSlowingMAType, InpDPeriod, InpDMAType)) if(CheckPointer(g_calculator) == POINTER_INVALID || !g_calculator.Init(InpKPeriod, InpSlowingPeriod, InpSlowingMAType, InpDPeriod, InpDMAType))
{ {
Print("Failed to create or initialize Slow Stochastic Calculator object."); Print("Failed to create or initialize Slow Stochastic Calculator object.");
return(INIT_FAILED); return(INIT_FAILED);
} }
//--- Set indicator display properties
IndicatorSetInteger(INDICATOR_DIGITS, 2); IndicatorSetInteger(INDICATOR_DIGITS, 2);
PlotIndexSetInteger(0, PLOT_DRAW_BEGIN, InpKPeriod + InpSlowingPeriod - 2); PlotIndexSetInteger(0, PLOT_DRAW_BEGIN, InpKPeriod + InpSlowingPeriod - 2);
PlotIndexSetInteger(1, PLOT_DRAW_BEGIN, InpKPeriod + InpSlowingPeriod + InpDPeriod - 3); PlotIndexSetInteger(1, PLOT_DRAW_BEGIN, InpKPeriod + InpSlowingPeriod + InpDPeriod - 3);
@@ -100,7 +98,6 @@ int OnInit()
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
void OnDeinit(const int reason) void OnDeinit(const int reason)
{ {
//--- Free the calculator object to prevent memory leaks
if(CheckPointer(g_calculator) != POINTER_INVALID) if(CheckPointer(g_calculator) != POINTER_INVALID)
delete g_calculator; delete g_calculator;
} }
@@ -109,7 +106,7 @@ void OnDeinit(const int reason)
//| Custom indicator calculation function | //| Custom indicator calculation function |
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
int OnCalculate(const int rates_total, int OnCalculate(const int rates_total,
const int prev_calculated, // <--- Now used! const int prev_calculated,
const datetime &time[], const datetime &time[],
const double &open[], const double &open[],
const double &high[], const double &high[],
@@ -122,7 +119,6 @@ int OnCalculate(const int rates_total,
if(CheckPointer(g_calculator) == POINTER_INVALID) if(CheckPointer(g_calculator) == POINTER_INVALID)
return 0; return 0;
//--- Delegate calculation with prev_calculated optimization
g_calculator.Calculate(rates_total, prev_calculated, open, high, low, close, BufferK, BufferD); g_calculator.Calculate(rates_total, prev_calculated, open, high, low, close, BufferK, BufferD);
return(rates_total); return(rates_total);