mirror of
https://github.com/softwaredevelop/mql5.git
synced 2026-07-27 20:47:44 +00:00
refactor: Restored Init signature for maximum compatibility across indicators
This commit is contained in:
@@ -2,10 +2,14 @@
|
||||
//| VScore_Calculator.mqh |
|
||||
//| Engine for V-Score (VWAP Z-Score). |
|
||||
//| Measures statistical deviation from VWAP. |
|
||||
//| VERSION 2.00: Strictly O(1) Incremental Optimized. |
|
||||
//| VERSION 2.12: Restored classic signature with auto-volume |
|
||||
//| Copyright 2026, xxxxxxxx |
|
||||
//+------------------------------------------------------------------+
|
||||
#property copyright "Copyright 2026, xxxxxxxx"
|
||||
#property version "2.12" // Restored Init signature for maximum compatibility across indicators
|
||||
|
||||
#ifndef VSCORE_CALCULATOR_MQH
|
||||
#define VSCORE_CALCULATOR_MQH
|
||||
|
||||
#include <MyIncludes\VWAP_Calculator.mqh>
|
||||
|
||||
@@ -27,6 +31,7 @@ public:
|
||||
CVScoreCalculator();
|
||||
virtual ~CVScoreCalculator();
|
||||
|
||||
//--- RESTORED: Classic Init signature
|
||||
bool Init(int period, ENUM_VWAP_PERIOD vwap_reset);
|
||||
|
||||
void Calculate(int rates_total, int prev_calculated,
|
||||
@@ -47,7 +52,7 @@ CVScoreCalculator::CVScoreCalculator() : m_vwap_calc(NULL)
|
||||
//+------------------------------------------------------------------+
|
||||
CVScoreCalculator::~CVScoreCalculator()
|
||||
{
|
||||
if(CheckPointer(m_vwap_calc) == POINTER_DYNAMIC)
|
||||
if(CheckPointer(m_vwap_calc) != POINTER_INVALID)
|
||||
delete m_vwap_calc;
|
||||
}
|
||||
|
||||
@@ -59,8 +64,15 @@ bool CVScoreCalculator::Init(int period, ENUM_VWAP_PERIOD vwap_reset)
|
||||
m_period = (period < 2) ? 2 : period;
|
||||
|
||||
m_vwap_calc = new CVWAPCalculator();
|
||||
// Init VWAP with Tick Volume, enabled
|
||||
if(!m_vwap_calc.Init(vwap_reset, VOLUME_TICK, 0, true))
|
||||
if(CheckPointer(m_vwap_calc) == POINTER_INVALID)
|
||||
return false;
|
||||
|
||||
// Dynamic volume type selection based on broker capability (Real Volume vs Tick Volume)
|
||||
long volume_limit = (long)SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_LIMIT);
|
||||
ENUM_APPLIED_VOLUME vol_type = (volume_limit > 0) ? VOLUME_REAL : VOLUME_TICK;
|
||||
|
||||
// Init VWAP with optimal volume, enabled
|
||||
if(!m_vwap_calc.Init(vwap_reset, vol_type, 0, true))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
@@ -85,20 +97,17 @@ void CVScoreCalculator::Calculate(int rates_total, int prev_calculated,
|
||||
ArrayResize(m_vwap_even, rates_total);
|
||||
}
|
||||
|
||||
// 2. Calculate VWAP Incrementally! (Passing prev_calculated)
|
||||
// 2. Calculate VWAP Incrementally
|
||||
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++)
|
||||
{
|
||||
// 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)
|
||||
{
|
||||
out_vscore[i] = 0.0;
|
||||
@@ -107,14 +116,12 @@ void CVScoreCalculator::Calculate(int rates_total, int prev_calculated,
|
||||
|
||||
double sum_sq_diff = 0;
|
||||
|
||||
// StdDev of deviation over the window
|
||||
for(int k = 0; k < m_period; k++)
|
||||
{
|
||||
int idx = i - k;
|
||||
double p = close[idx];
|
||||
double v = m_vwap_buf[idx];
|
||||
|
||||
// If history has bad vwap, use price (diff=0) to avoid spikes
|
||||
if(v == 0 || v == EMPTY_VALUE)
|
||||
v = p;
|
||||
|
||||
@@ -124,12 +131,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;
|
||||
}
|
||||
}
|
||||
//+------------------------------------------------------------------+
|
||||
|
||||
#endif // VSCORE_CALCULATOR_MQH
|
||||
//+------------------------------------------------------------------+
|
||||
|
||||
Reference in New Issue
Block a user