//+------------------------------------------------------------------+
//|                                               Williams %R MA.mq4 |
//|                                                           GumRai |
//|                                                             none |
//+------------------------------------------------------------------+
#property copyright "GumRai"
#property link      "none"
#property version   "1.00"
#property strict
#property indicator_separate_window
#property indicator_minimum -100
#property indicator_maximum 0
#property indicator_buffers 2
#property indicator_plots   2
//--- plot WilliamsR1
#property indicator_label1  "WilliamsR1"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrNONE
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
//--- plot WilliamsMA
#property indicator_label2  "WilliamsMA"
#property indicator_type2   DRAW_LINE
#property indicator_color2  clrDodgerBlue
#property indicator_style2  STYLE_SOLID
#property indicator_width2  1
//#property indicator_level1 -20
#property indicator_level2 -50
//#property indicator_level3 -80
//--- input parameters
input int      Period1=14;//Williams Period 
input int      PeriodMA=21;//EMA Period 
//--- indicator buffers
double         WilliamsR1Buffer[];
double         WilliamsMABuffer[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
   
  
//--- indicator buffers mapping
   SetIndexBuffer(0,WilliamsR1Buffer);
   SetIndexBuffer(1,WilliamsMABuffer);
   SetIndexDrawBegin(0, Period1);
   SetIndexDrawBegin(1, Period1+PeriodMA);
   SetIndexLabel(0, "Williams %R "+(string)Period1);
   SetIndexLabel(1, "Williams MA "+(string)PeriodMA);
   
//---
   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;
   if(rates_total <= Period1+PeriodMA) 
       return(0);
   if(prev_calculated==0)
      limit=rates_total-Period1;
   else
      limit=rates_total-prev_calculated+1;
   
   for(i=limit;i>=0;i--)
     {
       double period_high = High[Highest(NULL, 0, MODE_HIGH, Period1, i)];
       double period_low = Low[Lowest(NULL, 0, MODE_LOW, Period1, i)];      
       if(!CompareDouble((period_high - period_low), 0.0))
           WilliamsR1Buffer[i] = -100*(period_high - Close[i]) / (period_high - period_low);
     }
   if(limit>PeriodMA)
      limit-=PeriodMA;
   for(i=limit;i>=0;i--)
     {
      WilliamsMABuffer[i]=iMAOnArray(WilliamsR1Buffer,0,PeriodMA,0,MODE_SMMA,i);
     }
   
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
//| CompareDouble function                                           |
//+------------------------------------------------------------------+
bool CompareDouble(double Number1, double Number2)
  {
    bool Compare = NormalizeDouble(Number1 - Number2, 8) == 0;
    return(Compare);
  } 
