From f908b456152010db8c1da84f640def3f9e95525b Mon Sep 17 00:00:00 2001 From: Toh4iem9 Date: Mon, 22 Jun 2026 08:43:38 +0200 Subject: [PATCH] refactor: Refactored with dynamic MA Signal Line and VWMA volume integration --- .../MyIndicators/Quant/Velocity_Pro.mq5 | 187 ++++++++++++++---- 1 file changed, 153 insertions(+), 34 deletions(-) diff --git a/Indicators/MyIndicators/Quant/Velocity_Pro.mq5 b/Indicators/MyIndicators/Quant/Velocity_Pro.mq5 index e96cd2c..c2fa47f 100644 --- a/Indicators/MyIndicators/Quant/Velocity_Pro.mq5 +++ b/Indicators/MyIndicators/Quant/Velocity_Pro.mq5 @@ -4,13 +4,13 @@ //| Copyright 2026, xxxxxxxx | //+------------------------------------------------------------------+ #property copyright "Copyright 2026, xxxxxxxx" -#property version "3.10" // Added Mirrored Speed (Negative Scalar) -#property description "Displays Velocity (Histogram) and Speed Envelope." +#property version "3.20" // Refactored with dynamic MA Signal Line and VWMA volume integration +#property description "Displays Velocity (Histogram), Speed Envelope, and customizable Signal Line." #property description "Touching the envelope lines signals climatic efficiency." #property indicator_separate_window -#property indicator_buffers 4 -#property indicator_plots 3 +#property indicator_buffers 5 +#property indicator_plots 4 //--- Levels #property indicator_level1 1.0 @@ -39,97 +39,216 @@ #property indicator_style3 STYLE_SOLID #property indicator_width3 1 +//--- Plot 4: Optional Signal Line (Wyckoff Reversal Trigger) +#property indicator_label4 "Signal" +#property indicator_type4 DRAW_LINE +#property indicator_color4 clrMaroon +#property indicator_style4 STYLE_SOLID +#property indicator_width4 1 + #include #include +#include -//--- Parameters -input int InpVelPeriod = 3; -input int InpATRPeriod = 14; -input double InpThreshold = 1.0; -input bool InpShowSpeed = true; +//--- Parameters --- +input int InpVelPeriod = 3; // Velocity Vector Lookback +input int InpATRPeriod = 14; // Volatility Base (ATR) +input double InpThreshold = 1.0; // Expansion Threshold (Sigma) +input bool InpShowSpeed = true; // Show Speed Envelope? + +//--- Signal Line Parameters (Dynamic MA Engine Integration) +input bool InpShowSignal = true; // Show Signal Line? +input int InpSignalPeriod= 5; // Signal Line Period +input ENUM_MA_TYPE InpSignalType = SMA; // Signal Line MA Type //--- Buffers double BufVel[]; double BufCol[]; double BufSpeedPos[]; double BufSpeedNeg[]; +double BufSignal[]; -CATRCalculator *g_atr; +//--- Volume Cache to support Volume-Weighted types (VWMA) on custom arrays +double g_double_volume[]; + +//--- Global Engines +CATRCalculator *g_atr; +CMovingAverageCalculator *g_signal_calculator; //+------------------------------------------------------------------+ //| Init | //+------------------------------------------------------------------+ int OnInit() { - SetIndexBuffer(0, BufVel, INDICATOR_DATA); - SetIndexBuffer(1, BufCol, INDICATOR_COLOR_INDEX); +//--- Bind Buffers + SetIndexBuffer(0, BufVel, INDICATOR_DATA); + SetIndexBuffer(1, BufCol, INDICATOR_COLOR_INDEX); SetIndexBuffer(2, BufSpeedPos, INDICATOR_DATA); SetIndexBuffer(3, BufSpeedNeg, INDICATOR_DATA); + SetIndexBuffer(4, BufSignal, INDICATOR_DATA); + ArraySetAsSeries(BufVel, false); + ArraySetAsSeries(BufCol, false); + ArraySetAsSeries(BufSpeedPos, false); + ArraySetAsSeries(BufSpeedNeg, false); + ArraySetAsSeries(BufSignal, false); + +//--- Configure Speed Envelope Displays if(!InpShowSpeed) { PlotIndexSetInteger(1, PLOT_DRAW_TYPE, DRAW_NONE); PlotIndexSetInteger(2, PLOT_DRAW_TYPE, DRAW_NONE); } + else + { + PlotIndexSetInteger(1, PLOT_DRAW_TYPE, DRAW_LINE); + PlotIndexSetInteger(2, PLOT_DRAW_TYPE, DRAW_LINE); + } +//--- Configure Optional Signal Line Calculator + if(InpShowSignal) + { + // Explicitly restore DRAW_LINE & Label in case it was previously disabled + PlotIndexSetInteger(3, PLOT_DRAW_TYPE, DRAW_LINE); + PlotIndexSetString(3, PLOT_LABEL, "Signal"); + + g_signal_calculator = new CMovingAverageCalculator(); + if(CheckPointer(g_signal_calculator) == POINTER_INVALID || !g_signal_calculator.Init(InpSignalPeriod, InpSignalType)) + { + Print("Error: Failed to initialize Signal Line Calculator Engine."); + return INIT_FAILED; + } + } + else + { + // Set DRAW_NONE and clear Label to fully purge it from the MT5 Data Window + PlotIndexSetInteger(3, PLOT_DRAW_TYPE, DRAW_NONE); + PlotIndexSetString(3, PLOT_LABEL, NULL); + } + +//--- Configure Core Volatility Engine g_atr = new CATRCalculator(); - g_atr.Init(InpATRPeriod, ATR_POINTS); + if(CheckPointer(g_atr) == POINTER_INVALID || !g_atr.Init(InpATRPeriod, ATR_POINTS)) + { + Print("Error: Failed to initialize ATR Calculator Engine."); + return INIT_FAILED; + } + +//--- Dynamically set the indicator short name + string name = ""; + if(InpShowSignal) + { + string sig_name = EnumToString(InpSignalType); + StringToUpper(sig_name); + name = StringFormat("Velocity(%d) %s(%d)", InpVelPeriod, sig_name, InpSignalPeriod); + } + else + { + name = StringFormat("Velocity(%d)", InpVelPeriod); + } - string name = StringFormat("Velocity(%d)", InpVelPeriod); IndicatorSetString(INDICATOR_SHORTNAME, name); + PlotIndexSetString(0, PLOT_LABEL, "Velocity"); + PlotIndexSetString(1, PLOT_LABEL, "Speed (+)"); + PlotIndexSetString(2, PLOT_LABEL, "Speed (-)"); + IndicatorSetInteger(INDICATOR_DIGITS, 3); + return(INIT_SUCCEEDED); } -void OnDeinit(const int r) { if(CheckPointer(g_atr)==POINTER_DYNAMIC) delete g_atr; } +//+------------------------------------------------------------------+ +//| OnDeinit | +//+------------------------------------------------------------------+ +void OnDeinit(const int r) + { + if(CheckPointer(g_atr) != POINTER_INVALID) + delete g_atr; + if(CheckPointer(g_signal_calculator) != POINTER_INVALID) + delete g_signal_calculator; + } //+------------------------------------------------------------------+ //| Calculate | //+------------------------------------------------------------------+ -int OnCalculate(const int rates_total, const int prev_calculated, const datetime &time[], const double &open[], const double &high[], const double &low[], const double &close[], const long &tick_volume[], const long &volume[], const int &spread[]) +int OnCalculate(const int rates_total, + const int prev_calculated, + const datetime &time[], + const double &open[], + const double &high[], + const double &low[], + const double &close[], + const long &tick_volume[], + const long &volume[], + const int &spread[]) { - if(rates_total < InpATRPeriod+InpVelPeriod) + if(rates_total < InpATRPeriod + InpVelPeriod + 5) return 0; +//--- Convert long volume to double cache array in O(1) incrementally + ArrayResize(g_double_volume, rates_total); + int start_sync = (prev_calculated > 0) ? prev_calculated - 1 : 0; + long volume_limit = (long)SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_LIMIT); + + 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]; + } + +//--- 1. Calculate underlying Average True Range double atr_buf[]; g_atr.Calculate(rates_total, prev_calculated, open, high, low, close, atr_buf); - int start = (prev_calculated>0) ? prev_calculated-1 : InpATRPeriod+InpVelPeriod; +//--- 2. Calculate Velocity & Speed Envelopes (O(1) incremental) + int start = (prev_calculated > 0) ? prev_calculated - 1 : InpATRPeriod + InpVelPeriod; - for(int i=start; i InpThreshold) - BufCol[i] = 1.0; + BufCol[i] = 1.0; // Index 1: Lime else if(vel < -InpThreshold) - BufCol[i] = 2.0; + BufCol[i] = 2.0; // Index 2: Red else - BufCol[i] = 0.0; + BufCol[i] = 0.0; // Index 0: Gray (Neutral Noise) - // 2. Speed (Scalar) - Path Length - double path_length = 0; - for(int k=0; k