refactor(indicators): Refactored to use MovingAverage_Engine

This commit is contained in:
Toh4iem9
2026-01-01 15:18:06 +01:00
parent c11fc7b3ed
commit e3a5e9257f
+9 -25
View File
@@ -1,11 +1,9 @@
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
//| MFI_Pro.mq5| //| MFI_Pro.mq5|
//| Copyright 2025, xxxxxxxx| //| Copyright 2025, xxxxxxxx|
//| |
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
#property copyright "Copyright 2025, xxxxxxxx" #property copyright "Copyright 2025, xxxxxxxx"
#property link "" #property version "3.10" // Refactored to use MovingAverage_Engine
#property version "3.02" // Added selectable display mode for signal line
#property description "Professional Money Flow Index (MFI) with an optional signal line and" #property description "Professional Money Flow Index (MFI) with an optional signal line and"
#property description "selectable candle source (Standard or Heikin Ashi)." #property description "selectable candle source (Standard or Heikin Ashi)."
@@ -58,47 +56,42 @@ input ENUM_APPLIED_VOLUME InpVolumeType = VOLUME_TICK;
input group "Signal Line Settings" input group "Signal Line Settings"
input ENUM_DISPLAY_MODE InpDisplayMode = DISPLAY_MFI_AND_SIGNAL; input ENUM_DISPLAY_MODE InpDisplayMode = DISPLAY_MFI_AND_SIGNAL;
input int InpMAPeriod = 9; input int InpMAPeriod = 9;
input ENUM_MA_METHOD InpMAMethod = MODE_SMA; // UPDATED: Use ENUM_MA_TYPE
input ENUM_MA_TYPE InpMAMethod = SMA;
//--- Indicator Buffers --- //--- Indicator Buffers ---
double BufferMFI[]; double BufferMFI[];
double BufferSignal[]; double BufferSignal[];
//--- Global calculator object (as a base class pointer) --- //--- Global calculator object ---
CMFICalculator *g_calculator; CMFICalculator *g_calculator;
//+------------------------------------------------------------------+
//| Custom indicator initialization function. |
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
int OnInit() int OnInit()
{ {
//--- Map the buffers and set as non-timeseries
SetIndexBuffer(0, BufferMFI, INDICATOR_DATA); SetIndexBuffer(0, BufferMFI, INDICATOR_DATA);
SetIndexBuffer(1, BufferSignal, INDICATOR_DATA); SetIndexBuffer(1, BufferSignal, INDICATOR_DATA);
ArraySetAsSeries(BufferMFI, false); ArraySetAsSeries(BufferMFI, false);
ArraySetAsSeries(BufferSignal, false); ArraySetAsSeries(BufferSignal, false);
//--- Dynamically create the appropriate calculator instance
switch(InpCandleSource) switch(InpCandleSource)
{ {
case CANDLE_HEIKIN_ASHI: case CANDLE_HEIKIN_ASHI:
g_calculator = new CMFICalculator_HA(); g_calculator = new CMFICalculator_HA();
IndicatorSetString(INDICATOR_SHORTNAME, StringFormat("MFI HA(%d,%d)", InpMFIPeriod, InpMAPeriod)); IndicatorSetString(INDICATOR_SHORTNAME, StringFormat("MFI HA(%d,%d)", InpMFIPeriod, InpMAPeriod));
break; break;
default: // CANDLE_STANDARD default:
g_calculator = new CMFICalculator(); g_calculator = new CMFICalculator();
IndicatorSetString(INDICATOR_SHORTNAME, StringFormat("MFI(%d,%d)", InpMFIPeriod, InpMAPeriod)); IndicatorSetString(INDICATOR_SHORTNAME, StringFormat("MFI(%d,%d)", InpMFIPeriod, InpMAPeriod));
break; break;
} }
//--- Check if creation was successful and initialize
if(CheckPointer(g_calculator) == POINTER_INVALID || !g_calculator.Init(InpMFIPeriod, InpMAPeriod, InpMAMethod, InpVolumeType)) if(CheckPointer(g_calculator) == POINTER_INVALID || !g_calculator.Init(InpMFIPeriod, InpMAPeriod, InpMAMethod, InpVolumeType))
{ {
Print("Failed to create or initialize MFI Calculator object."); Print("Failed to create or initialize MFI Calculator object.");
return(INIT_FAILED); return(INIT_FAILED);
} }
//--- Set indicator display properties
PlotIndexSetInteger(0, PLOT_DRAW_BEGIN, InpMFIPeriod); PlotIndexSetInteger(0, PLOT_DRAW_BEGIN, InpMFIPeriod);
PlotIndexSetInteger(1, PLOT_DRAW_BEGIN, InpMFIPeriod + InpMAPeriod - 1); PlotIndexSetInteger(1, PLOT_DRAW_BEGIN, InpMFIPeriod + InpMAPeriod - 1);
IndicatorSetInteger(INDICATOR_DIGITS, 2); IndicatorSetInteger(INDICATOR_DIGITS, 2);
@@ -106,18 +99,13 @@ int OnInit()
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,
@@ -130,23 +118,19 @@ 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
g_calculator.Calculate(rates_total, open, high, low, close, tick_volume, volume, BufferMFI, BufferSignal); g_calculator.Calculate(rates_total, prev_calculated, open, high, low, close, tick_volume, volume, BufferMFI, BufferSignal);
//--- Hide signal line buffer if not needed
if(InpDisplayMode == DISPLAY_MFI_ONLY) if(InpDisplayMode == DISPLAY_MFI_ONLY)
{ {
for(int i = 0; i < rates_total; i++) int start = (prev_calculated > 0) ? prev_calculated - 1 : 0;
{ for(int i = start; i < rates_total; i++)
BufferSignal[i] = EMPTY_VALUE; BufferSignal[i] = EMPTY_VALUE;
}
} }
//--- Return rates_total for a full recalculation, ensuring stability
return(rates_total); return(rates_total);
} }
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+