﻿//+------------------------------------------------------------------+
//|                              ADR lines Shinigami v1.1 (W1).mq5  |
//|                                                     by Shinigami |
//|                                              Shini1984@gmail.com |
//|                               Converted to MQL5                  |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2009, Shinigami"
#property link      "Shini1984@gmail.com"
#property version   "1.10"

#property indicator_chart_window
#property indicator_buffers 5
#property indicator_plots   5

#property indicator_label1  "AWR High"
#property indicator_type1   DRAW_LINE
#property indicator_color1  Blue
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1

#property indicator_label2  "AWR Mid-High"
#property indicator_type2   DRAW_LINE
#property indicator_color2  DodgerBlue
#property indicator_style2  STYLE_SOLID
#property indicator_width2  1

#property indicator_label3  "AWR Open"
#property indicator_type3   DRAW_LINE
#property indicator_color3  Yellow
#property indicator_style3  STYLE_SOLID
#property indicator_width3  1

#property indicator_label4  "AWR Mid-Low"
#property indicator_type4   DRAW_LINE
#property indicator_color4  Pink
#property indicator_style4  STYLE_SOLID
#property indicator_width4  1

#property indicator_label5  "AWR Low"
#property indicator_type5   DRAW_LINE
#property indicator_color5  Red
#property indicator_style5  STYLE_SOLID
#property indicator_width5  1

//---- Input parameters
input int ADRperiod = 14;   // AWR Period (weeks)
input int Limit     = 1000; // Bars to calculate

//---- Buffers
double Buffer1[];  // Open + AWR
double Buffer2[];  // Open + AWR/2
double Buffer3[];  // Open
double Buffer4[];  // Open - AWR/2
double Buffer5[];  // Open - AWR

//+------------------------------------------------------------------+
int OnInit()
  {
   IndicatorSetString(INDICATOR_SHORTNAME, "ADR lines by Shinigami (W1)");
   IndicatorSetInteger(INDICATOR_DIGITS, _Digits);

   SetIndexBuffer(0, Buffer1, INDICATOR_DATA);
   SetIndexBuffer(1, Buffer2, INDICATOR_DATA);
   SetIndexBuffer(2, Buffer3, INDICATOR_DATA);
   SetIndexBuffer(3, Buffer4, INDICATOR_DATA);
   SetIndexBuffer(4, Buffer5, INDICATOR_DATA);

   for(int p = 0; p < 5; p++)
      PlotIndexSetDouble(p, PLOT_EMPTY_VALUE, 0.0);

   ArraySetAsSeries(Buffer1, true);
   ArraySetAsSeries(Buffer2, true);
   ArraySetAsSeries(Buffer3, true);
   ArraySetAsSeries(Buffer4, true);
   ArraySetAsSeries(Buffer5, true);

   return(INIT_SUCCEEDED);
  }

//+------------------------------------------------------------------+
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[])
  {
   ArraySetAsSeries(time, true);

   int limit = MathMin(Limit, rates_total);

   for(int i = 0; i < limit; i++)
     {
      //---- Find the W1 bar index that contains current bar's time
      int w1Shift = iBarShift(_Symbol, PERIOD_W1, time[i]);
      if(w1Shift < 0)
         continue;

      //---- Compute average weekly range over the past ADRperiod weeks
      double adr = 0.0;
      for(int k = 1; k <= ADRperiod; k++)
        {
         double wkHigh = iHigh(_Symbol, PERIOD_W1, w1Shift + k);
         double wkLow  = iLow (_Symbol, PERIOD_W1, w1Shift + k);
         if(wkHigh == 0.0 && wkLow == 0.0)
            break;
         adr += wkHigh - wkLow;
        }
      adr /= ADRperiod;

      //---- Weekly open of the current bar's week
      double wkOpen = iOpen(_Symbol, PERIOD_W1, w1Shift);

      Buffer1[i] = wkOpen + adr;
      Buffer2[i] = wkOpen + adr / 2.0;
      Buffer3[i] = wkOpen;
      Buffer4[i] = wkOpen - adr / 2.0;
      Buffer5[i] = wkOpen - adr;
     }

   return(rates_total);
  }
//+------------------------------------------------------------------+