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 |
//| Copyright 2025, xxxxxxxx|
//| |
//| Jurik_MA_Pro.mq5 |
//| Copyright 2026, xxxxxxxx|
//+------------------------------------------------------------------+
#property copyright "Copyright 2025, xxxxxxxx"
#property version "1.01"
#property description "Jurik Moving Average (JMA) indicator based on the revealed algorithm."
#property copyright "Copyright 2026, xxxxxxxx"
#property version "2.01" // Optimized for incremental calculation (O(1))
#property description "Professional Jurik Moving Average (JMA) with full Heikin Ashi support."
#property indicator_chart_window
#property indicator_buffers 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_color1 clrCrimson
#property indicator_style1 STYLE_SOLID
#property indicator_width1 2
#property indicator_label1 "JMA"
//--- Input Parameters ---
input int InpLength = 14; // JMA Length (influences smoothness)
input double InpPhase = 0; // JMA Phase (-100 to +100, influences overshoot/undershoot)
#include <MyIncludes\Jurik_Calculator.mqh>
//--- 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[];
//--- Global calculator object ---
CJurikMACalculator *g_calculator;
//--- Global Objects
CJurik_Calculator *g_calculator;
//+------------------------------------------------------------------+
//| Custom indicator initialization function. |
//| OnInit |
//+------------------------------------------------------------------+
int OnInit()
{
SetIndexBuffer(0, BufferJMA, INDICATOR_DATA);
ArraySetAsSeries(BufferJMA, false);
ArraySetAsSeries(BufferJMA, false); // Standard chronological order
PlotIndexSetInteger(0, PLOT_DRAW_BEGIN, InpLength);
IndicatorSetString(INDICATOR_SHORTNAME, StringFormat("JMA(%d, %.1f)", InpLength, InpPhase));
//--- Factory Logic for Calculator
// 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();
if(CheckPointer(g_calculator) == POINTER_INVALID || !g_calculator.Init(InpLength, InpPhase, 0))
//--- Initialize Calculator
if(CheckPointer(g_calculator) == POINTER_INVALID || !g_calculator.Init(InpLength, InpPhase))
{
Print("Failed to initialize Jurik Calculator.");
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);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function. |
//| OnDeinit |
//+------------------------------------------------------------------+
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,
const int prev_calculated,
@@ -73,13 +84,13 @@ int OnCalculate(const int rates_total,
const long &volume[],
const int &spread[])
{
if(CheckPointer(g_calculator) != POINTER_INVALID)
{
//--- Corrected: Pass dummy arrays for the unused Band and Volatility outputs
double dummy_upper[], dummy_lower[], dummy_volty[];
g_calculator.Calculate(rates_total, open, high, low, close,
BufferJMA, dummy_upper, dummy_lower, dummy_volty);
}
if(rates_total < InpLength)
return(0);
//--- Run Calculator
// We pass the custom enum directly
g_calculator.Calculate(rates_total, prev_calculated, InpPrice, open, high, low, close, BufferJMA);
return(rates_total);
}
//+------------------------------------------------------------------+