//+------------------------------------------------------------------+
//|                                          EMA_Trend_Indicator.mq4 |
//|                                Copyright © 2006, Robert L Hill   |
//+------------------------------------------------------------------+
// Uses an idea from david to use EMAs to determine trend

/* Changelog
	Date		Author		Version	Changes
	11/20/10 markdshark 	v1.1		Added trend change alerts per Brian Durnil's request
	11/21/10	markdshark	v1.2		Ah...forgot to add the arrows in v1.1. Added.


*/
	
#property copyright "Copyright © 2005, Metaquotes"
#property link      "mailto:metaquotes@metaquotes.net"

#property indicator_chart_window
#property indicator_buffers 6
#property indicator_color1 Yellow
#property indicator_color2 Purple
#property indicator_color3 Purple
#property indicator_color4 Yellow
#property indicator_color5 Blue
#property indicator_color6 Red

#property indicator_width1 1
#property indicator_width2 1
#property indicator_width3 1
#property indicator_width4 1
#property indicator_width5 1
#property indicator_width6 1

//---- input parameters
extern int FastMAPeriod=21;
extern int SlowMAPeriod=34;

//---- buffers
double slowEMA_HighBuffer[];
double slowEMA_LowBuffer[];
double fastEMA_HighBuffer[];
double fastEMA_LowBuffer[];
double buyArrow[];
double sellArrow[];

//---- variables
int 	trend=0, 
		prevTrend=0;

double slowLo,fastLo,slowHi,fastHi;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
{
   IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS)+1);
   //---- indicators
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,slowEMA_HighBuffer);
   
   SetIndexStyle(1,DRAW_LINE,STYLE_DASH,1);
   SetIndexBuffer(1,slowEMA_LowBuffer);
   
   SetIndexStyle(2,DRAW_LINE,STYLE_DASH,1);
   SetIndexBuffer(2,fastEMA_HighBuffer);
   
   SetIndexStyle(3,DRAW_LINE);
   SetIndexBuffer(3,fastEMA_LowBuffer);
   
	SetIndexStyle(4, DRAW_ARROW,0,1);
	SetIndexBuffer(4,buyArrow);
   SetIndexArrow(4, 233);

	SetIndexStyle(5,DRAW_ARROW,0,1);
	SetIndexBuffer(5,sellArrow);
	SetIndexArrow(5, 234);

	SetIndexDrawBegin(0,SlowMAPeriod);

	//---- name for DataWindow and indicator subwindow label
   IndicatorShortName("MA Trend ");
	SetIndexLabel(0,"Slow Hi");
	SetIndexLabel(1,"Slow Lo");
	SetIndexLabel(2,"Fast Hi");
	SetIndexLabel(3,"Fast Lo");
	SetIndexLabel(4,"BUY");
	SetIndexLabel(5,"SELL");
	
   return(0);
}

int start()
{
   int limit;
   int counted_bars = IndicatorCounted();
   //---- check for possible errors
   if (counted_bars<0) return(-1);
   //---- last counted bar will be recounted
   if (counted_bars>0) counted_bars--;
   limit = Bars - counted_bars;

	for(int i=limit; i>=0; i--){
		fastHi = iMA(NULL,0,FastMAPeriod,0,MODE_EMA,PRICE_HIGH,i);
		fastLo = iMA(NULL,0,FastMAPeriod,0,MODE_EMA,PRICE_LOW,i);
		slowHi = iMA(NULL,0,SlowMAPeriod,0,MODE_EMA,PRICE_HIGH,i);
		slowLo = iMA(NULL,0,SlowMAPeriod,0,MODE_EMA,PRICE_LOW,i);
     
		fastEMA_HighBuffer[i] = fastHi;
		fastEMA_LowBuffer[i] = fastLo;
		slowEMA_HighBuffer[i] = slowHi;
		slowEMA_LowBuffer[i] = slowLo;    
		
		// Check for trend change and arrow if trend has changed
	   if (	(slowHi < fastHi) && (fastLo > slowLo) ) trend = 1;
		else if  ( ( fastHi < slowHi) && (slowLo > fastLo) ) trend = 2;

		if (trend!=prevTrend){
			if(prevTrend==2){			
				buyArrow[i]=fastLo-10*Point;
				Alert("BUY!");
			} else if (prevTrend==1){
				sellArrow[i]=slowHi+10*Point;
				Alert("Sell!");
			}
			prevTrend=trend;
   	}

   }
   return(0);
}

