HVR 1[1].00

Download script
Author: mladen

The Historical Volatility Ratio (HVR) indicator is designed to measure and compare price fluctuations over two different timeframes simultaneously. By calculating the relationship between short-term and long-term market behavior, it helps traders visualize whether current price movements are unusually intense or relatively calm compared to historical norms.

How the Logic Works

The indicator operates through a three-step mathematical process:

1. Logarithmic Price Tracking Instead of looking at raw price changes (which can be distorted by large price swings), the script first converts price data into logarithmic values. This step measures the percentage-like change between one candle and the next. By using a logarithmic scale, the tool ensures that market movements are treated consistently, regardless of whether the asset's price is high or low.

2. Calculating Standard Deviation (Volatility) The script then calculates the "Standard Deviation" for two specific periods defined by the user:

  • Short Period: Captures immediate, recent price activity.
  • Long Period: Captures broader, historical price trends.

In simple terms, these calculations determine how much the price typically "wiggles" or deviates from its average path during those specific timeframes.

3. The Ratio Comparison The final output—the HVR line you see on your chart—is the result of dividing the short-term volatility by the long-term volatility. This produces a ratio that tells you:

  • If the ratio is high, it indicates that recent price action is significantly more volatile than what has been seen historically over the longer period.
  • If the ratio is low, it suggests that the market is currently moving with less intensity than its historical average.

Practical Application

By monitoring this ratio, traders can identify "overextended" markets. When the line spikes, it may signal a period of extreme panic or excitement; when it drops significantly, it may indicate a period of consolidation or stable growth. It serves as a relative gauge, allowing you to see not just how much the market is moving, but how those movements compare to the "normal" behavior of the asset.

Indicators Used
Moving average indicator
12 Views
0 Downloads
0 Favorites
HVR 1[1].00
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