//+------------------------------------------------------------------+
//|                                   Copyright © 2019, Ivan Kornilov|
//|                                               DeMarker_Stack.mq4 |
//| Modified by Robert Hill to use DeMarker                          |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2009, Ivan Kornilov. All rights reserved."
#property link "excelf@gmail.com"

#property indicator_separate_window
#property indicator_minimum 0
#property indicator_maximum 1
#property indicator_buffers 8
#property indicator_level1 0.3
#property indicator_level2 0.7

#property indicator_color8 C'50,50,50'   
#property indicator_color7 C'90,90,90'
#property indicator_color6 C'120,120,120'
#property indicator_color5 C'140,140,140'
#property indicator_color4 C'160,160,160'
#property indicator_color3 C'180,180,180'
#property indicator_color2 C'200,200,200'
#property indicator_color1 C'220,220,220'  

#define maxBuffer 8

extern double nextPeriod = 1.3;
extern int rPeriod1 = 13;
extern int rPeriod2 = 21;
extern int rPeriod3 = 34;
extern int rPeriod4 = 55;
extern int rPeriod5 = 72;
extern int rPeriod6 = 89;
extern int rPeriod7 = 116;
extern int rPeriod8 = 144;

extern int countLine = 8;

double lineBuffer0[];
double lineBuffer1[];
double lineBuffer2[];
double lineBuffer3[];
double lineBuffer4[];
double lineBuffer5[];
double lineBuffer6[];
double lineBuffer7[];

int init() {
    if(countLine > maxBuffer) {
        countLine = maxBuffer;
    } else if(countLine < 1) {
        countLine = 1;
    }
    if(nextPeriod != 0) {
        rPeriod2 = rPeriod1 * nextPeriod;
        rPeriod3 = rPeriod2 * nextPeriod;
        rPeriod4 = rPeriod3 * nextPeriod;
        rPeriod5 = rPeriod4 * nextPeriod;
        rPeriod6 = rPeriod5 * nextPeriod;
        rPeriod7 = rPeriod6 * nextPeriod;
        rPeriod8 = rPeriod7 * nextPeriod;
    } 

    string indicatorName = "DeMarker Stack(";
    SetIndexBuffer(0, lineBuffer0);
    SetIndexStyle(0, DRAW_LINE);
    indicatorName = indicatorName + rPeriod1;
    if(countLine > 1)  {
        SetIndexBuffer(1, lineBuffer1);
        SetIndexStyle(1, DRAW_LINE);
        indicatorName = indicatorName + "," + rPeriod2;
    }
    if(countLine > 2) {
        SetIndexBuffer(2, lineBuffer2);
        SetIndexStyle(2, DRAW_LINE);
        indicatorName = indicatorName + "," + rPeriod3;
    }
    if(countLine > 3) {
        SetIndexBuffer(3, lineBuffer3);
        SetIndexStyle(3, DRAW_LINE);
        indicatorName = indicatorName + "," + rPeriod4;
    }
    if(countLine > 4) {
        SetIndexBuffer(4, lineBuffer4);
        SetIndexStyle(4, DRAW_LINE);
        indicatorName = indicatorName + "," + rPeriod5;
    }
    if(countLine > 5) {
        SetIndexBuffer(5, lineBuffer5);
        SetIndexStyle(5, DRAW_LINE);
        indicatorName = indicatorName + "," + rPeriod6;
    }
    if(countLine > 6) {
        SetIndexBuffer(6, lineBuffer6);
        SetIndexStyle(6, DRAW_LINE);
        indicatorName = indicatorName + "," + rPeriod7;
    }
    if(countLine > 7) {
        SetIndexBuffer(7, lineBuffer7);
        SetIndexStyle(7, DRAW_LINE);
        indicatorName = indicatorName + "," + rPeriod8;
    }

    indicatorName = indicatorName + ")";
    IndicatorShortName(indicatorName);
}

int start(){
    int indicatorCounted = IndicatorCounted();
    if (indicatorCounted < 0) { 
        return (-1);
    }
    if(indicatorCounted > 0) {
       indicatorCounted--;
    }
   
    int limit = Bars - indicatorCounted;
    for(int i = limit - 1; i >= 0; i--) {
        lineBuffer0[i] = iDeMarker(NULL, 0, rPeriod1, i);
        if(countLine > 1)  {
            lineBuffer1[i] = iDeMarker(NULL, 0, rPeriod2, i);
        }
        if(countLine > 2) {
            lineBuffer2[i] = iDeMarker(NULL, 0, rPeriod3, i);
        }
        if(countLine > 3) {
            lineBuffer3[i] = iDeMarker(NULL, 0, rPeriod4, i);
        }
        if(countLine > 4) {
            lineBuffer4[i] = iDeMarker(NULL, 0, rPeriod5, i);
        }
        if(countLine > 5) {
            lineBuffer5[i] = iDeMarker(NULL, 0, rPeriod6, i);
        }
        if(countLine > 6) {
            lineBuffer6[i] = iDeMarker(NULL, 0, rPeriod7, i);
        }
        if(countLine > 7) {
            lineBuffer7[i] = iDeMarker(NULL, 0, rPeriod8, i);
        }
    }
}