//+------------------------------------------------------------------+
//|                                                        GoMA8.mq4 |
//|                                   Copyright © 2013, Milan Hemzal |
//|                                                  milan@dannys.cz |
//| ver.: 1.0
//+------------------------------------------------------------------+
#property copyright "Copyright © 2013, Milan Hemzal"
#property link      "http://www.dannys.cz"

#property indicator_chart_window

#property indicator_buffers 5

#property indicator_color1 Blue
#property indicator_color2 Red
#property indicator_color3 White
#property indicator_color4 Green


#define CANDLE_UP 1
#define CANDLE_DOWN 2

//---- buffers
double UpBuffer[];
double DownBuffer[];
double LineBuffer[];
double CloseBuffer[];

datetime last;
//---- export

//red
extern int Red_Period=9;
int    Red_Type= MODE_LWMA;
//purple
extern int Purple_Period= 89;   
int    Purple_Type= MODE_EMA;
//blue
extern int Blue_Period= 34;
int    Blue_Type= MODE_EMA;
// green
extern int Green_Period= 120;
int    Green_Type= MODE_SMA;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicator buffers mapping  
    SetIndexBuffer(0,UpBuffer);
    SetIndexStyle(0,DRAW_ARROW,STYLE_SOLID,5);
    
    SetIndexBuffer(1,DownBuffer);   
    SetIndexStyle(1,DRAW_ARROW,STYLE_SOLID, 3);
    
    SetIndexBuffer(3,CloseBuffer);  
    SetIndexStyle(3,DRAW_ARROW,STYLE_SOLID, 5);

    SetIndexBuffer(2,LineBuffer);   
    SetIndexStyle(2,DRAW_LINE, EMPTY, 1);

//---- drawing settings
    
    SetIndexArrow(0,SYMBOL_ARROWUP);    
    SetIndexArrow(1,SYMBOL_ARROWDOWN);

    //#SetIndexArrow(2,SYMBOL_ARROWDOWN);
    
    SetIndexArrow(3,84);

    
//----
    SetIndexEmptyValue(0,0.0);
    SetIndexEmptyValue(1,0.0);
    //SetIndexEmptyValue(2,0.0);
    //SetIndexEmptyValue(3,0.0);
//---- name for DataWindow
    SetIndexLabel(0,"Moon Up");
    SetIndexLabel(1,"Moon Down");
    SetIndexLabel(2,"oon Down");
    SetIndexLabel(3,"on Down");
//----
    IndicatorShortName("GoMA8 ("+Symbol()+")");

    last=Time[0];
	IndicatorDigits(Digits);
	
   return(0);
  }
//+------------------------------------------------------------------+
//| Custor indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//---- TODO: add your code here
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()  {
	double r,p,b,g;
	int counted_bars = IndicatorCounted();
   
	if (counted_bars < 0) return (-2);
	if (counted_bars > 0) counted_bars--;
	int limit = Bars - counted_bars;
	
	double dIma, ld_Pips;
    int si=0;
	for (int i=limit; i>=0; i--){
		//dIma=iMA(NULL,0,setIMA,0,MODE_LWMA,PRICE_CLOSE,i);
		DownBuffer[i]=0;
		UpBuffer[i]=0;
		//CloseBuffer[i]=0;
		
		r=myRed(i);
		p=myPurple(i);
		b=myBlue(i);
		g=myGreen(i);
		
		LineBuffer[i]=(r+p+b+g)/4;
		 if (si == CANDLE_UP && r<b){
		       // zavøit obchod
		       CloseBuffer[i]=iHigh(NULL,0,i);
		       si=0;
		       //Print("Up close ",i);
		    }
		    if (si == CANDLE_DOWN && r>b){
		       // zavøit obchod
		       CloseBuffer[i]=iLow(NULL,0,i);
		       si=0;
		    }
		//if (i==0) Print("line "+LineBuffer[i]+" r "+r);
		if ( r > b && b > p && p > g){ 
		 
			if (si == CANDLE_DOWN || si == 0){
				// zobraz modrou Å¡ipku
				UpBuffer[i]=iHigh(NULL,0,i);
				si=CANDLE_UP;
				if (i == 0 && last != Time[0]){ 
				  Alert("GoMA8 - UP ", Symbol(),"!!!");
				  last=Time[0];
				 }
				continue;
			}
		}else if(g>p && p > b && b>r){
		
			if (si == CANDLE_UP || si == 0){
				// zobraz Äervenou Å¡ipku
				DownBuffer[i]=iLow(NULL,0,i);
				si=CANDLE_DOWN;
				if (i == 0 && last != Time[0]){ 
				  Alert("GoMA8 - DOWN ", Symbol(),"!!!");
				  last=Time[0];
				 }
				continue;
			}
		}
		
	}
//----
   return(0);
}



//+------------------------------------------------------------------+
//| Calculate RED line                                        |
//+------------------------------------------------------------------+

double myRed(int i){
int BarShift;
double Trend;

	BarShift = iBarShift(NULL,NULL,Time[i],true);                       
	Trend= iMA(NULL,PERIOD_M15,Red_Period,0,Red_Type,PRICE_WEIGHTED,BarShift);
	
	//if (i == 0) Print("RED: ",Trend," sh ",BarShift);
	return (Trend);
}


//+------------------------------------------------------------------+
//| Calculate Purple line                                   |
//+------------------------------------------------------------------+

double myPurple(int i=0){
int BarShift;
double Trend;
	
	  BarShift = iBarShift(NULL,NULL,Time[i],true);                       
      Trend= iMA(NULL,PERIOD_M15,Purple_Period,0,Purple_Type,PRICE_CLOSE,BarShift);      
      return(Trend);
}

//+------------------------------------------------------------------+
//| Calculate Blue line                                   |
//+------------------------------------------------------------------+

double myBlue(int i=0){
int BarShift;
double Trend;
	
	  BarShift = iBarShift(NULL,NULL,Time[i],true);                       
      Trend= iMA(NULL,PERIOD_M15,Blue_Period,0,Blue_Type,PRICE_CLOSE,BarShift);      
      return(Trend);
}

//+------------------------------------------------------------------+
//| Calculate Green line                                   |
//+------------------------------------------------------------------+

double myGreen(int i=0){
int BarShift;
double Trend;
	
	  BarShift = iBarShift(NULL,NULL,Time[i],true);                       
      Trend= iMA(NULL,PERIOD_M15,Green_Period,0,Green_Type,PRICE_CLOSE,BarShift);     
      return(Trend);
}

//+------------------------------------------------------------------+

