//+------------------------------------------------------------------+
//|                                                 Volumes Hist.mq4 |
//|                         Copyright © Vadim Shumilov (DrShumiloff) |
//|                                                shumiloff@mail.ru |
//|                                                     ICQ 84796634 |
//+------------------------------------------------------------------+
#property copyright "Vadim Shumilov (DrShumiloff)"
#property link      "shumiloff@mail.ru"
//----
#property indicator_chart_window

extern int      MinutesCount = 5000;  // 3000 - 10000 ;
extern int      Amplitude = 20;       // 10 .. 100
extern int      RefreshPeriod = 1;    // 1 .. 60
extern color    HistColor = SteelBlue;
extern double   PricePoint = 0.0002;

// For the next versions
// extern int      Method = 0;           // 0 - SMA, 1 - EMA, 2 - SMMA, 3 - LWMA, 4 - LRMA
// extern int      VolumePeriod = 21;

double Hist[];
datetime OpenTime = 0;

int init()
{
    return(0);
}

int deinit()
{
    DeleteObjects();
    return(0);
}

int DeleteObjects()
{
    for (int i = 0; i< 3000; i++) ObjectDelete("VH"+i);
}

int DrawLine(int n, double price, int vol, int max)
{
    vol = MathRound(Amplitude * vol / max );
    if (vol > 0)
    {
        int first = WindowFirstVisibleBar();
        datetime dt1 = iTime(NULL, Period(), first);
        datetime dt2 = iTime(NULL, Period(), first-vol);
        ObjectCreate("VH"+n, OBJ_RECTANGLE, 0, dt1, price, dt2, price+PricePoint);
        ObjectSet("VH"+n, OBJPROP_STYLE, DRAW_HISTOGRAM);
        ObjectSet("VH"+n, OBJPROP_COLOR, HistColor);
        ObjectSet("VH"+n, OBJPROP_BACK, true);
    }
    return (0);
}

int start()
{
    if (OpenTime != iOpen(Symbol(), PERIOD_M1, 0))
    {
        OpenTime = iOpen(Symbol(), PERIOD_M1, 0);

        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] += iVolume(Symbol(), PERIOD_M1, i);    
        }

        MaxVolume = Hist[ArrayMaximum(Hist)];
        DeleteObjects();
        for (i = 0; i <= items; i++)
        {
            DrawLine(i, min + i*PricePoint, Hist[i], MaxVolume);
        }
    }
        return(0);
}