//+------------------------------------------------------------------+
//|                                    Price MA Breakout.mq4 |
//|                                    Copyright © 2014,TradeForexFx |
//+------------------------------------------------------------------+


#property copyright "Copyright © 2014, TradeForexFx "
#property link      ""

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Lime
#property indicator_color2 Red

double CrossUp[];
double CrossDown[];
extern int 	MA 					= 5;
extern int 	MA_Mode 			= 1;
extern int 	shift 				= 2;
extern bool AlertON				= true;
extern int 	ArrowPipDistance 	= 4;

double alertTag;
double pip_dist;

 
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   SetIndexStyle(0, DRAW_ARROW, EMPTY,0);
   SetIndexArrow(0, 233);
   SetIndexBuffer(0, CrossUp);
   SetIndexStyle(1, DRAW_ARROW, EMPTY,0);
   SetIndexArrow(1, 234);
   SetIndexBuffer(1, CrossDown);
   
	if ((Digits == 3) || (Digits == 5))
		pip_dist = ArrowPipDistance * Point *10;
	else pip_dist = ArrowPipDistance * Point;
   	
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//---- 

//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start() {
   int limit, i;
   double MA01, MA02, close01 , close02;
   
   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(i = 0; i <= limit; i++) {
   
       
      MA01 = iMA(NULL, 0, MA, shift, MA_Mode, PRICE_CLOSE, i+1);
      MA02 = iMA(NULL, 0, MA, shift, MA_Mode, PRICE_CLOSE, i+2);      

      close01  = iClose(NULL,0,i+1);
      close02  = iClose(NULL,0,i+2);
      
      if ((close01 > MA01) && (close02 <= MA02)) {
         CrossUp[i+1] = Low[i+1] - pip_dist;
      }
      else if ((close01 < MA01)  && (close02  >= MA02)) {
          CrossDown[i+1] = High[i+1] + pip_dist;
      }
        if (AlertON==true && i==1 && CrossUp[i] > CrossDown[i] && alertTag!=Time[0]){
         Alert("MA Close Trend going Down on ",Symbol()," ",Period());
        alertTag = Time[0];
        
      }
        if (AlertON==true && i==1 && CrossUp[i] < CrossDown[i] && alertTag!=Time[0]){
       Alert("MA Close Trend going Up on ",Symbol()," ",Period());
        alertTag = Time[0];
        } 
  }
   return(0);
}

