//+------------------------------------------------------------------+
//|                                                      ProjectName |
//|                                      Copyright 2018, CompanyName |
//|                                       http://www.companyname.net |
//+------------------------------------------------------------------+

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 SteelBlue
#property indicator_width1 5
#property indicator_color2 Crimson
#property indicator_width2 5




double A[];
double B[];
double RenkoHigh[];
double RenkoLow[];
double BoxValue;

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int init()
{
    IndicatorBuffers(4);
    SetIndexStyle(0, DRAW_HISTOGRAM);
    SetIndexBuffer(0, A);
    SetIndexStyle(1, DRAW_HISTOGRAM);
    SetIndexBuffer(1, B);
    SetIndexStyle(2, DRAW_NONE);
    SetIndexBuffer(2, RenkoHigh);
    SetIndexStyle(3, DRAW_NONE);
    SetIndexBuffer(3, RenkoLow);
    SetIndexLabel(0, "Up");
    SetIndexLabel(1, "Dn");
    IndicatorDigits(Digits);
    BoxValue = NormalizeDouble(iATR(_Symbol, 0, 14, 0), Digits);
    Comment("Renko |" + BoxValue);
    return (0);
}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int deinit()
{
    Comment("");
    return (0);
}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int start()
{
    double NewUpBox;
    double NewDwBox;
    double BoxCount;
    double Ld_32;
    int counted_bars = IndicatorCounted();
    if (counted_bars < 0) return (-1);
    if (counted_bars > 0) counted_bars--;
    A[Bars] = Close[Bars];
    B[Bars] = Close[Bars];
    RenkoHigh[Bars] = Close[Bars];
    RenkoLow[Bars] = Close[Bars];
    int limit = Bars - counted_bars;
    for (int i = limit; i >= 0; i--) {
        NewUpBox = NormalizeDouble(High[i] - (RenkoHigh[i + 1]) - BoxValue, Digits);
        NewDwBox = NormalizeDouble(Low[i] - (RenkoLow[i + 1]) + BoxValue, Digits);
        if (NewUpBox >= 0.0) {//up new box
            Ld_32 = NormalizeDouble((High[i] - (RenkoHigh[i + 1])) / BoxValue, Digits);
            BoxCount = NormalizeDouble(MathFloor(Ld_32), Digits);
            RenkoHigh[i] = RenkoHigh[i + 1] + BoxValue * BoxCount;
            RenkoLow[i] = RenkoHigh[i] - BoxValue * BoxCount;
            A[i] = RenkoHigh[i];
            B[i] = RenkoLow[i];
            RenkoLow[i] = RenkoHigh[i] - BoxValue;
        } else {//no new box
            if (NewDwBox <= 0.0) {// down new obx
                Ld_32 = NormalizeDouble((RenkoLow[i + 1] - Low[i]) / BoxValue, Digits);
                BoxCount = NormalizeDouble(MathFloor(Ld_32), Digits);
                RenkoLow[i] = RenkoLow[i + 1] - BoxValue * BoxCount;
                RenkoHigh[i] = RenkoLow[i] + BoxValue * BoxCount;
                B[i] = RenkoHigh[i];
                A[i] = RenkoLow[i];
                RenkoHigh[i] = RenkoLow[i] + BoxValue;
            } else {//down but no new box
                RenkoHigh[i] = RenkoHigh[i + 1];
                RenkoLow[i] = RenkoLow[i + 1];
                if (A[i + 1] > B[i + 1]) {
                    A[i] = A[i + 1];
                    B[i] = A[i] - BoxValue;
                }
                if (B[i + 1] > A[i + 1]) {
                    A[i] = A[i + 1];
                    B[i] = A[i] + BoxValue;
                }
            }
        }
    }
    return (0);
}
//+------------------------------------------------------------------+
