L1Trend/Indicators/L1VolatilityRegimeColor.mq5

162 lines
12 KiB
MQL5
Raw Permalink Normal View History

<EFBFBD><EFBFBD>//+------------------------------------------------------------------+
//| L1VolatilityRegimeColor.mq5 |
//| Copyright 2000-2026, MetaQuotes Ltd. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2000-2026, MetaQuotes Ltd."
#property link "https://www.mql5.com"
#property version "1.00"
#property indicator_separate_window
#property indicator_buffers 4
#property indicator_plots 4
//---
#property indicator_label1 "Range"
#property indicator_type1 DRAW_LINE
#property indicator_color1 clrDodgerBlue
#property indicator_width1 2
//---
#property indicator_label2 "Trend"
#property indicator_type2 DRAW_LINE
#property indicator_color2 clrLime
#property indicator_width2 2
//---
#property indicator_label3 "Expansion"
#property indicator_type3 DRAW_LINE
#property indicator_color3 clrOrange
#property indicator_width3 2
//---
#property indicator_label4 "Panic"
#property indicator_type4 DRAW_LINE
#property indicator_color4 clrRed
#property indicator_width4 2
//--- input parameters
input int BarsToShow = 1000; // Number of bars to calculate L1
input double CoefLambda = 0.015; // Lambda in lambda_max units
input int ATRPeriod = 14; // ATR period
input int SmoothPeriod = 10; // Smooth period
input double L1MoveThresh = 0.0; // Move volatility
input double LowVolThresh = 0.5; // Low volatility
input double HighVolThresh = 1.5; // High volatility
input double PanicMult = 2.0; // Panic volatility
//--- buffers
double Regime[];
double BufRange[], BufTrend[], BufExpansion[], BufPanic[];
//+------------------------------------------------------------------+
//| Indicator initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
SetIndexBuffer(0,BufRange,INDICATOR_DATA);
SetIndexBuffer(1,BufTrend,INDICATOR_DATA);
SetIndexBuffer(2,BufExpansion,INDICATOR_DATA);
SetIndexBuffer(3,BufPanic,INDICATOR_DATA);
//---
PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,EMPTY_VALUE);
PlotIndexSetDouble(1,PLOT_EMPTY_VALUE,EMPTY_VALUE);
PlotIndexSetDouble(2,PLOT_EMPTY_VALUE,EMPTY_VALUE);
PlotIndexSetDouble(3,PLOT_EMPTY_VALUE,EMPTY_VALUE);
//---
IndicatorSetInteger(INDICATOR_DIGITS,0);
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Indicator iteration function |
//+------------------------------------------------------------------+
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 < BarsToShow+ATRPeriod)
{
ArrayInitialize(Regime, EMPTY_VALUE);
ArrayInitialize(BufRange, EMPTY_VALUE);
ArrayInitialize(BufTrend, EMPTY_VALUE);
ArrayInitialize(BufExpansion, EMPTY_VALUE);
ArrayInitialize(BufPanic, EMPTY_VALUE);
return 0;
}
//--- new bars
static datetime last_bar_time=0;
bool new_bar=(time[0]!=last_bar_time);
bool need_recalc=(prev_calculated==0) || new_bar || (rates_total!=prev_calculated);
if(!need_recalc)
return prev_calculated;
last_bar_time=time[0];
//---
int start = rates_total - BarsToShow;
int count = BarsToShow;
//---
ArrayResize(Regime,rates_total);
for(int i=0;i<start;i++)
Regime[i]=EMPTY_VALUE;
//---
vector<double> DataClose(count), DataHigh(count), DataLow(count);
for(int i=0;i<count;i++)
{
DataClose[i]=close[start+i];
DataHigh[i]=high[start+i];
DataLow[i]=low[start+i];
}
//---
vector<double> L1(count);
if(!DataClose.L1TrendFilter(L1, CoefLambda, true))
return prev_calculated;
//---
vector<double> TR(count), ATR(count);
for(int i=0;i<count;i++)
{
if(i==0) TR[i]=DataHigh[i]-DataLow[i];
else
{
double h_l = DataHigh[i]-DataLow[i];
double h_pc = MathAbs(DataHigh[i]-DataClose[i-1]);
double l_pc = MathAbs(DataLow[i]-DataClose[i-1]);
TR[i] = MathMax(h_l, MathMax(h_pc, l_pc));
}
int from=MathMax(0,i-ATRPeriod+1);
double sumTR=0;
int n=i-from+1;
for(int j=from;j<=i;j++) sumTR+=TR[j];
ATR[i]=sumTR/n;
}
//---
vector<double> NormVol(count), SmoothVol(count);
for(int i=0;i<count;i++)
NormVol[i] = (ATR[i]>0) ? MathAbs(DataClose[i]-L1[i])/ATR[i] : 0;
//---
double alpha=2.0/(SmoothPeriod+1.0);
SmoothVol[0]=NormVol[0];
for(int i=1;i<count;i++)
SmoothVol[i]=alpha*NormVol[i] + (1-alpha)*SmoothVol[i-1];
//--- calc Regime[]
for(int i=0;i<count;i++)
{
double vol = SmoothVol[i];
double deltaL1 = (i>0) ? (L1[i]-L1[i-1]) : 0.0;
if(vol < LowVolThresh)
Regime[start+i]=0;
else if(vol < HighVolThresh)
Regime[start+i]= (MathAbs(deltaL1) > L1MoveThresh) ? 1 : 0;
else if(vol < HighVolThresh*PanicMult)
Regime[start+i]=2;
else
Regime[start+i]=3;
}
//--- buffers
for(int i=0;i<rates_total;i++)
{
BufRange[i] = (Regime[i]==0) ? Regime[i] : EMPTY_VALUE;
BufTrend[i] = (Regime[i]==1) ? Regime[i] : EMPTY_VALUE;
BufExpansion[i] = (Regime[i]==2) ? Regime[i] : EMPTY_VALUE;
BufPanic[i] = (Regime[i]==3) ? Regime[i] : EMPTY_VALUE;
}
//---
return rates_total;
}