refactor(indicators): Optimized for incremental calculation (O(1))

This commit is contained in:
Toh4iem9
2026-01-16 12:10:43 +01:00
parent 1f6664c31c
commit 3062e3576e
@@ -1,57 +1,68 @@
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
//| Jurik_MA.mq5 | //| Jurik_MA_Pro.mq5 |
//| Copyright 2025, xxxxxxxx| //| Copyright 2026, xxxxxxxx|
//| |
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
#property copyright "Copyright 2025, xxxxxxxx" #property copyright "Copyright 2026, xxxxxxxx"
#property version "1.01" #property version "2.01" // Optimized for incremental calculation (O(1))
#property description "Jurik Moving Average (JMA) indicator based on the revealed algorithm." #property description "Professional Jurik Moving Average (JMA) with full Heikin Ashi support."
#property indicator_chart_window #property indicator_chart_window
#property indicator_buffers 1 #property indicator_buffers 1
#property indicator_plots 1 #property indicator_plots 1
#include <MyIncludes\Jurik_Calculators.mqh>
//--- Plot 1: JMA Line
#property indicator_label1 "JMA"
#property indicator_type1 DRAW_LINE #property indicator_type1 DRAW_LINE
#property indicator_color1 clrCrimson #property indicator_color1 clrCrimson
#property indicator_style1 STYLE_SOLID #property indicator_style1 STYLE_SOLID
#property indicator_width1 2 #property indicator_width1 2
#property indicator_label1 "JMA"
//--- Input Parameters --- #include <MyIncludes\Jurik_Calculator.mqh>
input int InpLength = 14; // JMA Length (influences smoothness)
input double InpPhase = 0; // JMA Phase (-100 to +100, influences overshoot/undershoot)
//--- Indicator Buffers --- //--- Input Parameters
input int InpLength = 14; // JMA Length
input double InpPhase = 0; // JMA Phase (-100 to +100)
input ENUM_APPLIED_PRICE_HA_ALL InpPrice = PRICE_CLOSE_STD; // Applied Price
//--- Indicator Buffers
double BufferJMA[]; double BufferJMA[];
//--- Global calculator object --- //--- Global Objects
CJurikMACalculator *g_calculator; CJurik_Calculator *g_calculator;
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
//| Custom indicator initialization function. | //| OnInit |
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
int OnInit() int OnInit()
{ {
SetIndexBuffer(0, BufferJMA, INDICATOR_DATA); SetIndexBuffer(0, BufferJMA, INDICATOR_DATA);
ArraySetAsSeries(BufferJMA, false); ArraySetAsSeries(BufferJMA, false); // Standard chronological order
PlotIndexSetInteger(0, PLOT_DRAW_BEGIN, InpLength); //--- Factory Logic for Calculator
IndicatorSetString(INDICATOR_SHORTNAME, StringFormat("JMA(%d, %.1f)", InpLength, InpPhase)); // HA prices are usually negative in our enum, or specifically defined
if(InpPrice <= PRICE_HA_CLOSE)
g_calculator = new CJurik_Calculator_HA();
else
g_calculator = new CJurik_Calculator();
g_calculator = new CJurikMACalculator(); //--- Initialize Calculator
if(CheckPointer(g_calculator) == POINTER_INVALID || !g_calculator.Init(InpLength, InpPhase, 0)) if(CheckPointer(g_calculator) == POINTER_INVALID || !g_calculator.Init(InpLength, InpPhase))
{ {
Print("Failed to initialize Jurik Calculator."); Print("Failed to initialize Jurik Calculator.");
return(INIT_FAILED); return(INIT_FAILED);
} }
//--- Visual Setup
string price_str = "Std";
if(InpPrice <= PRICE_HA_CLOSE)
price_str = "HA";
IndicatorSetString(INDICATOR_SHORTNAME, StringFormat("JMA_Pro(%d, %.1f, %s)", InpLength, InpPhase, price_str));
PlotIndexSetInteger(0, PLOT_DRAW_BEGIN, InpLength);
return(INIT_SUCCEEDED); return(INIT_SUCCEEDED);
} }
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
//| Custom indicator deinitialization function. | //| OnDeinit |
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
void OnDeinit(const int reason) void OnDeinit(const int reason)
{ {
@@ -60,7 +71,7 @@ void OnDeinit(const int reason)
} }
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
//| Custom indicator iteration function. | //| OnCalculate |
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
int OnCalculate(const int rates_total, int OnCalculate(const int rates_total,
const int prev_calculated, const int prev_calculated,
@@ -73,13 +84,13 @@ int OnCalculate(const int rates_total,
const long &volume[], const long &volume[],
const int &spread[]) const int &spread[])
{ {
if(CheckPointer(g_calculator) != POINTER_INVALID) if(rates_total < InpLength)
{ return(0);
//--- Corrected: Pass dummy arrays for the unused Band and Volatility outputs
double dummy_upper[], dummy_lower[], dummy_volty[]; //--- Run Calculator
g_calculator.Calculate(rates_total, open, high, low, close, // We pass the custom enum directly
BufferJMA, dummy_upper, dummy_lower, dummy_volty); g_calculator.Calculate(rates_total, prev_calculated, InpPrice, open, high, low, close, BufferJMA);
}
return(rates_total); return(rates_total);
} }
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+