//+------------------------------------------------------------------+
//|                                                   ProfileATR.mq4 |
//|                        Copyright 2016, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Vadim Shumilov (DrShumiloff)"
#property link      "shumiloff@mail.ru"
//----
#property indicator_chart_window

extern int   HourOfTheDay=23;
//extern int    MinutesCount = 1440;  // 3000 - 10000 ;
extern int     Amplitude = 100;       // 10 .. 100
extern int      RefreshPeriod = 1;    // 1 .. 60
extern color   HistColor = SteelBlue;
extern double PricePoint = 0.00001;
extern int   ATRvalue  = 1;

// For the next versions
// extern int      Method = 0;           // 0 - SMA, 1 - EMA, 2 - SMMA, 3 - LWMA, 4 - LRMA
// extern int      VolumePeriod = 20;

double Hist[];
datetime OpenTime = 0;

int init()
{
    return(0);
}

int deinit()
{
    DeleteObjects();
    return(0);
}

int DeleteObjects()
{
    for (int i = 0; i< 3000; i++) ObjectDelete("VATR"+i);
}

int DrawLine(int n, double price, int vol, int max)
{
    vol = MathRound(Amplitude * vol / max );
    if (vol > 0)
    
        //int first =DayOfYear();
        int first =iBarShift(Symbol(), PERIOD_M1, StringToTime(StringConcatenate(StringSubstr(TimeToStr(iTime(NULL,PERIOD_D1,1)),0,10)," ","23:59:59")));
        datetime dt1 = iTime(NULL, Period(), first);
        datetime dt2 = iTime(NULL, Period(), first-vol);
        ObjectCreate("VATR"+n,  OBJ_RECTANGLE, 0, dt1, price, dt2, price+PricePoint);
        ObjectSet("VATR"+n, OBJPROP_STYLE, DRAW_HISTOGRAM);
        ObjectSet("VATR"+n, OBJPROP_COLOR, HistColor);
        ObjectSet("VATR"+n, OBJPROP_BACK, True);
    }

int start()
{
    if (OpenTime != iOpen(Symbol(), PERIOD_M1, 0))
    {        
        OpenTime = iOpen(Symbol(), PERIOD_M1, 0);
        
        int MinutesCount = iBarShift(Symbol(), PERIOD_M1, StringToTime(StringConcatenate(StringSubstr(TimeToStr(iTime(NULL,PERIOD_D1,1)),0,10)," ","23:59:59")));
         Print(MinutesCount);
          if (MinutesCount==-1) MinutesCount = iBarShift(Symbol(), PERIOD_M1, StringToTime(StringConcatenate(StringSubstr(TimeToStr(iTime(NULL,PERIOD_D1,2)),0,10)," ","23:59:59")));
           int n, MaxVolume;
            double max = iHigh(Symbol(), PERIOD_M1, iHighest(Symbol(), PERIOD_M1, MODE_HIGH, MinutesCount, 0));
             double min = iLow(Symbol(), PERIOD_M1, iLowest(Symbol(), PERIOD_M1, MODE_LOW, MinutesCount, 0));
              int items = MathRound((max - min) / PricePoint);

               if (max == 0)
        {
            Alert("There is no minutes data. Please download M1.");
            return (0);
        }

        ArrayResize(Hist, items);      
        ArrayInitialize(Hist, 0);
        for (int i = 1; i <= MinutesCount; i++)
        {
            n = MathRound((iClose(Symbol(), PERIOD_M1, i) - min) / PricePoint);
            Hist[n] += 10000*iATR(Symbol(), PERIOD_M1, ATRvalue, i);   
        }

        MaxVolume = Hist[ArrayMaximum(Hist)];
        DeleteObjects();
        for (i = 0; i <= items; i++)
        {
            DrawLine(i, min + i*PricePoint, Hist[i], MaxVolume);
        }
    }
        return(0);

}