refactor: Optimized for incremental calculation

This commit is contained in:
Toh4iem9
2025-12-28 08:32:44 +01:00
parent da4a8b3cf7
commit 64531dcee0
+7 -24
View File
@@ -1,19 +1,17 @@
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
//| Aroon_Pro.mq5 | //| Aroon_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 "1.10" // Added fixed 0-100 scale
#property description "Aroon indicator with selectable candle source (Standard or Heikin Ashi)." #property description "Aroon indicator with selectable candle source (Standard or Heikin Ashi)."
//--- Indicator Window and Level Properties --- //--- Indicator Window and Level Properties ---
#property indicator_separate_window #property indicator_separate_window
#property indicator_buffers 2 #property indicator_buffers 2
#property indicator_plots 2 #property indicator_plots 2
#property indicator_minimum 0 // <-- FIX: Force the bottom of the scale to 0 #property indicator_minimum 0
#property indicator_maximum 105 // <-- FIX: Force the top of the scale to 105 #property indicator_maximum 105
#property indicator_level1 30.0 #property indicator_level1 30.0
#property indicator_level2 50.0 #property indicator_level2 50.0
#property indicator_level3 70.0 #property indicator_level3 70.0
@@ -51,43 +49,35 @@ input ENUM_CANDLE_SOURCE InpCandleSource = CANDLE_STANDARD; // Candle source
double BufferAroonUp[]; double BufferAroonUp[];
double BufferAroonDown[]; double BufferAroonDown[];
//--- Global calculator object (as a base class pointer) --- //--- Global calculator object ---
CAroonCalculator *g_calculator; CAroonCalculator *g_calculator;
//+------------------------------------------------------------------+
//| Custom indicator initialization function. |
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
int OnInit() int OnInit()
{ {
//--- Map the buffers
SetIndexBuffer(0, BufferAroonUp, INDICATOR_DATA); SetIndexBuffer(0, BufferAroonUp, INDICATOR_DATA);
SetIndexBuffer(1, BufferAroonDown, INDICATOR_DATA); SetIndexBuffer(1, BufferAroonDown, INDICATOR_DATA);
//--- Set all buffers as non-timeseries for stable calculation
ArraySetAsSeries(BufferAroonUp, false); ArraySetAsSeries(BufferAroonUp, false);
ArraySetAsSeries(BufferAroonDown, false); ArraySetAsSeries(BufferAroonDown, false);
//--- Dynamically create the appropriate calculator instance
switch(InpCandleSource) switch(InpCandleSource)
{ {
case CANDLE_HEIKIN_ASHI: case CANDLE_HEIKIN_ASHI:
g_calculator = new CAroonCalculator_HA(); g_calculator = new CAroonCalculator_HA();
IndicatorSetString(INDICATOR_SHORTNAME, StringFormat("Aroon Pro HA(%d)", InpPeriodAroon)); IndicatorSetString(INDICATOR_SHORTNAME, StringFormat("Aroon Pro HA(%d)", InpPeriodAroon));
break; break;
default: // CANDLE_STANDARD default:
g_calculator = new CAroonCalculator(); g_calculator = new CAroonCalculator();
IndicatorSetString(INDICATOR_SHORTNAME, StringFormat("Aroon Pro(%d)", InpPeriodAroon)); IndicatorSetString(INDICATOR_SHORTNAME, StringFormat("Aroon Pro(%d)", InpPeriodAroon));
break; break;
} }
//--- Check if creation was successful and initialize
if(CheckPointer(g_calculator) == POINTER_INVALID || !g_calculator.Init(InpPeriodAroon)) if(CheckPointer(g_calculator) == POINTER_INVALID || !g_calculator.Init(InpPeriodAroon))
{ {
Print("Failed to create or initialize Aroon Calculator object."); Print("Failed to create or initialize Aroon Calculator object.");
return(INIT_FAILED); return(INIT_FAILED);
} }
//--- Set indicator properties
int period = g_calculator.GetPeriod(); int period = g_calculator.GetPeriod();
IndicatorSetInteger(INDICATOR_DIGITS, 2); IndicatorSetInteger(INDICATOR_DIGITS, 2);
PlotIndexSetInteger(0, PLOT_DRAW_BEGIN, period - 1); PlotIndexSetInteger(0, PLOT_DRAW_BEGIN, period - 1);
@@ -96,18 +86,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,
@@ -120,14 +105,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
g_calculator.Calculate(rates_total, high, low, BufferAroonUp, BufferAroonDown); g_calculator.Calculate(rates_total, prev_calculated, open, high, low, close, BufferAroonUp, BufferAroonDown);
//--- Return rates_total for a full recalculation, ensuring stability
return(rates_total); return(rates_total);
} }
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+