//+------------------------------------------------------------------+
//|                                                HL Volitility.mq4 |
//|                        Copyright 2016, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2016, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#property indicator_separate_window
#property indicator_buffers 2
#property indicator_plots   1
//--- plot HLV
#property indicator_label1  "HLV"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrRed
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
//--- input parameters
input int      MAPeriod=4;
//--- indicator buffers
double         HLVBuffer[];
double         HL[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   
   SetIndexBuffer(0,HLVBuffer,INDICATOR_DATA);
   SetIndexBuffer(1,HL,INDICATOR_CALCULATIONS);
   SetIndexLabel(1,NULL);
   IndicatorDigits(_Digits);
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom 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[])
  {
//---
   int    i,limit;
   limit=rates_total-prev_calculated;
   if(prev_calculated>0)
     {
      limit++;
     }
   for(i=limit; i>=0; i--)
     {
      if(i>Bars(Symbol(),0)-MAPeriod*2)
        {
         continue;
        }
      else
        {
         HL[i]=high[i]-low[i];
        }
     }
   for(i=limit; i>=0; i--)
     {
      if(i>Bars(Symbol(),0)-MAPeriod*2)
        {
         continue;
        }
      else
        {
         HLVBuffer[i]=iMAOnArray(HL,0,MAPeriod,0,MODE_SMMA,i);
        }
     }
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
