mirror of
https://github.com/softwaredevelop/mql5.git
synced 2026-08-19 15:28:06 +00:00
refactor: Refactored with dynamic MA Signal Line and VWMA volume integration
This commit is contained in:
@@ -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 <MyIncludes\ATR_Calculator.mqh>
|
||||
#include <MyIncludes\Metrics_Tools.mqh>
|
||||
#include <MyIncludes\MovingAverage_Engine.mqh>
|
||||
|
||||
//--- 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<rates_total; i++)
|
||||
for(int i = start; i < rates_total; i++)
|
||||
{
|
||||
double atr = atr_buf[i];
|
||||
if(atr == 0)
|
||||
{
|
||||
BufVel[i]=0;
|
||||
BufSpeedPos[i]=0;
|
||||
BufSpeedNeg[i]=0;
|
||||
BufVel[i] = 0.0;
|
||||
BufSpeedPos[i] = 0.0;
|
||||
BufSpeedNeg[i] = 0.0;
|
||||
continue;
|
||||
}
|
||||
|
||||
// 1. Velocity (Vector)
|
||||
double vel = CMetricsTools::CalculateSlope(close[i], close[i-InpVelPeriod], atr, InpVelPeriod);
|
||||
// Calculate Velocity Vector (Directional Slope)
|
||||
double vel = CMetricsTools::CalculateSlope(close[i], close[i - InpVelPeriod], atr, InpVelPeriod);
|
||||
BufVel[i] = vel;
|
||||
|
||||
// Color
|
||||
// Color coding logic based on significance threshold
|
||||
if(vel > 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<InpVelPeriod; k++)
|
||||
// Calculate Speed Scalar (Total Path Length over Period / normalizer)
|
||||
double path_length = 0.0;
|
||||
for(int k = 0; k < InpVelPeriod; k++)
|
||||
{
|
||||
path_length += MathAbs(close[i-k] - close[i-k-1]);
|
||||
path_length += MathAbs(close[i - k] - close[i - k - 1]);
|
||||
}
|
||||
double speed = (path_length / InpVelPeriod) / atr;
|
||||
|
||||
// 3. Mirroring
|
||||
// Mirroring envelopes for both upside and downside analysis
|
||||
BufSpeedPos[i] = speed;
|
||||
BufSpeedNeg[i] = -speed; // Mirror for downside analysis
|
||||
BufSpeedNeg[i] = -speed;
|
||||
}
|
||||
|
||||
//--- 3. Calculate Optional Signal Line on top of Velocity values (Handles VWMA dynamically)
|
||||
if(InpShowSignal && CheckPointer(g_signal_calculator) != POINTER_INVALID)
|
||||
{
|
||||
// Source start index is 'InpATRPeriod + InpVelPeriod' since Velocity before that is invalid
|
||||
g_signal_calculator.CalculateOnArray(rates_total, prev_calculated, BufVel, g_double_volume, BufSignal, InpATRPeriod + InpVelPeriod);
|
||||
}
|
||||
|
||||
return rates_total;
|
||||
|
||||
Reference in New Issue
Block a user