﻿//+------------------------------------------------------------------+
//|                             ADR lines Shinigami v1.1 (MN1).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  "AMR High"
#property indicator_type1   DRAW_LINE
#property indicator_color1  Blue
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1

#property indicator_label2  "AMR Mid-High"
#property indicator_type2   DRAW_LINE
#property indicator_color2  DodgerBlue
#property indicator_style2  STYLE_SOLID
#property indicator_width2  1

#property indicator_label3  "AMR Open"
#property indicator_type3   DRAW_LINE
#property indicator_color3  Yellow
#property indicator_style3  STYLE_SOLID
#property indicator_width3  1

#property indicator_label4  "AMR Mid-Low"
#property indicator_type4   DRAW_LINE
#property indicator_color4  Pink
#property indicator_style4  STYLE_SOLID
#property indicator_width4  1

#property indicator_label5  "AMR 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;   // AMR Period (months)
input int Limit     = 1000; // Bars to calculate

//---- Buffers
double Buffer1[];  // Open + AMR
double Buffer2[];  // Open + AMR/2
double Buffer3[];  // Open
double Buffer4[];  // Open - AMR/2
double Buffer5[];  // Open - AMR

//+------------------------------------------------------------------+
int OnInit()
  {
   IndicatorSetString(INDICATOR_SHORTNAME, "ADR lines by Shinigami (MN1)");
   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 MN1 bar index that contains current bar's time
      int mn1Shift = iBarShift(_Symbol, PERIOD_MN1, time[i]);
      if(mn1Shift < 0)
         continue;

      //---- Compute average monthly range over the past ADRperiod months
      double adr = 0.0;
      for(int k = 1; k <= ADRperiod; k++)
        {
         double mnHigh = iHigh(_Symbol, PERIOD_MN1, mn1Shift + k);
         double mnLow  = iLow (_Symbol, PERIOD_MN1, mn1Shift + k);
         if(mnHigh == 0.0 && mnLow == 0.0)
            break;
         adr += mnHigh - mnLow;
        }
      adr /= ADRperiod;

      //---- Monthly open of the current bar's month
      double mnOpen = iOpen(_Symbol, PERIOD_MN1, mn1Shift);

      Buffer1[i] = mnOpen + adr;
      Buffer2[i] = mnOpen + adr / 2.0;
      Buffer3[i] = mnOpen;
      Buffer4[i] = mnOpen - adr / 2.0;
      Buffer5[i] = mnOpen - adr;
     }

   return(rates_total);
  }
//+------------------------------------------------------------------+