refactor: Optimized for incremental calculation

This commit is contained in:
Toh4iem9
2025-12-08 12:44:11 +01:00
parent 9992f791f7
commit 84edef35ae
@@ -1,10 +1,9 @@
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
//| Bollinger_ATR_Oscillator.mq5 | //| Bollinger_ATR_Oscillator.mq5 |
//| Copyright 2025, xxxxxxxx| //| Copyright 2025, xxxxxxxx|
//| |
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
#property copyright "Copyright 2025, xxxxxxxx" #property copyright "Copyright 2025, xxxxxxxx"
#property version "2.01" #property version "2.20" // Optimized for incremental calculation
#property description "Bollinger Bands ATR Oscillator by Jon Anderson." #property description "Bollinger Bands ATR Oscillator by Jon Anderson."
#property description "Includes a full range of standard and Heikin Ashi price sources." #property description "Includes a full range of standard and Heikin Ashi price sources."
@@ -26,6 +25,7 @@ input int InpAtrPeriod = 22;
input int InpBandsPeriod = 55; input int InpBandsPeriod = 55;
input double InpBandsDev = 2.0; input double InpBandsDev = 2.0;
input ENUM_APPLIED_PRICE_HA_ALL InpSourcePrice = PRICE_CLOSE_STD; input ENUM_APPLIED_PRICE_HA_ALL InpSourcePrice = PRICE_CLOSE_STD;
input ENUM_ATR_SOURCE InpAtrSource = ATR_SOURCE_STANDARD; // Default to Standard (Hybrid)
//--- Indicator Buffers --- //--- Indicator Buffers ---
double BufferOscillator[]; double BufferOscillator[];
@@ -33,8 +33,6 @@ double BufferOscillator[];
//--- Global calculator object --- //--- Global calculator object ---
CBollingerATROscillatorCalculator *g_calculator; CBollingerATROscillatorCalculator *g_calculator;
//+------------------------------------------------------------------+
//| Custom indicator initialization function. |
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
int OnInit() int OnInit()
{ {
@@ -44,29 +42,31 @@ int OnInit()
if(InpSourcePrice <= PRICE_HA_CLOSE) if(InpSourcePrice <= PRICE_HA_CLOSE)
{ {
g_calculator = new CBollingerATROscillatorCalculator_HA(); g_calculator = new CBollingerATROscillatorCalculator_HA();
IndicatorSetString(INDICATOR_SHORTNAME, StringFormat("BB_ATR_Osc HA(%d, %d)", InpAtrPeriod, InpBandsPeriod));
} }
else else
{ {
g_calculator = new CBollingerATROscillatorCalculator(); g_calculator = new CBollingerATROscillatorCalculator();
IndicatorSetString(INDICATOR_SHORTNAME, StringFormat("BB_ATR_Osc(%d, %d)", InpAtrPeriod, InpBandsPeriod));
} }
if(CheckPointer(g_calculator) == POINTER_INVALID || if(CheckPointer(g_calculator) == POINTER_INVALID ||
!g_calculator.Init(InpAtrPeriod, InpBandsPeriod, InpBandsDev)) !g_calculator.Init(InpAtrPeriod, InpBandsPeriod, InpBandsDev, InpAtrSource))
{ {
Print("Failed to initialize Bollinger ATR Oscillator Calculator."); Print("Failed to initialize Bollinger ATR Oscillator Calculator.");
return(INIT_FAILED); return(INIT_FAILED);
} }
string atr_src_str = (InpAtrSource == ATR_SOURCE_HEIKIN_ASHI) ? "HA" : "Std";
if(InpSourcePrice <= PRICE_HA_CLOSE)
IndicatorSetString(INDICATOR_SHORTNAME, StringFormat("BB_ATR_Osc HA(%d, %d, ATR:%s)", InpAtrPeriod, InpBandsPeriod, atr_src_str));
else
IndicatorSetString(INDICATOR_SHORTNAME, StringFormat("BB_ATR_Osc(%d, %d, ATR:%s)", InpAtrPeriod, InpBandsPeriod, atr_src_str));
PlotIndexSetInteger(0, PLOT_DRAW_BEGIN, MathMax(InpAtrPeriod, InpBandsPeriod)); PlotIndexSetInteger(0, PLOT_DRAW_BEGIN, MathMax(InpAtrPeriod, InpBandsPeriod));
IndicatorSetInteger(INDICATOR_DIGITS, 4); IndicatorSetInteger(INDICATOR_DIGITS, 4);
return(INIT_SUCCEEDED); return(INIT_SUCCEEDED);
} }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function. |
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
void OnDeinit(const int reason) void OnDeinit(const int reason)
{ {
@@ -75,9 +75,7 @@ void OnDeinit(const int reason)
} }
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
//| Custom indicator iteration function. | int OnCalculate(const int rates_total, const int prev_calculated, const datetime&[], const double &open[], const double &high[], const double &low[], const double &close[], const long&[], const long&[], const int&[])
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total, const int, const datetime&[], const double &open[], const double &high[], const double &low[], const double &close[], const long&[], const long&[], const int&[])
{ {
if(CheckPointer(g_calculator) != POINTER_INVALID) if(CheckPointer(g_calculator) != POINTER_INVALID)
{ {
@@ -87,9 +85,9 @@ int OnCalculate(const int rates_total, const int, const datetime&[], const doubl
else else
price_type = (ENUM_APPLIED_PRICE)InpSourcePrice; price_type = (ENUM_APPLIED_PRICE)InpSourcePrice;
g_calculator.Calculate(rates_total, price_type, open, high, low, close, BufferOscillator); //--- Delegate calculation with prev_calculated optimization
g_calculator.Calculate(rates_total, prev_calculated, price_type, open, high, low, close, BufferOscillator);
} }
return(rates_total); return(rates_total);
} }
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
//+------------------------------------------------------------------+