mirror of
https://github.com/softwaredevelop/mql5.git
synced 2026-08-01 14:47:43 +00:00
refactor(indicators): Strictly O(1) Incremental Optimized
This commit is contained in:
@@ -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 <MyIncludes\VWAP_Calculator.mqh>
|
||||
|
||||
//+------------------------------------------------------------------+
|
||||
//| |
|
||||
//| 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<rates_total; i++)
|
||||
{
|
||||
if(odd[i] != EMPTY_VALUE && odd[i] != 0)
|
||||
m_vwap_buf[i] = odd[i];
|
||||
else
|
||||
m_vwap_buf[i] = even[i];
|
||||
}
|
||||
}
|
||||
//+------------------------------------------------------------------+
|
||||
|
||||
Reference in New Issue
Block a user