mirror of
https://github.com/softwaredevelop/mql5.git
synced 2026-08-19 15:28:06 +00:00
refactor: Optimized for incremental MTF calculation
This commit is contained in:
@@ -1,13 +1,10 @@
|
|||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
//| VIDYA_MTF_Pro.mq5 |
|
//| VIDYA_MTF_Pro.mq5 |
|
||||||
//| Copyright 2025, xxxxxxxx|
|
//| Copyright 2025, xxxxxxxx|
|
||||||
//| |
|
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
#property copyright "Copyright 2025, xxxxxxxx"
|
#property copyright "Copyright 2025, xxxxxxxx"
|
||||||
#property link ""
|
#property version "2.20" // Optimized for incremental MTF calculation
|
||||||
#property version "2.10" // REFACTORED: Handles current timeframe correctly.
|
|
||||||
#property description "Multi-Timeframe (MTF) Variable Index Dynamic Average (VIDYA)."
|
#property description "Multi-Timeframe (MTF) Variable Index Dynamic Average (VIDYA)."
|
||||||
#property description "Displays VIDYA from a higher or the current timeframe on the chart."
|
|
||||||
|
|
||||||
//--- Indicator Window and Plot Properties ---
|
//--- Indicator Window and Plot Properties ---
|
||||||
#property indicator_chart_window
|
#property indicator_chart_window
|
||||||
@@ -31,22 +28,21 @@ input ENUM_APPLIED_PRICE_HA_ALL InpSourcePrice = PRICE_CLOSE_STD;
|
|||||||
//--- Indicator Buffers ---
|
//--- Indicator Buffers ---
|
||||||
double BufferVIDYA_MTF[];
|
double BufferVIDYA_MTF[];
|
||||||
|
|
||||||
|
//--- Internal Buffer for HTF Calculation (Global to persist state)
|
||||||
|
double BufferVIDYA_HTF_Internal[];
|
||||||
|
|
||||||
//--- Global variables ---
|
//--- Global variables ---
|
||||||
CVIDYACalculator *g_calculator;
|
CVIDYACalculator *g_calculator;
|
||||||
bool g_is_mtf_mode = false;
|
bool g_is_mtf_mode = false;
|
||||||
ENUM_TIMEFRAMES g_calc_timeframe;
|
ENUM_TIMEFRAMES g_calc_timeframe;
|
||||||
|
|
||||||
//+------------------------------------------------------------------+
|
|
||||||
//| Custom indicator initialization function. |
|
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
int OnInit()
|
int OnInit()
|
||||||
{
|
{
|
||||||
// --- Determine calculation mode (MTF or Current) ---
|
// --- Determine calculation mode (MTF or Current) ---
|
||||||
g_calc_timeframe = InpUpperTimeframe;
|
g_calc_timeframe = InpUpperTimeframe;
|
||||||
if(g_calc_timeframe == PERIOD_CURRENT)
|
if(g_calc_timeframe == PERIOD_CURRENT)
|
||||||
{
|
|
||||||
g_calc_timeframe = (ENUM_TIMEFRAMES)Period();
|
g_calc_timeframe = (ENUM_TIMEFRAMES)Period();
|
||||||
}
|
|
||||||
|
|
||||||
if(g_calc_timeframe < Period())
|
if(g_calc_timeframe < Period())
|
||||||
{
|
{
|
||||||
@@ -56,19 +52,15 @@ int OnInit()
|
|||||||
|
|
||||||
g_is_mtf_mode = (g_calc_timeframe > Period());
|
g_is_mtf_mode = (g_calc_timeframe > Period());
|
||||||
|
|
||||||
// --- Standard buffer and calculator setup ---
|
// --- Standard buffer setup ---
|
||||||
SetIndexBuffer(0, BufferVIDYA_MTF, INDICATOR_DATA);
|
SetIndexBuffer(0, BufferVIDYA_MTF, INDICATOR_DATA);
|
||||||
ArraySetAsSeries(BufferVIDYA_MTF, false);
|
ArraySetAsSeries(BufferVIDYA_MTF, false);
|
||||||
PlotIndexSetDouble(0, PLOT_EMPTY_VALUE, EMPTY_VALUE);
|
PlotIndexSetDouble(0, PLOT_EMPTY_VALUE, EMPTY_VALUE);
|
||||||
|
|
||||||
if(InpSourcePrice <= PRICE_HA_CLOSE)
|
if(InpSourcePrice <= PRICE_HA_CLOSE)
|
||||||
{
|
|
||||||
g_calculator = new CVIDYACalculator_HA();
|
g_calculator = new CVIDYACalculator_HA();
|
||||||
}
|
|
||||||
else
|
else
|
||||||
{
|
|
||||||
g_calculator = new CVIDYACalculator();
|
g_calculator = new CVIDYACalculator();
|
||||||
}
|
|
||||||
|
|
||||||
if(CheckPointer(g_calculator) == POINTER_INVALID || !g_calculator.Init(InpPeriodCMO, InpPeriodEMA))
|
if(CheckPointer(g_calculator) == POINTER_INVALID || !g_calculator.Init(InpPeriodCMO, InpPeriodEMA))
|
||||||
{
|
{
|
||||||
@@ -87,19 +79,17 @@ int OnInit()
|
|||||||
return(INIT_SUCCEEDED);
|
return(INIT_SUCCEEDED);
|
||||||
}
|
}
|
||||||
|
|
||||||
//+------------------------------------------------------------------+
|
|
||||||
//| Custom indicator deinitialization function. |
|
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
void OnDeinit(const int reason)
|
void OnDeinit(const int reason)
|
||||||
{
|
{
|
||||||
if(CheckPointer(g_calculator) != POINTER_INVALID)
|
if(CheckPointer(g_calculator) != POINTER_INVALID)
|
||||||
delete g_calculator;
|
delete g_calculator;
|
||||||
|
|
||||||
|
ArrayFree(BufferVIDYA_HTF_Internal);
|
||||||
}
|
}
|
||||||
|
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
//| 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[])
|
||||||
//+------------------------------------------------------------------+
|
|
||||||
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 &tick_volume[], const long &volume[], const int &spread[])
|
|
||||||
{
|
{
|
||||||
if(rates_total < 2 || CheckPointer(g_calculator) == POINTER_INVALID)
|
if(rates_total < 2 || CheckPointer(g_calculator) == POINTER_INVALID)
|
||||||
return 0;
|
return 0;
|
||||||
@@ -118,46 +108,61 @@ int OnCalculate(const int rates_total, const int, const datetime &time[], const
|
|||||||
if(htf_rates_total < InpPeriodCMO + InpPeriodEMA)
|
if(htf_rates_total < InpPeriodCMO + InpPeriodEMA)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
|
// --- Manage HTF State (Incremental Logic) ---
|
||||||
|
static int htf_prev_calculated = 0;
|
||||||
|
if(prev_calculated == 0)
|
||||||
|
htf_prev_calculated = 0;
|
||||||
|
|
||||||
datetime htf_time[];
|
datetime htf_time[];
|
||||||
double htf_open[], htf_high[], htf_low[], htf_close[];
|
double htf_open[], htf_high[], htf_low[], htf_close[];
|
||||||
|
|
||||||
|
// Optimization: We could copy only new bars, but for safety with CopyTime/BarShift,
|
||||||
|
// copying full history on HTF is usually fast enough. The math is the bottleneck.
|
||||||
if(CopyTime(_Symbol, g_calc_timeframe, 0, htf_rates_total, htf_time) <= 0 ||
|
if(CopyTime(_Symbol, g_calc_timeframe, 0, htf_rates_total, htf_time) <= 0 ||
|
||||||
CopyOpen(_Symbol, g_calc_timeframe, 0, htf_rates_total, htf_open) <= 0 ||
|
CopyOpen(_Symbol, g_calc_timeframe, 0, htf_rates_total, htf_open) <= 0 ||
|
||||||
CopyHigh(_Symbol, g_calc_timeframe, 0, htf_rates_total, htf_high) <= 0 ||
|
CopyHigh(_Symbol, g_calc_timeframe, 0, htf_rates_total, htf_high) <= 0 ||
|
||||||
CopyLow(_Symbol, g_calc_timeframe, 0, htf_rates_total, htf_low) <= 0 ||
|
CopyLow(_Symbol, g_calc_timeframe, 0, htf_rates_total, htf_low) <= 0 ||
|
||||||
CopyClose(_Symbol, g_calc_timeframe, 0, htf_rates_total, htf_close) <= 0)
|
CopyClose(_Symbol, g_calc_timeframe, 0, htf_rates_total, htf_close) <= 0)
|
||||||
{
|
{
|
||||||
return 0; // Data not ready
|
return 0; // Data not fully ready
|
||||||
}
|
}
|
||||||
|
|
||||||
double htf_vidya_buffer[];
|
if(ArraySize(BufferVIDYA_HTF_Internal) != htf_rates_total)
|
||||||
ArrayResize(htf_vidya_buffer, htf_rates_total);
|
ArrayResize(BufferVIDYA_HTF_Internal, htf_rates_total);
|
||||||
g_calculator.Calculate(htf_rates_total, price_type, htf_open, htf_high, htf_low, htf_close, htf_vidya_buffer);
|
|
||||||
|
|
||||||
ArraySetAsSeries(htf_vidya_buffer, true);
|
// Incremental Calculation on HTF
|
||||||
|
g_calculator.Calculate(htf_rates_total, htf_prev_calculated, price_type, htf_open, htf_high, htf_low, htf_close, BufferVIDYA_HTF_Internal);
|
||||||
|
|
||||||
|
htf_prev_calculated = htf_rates_total;
|
||||||
|
|
||||||
|
// Mapping (Optimized Loop)
|
||||||
|
ArraySetAsSeries(BufferVIDYA_HTF_Internal, true);
|
||||||
ArraySetAsSeries(htf_time, true);
|
ArraySetAsSeries(htf_time, true);
|
||||||
ArraySetAsSeries(time, true);
|
ArraySetAsSeries(time, true);
|
||||||
ArraySetAsSeries(BufferVIDYA_MTF, true);
|
ArraySetAsSeries(BufferVIDYA_MTF, true);
|
||||||
|
|
||||||
for(int i = 0; i < rates_total; i++)
|
int limit = (prev_calculated > 0) ? rates_total - prev_calculated : rates_total;
|
||||||
|
|
||||||
|
for(int i = 0; i < limit; i++)
|
||||||
{
|
{
|
||||||
int htf_bar_shift = iBarShift(_Symbol, g_calc_timeframe, time[i]);
|
int htf_bar_shift = iBarShift(_Symbol, g_calc_timeframe, time[i], false);
|
||||||
if(htf_bar_shift < htf_rates_total && htf_bar_shift >= 0)
|
if(htf_bar_shift < htf_rates_total && htf_bar_shift >= 0)
|
||||||
BufferVIDYA_MTF[i] = htf_vidya_buffer[htf_bar_shift];
|
BufferVIDYA_MTF[i] = BufferVIDYA_HTF_Internal[htf_bar_shift];
|
||||||
else
|
else
|
||||||
BufferVIDYA_MTF[i] = EMPTY_VALUE;
|
BufferVIDYA_MTF[i] = EMPTY_VALUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
ArraySetAsSeries(BufferVIDYA_MTF, false);
|
ArraySetAsSeries(BufferVIDYA_MTF, false);
|
||||||
ArraySetAsSeries(time, false);
|
ArraySetAsSeries(time, false);
|
||||||
|
ArraySetAsSeries(BufferVIDYA_HTF_Internal, false);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// --- Current Timeframe Mode ---
|
// --- Current Timeframe Mode ---
|
||||||
// This is the simple logic from the original VIDYA_Pro.mq5
|
// Incremental Calculation
|
||||||
g_calculator.Calculate(rates_total, price_type, open, high, low, close, BufferVIDYA_MTF);
|
g_calculator.Calculate(rates_total, prev_calculated, price_type, open, high, low, close, BufferVIDYA_MTF);
|
||||||
}
|
}
|
||||||
|
|
||||||
return(rates_total);
|
return(rates_total);
|
||||||
}
|
}
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
//+------------------------------------------------------------------+
|
|
||||||
|
|||||||
Reference in New Issue
Block a user