mirror of
https://github.com/softwaredevelop/mql5.git
synced 2026-08-26 02:38:05 +00:00
refactor: Fully functional dynamic Multi-Timeframe V-Score (VWAP Z-Score) with flat-force step-alignment
This commit is contained in:
@@ -1,65 +1,89 @@
|
|||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
//| VScore_MTF_Pro.mq5 |
|
//| VScore_MTF_Pro.mq5|
|
||||||
//| Copyright 2026, xxxxxxxx|
|
//| Copyright 2026, xxxxxxxx|
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
#property copyright "Copyright 2026, xxxxxxxx"
|
#property copyright "Copyright 2026, xxxxxxxx"
|
||||||
#property version "1.21" // Fixed dynamic live-updating cumulative state corruption bug
|
#property version "1.00" // Fully functional dynamic Multi-Timeframe V-Score (VWAP Z-Score) with flat-force step-alignment
|
||||||
#property description "V-Score (Multi-Timeframe)."
|
#property description "Professional Multi-Timeframe (MTF) V-Score Oscillator with dynamic Signal Line."
|
||||||
#property description "Displays HTF VWAP Deviation Z-Score cleanly without live-bar warping."
|
#property description "Displays HTF V-Score cleanly directly on lower TF charts without live-bar warping."
|
||||||
|
|
||||||
#property indicator_separate_window
|
#property indicator_separate_window
|
||||||
#property indicator_buffers 2
|
#property indicator_buffers 3
|
||||||
#property indicator_plots 1
|
#property indicator_plots 2
|
||||||
|
|
||||||
//--- Institutional Levels Configuration
|
//--- Plot 1: Color Histogram (5-Zone Swapped Thermal Palette)
|
||||||
#property indicator_level1 2.5
|
|
||||||
#property indicator_level2 2.0
|
|
||||||
#property indicator_level3 1.5 // Point of No Return
|
|
||||||
#property indicator_level4 -1.5 // Point of No Return
|
|
||||||
#property indicator_level5 -2.0
|
|
||||||
#property indicator_level6 -2.5
|
|
||||||
|
|
||||||
// Level Colors & Styles
|
|
||||||
#property indicator_levelcolor clrSilver
|
|
||||||
#property indicator_levelstyle STYLE_DOT
|
|
||||||
|
|
||||||
//--- Plot: Color Histogram (5-Zone Thermal Palette)
|
|
||||||
#property indicator_label1 "V-Score MTF"
|
#property indicator_label1 "V-Score MTF"
|
||||||
#property indicator_type1 DRAW_COLOR_HISTOGRAM // FIXED: Removed string quotes to prevent compiler error
|
#property indicator_type1 DRAW_COLOR_HISTOGRAM
|
||||||
|
// Swapped Palette:
|
||||||
// 5-Color Palette:
|
|
||||||
// 0: Noise/Neutral (Gray)
|
// 0: Noise/Neutral (Gray)
|
||||||
// 1: Bull Flow (Coral - warming up)
|
// 1: Bullish Flow (LightSkyBlue)
|
||||||
// 2: Bull Extreme (OrangeRed - hot/overbought)
|
// 2: Bullish Climax (DeepSkyBlue)
|
||||||
// 3: Bear Flow (LightSkyBlue - cooling down)
|
// 3: Bearish Flow (Coral)
|
||||||
// 4: Bear Extreme (DeepSkyBlue - freezing/oversold)
|
// 4: Bearish Climax (OrangeRed)
|
||||||
#property indicator_color1 clrGray, clrCoral, clrOrangeRed, clrLightSkyBlue, clrDeepSkyBlue
|
#property indicator_color1 clrGray, clrLightSkyBlue, clrDeepSkyBlue, clrCoral, clrOrangeRed
|
||||||
#property indicator_style1 STYLE_SOLID
|
#property indicator_style1 STYLE_SOLID
|
||||||
#property indicator_width1 2
|
#property indicator_width1 2
|
||||||
|
|
||||||
#include <MyIncludes\VScore_Calculator.mqh>
|
//--- Plot 2: Smoothed Signal Line (Filters high-frequency noise)
|
||||||
|
#property indicator_label2 "Signal MTF"
|
||||||
|
#property indicator_type2 DRAW_LINE
|
||||||
|
#property indicator_color2 clrFireBrick
|
||||||
|
#property indicator_style2 STYLE_SOLID
|
||||||
|
#property indicator_width2 1
|
||||||
|
|
||||||
//--- Input Parameters
|
#include <MyIncludes\VScore_Calculator.mqh>
|
||||||
input ENUM_TIMEFRAMES InpTimeframe = PERIOD_H1; // Target Higher Timeframe
|
#include <MyIncludes\MovingAverage_Engine.mqh>
|
||||||
input int InpPeriod = 20; // Volatility Lookback
|
|
||||||
input ENUM_VWAP_PERIOD InpVWAPReset = PERIOD_SESSION; // VWAP Anchor
|
//--- Input Parameters ---
|
||||||
|
input group "Timeframe Settings"
|
||||||
|
input ENUM_TIMEFRAMES InpTimeframe = PERIOD_H1; // Target Higher Timeframe
|
||||||
|
|
||||||
|
input group "V-Score Settings"
|
||||||
|
input int InpPeriod = 20; // Volatility Lookback
|
||||||
|
input ENUM_VWAP_PERIOD InpVWAPReset = PERIOD_SESSION; // VWAP Anchor
|
||||||
|
|
||||||
|
input group "Signal Line Settings"
|
||||||
|
input bool InpShowSignal = true; // Show Signal Line?
|
||||||
|
input int InpSignalPeriod = 5; // Signal Line Period
|
||||||
|
input ENUM_MA_TYPE InpSignalType = SMA; // Signal Line MA Type
|
||||||
|
|
||||||
|
input group "Indicator Levels"
|
||||||
|
input double InpLevelFlowHigh = 1.5; // High Warning Level (Bullish Flow)
|
||||||
|
input double InpLevelFlowLow = -1.5; // Low Warning Level (Bearish Flow)
|
||||||
|
input double InpLevelClimaxHigh = 2.0; // High Climax Level (Bullish Climax)
|
||||||
|
input double InpLevelClimaxLow = -2.0; // Low Climax Level (Bearish Climax)
|
||||||
|
input double InpLevelExtremeHigh= 2.5; // High Exhaustion Level
|
||||||
|
input double InpLevelExtremeLow = -2.5; // Low Exhaustion Level
|
||||||
|
input color InpLevelColor = clrSilver; // Levels Color
|
||||||
|
input ENUM_LINE_STYLE InpLevelStyle = STYLE_DOT; // Levels Style
|
||||||
|
|
||||||
//--- Buffers
|
//--- Buffers
|
||||||
double BufV[];
|
double BufferVScore_MTF[];
|
||||||
double BufCol[];
|
double BufferColors_MTF[];
|
||||||
|
double BufferSignal_MTF[];
|
||||||
|
|
||||||
//--- Internal HTF Data Caches
|
//--- Internal HTF Data Caches
|
||||||
double h_open[], h_high[], h_low[], h_close[];
|
double h_res_vscore[]; // HTF VScore Results cached
|
||||||
long h_vol[]; // Volume needed for VWAP
|
double h_res_sig[]; // HTF Signal Results cached
|
||||||
datetime h_time[]; // Time needed for reset logic
|
datetime h_time[]; // HTF Time index
|
||||||
double h_res[]; // HTF Results cached
|
double h_open[], h_high[], h_low[], h_close[]; // HTF Price Data
|
||||||
|
long h_tick_volume[];// HTF raw tick volume
|
||||||
|
long h_vol[]; // HTF raw volume cache
|
||||||
|
double h_vol_double[]; // HTF volume cast to double to support VWMA Signal line
|
||||||
|
|
||||||
|
//--- Volume Cache to support Volume-Weighted types (VWMA) on current timeframe
|
||||||
|
double g_double_volume[];
|
||||||
|
|
||||||
//--- Global HTF State Tracking
|
//--- Global HTF State Tracking
|
||||||
CVScoreCalculator *g_calc;
|
CVScoreCalculator *g_calculator;
|
||||||
datetime g_last_htf_time = 0;
|
CMovingAverageCalculator *g_signal_calculator;
|
||||||
|
datetime g_last_htf_time = 0;
|
||||||
int g_htf_count = 0;
|
int g_htf_count = 0;
|
||||||
bool g_data_ready = false;
|
bool g_data_ready = false;
|
||||||
bool g_data_synced = false;
|
bool g_data_synced = false;
|
||||||
|
|
||||||
|
bool g_is_mtf_mode = false;
|
||||||
|
ENUM_TIMEFRAMES g_calc_timeframe;
|
||||||
|
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
//| EnsureHTFDataReady |
|
//| EnsureHTFDataReady |
|
||||||
@@ -83,35 +107,92 @@ int OnInit()
|
|||||||
{
|
{
|
||||||
g_data_ready = false;
|
g_data_ready = false;
|
||||||
g_data_synced = false;
|
g_data_synced = false;
|
||||||
g_last_htf_time = 0;
|
|
||||||
g_htf_count = 0;
|
g_htf_count = 0;
|
||||||
|
g_last_htf_time = 0;
|
||||||
|
|
||||||
if(InpTimeframe <= Period() && InpTimeframe != PERIOD_CURRENT)
|
//--- 1. Resolve Timeframe
|
||||||
|
g_calc_timeframe = InpTimeframe;
|
||||||
|
if(g_calc_timeframe == PERIOD_CURRENT)
|
||||||
|
g_calc_timeframe = (ENUM_TIMEFRAMES)Period();
|
||||||
|
|
||||||
|
if(g_calc_timeframe < Period())
|
||||||
{
|
{
|
||||||
Print("Warning: Target Timeframe should be > Current Timeframe.");
|
PrintFormat("Error: Target timeframe (%s) must be >= current timeframe (%s).",
|
||||||
|
EnumToString(g_calc_timeframe), EnumToString(Period()));
|
||||||
|
return(INIT_FAILED);
|
||||||
|
}
|
||||||
|
g_is_mtf_mode = (g_calc_timeframe > Period());
|
||||||
|
|
||||||
|
//--- 2. Setup Buffers
|
||||||
|
SetIndexBuffer(0, BufferVScore_MTF, INDICATOR_DATA);
|
||||||
|
SetIndexBuffer(1, BufferColors_MTF, INDICATOR_COLOR_INDEX);
|
||||||
|
SetIndexBuffer(2, BufferSignal_MTF, INDICATOR_DATA);
|
||||||
|
|
||||||
|
ArraySetAsSeries(BufferVScore_MTF, false);
|
||||||
|
ArraySetAsSeries(BufferColors_MTF, false);
|
||||||
|
ArraySetAsSeries(BufferSignal_MTF, false);
|
||||||
|
|
||||||
|
//--- 3. Dynamically configure horizontal levels
|
||||||
|
IndicatorSetInteger(INDICATOR_LEVELS, 6);
|
||||||
|
IndicatorSetDouble(INDICATOR_LEVELVALUE, 0, InpLevelFlowHigh);
|
||||||
|
IndicatorSetDouble(INDICATOR_LEVELVALUE, 1, InpLevelFlowLow);
|
||||||
|
IndicatorSetDouble(INDICATOR_LEVELVALUE, 2, InpLevelClimaxHigh);
|
||||||
|
IndicatorSetDouble(INDICATOR_LEVELVALUE, 3, InpLevelClimaxLow);
|
||||||
|
IndicatorSetDouble(INDICATOR_LEVELVALUE, 4, InpLevelExtremeHigh);
|
||||||
|
IndicatorSetDouble(INDICATOR_LEVELVALUE, 5, InpLevelExtremeLow);
|
||||||
|
|
||||||
|
IndicatorSetInteger(INDICATOR_LEVELCOLOR, InpLevelColor);
|
||||||
|
IndicatorSetInteger(INDICATOR_LEVELSTYLE, InpLevelStyle);
|
||||||
|
|
||||||
|
//--- 4. Initialize Calculator
|
||||||
|
g_calculator = new CVScoreCalculator();
|
||||||
|
if(CheckPointer(g_calculator) == POINTER_INVALID || !g_calculator.Init(InpPeriod, InpVWAPReset))
|
||||||
|
{
|
||||||
|
Print("Failed to create or initialize V-Score Calculator.");
|
||||||
|
return(INIT_FAILED);
|
||||||
}
|
}
|
||||||
|
|
||||||
SetIndexBuffer(0, BufV, INDICATOR_DATA);
|
if(InpShowSignal)
|
||||||
SetIndexBuffer(1, BufCol, INDICATOR_COLOR_INDEX);
|
|
||||||
|
|
||||||
ArraySetAsSeries(BufV, false);
|
|
||||||
ArraySetAsSeries(BufCol, false);
|
|
||||||
|
|
||||||
//--- Configure classic Z-Score Calculator
|
|
||||||
g_calc = new CVScoreCalculator();
|
|
||||||
if(CheckPointer(g_calc) == POINTER_INVALID || !g_calc.Init(InpPeriod, InpVWAPReset))
|
|
||||||
{
|
{
|
||||||
Print("Error: Failed to initialize VScore Calculator.");
|
PlotIndexSetInteger(1, PLOT_DRAW_TYPE, DRAW_LINE);
|
||||||
return INIT_FAILED;
|
PlotIndexSetString(1, PLOT_LABEL, "Signal MTF");
|
||||||
|
|
||||||
|
g_signal_calculator = new CMovingAverageCalculator();
|
||||||
|
if(CheckPointer(g_signal_calculator) == POINTER_INVALID || !g_signal_calculator.Init(InpSignalPeriod, InpSignalType))
|
||||||
|
{
|
||||||
|
Print("Failed to initialize Signal Line Calculator.");
|
||||||
|
return(INIT_FAILED);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
PlotIndexSetInteger(1, PLOT_DRAW_TYPE, DRAW_NONE);
|
||||||
|
PlotIndexSetString(1, PLOT_LABEL, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
string tf_name = StringSubstr(EnumToString(InpTimeframe), 7);
|
//--- 5. Set Shortname
|
||||||
string name = StringFormat("V-Score MTF %s(%d)", tf_name, InpPeriod);
|
string tf_str = g_is_mtf_mode ? (" " + EnumToString(g_calc_timeframe)) : "";
|
||||||
IndicatorSetString(INDICATOR_SHORTNAME, name);
|
string name = StringFormat("V-Score Pro%s(%d)", tf_str, InpPeriod);
|
||||||
|
string short_name = "";
|
||||||
|
|
||||||
|
if(InpShowSignal)
|
||||||
|
{
|
||||||
|
string sig_name = EnumToString(InpSignalType);
|
||||||
|
StringToUpper(sig_name);
|
||||||
|
short_name = StringFormat("%s %s(%d)", name, sig_name, InpSignalPeriod);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
short_name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
IndicatorSetString(INDICATOR_SHORTNAME, short_name);
|
||||||
|
PlotIndexSetString(0, PLOT_LABEL, "V-Score MTF");
|
||||||
IndicatorSetInteger(INDICATOR_DIGITS, 2);
|
IndicatorSetInteger(INDICATOR_DIGITS, 2);
|
||||||
|
|
||||||
//--- Initialize 1-second timer for weekend/async chart refreshes
|
//--- Initialize 1-second timer for weekend/async chart refreshes (Only if MTF mode is active)
|
||||||
EventSetTimer(1);
|
if(g_is_mtf_mode)
|
||||||
|
EventSetTimer(1);
|
||||||
|
|
||||||
return(INIT_SUCCEEDED);
|
return(INIT_SUCCEEDED);
|
||||||
}
|
}
|
||||||
@@ -119,11 +200,13 @@ int OnInit()
|
|||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
//| OnDeinit |
|
//| OnDeinit |
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
void OnDeinit(const int r)
|
void OnDeinit(const int reason)
|
||||||
{
|
{
|
||||||
EventKillTimer();
|
EventKillTimer();
|
||||||
if(CheckPointer(g_calc) != POINTER_INVALID)
|
if(CheckPointer(g_calculator) != POINTER_INVALID)
|
||||||
delete g_calc;
|
delete g_calculator;
|
||||||
|
if(CheckPointer(g_signal_calculator) != POINTER_INVALID)
|
||||||
|
delete g_signal_calculator;
|
||||||
}
|
}
|
||||||
|
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
@@ -140,9 +223,82 @@ int OnCalculate(const int rates_total,
|
|||||||
const long &volume[],
|
const long &volume[],
|
||||||
const int &spread[])
|
const int &spread[])
|
||||||
{
|
{
|
||||||
|
if(rates_total < 2)
|
||||||
|
return(0);
|
||||||
|
|
||||||
|
if(CheckPointer(g_calculator) == POINTER_INVALID)
|
||||||
|
return(0);
|
||||||
|
|
||||||
|
//--- Force strict chronological indexing for state-safety on input price arrays
|
||||||
|
ArraySetAsSeries(time, false);
|
||||||
|
ArraySetAsSeries(open, false);
|
||||||
|
ArraySetAsSeries(high, false);
|
||||||
|
ArraySetAsSeries(low, false);
|
||||||
|
ArraySetAsSeries(close, false);
|
||||||
|
|
||||||
|
ENUM_APPLIED_PRICE price_type = PRICE_CLOSE; // Standard Close for V-Score calculations
|
||||||
|
|
||||||
|
//================================================================
|
||||||
|
// MODE 1: Current Timeframe (Standard)
|
||||||
|
//================================================================
|
||||||
|
if(!g_is_mtf_mode)
|
||||||
|
{
|
||||||
|
long volume_limit = (long)SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_LIMIT);
|
||||||
|
|
||||||
|
ArrayResize(g_double_volume, rates_total);
|
||||||
|
int start_sync = (prev_calculated > 0) ? prev_calculated - 1 : 0;
|
||||||
|
|
||||||
|
if(volume_limit > 0)
|
||||||
|
{
|
||||||
|
for(int i = start_sync; i < rates_total; i++)
|
||||||
|
g_double_volume[i] = (double)volume[i];
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
for(int i = start_sync; i < rates_total; i++)
|
||||||
|
g_double_volume[i] = (double)tick_volume[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
// Calculate V-Score
|
||||||
|
g_calculator.Calculate(rates_total, prev_calculated, time, open, high, low, close, tick_volume, volume, BufferVScore_MTF);
|
||||||
|
|
||||||
|
// Calculate Signal Line
|
||||||
|
if(InpShowSignal && CheckPointer(g_signal_calculator) != POINTER_INVALID)
|
||||||
|
{
|
||||||
|
g_signal_calculator.CalculateOnArray(rates_total, prev_calculated, BufferVScore_MTF, g_double_volume, BufferSignal_MTF, InpPeriod);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Apply 5-Zone Swapped Thermal Coloring Logic (Corrected Polarity)
|
||||||
|
int start_index = (prev_calculated > 0) ? prev_calculated - 1 : 0;
|
||||||
|
for(int i = start_index; i < rates_total; i++)
|
||||||
|
{
|
||||||
|
double v = BufferVScore_MTF[i];
|
||||||
|
|
||||||
|
if(v >= InpLevelClimaxHigh)
|
||||||
|
BufferColors_MTF[i] = 2.0;
|
||||||
|
else
|
||||||
|
if(v >= InpLevelFlowHigh)
|
||||||
|
BufferColors_MTF[i] = 1.0;
|
||||||
|
else
|
||||||
|
if(v <= InpLevelClimaxLow)
|
||||||
|
BufferColors_MTF[i] = 4.0;
|
||||||
|
else
|
||||||
|
if(v <= InpLevelFlowLow)
|
||||||
|
BufferColors_MTF[i] = 3.0;
|
||||||
|
else
|
||||||
|
BufferColors_MTF[i] = 0.0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return(rates_total);
|
||||||
|
}
|
||||||
|
|
||||||
|
//================================================================
|
||||||
|
// MODE 2: Multi-Timeframe (MTF Engine)
|
||||||
|
//================================================================
|
||||||
|
|
||||||
//--- Ensure target timeframe history is ready
|
//--- Ensure target timeframe history is ready
|
||||||
int required_bars = InpPeriod + 10;
|
int required_bars = InpPeriod + 10;
|
||||||
if(!EnsureHTFDataReady(_Symbol, InpTimeframe, required_bars))
|
if(!EnsureHTFDataReady(_Symbol, g_calc_timeframe, required_bars))
|
||||||
{
|
{
|
||||||
g_data_synced = false;
|
g_data_synced = false;
|
||||||
return 0; // Wait for next tick to let history load
|
return 0; // Wait for next tick to let history load
|
||||||
@@ -154,14 +310,14 @@ int OnCalculate(const int rates_total,
|
|||||||
long volume_limit = (long)SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_LIMIT);
|
long volume_limit = (long)SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_LIMIT);
|
||||||
|
|
||||||
//--- 1. Check if a new HTF bar has formed
|
//--- 1. Check if a new HTF bar has formed
|
||||||
datetime htf_time_current = iTime(_Symbol, InpTimeframe, 0);
|
datetime htf_time_current = iTime(_Symbol, g_calc_timeframe, 0);
|
||||||
bool htf_updated = (htf_time_current != g_last_htf_time);
|
bool htf_updated = (htf_time_current != g_last_htf_time);
|
||||||
|
|
||||||
if(htf_updated || prev_calculated == 0)
|
if(htf_updated || prev_calculated == 0)
|
||||||
{
|
{
|
||||||
g_last_htf_time = htf_time_current;
|
g_last_htf_time = htf_time_current;
|
||||||
|
|
||||||
int htf_bars = iBars(_Symbol, InpTimeframe);
|
int htf_bars = iBars(_Symbol, g_calc_timeframe);
|
||||||
if(htf_bars < required_bars)
|
if(htf_bars < required_bars)
|
||||||
{
|
{
|
||||||
g_data_ready = false;
|
g_data_ready = false;
|
||||||
@@ -170,30 +326,44 @@ int OnCalculate(const int rates_total,
|
|||||||
|
|
||||||
g_htf_count = MathMin(htf_bars, 3000);
|
g_htf_count = MathMin(htf_bars, 3000);
|
||||||
|
|
||||||
ArrayResize(h_time, g_htf_count);
|
ArrayResize(h_time, g_htf_count);
|
||||||
ArrayResize(h_open, g_htf_count);
|
ArrayResize(h_open, g_htf_count);
|
||||||
ArrayResize(h_high, g_htf_count);
|
ArrayResize(h_high, g_htf_count);
|
||||||
ArrayResize(h_low, g_htf_count);
|
ArrayResize(h_low, g_htf_count);
|
||||||
ArrayResize(h_close, g_htf_count);
|
ArrayResize(h_close, g_htf_count);
|
||||||
ArrayResize(h_vol, g_htf_count);
|
ArrayResize(h_tick_volume, g_htf_count);
|
||||||
ArrayResize(h_res, g_htf_count);
|
ArrayResize(h_vol, g_htf_count);
|
||||||
|
ArrayResize(h_vol_double, g_htf_count);
|
||||||
|
|
||||||
if(CopyTime(_Symbol, InpTimeframe, 0, g_htf_count, h_time) != g_htf_count ||
|
ArrayResize(h_res_vscore, g_htf_count);
|
||||||
CopyOpen(_Symbol, InpTimeframe, 0, g_htf_count, h_open) != g_htf_count ||
|
ArrayResize(h_res_sig, g_htf_count);
|
||||||
CopyHigh(_Symbol, InpTimeframe, 0, g_htf_count, h_high) != g_htf_count ||
|
|
||||||
CopyLow(_Symbol, InpTimeframe, 0, g_htf_count, h_low) != g_htf_count ||
|
// Force chronological array alignment on HTF caches after resize
|
||||||
CopyClose(_Symbol, InpTimeframe, 0, g_htf_count, h_close) != g_htf_count)
|
ArraySetAsSeries(h_time, false);
|
||||||
|
ArraySetAsSeries(h_open, false);
|
||||||
|
ArraySetAsSeries(h_high, false);
|
||||||
|
ArraySetAsSeries(h_low, false);
|
||||||
|
ArraySetAsSeries(h_close, false);
|
||||||
|
ArraySetAsSeries(h_tick_volume, false);
|
||||||
|
ArraySetAsSeries(h_vol, false);
|
||||||
|
|
||||||
|
if(CopyTime(_Symbol, g_calc_timeframe, 0, g_htf_count, h_time) != g_htf_count ||
|
||||||
|
CopyOpen(_Symbol, g_calc_timeframe, 0, g_htf_count, h_open) != g_htf_count ||
|
||||||
|
CopyHigh(_Symbol, g_calc_timeframe, 0, g_htf_count, h_high) != g_htf_count ||
|
||||||
|
CopyLow(_Symbol, g_calc_timeframe, 0, g_htf_count, h_low) != g_htf_count ||
|
||||||
|
CopyClose(_Symbol, g_calc_timeframe, 0, g_htf_count, h_close) != g_htf_count ||
|
||||||
|
CopyTickVolume(_Symbol, g_calc_timeframe, 0, g_htf_count, h_tick_volume) != g_htf_count)
|
||||||
{
|
{
|
||||||
g_data_ready = false;
|
g_data_ready = false;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// High-Performance dynamic volume routing on the HTF Timeline (VWAP support)
|
// High-Performance dynamic volume routing on the HTF Timeline
|
||||||
int copied_vol = 0;
|
int copied_vol = 0;
|
||||||
if(volume_limit > 0)
|
if(volume_limit > 0)
|
||||||
copied_vol = CopyRealVolume(_Symbol, InpTimeframe, 0, g_htf_count, h_vol);
|
copied_vol = CopyRealVolume(_Symbol, g_calc_timeframe, 0, g_htf_count, h_vol);
|
||||||
else
|
else
|
||||||
copied_vol = CopyTickVolume(_Symbol, InpTimeframe, 0, g_htf_count, h_vol);
|
copied_vol = CopyTickVolume(_Symbol, g_calc_timeframe, 0, g_htf_count, h_vol);
|
||||||
|
|
||||||
if(copied_vol != g_htf_count)
|
if(copied_vol != g_htf_count)
|
||||||
{
|
{
|
||||||
@@ -201,8 +371,18 @@ int OnCalculate(const int rates_total,
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
//--- Calculate V-Score on HTF (Closed bars and forming bar initialized)
|
// Convert HTF volume cache to double precision
|
||||||
g_calc.Calculate(g_htf_count, 0, h_time, h_open, h_high, h_low, h_close, h_vol, h_vol, h_res);
|
for(int j = 0; j < g_htf_count; j++)
|
||||||
|
h_vol_double[j] = (double)h_vol[j];
|
||||||
|
|
||||||
|
//--- Calculate VScore on HTF (Closed bars and forming bar initialized)
|
||||||
|
g_calculator.Calculate(g_htf_count, 0, h_time, h_open, h_high, h_low, h_close, h_tick_volume, h_vol, h_res_vscore);
|
||||||
|
|
||||||
|
//--- Calculate HTF Signal Line
|
||||||
|
if(InpShowSignal && CheckPointer(g_signal_calculator) != POINTER_INVALID)
|
||||||
|
{
|
||||||
|
g_signal_calculator.CalculateOnArray(g_htf_count, 0, h_res_vscore, h_vol_double, h_res_sig, InpPeriod);
|
||||||
|
}
|
||||||
|
|
||||||
g_data_ready = true;
|
g_data_ready = true;
|
||||||
}
|
}
|
||||||
@@ -212,37 +392,46 @@ int OnCalculate(const int rates_total,
|
|||||||
|
|
||||||
//--- 2. Live Update for the Current Forming HTF Bar (Index: g_htf_count - 1) on every tick!
|
//--- 2. Live Update for the Current Forming HTF Bar (Index: g_htf_count - 1) on every tick!
|
||||||
int live_idx = g_htf_count - 1;
|
int live_idx = g_htf_count - 1;
|
||||||
if(live_idx >= InpPeriod)
|
if(live_idx >= InpPeriod + 5)
|
||||||
{
|
{
|
||||||
double o[1], h[1], l[1], c[1];
|
double o[1], h[1], l[1], c[1];
|
||||||
long vol[1];
|
long tick_vol[1], vol[1];
|
||||||
int shift = iBarShift(_Symbol, InpTimeframe, htf_time_current, false);
|
int shift = iBarShift(_Symbol, g_calc_timeframe, htf_time_current, false);
|
||||||
if(shift >= 0 &&
|
if(shift >= 0 &&
|
||||||
CopyOpen(_Symbol, InpTimeframe, shift, 1, o) == 1 &&
|
CopyOpen(_Symbol, g_calc_timeframe, shift, 1, o) == 1 &&
|
||||||
CopyHigh(_Symbol, InpTimeframe, shift, 1, h) == 1 &&
|
CopyHigh(_Symbol, g_calc_timeframe, shift, 1, h) == 1 &&
|
||||||
CopyLow(_Symbol, InpTimeframe, shift, 1, l) == 1 &&
|
CopyLow(_Symbol, g_calc_timeframe, shift, 1, l) == 1 &&
|
||||||
CopyClose(_Symbol, InpTimeframe, shift, 1, c) == 1)
|
CopyClose(_Symbol, g_calc_timeframe, shift, 1, c) == 1 &&
|
||||||
|
CopyTickVolume(_Symbol, g_calc_timeframe, shift, 1, tick_vol) == 1)
|
||||||
{
|
{
|
||||||
h_open[live_idx] = o[0];
|
h_open[live_idx] = o[0];
|
||||||
h_high[live_idx] = h[0];
|
h_high[live_idx] = h[0];
|
||||||
h_low[live_idx] = l[0];
|
h_low[live_idx] = l[0];
|
||||||
h_close[live_idx] = c[0];
|
h_close[live_idx] = c[0];
|
||||||
|
h_tick_volume[live_idx] = tick_vol[0];
|
||||||
|
|
||||||
// Copy live volume dynamically
|
// Copy live volume dynamically
|
||||||
int copied = 0;
|
int copied = 0;
|
||||||
if(volume_limit > 0)
|
if(volume_limit > 0)
|
||||||
copied = CopyRealVolume(_Symbol, InpTimeframe, shift, 1, vol);
|
copied = CopyRealVolume(_Symbol, g_calc_timeframe, shift, 1, vol);
|
||||||
else
|
else
|
||||||
copied = CopyTickVolume(_Symbol, InpTimeframe, shift, 1, vol);
|
copied = CopyTickVolume(_Symbol, g_calc_timeframe, shift, 1, vol);
|
||||||
|
|
||||||
if(copied == 1)
|
if(copied == 1)
|
||||||
{
|
{
|
||||||
h_vol[live_idx] = vol[0];
|
h_vol[live_idx] = vol[0];
|
||||||
|
h_vol_double[live_idx] = (double)vol[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
// FIXED: Passed g_htf_count as prev_calculated (instead of live_idx) to mimic native MT5 OnCalculate live ticks
|
// Incremental recalculation on the live HTF index in O(1)
|
||||||
// This completely prevents infinite cumulative volume summation on every live tick!
|
// Passed g_htf_count as prev_calculated to preserve state safety
|
||||||
g_calc.Calculate(g_htf_count, g_htf_count, h_time, h_open, h_high, h_low, h_close, h_vol, h_vol, h_res);
|
g_calculator.Calculate(g_htf_count, g_htf_count, h_time, h_open, h_high, h_low, h_close, h_tick_volume, h_vol, h_res_vscore);
|
||||||
|
|
||||||
|
// Recalculate Signal Line on HTF live index
|
||||||
|
if(InpShowSignal && CheckPointer(g_signal_calculator) != POINTER_INVALID)
|
||||||
|
{
|
||||||
|
g_signal_calculator.CalculateOnArray(g_htf_count, g_htf_count, h_res_vscore, h_vol_double, h_res_sig, InpPeriod);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -252,7 +441,7 @@ int OnCalculate(const int rates_total,
|
|||||||
|
|
||||||
int first_bar_of_forming_htf = rates_total - 1;
|
int first_bar_of_forming_htf = rates_total - 1;
|
||||||
while(first_bar_of_forming_htf > 0 &&
|
while(first_bar_of_forming_htf > 0 &&
|
||||||
iBarShift(_Symbol, InpTimeframe, time[first_bar_of_forming_htf], false) == 0)
|
iBarShift(_Symbol, g_calc_timeframe, time[first_bar_of_forming_htf], false) == 0)
|
||||||
{
|
{
|
||||||
first_bar_of_forming_htf--;
|
first_bar_of_forming_htf--;
|
||||||
}
|
}
|
||||||
@@ -265,41 +454,54 @@ int OnCalculate(const int rates_total,
|
|||||||
for(int i = start; i < rates_total; i++)
|
for(int i = start; i < rates_total; i++)
|
||||||
{
|
{
|
||||||
datetime t = time[i];
|
datetime t = time[i];
|
||||||
int shift_htf = iBarShift(_Symbol, InpTimeframe, t, false);
|
int shift_htf = iBarShift(_Symbol, g_calc_timeframe, t, false);
|
||||||
|
|
||||||
if(shift_htf >= 0)
|
if(shift_htf >= 0)
|
||||||
{
|
{
|
||||||
int idx_htf = g_htf_count - 1 - shift_htf;
|
int idx_htf = g_htf_count - 1 - shift_htf;
|
||||||
if(idx_htf >= 0 && idx_htf < g_htf_count)
|
if(idx_htf >= 0 && idx_htf < g_htf_count)
|
||||||
{
|
{
|
||||||
double val = h_res[idx_htf];
|
double v = h_res_vscore[idx_htf];
|
||||||
BufV[i] = val;
|
|
||||||
|
|
||||||
// Simplified 5-Zone Color Mapping
|
BufferVScore_MTF[i] = v;
|
||||||
if(val >= 2.0)
|
|
||||||
BufCol[i] = 2.0; // Index 2: OrangeRed (Extreme Bullish / Expensive)
|
if(InpShowSignal)
|
||||||
|
BufferSignal_MTF[i] = h_res_sig[idx_htf];
|
||||||
else
|
else
|
||||||
if(val >= 1.5)
|
BufferSignal_MTF[i] = EMPTY_VALUE;
|
||||||
BufCol[i] = 1.0; // Index 1: Coral (Bullish Flow / Warming)
|
|
||||||
|
// Swapped 5-Zone Color Logic dynamically mapped to user-defined level parameters:
|
||||||
|
// v >= ClimaxHigh -> DeepSkyBlue Climax (Bullish)
|
||||||
|
// v >= FlowHigh -> LightSkyBlue Flow (Bullish)
|
||||||
|
// v <= ClimaxLow -> OrangeRed Climax (Bearish)
|
||||||
|
// v <= FlowLow -> Coral Flow (Bearish)
|
||||||
|
// Else -> Gray Neutral
|
||||||
|
if(v >= InpLevelClimaxHigh)
|
||||||
|
BufferColors_MTF[i] = 2.0;
|
||||||
|
else
|
||||||
|
if(v >= InpLevelFlowHigh)
|
||||||
|
BufferColors_MTF[i] = 1.0;
|
||||||
else
|
else
|
||||||
if(val <= -2.0)
|
if(v <= InpLevelClimaxLow)
|
||||||
BufCol[i] = 4.0; // Index 4: DeepSkyBlue (Extreme Bearish / Cheap)
|
BufferColors_MTF[i] = 4.0;
|
||||||
else
|
else
|
||||||
if(val <= -1.5)
|
if(v <= InpLevelFlowLow)
|
||||||
BufCol[i] = 3.0; // Index 3: LightSkyBlue (Bearish Flow / Cooling)
|
BufferColors_MTF[i] = 3.0;
|
||||||
else
|
else
|
||||||
BufCol[i] = 0.0; // Index 0: Gray (Neutral Noise)
|
BufferColors_MTF[i] = 0.0;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
BufV[i] = EMPTY_VALUE;
|
BufferVScore_MTF[i] = EMPTY_VALUE;
|
||||||
BufCol[i] = 0.0;
|
BufferSignal_MTF[i] = EMPTY_VALUE;
|
||||||
|
BufferColors_MTF[i] = 0.0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
BufV[i] = EMPTY_VALUE;
|
BufferVScore_MTF[i] = EMPTY_VALUE;
|
||||||
BufCol[i] = 0.0;
|
BufferSignal_MTF[i] = EMPTY_VALUE;
|
||||||
|
BufferColors_MTF[i] = 0.0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -315,7 +517,7 @@ void OnTimer()
|
|||||||
if(!g_data_synced)
|
if(!g_data_synced)
|
||||||
{
|
{
|
||||||
int required_bars = InpPeriod + 5;
|
int required_bars = InpPeriod + 5;
|
||||||
if(EnsureHTFDataReady(_Symbol, InpTimeframe, required_bars))
|
if(EnsureHTFDataReady(_Symbol, g_calc_timeframe, required_bars))
|
||||||
{
|
{
|
||||||
g_data_synced = true;
|
g_data_synced = true;
|
||||||
ChartRedraw(); // Force MT5 to invoke OnCalculate
|
ChartRedraw(); // Force MT5 to invoke OnCalculate
|
||||||
@@ -323,4 +525,3 @@ void OnTimer()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
//+------------------------------------------------------------------+
|
|
||||||
|
|||||||
Reference in New Issue
Block a user