refactor(indicators): Optimized for incremental calculation

This commit is contained in:
Toh4iem9
2025-12-31 17:29:52 +01:00
parent 3f50b02443
commit c2f76b8fb1
+5 -20
View File
@@ -1,11 +1,9 @@
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
//| AD_Pro.mq5 | //| AD_Pro.mq5 |
//| Copyright 2025, xxxxxxxx| //| Copyright 2025, xxxxxxxx|
//| |
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
#property copyright "Copyright 2025, xxxxxxxx" #property copyright "Copyright 2025, xxxxxxxx"
#property link "" #property version "2.00" // Optimized for incremental calculation
#property version "2.01" // Corrected calculator logic
#property description "Professional Accumulation/Distribution Line with selectable" #property description "Professional Accumulation/Distribution Line with selectable"
#property description "candle source (Standard or Heikin Ashi)." #property description "candle source (Standard or Heikin Ashi)."
@@ -34,57 +32,46 @@ input ENUM_APPLIED_VOLUME InpVolumeType = VOLUME_TICK; // Volume type
//--- Indicator Buffers --- //--- Indicator Buffers ---
double BufferAD[]; double BufferAD[];
//--- Global calculator object (as a base class pointer) --- //--- Global calculator object ---
CADCalculator *g_calculator; CADCalculator *g_calculator;
//+------------------------------------------------------------------+
//| Custom indicator initialization function. |
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
int OnInit() int OnInit()
{ {
//--- Map the buffer and set as non-timeseries
SetIndexBuffer(0, BufferAD, INDICATOR_DATA); SetIndexBuffer(0, BufferAD, INDICATOR_DATA);
ArraySetAsSeries(BufferAD, false); ArraySetAsSeries(BufferAD, false);
//--- Dynamically create the appropriate calculator instance
switch(InpCandleSource) switch(InpCandleSource)
{ {
case CANDLE_HEIKIN_ASHI: case CANDLE_HEIKIN_ASHI:
g_calculator = new CADCalculator_HA(); g_calculator = new CADCalculator_HA();
IndicatorSetString(INDICATOR_SHORTNAME, "HA A/D"); IndicatorSetString(INDICATOR_SHORTNAME, "HA A/D");
break; break;
default: // CANDLE_STANDARD default:
g_calculator = new CADCalculator(); g_calculator = new CADCalculator();
IndicatorSetString(INDICATOR_SHORTNAME, "A/D"); IndicatorSetString(INDICATOR_SHORTNAME, "A/D");
break; break;
} }
//--- Check if creation was successful
if(CheckPointer(g_calculator) == POINTER_INVALID) if(CheckPointer(g_calculator) == POINTER_INVALID)
{ {
Print("Failed to create A/D Calculator object."); Print("Failed to create A/D Calculator object.");
return(INIT_FAILED); return(INIT_FAILED);
} }
//--- Set indicator display properties
IndicatorSetInteger(INDICATOR_DIGITS, 0); IndicatorSetInteger(INDICATOR_DIGITS, 0);
PlotIndexSetInteger(0, PLOT_DRAW_BEGIN, 1); PlotIndexSetInteger(0, PLOT_DRAW_BEGIN, 1);
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,
@@ -97,14 +84,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 (now passing 'open') // Delegate calculation with incremental optimization
g_calculator.Calculate(rates_total, open, high, low, close, tick_volume, volume, InpVolumeType, BufferAD); g_calculator.Calculate(rates_total, prev_calculated, open, high, low, close, tick_volume, volume, InpVolumeType, BufferAD);
//--- Return rates_total for a full recalculation, ensuring stability
return(rates_total); return(rates_total);
} }
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+