#property copyright "x"
#property link      "x"
#property version   "1.10"
#property strict
#property indicator_separate_window
#property indicator_buffers 2
#property indicator_plots   2

//#property indicator_maximum 1.0
//#property indicator_minimum 0.9

//--- plot Buffer
#property indicator_label1  "Buffer0"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrOrchid
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1

#property indicator_label2  "Buffer1"
#property indicator_type2   DRAW_LINE
#property indicator_color2  clrOrange
#property indicator_style2  STYLE_SOLID
#property indicator_width2  2

//--- input parameters
input int      barsLimit=0;
input int      maPeriod=13;
input int      maMode=3;

bool     recalculateBars=true;
bool     recalculateBars2=true;


//--- indicator buffers
double         Buffer0[];
double         Buffer1[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,Buffer0);
   SetIndexBuffer(1,Buffer1);
   
   recalculateBars=true;
   recalculateBars2=true;
   
//---
   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;

   for(i=barsToCalculate();i>=0;i--)
   {
      double priceHigh = iHigh(Symbol(), Period(), i);
      double priceLow = iLow(Symbol(), Period(), i);
      double priceClose = iClose(Symbol(), Period(), i);
      double partResult;
      
      if(priceLow != 0 && priceClose != 0) 
      { 
         partResult = priceHigh / priceLow;
         partResult = (partResult / priceClose) * 100;
         
         Buffer0[i] = partResult;
      }
      else
      {
         Buffer0[i] = 0;
      }
   }
   
   for(i=barsToCalculate2();i>=0;i--)
   {
      Buffer1[i] = iMAOnArray(Buffer0, 0, maPeriod, 0, (ENUM_MA_METHOD)maMode, i); 
   }

   
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+



int barsToCalculate()
{
   int limit;
   if(recalculateBars==true) 
   {
      if(barsLimit==0) { limit=Bars-20; }
      else { limit=barsLimit; }
      recalculateBars=false;
   }
   else limit=0;
   return(limit);
}


int barsToCalculate2()
{
   int limit;
   if(recalculateBars2==true) 
   {
      if(barsLimit==0) { limit=Bars-20; }
      else { limit=barsLimit; }
      recalculateBars2=false;
   }
   else limit=0;
   return(limit);
}
