
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "www,forex-tsd.com"
#property link      "www,forex-tsd.com"

#property indicator_chart_window
#property indicator_buffers 3
#property indicator_color1  RoyalBlue//wicks
#property indicator_color2  Red//wicks
#property indicator_color3  Gold
#property indicator_width3  2

//
//
//
//
//

extern int    MA_BarPeriod	   = 15;
extern int    MA_BarType	   = 1;
extern int    MA_BarPrice   	= 0;
extern int    MAPeriod	      = 50;
extern int    MAType	         = 0;
extern int    MAPrice   	   = 0;
extern string Type_Key        = "0: SMA, 1: EMA, 2: SMMA, 3: LWMA"; 
extern string Price_Key       = "0: Close, 1: Open, 2: High";
extern string Price_Key_cont  = " 3: Low, 4: Median";

extern bool   alertsOn        = true;
extern bool   alertsOnCurrent = false;
extern bool   alertsMessage   = true;
extern bool   alertsSound     = false;
extern bool   alertsEmail     = false;
extern bool   alertsNotify    = false;
extern string soundFile       = "alert2.wav";

//
//
//
//
//

double Bar1[], Bar2[], MA[];
double trend[];

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//

int init()
{
   IndicatorBuffers(4);   
   SetIndexBuffer(0,Bar1); SetIndexStyle(0,DRAW_HISTOGRAM);
   SetIndexBuffer(1,Bar2); SetIndexStyle(1,DRAW_HISTOGRAM);
   SetIndexBuffer(2,MA); 
   SetIndexBuffer(3,trend);
   IndicatorShortName("Price cross Ma Candles"); 
return(0);
}  
int deinit()
{
   return(0);
}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//

int start() 
{
   int counted_bars=IndicatorCounted();
   int i,limit;

   if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
         limit = MathMin(Bars-counted_bars,Bars-1);

   //
   //
   //
   //
   //

     for(i=limit; i>=0; i--)
     {
        double ma    = iMA(NULL,0,MA_BarPeriod,0,MA_BarType,MA_BarPrice,i);
        double pr    = iMA(NULL,0,1,0,MODE_SMA,PRICE_CLOSE,i);
               MA[i] = iMA(NULL,0,MAPeriod,0,MAType,MAPrice,i);
       
        Bar1[i]  = EMPTY_VALUE;
        Bar2[i]  = EMPTY_VALUE;
        trend[i] = trend[i+1];
           if (pr>ma) trend[i] = 1;
           if (pr<ma) trend[i] =-1;
           if (trend[i] == 1) { Bar1[i] = High[i];	Bar2[i] = Low[i]; }
           if (trend[i] ==-1) { Bar2[i] = High[i];	Bar1[i] = Low[i]; }
           
   }
   
   //
   //
   //
   //
   //
      
   if (alertsOn)
   {
      if (alertsOnCurrent)
           int whichBar = 0;
      else     whichBar = 1;
      if (trend[whichBar] != trend[whichBar+1])
      if (trend[whichBar] == 1)
            doAlert("crossed up");
      else  doAlert("crossed down");       
   } 
   return(0);
}

//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//
//

void doAlert(string doWhat)
{
   static string   previousAlert="nothing";
   static datetime previousTime;
   string message;
   
      if (previousAlert != doWhat || previousTime != Time[0]) {
          previousAlert  = doWhat;
          previousTime   = Time[0];

          //
          //
          //
          //
          //

          message =  StringConcatenate(Symbol()," at ",TimeToStr(TimeLocal(),TIME_SECONDS)," MA_Bars ",doWhat);
             if (alertsMessage) Alert(message);
             if (alertsNotify)  SendNotification(message);
             if (alertsEmail)   SendMail(StringConcatenate(Symbol()," MA_Bars "),message);
             if (alertsSound)   PlaySound(soundFile);
      }
}

