mirror of
https://github.com/softwaredevelop/mql5.git
synced 2026-08-26 02:38:05 +00:00
refactor(indicators): Added Mirrored Speed (Negative Scalar)
This commit is contained in:
@@ -1,16 +1,16 @@
|
|||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
//| Velocity_Pro.mq5 |
|
//| Velocity_Pro.mq5 |
|
||||||
//| Velocity (Vector) vs Speed (Scalar) |
|
//| Velocity vs Scaler Speed Envelope |
|
||||||
//| Copyright 2026, xxxxxxxx |
|
//| Copyright 2026, xxxxxxxx |
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
#property copyright "Copyright 2026, xxxxxxxx"
|
#property copyright "Copyright 2026, xxxxxxxx"
|
||||||
#property version "3.00"
|
#property version "3.10" // Added Mirrored Speed (Negative Scalar)
|
||||||
#property description "Displays Velocity (Histogram) and Speed (Line)."
|
#property description "Displays Velocity (Histogram) and Speed Envelope."
|
||||||
#property description "Velocity = Directional. Speed = Distance Traveled."
|
#property description "Touching the envelope lines signals climatic efficiency."
|
||||||
|
|
||||||
#property indicator_separate_window
|
#property indicator_separate_window
|
||||||
#property indicator_buffers 3
|
#property indicator_buffers 4
|
||||||
#property indicator_plots 2
|
#property indicator_plots 3
|
||||||
|
|
||||||
//--- Levels
|
//--- Levels
|
||||||
#property indicator_level1 1.0
|
#property indicator_level1 1.0
|
||||||
@@ -20,32 +20,40 @@
|
|||||||
#property indicator_levelstyle STYLE_DOT
|
#property indicator_levelstyle STYLE_DOT
|
||||||
|
|
||||||
//--- Plot 1: Velocity Histogram
|
//--- Plot 1: Velocity Histogram
|
||||||
#property indicator_label1 "Velocity (Vector)"
|
#property indicator_label1 "Velocity"
|
||||||
#property indicator_type1 DRAW_COLOR_HISTOGRAM
|
#property indicator_type1 DRAW_COLOR_HISTOGRAM
|
||||||
#property indicator_color1 clrGray, clrLime, clrRed // Neutral, Up, Down
|
#property indicator_color1 clrGray, clrLime, clrRed
|
||||||
#property indicator_style1 STYLE_SOLID
|
#property indicator_style1 STYLE_SOLID
|
||||||
#property indicator_width1 2
|
#property indicator_width1 2
|
||||||
|
|
||||||
//--- Plot 2: Speed Line (Optional)
|
//--- Plot 2: Speed Positive (Top)
|
||||||
#property indicator_label2 "Speed (Scalar)"
|
#property indicator_label2 "Speed (+)"
|
||||||
#property indicator_type2 DRAW_LINE
|
#property indicator_type2 DRAW_LINE
|
||||||
#property indicator_color2 clrGold
|
#property indicator_color2 clrGold
|
||||||
#property indicator_style2 STYLE_SOLID
|
#property indicator_style2 STYLE_SOLID
|
||||||
#property indicator_width2 1
|
#property indicator_width2 1
|
||||||
|
|
||||||
|
//--- Plot 3: Speed Negative (Bottom)
|
||||||
|
#property indicator_label3 "Speed (-)"
|
||||||
|
#property indicator_type3 DRAW_LINE
|
||||||
|
#property indicator_color3 clrGold
|
||||||
|
#property indicator_style3 STYLE_SOLID
|
||||||
|
#property indicator_width3 1
|
||||||
|
|
||||||
#include <MyIncludes\ATR_Calculator.mqh>
|
#include <MyIncludes\ATR_Calculator.mqh>
|
||||||
#include <MyIncludes\Metrics_Tools.mqh>
|
#include <MyIncludes\Metrics_Tools.mqh>
|
||||||
|
|
||||||
//--- Parameters
|
//--- Parameters
|
||||||
input int InpVelPeriod = 3; // Lookback Period
|
input int InpVelPeriod = 3;
|
||||||
input int InpATRPeriod = 14; // Normalization ATR
|
input int InpATRPeriod = 14;
|
||||||
input double InpThreshold = 1.0; // High Velocity Threshold
|
input double InpThreshold = 1.0;
|
||||||
input bool InpShowSpeed = true; // Show Scalar Speed Line?
|
input bool InpShowSpeed = true;
|
||||||
|
|
||||||
//--- Buffers
|
//--- Buffers
|
||||||
double BufVel[];
|
double BufVel[];
|
||||||
double BufCol[];
|
double BufCol[];
|
||||||
double BufSpeed[];
|
double BufSpeedPos[];
|
||||||
|
double BufSpeedNeg[];
|
||||||
|
|
||||||
CATRCalculator *g_atr;
|
CATRCalculator *g_atr;
|
||||||
|
|
||||||
@@ -56,11 +64,14 @@ int OnInit()
|
|||||||
{
|
{
|
||||||
SetIndexBuffer(0, BufVel, INDICATOR_DATA);
|
SetIndexBuffer(0, BufVel, INDICATOR_DATA);
|
||||||
SetIndexBuffer(1, BufCol, INDICATOR_COLOR_INDEX);
|
SetIndexBuffer(1, BufCol, INDICATOR_COLOR_INDEX);
|
||||||
SetIndexBuffer(2, BufSpeed, INDICATOR_DATA);
|
SetIndexBuffer(2, BufSpeedPos, INDICATOR_DATA);
|
||||||
|
SetIndexBuffer(3, BufSpeedNeg, INDICATOR_DATA);
|
||||||
|
|
||||||
// Hide Speed line if requested
|
|
||||||
if(!InpShowSpeed)
|
if(!InpShowSpeed)
|
||||||
|
{
|
||||||
PlotIndexSetInteger(1, PLOT_DRAW_TYPE, DRAW_NONE);
|
PlotIndexSetInteger(1, PLOT_DRAW_TYPE, DRAW_NONE);
|
||||||
|
PlotIndexSetInteger(2, PLOT_DRAW_TYPE, DRAW_NONE);
|
||||||
|
}
|
||||||
|
|
||||||
g_atr = new CATRCalculator();
|
g_atr = new CATRCalculator();
|
||||||
g_atr.Init(InpATRPeriod, ATR_POINTS);
|
g_atr.Init(InpATRPeriod, ATR_POINTS);
|
||||||
@@ -70,9 +81,6 @@ int OnInit()
|
|||||||
return(INIT_SUCCEEDED);
|
return(INIT_SUCCEEDED);
|
||||||
}
|
}
|
||||||
|
|
||||||
//+------------------------------------------------------------------+
|
|
||||||
//| Deinit |
|
|
||||||
//+------------------------------------------------------------------+
|
|
||||||
void OnDeinit(const int r) { if(CheckPointer(g_atr)==POINTER_DYNAMIC) delete g_atr; }
|
void OnDeinit(const int r) { if(CheckPointer(g_atr)==POINTER_DYNAMIC) delete g_atr; }
|
||||||
|
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
@@ -83,7 +91,6 @@ int OnCalculate(const int rates_total, const int prev_calculated, const datetime
|
|||||||
if(rates_total < InpATRPeriod+InpVelPeriod)
|
if(rates_total < InpATRPeriod+InpVelPeriod)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
// 1. Calc ATR
|
|
||||||
double atr_buf[];
|
double atr_buf[];
|
||||||
g_atr.Calculate(rates_total, prev_calculated, open, high, low, close, atr_buf);
|
g_atr.Calculate(rates_total, prev_calculated, open, high, low, close, atr_buf);
|
||||||
|
|
||||||
@@ -95,34 +102,35 @@ int OnCalculate(const int rates_total, const int prev_calculated, const datetime
|
|||||||
if(atr == 0)
|
if(atr == 0)
|
||||||
{
|
{
|
||||||
BufVel[i]=0;
|
BufVel[i]=0;
|
||||||
BufSpeed[i]=0;
|
BufSpeedPos[i]=0;
|
||||||
|
BufSpeedNeg[i]=0;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// --- A. Velocity (Vector) ---
|
// 1. Velocity (Vector)
|
||||||
// (Close[i] - Close[i-N]) / (N * ATR)
|
|
||||||
double vel = CMetricsTools::CalculateSlope(close[i], close[i-InpVelPeriod], atr, InpVelPeriod);
|
double vel = CMetricsTools::CalculateSlope(close[i], close[i-InpVelPeriod], atr, InpVelPeriod);
|
||||||
BufVel[i] = vel;
|
BufVel[i] = vel;
|
||||||
|
|
||||||
// Color Logic for Velocity
|
// Color
|
||||||
if(vel > InpThreshold)
|
if(vel > InpThreshold)
|
||||||
BufCol[i] = 1.0; // Lime
|
BufCol[i] = 1.0;
|
||||||
else
|
else
|
||||||
if(vel < -InpThreshold)
|
if(vel < -InpThreshold)
|
||||||
BufCol[i] = 2.0;// Red
|
BufCol[i] = 2.0;
|
||||||
else
|
else
|
||||||
BufCol[i] = 0.0; // Gray
|
BufCol[i] = 0.0;
|
||||||
|
|
||||||
// --- B. Speed (Scalar) ---
|
// 2. Speed (Scalar) - Path Length
|
||||||
// Avg(Abs(Close[k] - Close[k-1])) / ATR
|
|
||||||
// Path Length over period
|
|
||||||
double path_length = 0;
|
double path_length = 0;
|
||||||
for(int k=0; k<InpVelPeriod; k++)
|
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 avg_step = path_length / InpVelPeriod;
|
double speed = (path_length / InpVelPeriod) / atr;
|
||||||
BufSpeed[i] = avg_step / atr;
|
|
||||||
|
// 3. Mirroring
|
||||||
|
BufSpeedPos[i] = speed;
|
||||||
|
BufSpeedNeg[i] = -speed; // Mirror for downside analysis
|
||||||
}
|
}
|
||||||
|
|
||||||
return rates_total;
|
return rates_total;
|
||||||
|
|||||||
Reference in New Issue
Block a user