refactor: True O(1) VWAP and Zero-Lag Tick Pipeline

This commit is contained in:
Toh4iem9
2026-09-27 14:02:03 +02:00
parent 3872c85ddb
commit 7d37b990f5
@@ -2,17 +2,16 @@
//| Session_Analysis_Single_Pro.mq5 |
//| Copyright 2026, xxxxxxxx|
//+------------------------------------------------------------------+
#property copyright "Copyright 2026, xxxxxxxx"
#property version "1.21" // Fixed incremental VWAP buffer-wipe ghost remnants
#property copyright "Copyright 2026, xxxxxxxx"
#property version "2.00" // Enterprise Refactor: True O(1) VWAP and Zero-Lag Tick Pipeline
#property description "Session Analysis for a SINGLE market."
#property description "Fully optimized for flicker-free real-time drawing and state-safe VWAP."
#property description "Optimized for massive multi-window execution with zero UI thread stuttering."
#property indicator_chart_window
#property indicator_buffers 8
#property indicator_plots 8
//--- Plot Properties ---
// Session 1: Pre-Market
#property indicator_label1 "Pre VWAP"
#property indicator_type1 DRAW_LINE
#property indicator_color1 clrSlateBlue
@@ -24,7 +23,6 @@
#property indicator_style2 STYLE_SOLID
#property indicator_width2 1
// Session 2: Core Trading
#property indicator_label3 "Core VWAP"
#property indicator_type3 DRAW_LINE
#property indicator_color3 clrSlateBlue
@@ -36,7 +34,6 @@
#property indicator_style4 STYLE_SOLID
#property indicator_width4 1
// Session 3: Post-Market
#property indicator_label5 "Post VWAP"
#property indicator_type5 DRAW_LINE
#property indicator_color5 clrSlateBlue
@@ -48,7 +45,6 @@
#property indicator_style6 STYLE_SOLID
#property indicator_width6 1
// Session 4: Full Day
#property indicator_label7 "Full VWAP"
#property indicator_type7 DRAW_LINE
#property indicator_color7 clrGray
@@ -119,7 +115,7 @@ input bool InpFull_ShowMean = false;
input bool InpFull_ShowLinReg = false;
//--- Indicator Buffers ---
double BufferPre_Odd[], BufferPre_Even[];
double BufferPre_Odd[], BufferPre_Even[];
double BufferCore_Odd[], BufferCore_Even[];
double BufferPost_Odd[], BufferPost_Even[];
double BufferFull_Odd[], BufferFull_Even[];
@@ -128,14 +124,14 @@ double BufferFull_Odd[], BufferFull_Even[];
#define SESSIONS_COUNT 4
CSessionAnalyzer *g_box_analyzers[SESSIONS_COUNT];
CVWAPCalculator *g_vwap_calculators[SESSIONS_COUNT];
string g_unique_prefix;
string g_unique_prefix;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//--- Bind Buffers to index mapping
// Bind Buffers
SetIndexBuffer(0, BufferPre_Odd, INDICATOR_DATA);
SetIndexBuffer(1, BufferPre_Even, INDICATOR_DATA);
SetIndexBuffer(2, BufferCore_Odd, INDICATOR_DATA);
@@ -145,25 +141,18 @@ int OnInit()
SetIndexBuffer(6, BufferFull_Odd, INDICATOR_DATA);
SetIndexBuffer(7, BufferFull_Even, INDICATOR_DATA);
//--- Force strict chronological alignment and empty value fallbacks (Unrolled loop)
ArraySetAsSeries(BufferPre_Odd, false);
PlotIndexSetDouble(0, PLOT_EMPTY_VALUE, EMPTY_VALUE);
ArraySetAsSeries(BufferPre_Even, false);
PlotIndexSetDouble(1, PLOT_EMPTY_VALUE, EMPTY_VALUE);
ArraySetAsSeries(BufferCore_Odd, false);
PlotIndexSetDouble(2, PLOT_EMPTY_VALUE, EMPTY_VALUE);
ArraySetAsSeries(BufferPre_Odd, false);
ArraySetAsSeries(BufferPre_Even, false);
ArraySetAsSeries(BufferCore_Odd, false);
ArraySetAsSeries(BufferCore_Even, false);
PlotIndexSetDouble(3, PLOT_EMPTY_VALUE, EMPTY_VALUE);
ArraySetAsSeries(BufferPost_Odd, false);
PlotIndexSetDouble(4, PLOT_EMPTY_VALUE, EMPTY_VALUE);
ArraySetAsSeries(BufferPost_Odd, false);
ArraySetAsSeries(BufferPost_Even, false);
PlotIndexSetDouble(5, PLOT_EMPTY_VALUE, EMPTY_VALUE);
ArraySetAsSeries(BufferFull_Odd, false);
PlotIndexSetDouble(6, PLOT_EMPTY_VALUE, EMPTY_VALUE);
ArraySetAsSeries(BufferFull_Odd, false);
ArraySetAsSeries(BufferFull_Even, false);
PlotIndexSetDouble(7, PLOT_EMPTY_VALUE, EMPTY_VALUE);
//--- Apply Custom Session Colors
for(int i = 0; i < 8; i++)
PlotIndexSetDouble(i, PLOT_EMPTY_VALUE, EMPTY_VALUE);
PlotIndexSetInteger(0, PLOT_LINE_COLOR, InpPre_Color);
PlotIndexSetInteger(1, PLOT_LINE_COLOR, InpPre_Color);
PlotIndexSetInteger(2, PLOT_LINE_COLOR, InpCore_Color);
@@ -173,46 +162,35 @@ int OnInit()
PlotIndexSetInteger(6, PLOT_LINE_COLOR, InpFull_Color);
PlotIndexSetInteger(7, PLOT_LINE_COLOR, InpFull_Color);
//--- Generate Unique Object Prefix to prevent collisions on multiple instances
MathSrand((int)TimeCurrent() + (int)ChartID());
string temp_short_name = StringFormat("SessSingle_TempID_%d_%d", TimeCurrent(), MathRand());
IndicatorSetString(INDICATOR_SHORTNAME, temp_short_name);
ChartRedraw();
int window_index = ChartWindowFind(0, temp_short_name);
if(window_index < 0)
window_index = 0;
g_unique_prefix = StringFormat("SessSingle_%s_%d_%d_", InpMarketName, ChartID(), window_index);
// Unique Prefix Setup
g_unique_prefix = StringFormat("SessSingle_%s_%I64d_", InpMarketName, ChartID());
ObjectsDeleteAll(0, g_unique_prefix);
bool is_ha_mode = (InpCandleSource == CANDLE_HEIKIN_ASHI);
//--- Instantiate Polymorphic Engines
for(int i = 0; i < SESSIONS_COUNT; i++)
{
if(is_ha_mode)
{
g_box_analyzers[i] = new CSessionAnalyzer_HA();
g_box_analyzers[i] = new CSessionAnalyzer_HA();
g_vwap_calculators[i] = new CVWAPCalculator_HA();
}
else
{
g_box_analyzers[i] = new CSessionAnalyzer();
g_box_analyzers[i] = new CSessionAnalyzer();
g_vwap_calculators[i] = new CVWAPCalculator();
}
}
//--- Initialize Object-drawing Analyzers
g_box_analyzers[0].Init(InpPre_Enable, InpPre_Start, InpPre_End, InpPre_Color, InpFillBoxes, InpPre_ShowMean, InpPre_ShowLinReg, g_unique_prefix + "Pre_", InpMaxHistoryDays);
g_box_analyzers[0].Init(InpPre_Enable, InpPre_Start, InpPre_End, InpPre_Color, InpFillBoxes, InpPre_ShowMean, InpPre_ShowLinReg, g_unique_prefix + "Pre_", InpMaxHistoryDays);
g_box_analyzers[1].Init(InpCore_Enable, InpCore_Start, InpCore_End, InpCore_Color, InpFillBoxes, InpCore_ShowMean, InpCore_ShowLinReg, g_unique_prefix + "Core_", InpMaxHistoryDays);
g_box_analyzers[2].Init(InpPost_Enable, InpPost_Start, InpPost_End, InpPost_Color, InpFillBoxes, InpPost_ShowMean, InpPost_ShowLinReg, g_unique_prefix + "Post_", InpMaxHistoryDays);
g_box_analyzers[3].Init(InpFull_Enable, InpPre_Start, InpPost_End, InpFull_Color, InpFillBoxes, InpFull_ShowMean, InpFull_ShowLinReg, g_unique_prefix + "Full_", InpMaxHistoryDays);
g_box_analyzers[3].Init(InpFull_Enable, InpPre_Start, InpPost_End, InpFull_Color, InpFillBoxes, InpFull_ShowMean, InpFull_ShowLinReg, g_unique_prefix + "Full_", InpMaxHistoryDays);
//--- Initialize Stateful VWAP Engines
g_vwap_calculators[0].Init(InpPre_Start, InpPre_End, InpVolumeType, InpPre_Enable && InpPre_ShowVWAP, InpMaxHistoryDays);
g_vwap_calculators[0].Init(InpPre_Start, InpPre_End, InpVolumeType, InpPre_Enable && InpPre_ShowVWAP, InpMaxHistoryDays);
g_vwap_calculators[1].Init(InpCore_Start, InpCore_End, InpVolumeType, InpCore_Enable && InpCore_ShowVWAP, InpMaxHistoryDays);
g_vwap_calculators[2].Init(InpPost_Start, InpPost_End, InpVolumeType, InpPost_Enable && InpPost_ShowVWAP, InpMaxHistoryDays);
g_vwap_calculators[3].Init(InpPre_Start, InpPost_End, InpVolumeType, InpFull_Enable && InpFull_ShowVWAP, InpMaxHistoryDays);
g_vwap_calculators[3].Init(InpPre_Start, InpPost_End, InpVolumeType, InpFull_Enable && InpFull_ShowVWAP, InpMaxHistoryDays);
IndicatorSetString(INDICATOR_SHORTNAME, "Session Analysis Single (" + InpMarketName + ")" + (is_ha_mode ? " HA" : ""));
IndicatorSetInteger(INDICATOR_DIGITS, _Digits);
@@ -231,15 +209,19 @@ void OnDeinit(const int reason)
{
g_box_analyzers[i].Cleanup();
delete g_box_analyzers[i];
g_box_analyzers[i] = NULL;
}
if(CheckPointer(g_vwap_calculators[i]) != POINTER_INVALID)
{
delete g_vwap_calculators[i];
g_vwap_calculators[i] = NULL;
}
}
ObjectsDeleteAll(0, g_unique_prefix);
}
//+------------------------------------------------------------------+
//| Custom indicator calculation loop (Real-time and O(1) optimized) |
//| Custom indicator calculation loop (Zero-Lag O(1) Architecture) |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
const int prev_calculated,
@@ -255,7 +237,6 @@ int OnCalculate(const int rates_total,
if(rates_total < 10)
return 0;
//--- Chronological safety safeguards
ArraySetAsSeries(time, false);
ArraySetAsSeries(open, false);
ArraySetAsSeries(high, false);
@@ -264,8 +245,6 @@ int OnCalculate(const int rates_total,
ArraySetAsSeries(tick_volume, false);
ArraySetAsSeries(volume, false);
//--- FIXED: Only wipe buffers on the very first run (prev_calculated == 0)
//--- This preserves historical segments during incremental tick calculations, completely curing ghost lines!
if(prev_calculated == 0)
{
ArrayInitialize(BufferPre_Odd, EMPTY_VALUE);
@@ -278,14 +257,14 @@ int OnCalculate(const int rates_total,
ArrayInitialize(BufferFull_Even, EMPTY_VALUE);
}
//--- 1. Update Object Drawing Logic (True O(1) state-preservation)
// 1. Update Box Objects with Change Guard
for(int i = 0; i < SESSIONS_COUNT; i++)
{
if(CheckPointer(g_box_analyzers[i]) != POINTER_INVALID)
g_box_analyzers[i].Update(rates_total, prev_calculated, time, open, high, low, close, InpSourcePrice);
}
//--- 2. Calculate Stateful VWAP Buffers (Teamed with prev_calculated for extreme efficiency!)
// 2. Incremental O(1) VWAP Calculations (Now lightning fast!)
if(CheckPointer(g_vwap_calculators[0]) != POINTER_INVALID)
g_vwap_calculators[0].Calculate(rates_total, prev_calculated, time, open, high, low, close, tick_volume, volume, BufferPre_Odd, BufferPre_Even);
if(CheckPointer(g_vwap_calculators[1]) != POINTER_INVALID)
@@ -295,7 +274,7 @@ int OnCalculate(const int rates_total,
if(CheckPointer(g_vwap_calculators[3]) != POINTER_INVALID)
g_vwap_calculators[3].Calculate(rates_total, prev_calculated, time, open, high, low, close, tick_volume, volume, BufferFull_Odd, BufferFull_Even);
ChartRedraw();
// CRITICAL FIX: ChartRedraw() REMOVED! Buffer repainting is natively handled by MT5.
return(rates_total);
}
//+------------------------------------------------------------------+