//
// "00-MA6_v101.mq4" -- MA x6
//
//    Ver. 1.00  2009/02/18(Wed)  initial version
//    Ver. 1.01  2009/02/18(Wed)  modified defaults
//
//
#property  copyright "00"
#property  link      "http://www.mql4.com/"

//---- defines

//---- indicator settings
#property  indicator_chart_window

#property  indicator_buffers  8

#property  indicator_color1  Red
#property  indicator_color2  Red
#property  indicator_color3  Red
#property  indicator_color4  Red
#property  indicator_color5  Red
#property  indicator_color6  Red
#property  indicator_color7  DodgerBlue
#property  indicator_color8  Crimson

#property  indicator_width1  1
#property  indicator_width2  1
#property  indicator_width3  1
#property  indicator_width4  1
#property  indicator_width5  1
#property  indicator_width6  1
#property  indicator_width7  1
#property  indicator_width8  1

#property  indicator_style1  STYLE_DOT
#property  indicator_style2  STYLE_DOT
#property  indicator_style3  STYLE_DOT
#property  indicator_style4  STYLE_DOT
#property  indicator_style5  STYLE_DOT
#property  indicator_style6  STYLE_DOT
#property  indicator_style7  STYLE_DOT
#property  indicator_style8  STYLE_DOT

//---- indicator parameters
extern int  timeFrame   = 0;         // time frame
extern int  period1     = 25;        // period 1
extern int  period2     = 50;        // period 2
extern int  period3     = 100;       // period 3
extern int  period4     = 200;       // period 4
extern int  period5     = 300;       // period 5
extern int  period6     = 400;       // period 6
extern int  maMethod1   = MODE_EMA;  // 
extern int  maMethod2   = MODE_EMA;  // 
extern int  maMethod3   = MODE_EMA;  // 
extern int  maMethod4   = MODE_EMA;  // 
extern int  maMethod5   = MODE_EMA;  // 
extern int  maMethod6   = MODE_EMA;  // 
extern bool bDrawHisto  = true;
extern int  nMaxBars    = 2000;      // maximum number of bars to calculate, 0: no limit

//---- indicator buffers
double BufferMa1[];            // 0: MA1
double BufferMa2[];            // 1: MA2
double BufferMa3[];            // 2: MA3
double BufferMa4[];            // 3: MA4
double BufferMa5[];            // 4: MA5
double BufferMa6[];            // 5: MA6
double BufferTrendingUpper[];  // 6: trending upper
double BufferTrendingLower[];  // 7: trending lower

//---- vars
string sIndicatorName  = "";
string sIndSelf        = "00-MA6_v101";

//----------------------------------------------------------------------
string TimeFrameToStr(int timeFrame)
{
    switch (timeFrame) {
    case 1:     return("M1");
    case 5:     return("M5");
    case 15:    return("M15");
    case 30:    return("M30");
    case 60:    return("H1");
    case 240:   return("H4");
    case 1440:  return("D1");
    case 10080: return("W1");
    case 43200: return("MN");
    }
    
    return("??");
}

//----------------------------------------------------------------------
void init()
{
    if (timeFrame == 0) {
	timeFrame = Period();
    }
    
    string tf = TimeFrameToStr(timeFrame);
    sIndicatorName = sIndSelf + ("(" + tf + "," + 
				 period1 + ","  + period2 + ","  + period3 + "," +
				 period4 + ","  + period5 + ","  + period6 + ")");
    
    IndicatorShortName(sIndicatorName);
    
    SetIndexBuffer(0, BufferMa1);
    SetIndexBuffer(1, BufferMa2);
    SetIndexBuffer(2, BufferMa3);
    SetIndexBuffer(3, BufferMa4);
    SetIndexBuffer(4, BufferMa5);
    SetIndexBuffer(5, BufferMa6);
    SetIndexBuffer(6, BufferTrendingUpper);
    SetIndexBuffer(7, BufferTrendingLower);
    
    SetIndexLabel(0, "MA1");
    SetIndexLabel(1, "MA2");
    SetIndexLabel(2, "MA3");
    SetIndexLabel(3, "MA4");
    SetIndexLabel(4, "MA5");
    SetIndexLabel(5, "MA6");
    SetIndexLabel(6, "Trending upper");
    SetIndexLabel(7, "Trending lower");
    
    SetIndexStyle(0, DRAW_LINE);
    SetIndexStyle(1, DRAW_LINE);
    SetIndexStyle(2, DRAW_LINE);
    SetIndexStyle(3, DRAW_LINE);
    SetIndexStyle(4, DRAW_LINE);
    SetIndexStyle(5, DRAW_LINE);
    if (bDrawHisto) {
	SetIndexStyle(6, DRAW_HISTOGRAM);
	SetIndexStyle(7, DRAW_HISTOGRAM);
    } else {
	SetIndexStyle(6, DRAW_LINE);
	SetIndexStyle(7, DRAW_LINE);
    }
    
    SetIndexDrawBegin(0, period1);
    SetIndexDrawBegin(1, period2);
    SetIndexDrawBegin(2, period3);
    SetIndexDrawBegin(3, period4);
    SetIndexDrawBegin(4, period5);
    SetIndexDrawBegin(5, period6);
    SetIndexDrawBegin(6, period6);
    SetIndexDrawBegin(7, period6);
}

//----------------------------------------------------------------------
void start()
{
    int limit;
    int counted_bars = IndicatorCounted();
    
    if (counted_bars > 0) {
	counted_bars--;
    }
    
    limit = Bars - counted_bars;
    int limit0 = limit;
    if (nMaxBars > 0) {
	limit = MathMin(limit, nMaxBars);
    }
    
    // clear beyond limits
    for (int i = limit0 - 1; i >= limit; i--) {
	BufferMa1[i]           = EMPTY_VALUE;
	BufferMa2[i]           = EMPTY_VALUE;
	BufferMa3[i]           = EMPTY_VALUE;
	BufferMa4[i]           = EMPTY_VALUE;
	BufferMa5[i]           = EMPTY_VALUE;
	BufferMa6[i]           = EMPTY_VALUE;
	BufferTrendingUpper[i] = EMPTY_VALUE;
	BufferTrendingLower[i] = EMPTY_VALUE;
    }
    
    if (timeFrame != Period()) {
	// MTF
	limit = MathMax(limit, timeFrame / Period());
	for (i = limit - 1; i >= 0; i--) {
	    int x = iBarShift(NULL, timeFrame, Time[i]);
	    BufferMa1[i]           = iCustom(NULL, timeFrame, sIndSelf, 0, period1, period2, period3, period4, period5, period6, 
					     maMethod1, maMethod2, maMethod3, maMethod4, maMethod5, maMethod6, bDrawHisto, nMaxBars, 0, x);
	    BufferMa2[i]           = iCustom(NULL, timeFrame, sIndSelf, 0, period1, period2, period3, period4, period5, period6, 
					     maMethod1, maMethod2, maMethod3, maMethod4, maMethod5, maMethod6, bDrawHisto, nMaxBars, 1, x);
	    BufferMa3[i]           = iCustom(NULL, timeFrame, sIndSelf, 0, period1, period2, period3, period4, period5, period6, 
					     maMethod1, maMethod2, maMethod3, maMethod4, maMethod5, maMethod6, bDrawHisto, nMaxBars, 2, x);
	    BufferMa4[i]           = iCustom(NULL, timeFrame, sIndSelf, 0, period1, period2, period3, period4, period5, period6, 
					     maMethod1, maMethod2, maMethod3, maMethod4, maMethod5, maMethod6, bDrawHisto, nMaxBars, 3, x);
	    BufferMa5[i]           = iCustom(NULL, timeFrame, sIndSelf, 0, period1, period2, period3, period4, period5, period6, 
					     maMethod1, maMethod2, maMethod3, maMethod4, maMethod5, maMethod6, bDrawHisto, nMaxBars, 4, x);
	    BufferMa6[i]           = iCustom(NULL, timeFrame, sIndSelf, 0, period1, period2, period3, period4, period5, period6, 
					     maMethod1, maMethod2, maMethod3, maMethod4, maMethod5, maMethod6, bDrawHisto, nMaxBars, 5, x);
	    BufferTrendingUpper[i] = iCustom(NULL, timeFrame, sIndSelf, 0, period1, period2, period3, period4, period5, period6, 
					     maMethod1, maMethod2, maMethod3, maMethod4, maMethod5, maMethod6, bDrawHisto, nMaxBars, 6, x);
	    BufferTrendingLower[i] = iCustom(NULL, timeFrame, sIndSelf, 0, period1, period2, period3, period4, period5, period6, 
					     maMethod1, maMethod2, maMethod3, maMethod4, maMethod5, maMethod6, bDrawHisto, nMaxBars, 7, x);
	}
	
	return;
    }
    
    // timeFrame == Period()
    for (i = limit - 1; i >= 0; i--) {
	double ma1 = iMA(NULL, 0, period1, 0, maMethod1, PRICE_CLOSE, i);
	double ma2 = iMA(NULL, 0, period2, 0, maMethod2, PRICE_CLOSE, i);
	double ma3 = iMA(NULL, 0, period3, 0, maMethod3, PRICE_CLOSE, i);
	double ma4 = iMA(NULL, 0, period4, 0, maMethod4, PRICE_CLOSE, i);
	double ma5 = iMA(NULL, 0, period5, 0, maMethod5, PRICE_CLOSE, i);
	double ma6 = iMA(NULL, 0, period6, 0, maMethod6, PRICE_CLOSE, i);
	
	BufferMa1[i] = ma1;
	BufferMa2[i] = ma2;
	BufferMa3[i] = ma3;
	BufferMa4[i] = ma4;
	BufferMa5[i] = ma5;
	BufferMa6[i] = ma6;
	
	bool bTrendingUp   = (ma1 < ma2 && ma2 < ma3 && ma3 < ma4 && ma4 < ma5 && ma5 < ma6);
	bool bTrendingDown = (ma1 > ma2 && ma2 > ma3 && ma3 > ma4 && ma4 > ma5 && ma5 > ma6);
	bool bTrending = (bTrendingUp || bTrendingDown);
	
	if (bTrending) {
	    BufferTrendingUpper[i] = ma1;
	    BufferTrendingLower[i] = ma6;
	} else {
	    BufferTrendingUpper[i] = EMPTY_VALUE;
	    BufferTrendingLower[i] = EMPTY_VALUE;
	}
    }
}
