refactor: Optimized for incremental calculation & Consolidated Engine

This commit is contained in:
Toh4iem9
2025-11-28 18:02:57 +01:00
parent 7eb18a1030
commit 3213507516
@@ -4,14 +4,14 @@
//| |
//+------------------------------------------------------------------+
#property copyright "Copyright 2025, xxxxxxxx"
#property version "2.10" // Simplified to use a single, central timeframe
#property version "2.20" // Optimized for incremental calculation & Consolidated Engine
#property description "A 4-line MA Ribbon calculated on a single, user-selected timeframe."
#property indicator_chart_window
#property indicator_buffers 4
#property indicator_plots 4
//--- Plot Properties (Unchanged) ---
//--- Plot Properties
#property indicator_label1 "MA 1"
#property indicator_type1 DRAW_LINE
#property indicator_color1 clrLightSkyBlue
@@ -33,9 +33,10 @@
#property indicator_style4 STYLE_SOLID
#property indicator_width4 1
//--- Include the consolidated calculator engine
#include <MyIncludes\MovingAverage_Ribbon_MTF_Calculator.mqh>
//--- Input Parameters (SIMPLIFIED) ---
//--- Input Parameters
input group "Timeframe & Price Source"
input ENUM_TIMEFRAMES InpUpperTimeframe = PERIOD_H1;
input ENUM_APPLIED_PRICE_HA_ALL InpSourcePrice = PRICE_CLOSE_STD;
@@ -56,29 +57,36 @@ input group "MA 4 Settings"
input int InpPeriod4 = 34;
input ENUM_MA_TYPE InpMAType4 = EMA;
//--- Indicator Buffers ---
//--- Indicator Buffers
double BufferMA1[], BufferMA2[], BufferMA3[], BufferMA4[];
//--- Global calculator object ---
//--- Global calculator object
CMovingAverageRibbonMTFCalculator *g_calculator;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//--- Map Buffers
SetIndexBuffer(0, BufferMA1, INDICATOR_DATA);
SetIndexBuffer(1, BufferMA2, INDICATOR_DATA);
SetIndexBuffer(2, BufferMA3, INDICATOR_DATA);
SetIndexBuffer(3, BufferMA4, INDICATOR_DATA);
//--- Set as non-timeseries for standard loop
ArraySetAsSeries(BufferMA1, false);
ArraySetAsSeries(BufferMA2, false);
ArraySetAsSeries(BufferMA3, false);
ArraySetAsSeries(BufferMA4, false);
//--- Initialize Calculator
g_calculator = new CMovingAverageRibbonMTFCalculator();
bool is_ha = (InpSourcePrice <= PRICE_HA_CLOSE);
//--- UPDATED: Pass the single central timeframe to all four slots ---
//--- Initialize with parameters
//--- Note: We pass the same InpUpperTimeframe to all 4 lines as per this specific indicator design
if(CheckPointer(g_calculator) == POINTER_INVALID ||
!g_calculator.Init(InpUpperTimeframe, InpPeriod1, InpMAType1,
InpUpperTimeframe, InpPeriod2, InpMAType2,
@@ -90,35 +98,60 @@ int OnInit()
return(INIT_FAILED);
}
//--- Set Short Name
ENUM_TIMEFRAMES calc_tf = (InpUpperTimeframe == PERIOD_CURRENT) ? (ENUM_TIMEFRAMES)Period() : InpUpperTimeframe;
IndicatorSetString(INDICATOR_SHORTNAME, StringFormat("MA Ribbon MTF%s(%s)", (is_ha ? " HA" : ""), EnumToString(calc_tf)));
//--- Set Labels
PlotIndexSetString(0, PLOT_LABEL, StringFormat("%s(%d)", EnumToString(InpMAType1), InpPeriod1));
PlotIndexSetString(1, PLOT_LABEL, StringFormat("%s(%d)", EnumToString(InpMAType2), InpPeriod2));
PlotIndexSetString(2, PLOT_LABEL, StringFormat("%s(%d)", EnumToString(InpMAType3), InpPeriod3));
PlotIndexSetString(3, PLOT_LABEL, StringFormat("%s(%d)", EnumToString(InpMAType4), InpPeriod4));
//--- Set Draw Begin
int max_period = MathMax(InpPeriod1, MathMax(InpPeriod2, MathMax(InpPeriod3, InpPeriod4)));
PlotIndexSetInteger(0, PLOT_DRAW_BEGIN, max_period);
PlotIndexSetInteger(1, PLOT_DRAW_BEGIN, max_period);
PlotIndexSetInteger(2, PLOT_DRAW_BEGIN, max_period);
PlotIndexSetInteger(3, PLOT_DRAW_BEGIN, max_period);
IndicatorSetInteger(INDICATOR_DIGITS, _Digits);
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
void OnDeinit(const int reason) { if(CheckPointer(g_calculator) != POINTER_INVALID) delete g_calculator; }
//| Deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
if(CheckPointer(g_calculator) != POINTER_INVALID)
delete g_calculator;
}
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total, const int, const datetime &time[], const double &open[], const double &high[], const double &low[], const double &close[], const long&[], const long&[], const int&[])
//| Custom indicator calculation function |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
const int prev_calculated,
const datetime &time[],
const double &open[],
const double &high[],
const double &low[],
const double &close[],
const long &tick_volume[],
const long &volume[],
const int &spread[])
{
if(CheckPointer(g_calculator) == POINTER_INVALID)
return 0;
ENUM_APPLIED_PRICE price_type = (InpSourcePrice <= PRICE_HA_CLOSE) ? (ENUM_APPLIED_PRICE)(-(int)InpSourcePrice) : (ENUM_APPLIED_PRICE)InpSourcePrice;
g_calculator.Calculate(rates_total, time, price_type, open, high, low, close, BufferMA1, BufferMA2, BufferMA3, BufferMA4);
//--- Delegate calculation with prev_calculated optimization
g_calculator.Calculate(rates_total, prev_calculated, time, price_type, open, high, low, close,
BufferMA1, BufferMA2, BufferMA3, BufferMA4);
return(rates_total);
}
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+