#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1  clrWhite
#property indicator_color2  clrWhite
#property strict


input int            Fast_MA_Period  = 1;            // Fast ma period
input int            Slow_MA_Period  = 20;           // Slow ma period
input int            Signal_period   = 3;            // Signal period
input double         ArrowGapUp      = 0.5;          // Gap for arrow up        
input double         ArrowGapDn      = 0.5;          // Gap for arrow down
input bool           alertsOn        = false;        // Turn alerts on?
input bool           alertsOnCurrent = true;         // Alerts on current (still opened) bar?
input bool           alertsMessage   = true;         // Alerts should show pop-up message?
input bool           alertsSound     = false;        // Alerts should play alert sound?
input bool           alertsPushNotif = false;        // Alerts should send push notification?
input bool           alertsEmail     = false;        // Alerts should send email?
input string         soundFile       = "alert2.wav"; // Sound file

double b2[],b3[],Buffer1[],Buffer2[],trend[];
      
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
{
   IndicatorBuffers(5);
   SetIndexBuffer(0,b2); SetIndexStyle(0,DRAW_ARROW,STYLE_SOLID,1); SetIndexArrow(0,242); 
   SetIndexBuffer(1,b3); SetIndexStyle(1,DRAW_ARROW,STYLE_SOLID,1); SetIndexArrow(1,241);   
   SetIndexBuffer(2,Buffer1);
   SetIndexBuffer(3,Buffer2);
   SetIndexBuffer(4,trend);
return(INIT_SUCCEEDED);
}
void OnDeinit(const int reason) { }


int OnCalculate(const int rates_total,const int prev_calculated,const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
    
   int i,counted_bars=prev_calculated;
      if(counted_bars<0) return(-1);
      if(counted_bars>0) counted_bars--;
           int limit = fmin(rates_total-prev_calculated,rates_total-1);
   
   //
   //
   //
   //
   //
   
   for(i=limit; i>=0; i--) Buffer1[i] = iMA(NULL,0,Fast_MA_Period,0,MODE_SMA,PRICE_MEDIAN,i)-iMA(NULL,0,Slow_MA_Period,0,MODE_SMA,PRICE_MEDIAN,i);
   for(i=limit; i>=0; i--)
   {
      Buffer2[i] = iMAOnArray(Buffer1,rates_total,Signal_period,0,MODE_LWMA,i);
      trend[i]   = (i<Bars-1) ? (Buffer2[i]>Buffer1[i]) ? 1 : (Buffer2[i]<Buffer1[i]) ? -1 : trend[i+1] : 0;
      b2[i] = EMPTY_VALUE;
      b3[i] = EMPTY_VALUE;
      if (i<rates_total-1 && trend[i]!=trend[i+1])
      {
            if (trend[i] ==  1) b3[i] = Low[i] -iATR(NULL,0,15,i)*ArrowGapUp;
            if (trend[i] == -1) b2[i] = High[i]+iATR(NULL,0,15,i)*ArrowGapDn;
      }
   }   
      
         
      //
      //
      //
      //
      //
        
      if (alertsOn)
      {
         int whichBar = 1; if (alertsOnCurrent) whichBar = 0; 
         if (trend[whichBar] != trend[whichBar+1])
         {
            if (trend[whichBar] == 1) doAlert(" up");
            if (trend[whichBar] ==-1) doAlert(" down");       
         }         
       }              
return(rates_total);
}

//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//
//

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 = timeFrameToString(_Period)+" "+_Symbol+" at "+TimeToStr(TimeLocal(),TIME_SECONDS)+" SMA CrossOver "+doWhat;
             if (alertsMessage)     Alert(message);
             if (alertsPushNotif )  SendNotification(message);
             if (alertsEmail)       SendMail(_Symbol+" SMA CrossOver ",message);
             if (alertsSound)       PlaySound(soundFile);
      }
}

//
//
//
//
//

string sTfTable[] = {"M1","M5","M15","M30","H1","H4","D1","W1","MN"};
int    iTfTable[] = {1,5,15,30,60,240,1440,10080,43200};

string timeFrameToString(int tf)
{
   for (int i=ArraySize(iTfTable)-1; i>=0; i--) 
         if (tf==iTfTable[i]) return(sTfTable[i]);
                              return("");
}





