refactor: VIDYA_Calculator

This commit is contained in:
Toh4iem9
2025-11-06 08:40:43 +01:00
parent 49ef1e344f
commit 29129cb363
+6 -30
View File
@@ -9,7 +9,6 @@
#property description "Professional Variable Index Dynamic Average (VIDYA) with selectable" #property description "Professional Variable Index Dynamic Average (VIDYA) with selectable"
#property description "price source (Standard and Heikin Ashi)." #property description "price source (Standard and Heikin Ashi)."
//--- Indicator Window and Plot Properties ---
#property indicator_chart_window #property indicator_chart_window
#property indicator_buffers 1 #property indicator_buffers 1
#property indicator_plots 1 #property indicator_plots 1
@@ -19,22 +18,17 @@
#property indicator_width1 1 #property indicator_width1 1
#property indicator_label1 "VIDYA" #property indicator_label1 "VIDYA"
//--- Include the calculator engine ---
#include <MyIncludes\VIDYA_Calculator.mqh> #include <MyIncludes\VIDYA_Calculator.mqh>
//--- Input Parameters ---
input int InpPeriodCMO = 9; input int InpPeriodCMO = 9;
input int InpPeriodEMA = 12; input int InpPeriodEMA = 12;
input ENUM_APPLIED_PRICE_HA_ALL InpSourcePrice = PRICE_CLOSE_STD; input ENUM_APPLIED_PRICE_HA_ALL InpSourcePrice = PRICE_CLOSE_STD;
//--- Indicator Buffers ---
double BufferVIDYA[]; double BufferVIDYA[];
//--- Global calculator object (as a base class pointer) ---
CVIDYACalculator *g_calculator; CVIDYACalculator *g_calculator;
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
//| Custom indicator initialization function. | //| |
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
int OnInit() int OnInit()
{ {
@@ -42,15 +36,9 @@ int OnInit()
ArraySetAsSeries(BufferVIDYA, false); ArraySetAsSeries(BufferVIDYA, false);
if(InpSourcePrice <= PRICE_HA_CLOSE) if(InpSourcePrice <= PRICE_HA_CLOSE)
{
g_calculator = new CVIDYACalculator_HA(); g_calculator = new CVIDYACalculator_HA();
IndicatorSetString(INDICATOR_SHORTNAME, StringFormat("VIDYA HA(%d,%d)", InpPeriodCMO, InpPeriodEMA));
}
else else
{
g_calculator = new CVIDYACalculator(); g_calculator = new CVIDYACalculator();
IndicatorSetString(INDICATOR_SHORTNAME, StringFormat("VIDYA(%d,%d)", InpPeriodCMO, InpPeriodEMA));
}
if(CheckPointer(g_calculator) == POINTER_INVALID || !g_calculator.Init(InpPeriodCMO, InpPeriodEMA)) if(CheckPointer(g_calculator) == POINTER_INVALID || !g_calculator.Init(InpPeriodCMO, InpPeriodEMA))
{ {
@@ -58,37 +46,25 @@ int OnInit()
return(INIT_FAILED); return(INIT_FAILED);
} }
IndicatorSetString(INDICATOR_SHORTNAME, StringFormat("VIDYA%s(%d,%d)", (InpSourcePrice <= PRICE_HA_CLOSE ? " HA" : ""), InpPeriodCMO, InpPeriodEMA));
PlotIndexSetInteger(0, PLOT_DRAW_BEGIN, InpPeriodCMO + InpPeriodEMA); PlotIndexSetInteger(0, PLOT_DRAW_BEGIN, InpPeriodCMO + InpPeriodEMA);
IndicatorSetInteger(INDICATOR_DIGITS, _Digits); IndicatorSetInteger(INDICATOR_DIGITS, _Digits);
return(INIT_SUCCEEDED); return(INIT_SUCCEEDED);
} }
//+------------------------------------------------------------------+ void OnDeinit(const int reason) { if(CheckPointer(g_calculator) != POINTER_INVALID) delete g_calculator; }
//| Custom indicator deinitialization function. |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
if(CheckPointer(g_calculator) != POINTER_INVALID)
delete g_calculator;
}
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
//| Custom indicator calculation function. | //| |
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
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&[]) 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)
return 0; return 0;
ENUM_APPLIED_PRICE price_type = (InpSourcePrice <= PRICE_HA_CLOSE) ? (ENUM_APPLIED_PRICE)(-(int)InpSourcePrice) : (ENUM_APPLIED_PRICE)InpSourcePrice;
ENUM_APPLIED_PRICE price_type; //--- This call automatically resolves to the single-buffer version ---
if(InpSourcePrice <= PRICE_HA_CLOSE)
price_type = (ENUM_APPLIED_PRICE)(-(int)InpSourcePrice);
else
price_type = (ENUM_APPLIED_PRICE)InpSourcePrice;
g_calculator.Calculate(rates_total, price_type, open, high, low, close, BufferVIDYA); g_calculator.Calculate(rates_total, price_type, open, high, low, close, BufferVIDYA);
return(rates_total); return(rates_total);
} }
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+