//+------------------------------------------------------------------+
//|                                                       MAs_EO_M15 |
//|                                           Copyright © 2010, SNM. |
//|                                              http://www.SNM.net/ |
//+------------------------------------------------------------------+
#property copyright "SNM."
#property link      "http://www.SNM.net/"

#property indicator_chart_window
#property indicator_buffers 7
#property indicator_color1 Lime
#property indicator_style1 STYLE_SOLID
#property indicator_width1 2
#property indicator_color2 Black
#property indicator_style2 STYLE_SOLID
#property indicator_width2 2
#property indicator_color3 Blue
#property indicator_style3 STYLE_SOLID
#property indicator_width3 2
#property indicator_color4 Aqua
#property indicator_style4 STYLE_SOLID
#property indicator_width4 2
#property indicator_color5 Maroon
#property indicator_style5 STYLE_SOLID
#property indicator_width5 2
#property indicator_color6 Magenta
#property indicator_style6 STYLE_SOLID
#property indicator_width6 2
#property indicator_color7 Green
#property indicator_style7 STYLE_SOLID
#property indicator_width7 2
//---- indicator parameters
//extern bool MA5_8CrossAlert=false;
extern bool MA35_50_PrAlert=true;
extern bool MA5_PrAlert=false;
extern bool MA10_PrAlert=false;
/*/extern bool MA21_PrAlert=false;
//extern bool MA55_PrAlert=false;
//extern bool MA89_PrAlert=false;
*/
extern string PrPip_note="PrPip Tolerance for MA5_10PrAlerts";
extern double PrPip = 3;
extern string k_note     = "k=1/10 for 4/5 digit broker";
extern int    k          = 10;


int MA1_Period=5;
int MA1_Method=MODE_EMA;
int MA2_Period=10;
int MA2_Method=MODE_EMA;
int MA3_Period=21;
int MA3_Method=MODE_EMA;
int MA4_Period=35;
int MA4_Method=MODE_SMA;
int MA5_Period=50;
int MA5_Method=MODE_EMA;
int MA6_Period=50;
int MA6_Method=MODE_SMA;
int MA7_Period=200;
int MA7_Method=MODE_SMA;

bool	PopupAlert=true;
bool	SoundON=true;
bool	EmailAlert=false;

//---- indicator buffers
double MA1[];
double MA2[];
double MA3[];
double MA4[];
double MA5[];
double MA6[];
double MA7[];

//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {

     return(0);
  }


//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   
	// tell the indicator which arrays to use as buffers
	SetIndexBuffer(0,MA1);
	SetIndexBuffer(1,MA2);
	SetIndexBuffer(2,MA3);
	SetIndexBuffer(3,MA4);
	SetIndexBuffer(4,MA5);
	SetIndexBuffer(5,MA6);
	SetIndexBuffer(6,MA7);	
	
	// label the indicies (shows on mouseover of lines and in Data Window)
	SetIndexLabel(0,"EMA5");
	SetIndexLabel(1,"EMA10");
	SetIndexLabel(2,"EMA21");
	SetIndexLabel(3,"SMA35");
	SetIndexLabel(4,"EMA50");
	SetIndexLabel(5,"SMA50");
	SetIndexLabel(6,"SMA200");
	
	}

// function which is called with every tick
int start() {

	// keep track of the bar on which an alert was last fired
	// since this variable is static, it will "remember" its value between calls
	static datetime tLastAlert = 0;

	// determine how far back to iterate
	// always recalc last completed bar incase the client was too busy to handle
	// the last tick of the last bar (very, very rare).
	int	iBarsToCalc = Bars - IndicatorCounted();
	if (iBarsToCalc < Bars) iBarsToCalc++;
	
	// iterate over bars to be calculated, starting with oldest
	for (int i=iBarsToCalc-i;i>=0;i--) {
	
		// calculate the MA values for the bar
		MA1[i]= iMA(NULL,0,MA1_Period,0,MA1_Method,PRICE_CLOSE,i);
		MA2[i]= iMA(NULL,0,MA2_Period,0,MA2_Method,PRICE_CLOSE,i);
		MA3[i]= iMA(NULL,0,MA3_Period,0,MA3_Method,PRICE_CLOSE,i);
		MA4[i]= iMA(NULL,0,MA4_Period,0,MA4_Method,PRICE_CLOSE,i);
		MA5[i]= iMA(NULL,0,MA5_Period,0,MA5_Method,PRICE_CLOSE,i);
		MA6[i]= iMA(NULL,0,MA6_Period,0,MA6_Method,PRICE_CLOSE,i);
		MA7[i]= iMA(NULL,0,MA7_Period,0,MA7_Method,PRICE_CLOSE,i);
		
	   }
 
	// now, the indicator work has been done, all the code below could be
	// deleted and the indicator would still function, but without alerts.
	// it's time to check for an alert if there hasn't already been one this bar
if(tLastAlert < Time[0]) {
		
		//For pr near MAs.........
		double d0_5 = MA1[0]-Close[0];
		double d0_10 = MA2[0]-Close[0];
		/*double d0_21 = MA3[0]-Close[0];
		double d0_55 = MA4[0]-Close[0];
		double d0_89 = MA5[0]-Close[0];*/
		
		double d1_5 = MA1[1]-Close[1];
		double d1_10 = MA2[1]-Close[1];
		/*double d1_21 = MA3[1]-Close[1];
		double d1_55 = MA4[1]-Close[1];
		double d1_89 = MA5[1]-Close[1];*/
		
			
		// if price crossed MA35/50 to the upper side...
		if (MA35_50_PrAlert==true && Open[0]<MA4[0] && Open[0]<MA6[0] && Close[0]>MA4[0] && Close[0]>MA6[0]){
			fireAlerts("Price crossed MA35/50 on " + Symbol() + Period());
			tLastAlert = Time[0];
		}
		// if price crossed MA35/50 to the down side...
		if (MA35_50_PrAlert==true && Open[0]>MA4[0] && Open[0]>MA6[0] && Close[0]<MA4[0] && Close[0]<MA6[0]){
			fireAlerts("Price crossed MA35/50 on " + Symbol() + Period());
			tLastAlert = Time[0];
		}
	   
	   // if price is nearing MA5..........
		if (MA5_PrAlert==true && (MathAbs(d0_5) <= (PrPip*Point*k)) && (MathAbs(d1_5) > (PrPip*Point*k))) {
			fireAlerts("Price near MA5 on " + Symbol() + Period());
			tLastAlert = Time[0];
		}
		
		// if price is nearing MA10..........
		if (MA10_PrAlert==true && (MathAbs(d0_10) <= (PrPip*Point*k)) && (MathAbs(d1_10) > (PrPip*Point*k))) {
			fireAlerts("Price near MA10 on " + Symbol() + Period());
			tLastAlert = Time[0];
		}
		
		/*/ if price is nearing MA21..........
		if (MA21_PrAlert==true && (MathAbs(d0_21) <= (PrPip*Point*k)) && (MathAbs(d1_21) > (PrPip*Point*k))) {
			fireAlerts("Price near MA21 on " + Symbol() + Period());
			tLastAlert = Time[0];
		}
		
		// if price is nearing MA55..........
		if (MA55_PrAlert==true && (MathAbs(d0_55) <= (PrPip*Point*k)) && (MathAbs(d1_55) > (PrPip*Point*k))) {
			fireAlerts("Price near MA55 on " + Symbol() + Period());
			tLastAlert = Time[0];
		}
		
		// if price is nearing MA89..........
		if (MA89_PrAlert==true && (MathAbs(d0_89) <= (PrPip*Point*k)) && (MathAbs(d1_89) > (PrPip*Point*k))) {
			fireAlerts("Price near MA89 on " + Symbol() + Period());
			tLastAlert = Time[0];
		}*/
		
	}
}

void fireAlerts(string sMsg) {

	if(PopupAlert)
		Alert(sMsg);
		
	if(SoundON)
	  PlaySound("Alert.wav");
	  
	if(EmailAlert)
		SendMail("MA alert On " + Symbol(),sMsg);
}

