From 98d4d2d0812e7ace01c1ae8ffbfb97981470d7b9 Mon Sep 17 00:00:00 2001 From: Toh4iem9 Date: Sun, 15 Mar 2026 15:54:12 +0100 Subject: [PATCH] refactor(indicators): Strictly O(1) Incremental Optimized --- Include/MyIncludes/VScore_Calculator.mqh | 53 +++++++++--------------- 1 file changed, 20 insertions(+), 33 deletions(-) diff --git a/Include/MyIncludes/VScore_Calculator.mqh b/Include/MyIncludes/VScore_Calculator.mqh index b8b65a6..d49255d 100644 --- a/Include/MyIncludes/VScore_Calculator.mqh +++ b/Include/MyIncludes/VScore_Calculator.mqh @@ -2,6 +2,7 @@ //| VScore_Calculator.mqh | //| Engine for V-Score (VWAP Z-Score). | //| Measures statistical deviation from VWAP. | +//| VERSION 2.00: Strictly O(1) Incremental Optimized. | //| Copyright 2026, xxxxxxxx | //+------------------------------------------------------------------+ #property copyright "Copyright 2026, xxxxxxxx" @@ -9,7 +10,7 @@ #include //+------------------------------------------------------------------+ -//| | +//| Class CVScoreCalculator | //+------------------------------------------------------------------+ class CVScoreCalculator { @@ -17,11 +18,10 @@ protected: int m_period; CVWAPCalculator *m_vwap_calc; - // Buffers + // Persistent Buffers for Incremental Calculation double m_vwap_buf[]; - double m_diff_sq[]; // Squared differences buffer - - void PrepareVWAP(int rates_total, const datetime &time[], const double &open[], const double &high[], const double &low[], const double &close[], const long &tick_volume[], const long &volume[]); + double m_vwap_odd[]; + double m_vwap_even[]; public: CVScoreCalculator(); @@ -67,7 +67,7 @@ bool CVScoreCalculator::Init(int period, ENUM_VWAP_PERIOD vwap_reset) } //+------------------------------------------------------------------+ -//| Main Calculation | +//| Main Calculation (Strictly O(1) Optimized) | //+------------------------------------------------------------------+ void CVScoreCalculator::Calculate(int rates_total, int prev_calculated, const datetime &time[], const double &open[], const double &high[], const double &low[], const double &close[], @@ -77,19 +77,26 @@ void CVScoreCalculator::Calculate(int rates_total, int prev_calculated, if(rates_total < m_period) return; -// 1. Prepare VWAP Buffer +// 1. Manage Internal Buffers if(ArraySize(m_vwap_buf) != rates_total) + { ArrayResize(m_vwap_buf, rates_total); - PrepareVWAP(rates_total, time, open, high, low, close, tick_volume, volume); + ArrayResize(m_vwap_odd, rates_total); + ArrayResize(m_vwap_even, rates_total); + } -// 2. Calculate Standard Deviation of (Price - VWAP) -// We look back 'm_period' bars to calculate the volatility of the deviation +// 2. Calculate VWAP Incrementally! (Passing prev_calculated) + m_vwap_calc.Calculate(rates_total, prev_calculated, time, open, high, low, close, tick_volume, volume, m_vwap_odd, m_vwap_even); +// 3. Calculate Standard Deviation of (Price - VWAP) +// Determine start index for true O(1) performance int start = (prev_calculated > m_period) ? prev_calculated - 1 : m_period; for(int i = start; i < rates_total; i++) { - double current_vwap = m_vwap_buf[i]; + // Merge Odd/Even logic continuously + double current_vwap = (m_vwap_odd[i] != EMPTY_VALUE && m_vwap_odd[i] != 0) ? m_vwap_odd[i] : m_vwap_even[i]; + m_vwap_buf[i] = current_vwap; // If VWAP is newly reset (0 or empty), VScore is 0 if(current_vwap == 0 || current_vwap == EMPTY_VALUE) @@ -107,7 +114,7 @@ void CVScoreCalculator::Calculate(int rates_total, int prev_calculated, double p = close[idx]; double v = m_vwap_buf[idx]; - // If history has bad vwap, use price (diff=0) + // If history has bad vwap, use price (diff=0) to avoid spikes if(v == 0 || v == EMPTY_VALUE) v = p; @@ -117,32 +124,12 @@ void CVScoreCalculator::Calculate(int rates_total, int prev_calculated, double std_dev = MathSqrt(sum_sq_diff / m_period); + // Z-Score calculation if(std_dev > 1.0e-9) out_vscore[i] = (close[i] - current_vwap) / std_dev; else out_vscore[i] = 0.0; } } - //+------------------------------------------------------------------+ -//| Helper: Fill Internal VWAP Buffer | -//+------------------------------------------------------------------+ -void CVScoreCalculator::PrepareVWAP(int rates_total, const datetime &time[], const double &open[], const double &high[], const double &low[], const double &close[], const long &tick_volume[], const long &volume[]) - { - double odd[], even[]; - ArrayResize(odd, rates_total); - ArrayResize(even, rates_total); - - m_vwap_calc.Calculate(rates_total, 0, time, open, high, low, close, tick_volume, volume, odd, even); - -// Determine active buffer (Odd/Even logic of VWAP engine) -// We merge them into one continuous buffer - for(int i=0; i