this //+------------------------------------------------------------------+
//| Historical Volatility ratio indicator |
//| HVR.mq4 |
//| mladen |
//+------------------------------------------------------------------+
#property copyright "mladen"
#property link ""
#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1 Red
#property indicator_minimum 0
//
//
//
//
//
extern int ShortPeriod = 6;
extern int LongPeriod = 100;
extern int AverageMethod = 0;
//
//
//
//
//
double HvrValue[];
double LogValue[];
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int init()
{
IndicatorBuffers(2);
SetIndexBuffer(0,HvrValue);
SetIndexBuffer(1,LogValue);
//
//
//
//
//
IndicatorShortName("HVR ("+ShortPeriod+","+LongPeriod+")");
return(0);
}
int deinit()
{
return(0);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int start()
{
double dev;
int counted_bars=IndicatorCounted();
int limit,i,k;
if(counted_bars < 0) return(-1);
limit = Bars-counted_bars;
//
//
//
//
//
for (i = limit; i>=0; i--)
if (Close[i+1]!=0)
LogValue[i] = MathLog(Close[i]/Close[i+1]);
else LogValue[i] = 0;
//
//
//
//
//
for (i = limit; i>=0; i--) {
dev = hvDeviation(LongPeriod,i);
if (dev!=0)
HvrValue[i] = hvDeviation(ShortPeriod,i)/dev;
else HvrValue[i] = 0;
}
return(0);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
//
//
//
//
//
double hvDeviation(int period,int shift)
{
double dMA = iMAOnArray(LogValue,0,period,0,AverageMethod,shift);
double dSum = 0;
int i;
for(i=0; i<period; i++) dSum += (LogValue[shift+i]-dMA)*(LogValue[shift+i]-dMA);
return(MathSqrt(dSum/period));
}
Comments