refactor(indicators): Refactored to use MovingAverage_Engine

This commit is contained in:
Toh4iem9
2025-12-31 18:26:43 +01:00
parent 7a2e518c32
commit b4ecd635bf
+9 -25
View File
@@ -1,11 +1,9 @@
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
//| CHO_Pro.mq5| //| CHO_Pro.mq5|
//| Copyright 2025, xxxxxxxx| //| Copyright 2025, xxxxxxxx|
//| |
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
#property copyright "Copyright 2025, xxxxxxxx" #property copyright "Copyright 2025, xxxxxxxx"
#property link "" #property version "3.00" // Refactored to use MovingAverage_Engine
#property version "2.01" // Corrected calculator call signature
#property description "Professional Chaikin Oscillator (CHO) with selectable MA type and" #property description "Professional Chaikin Oscillator (CHO) with selectable MA type and"
#property description "candle source (Standard or Heikin Ashi) for the underlying ADL." #property description "candle source (Standard or Heikin Ashi) for the underlying ADL."
@@ -32,64 +30,54 @@ enum ENUM_CANDLE_SOURCE
//--- Input Parameters --- //--- Input Parameters ---
input int InpFastPeriod = 3; input int InpFastPeriod = 3;
input int InpSlowPeriod = 10; input int InpSlowPeriod = 10;
input ENUM_MA_METHOD InpMaMethod = MODE_EMA; // UPDATED: Use ENUM_MA_TYPE
input ENUM_MA_TYPE InpMaMethod = EMA;
input ENUM_APPLIED_VOLUME InpVolumeType = VOLUME_TICK; input ENUM_APPLIED_VOLUME InpVolumeType = VOLUME_TICK;
input ENUM_CANDLE_SOURCE InpCandleSource = CANDLE_STANDARD; // Candle source for ADL input ENUM_CANDLE_SOURCE InpCandleSource = CANDLE_STANDARD; // Candle source for ADL
//--- Indicator Buffers --- //--- Indicator Buffers ---
double BufferCHO[]; double BufferCHO[];
//--- Global calculator object (as a base class pointer) --- //--- Global calculator object ---
CCHOCalculator *g_calculator; CCHOCalculator *g_calculator;
//+------------------------------------------------------------------+
//| Custom indicator initialization function. |
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
int OnInit() int OnInit()
{ {
//--- Map the buffer and set as non-timeseries
SetIndexBuffer(0, BufferCHO, INDICATOR_DATA); SetIndexBuffer(0, BufferCHO, INDICATOR_DATA);
ArraySetAsSeries(BufferCHO, false); ArraySetAsSeries(BufferCHO, false);
//--- Dynamically create the appropriate calculator instance
switch(InpCandleSource) switch(InpCandleSource)
{ {
case CANDLE_HEIKIN_ASHI: case CANDLE_HEIKIN_ASHI:
g_calculator = new CCHOCalculator_HA(); g_calculator = new CCHOCalculator_HA();
IndicatorSetString(INDICATOR_SHORTNAME, StringFormat("CHO HA(%d,%d)", InpFastPeriod, InpSlowPeriod)); IndicatorSetString(INDICATOR_SHORTNAME, StringFormat("CHO HA(%d,%d,%s)", InpFastPeriod, InpSlowPeriod, EnumToString(InpMaMethod)));
break; break;
default: // CANDLE_STANDARD default:
g_calculator = new CCHOCalculator_Std(); g_calculator = new CCHOCalculator_Std();
IndicatorSetString(INDICATOR_SHORTNAME, StringFormat("CHO(%d,%d)", InpFastPeriod, InpSlowPeriod)); IndicatorSetString(INDICATOR_SHORTNAME, StringFormat("CHO(%d,%d,%s)", InpFastPeriod, InpSlowPeriod, EnumToString(InpMaMethod)));
break; break;
} }
//--- Check if creation was successful and initialize
if(CheckPointer(g_calculator) == POINTER_INVALID || !g_calculator.Init(InpFastPeriod, InpSlowPeriod, InpMaMethod, InpVolumeType)) if(CheckPointer(g_calculator) == POINTER_INVALID || !g_calculator.Init(InpFastPeriod, InpSlowPeriod, InpMaMethod, InpVolumeType))
{ {
Print("Failed to create or initialize CHO Calculator object."); Print("Failed to create or initialize CHO Calculator object.");
return(INIT_FAILED); return(INIT_FAILED);
} }
//--- Set indicator display properties
PlotIndexSetInteger(0, PLOT_DRAW_BEGIN, g_calculator.GetSlowPeriod() - 1); PlotIndexSetInteger(0, PLOT_DRAW_BEGIN, g_calculator.GetSlowPeriod() - 1);
IndicatorSetInteger(INDICATOR_DIGITS, 0); IndicatorSetInteger(INDICATOR_DIGITS, 0);
return(INIT_SUCCEEDED); return(INIT_SUCCEEDED);
} }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function. |
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
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;
} }
//+------------------------------------------------------------------+
//| Custom indicator calculation function. |
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
int OnCalculate(const int rates_total, int OnCalculate(const int rates_total,
const int prev_calculated, const int prev_calculated,
@@ -102,16 +90,12 @@ int OnCalculate(const int rates_total,
const long &volume[], const long &volume[],
const int &spread[]) const int &spread[])
{ {
//--- Ensure the calculator object is valid
if(CheckPointer(g_calculator) == POINTER_INVALID) if(CheckPointer(g_calculator) == POINTER_INVALID)
return 0; return 0;
//--- Delegate the entire calculation to our calculator object // Delegate calculation with incremental optimization
//--- CORRECTED: Pass the 'open' array to the calculator g_calculator.Calculate(rates_total, prev_calculated, open, high, low, close, tick_volume, volume, InpVolumeType, BufferCHO);
g_calculator.Calculate(rates_total, open, high, low, close, tick_volume, volume, BufferCHO);
//--- Return rates_total for a full recalculation, ensuring stability
return(rates_total); return(rates_total);
} }
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
//+------------------------------------------------------------------+