//+------------------------------------------------------------------+
//|                                                     Momentum.mq4 |
//|                      Copyright © 2004, MetaQuotes Software Corp. |
//|                                       http://www.metaquotes.net/ |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2004, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net/"

#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1 DodgerBlue
//---- input parameters
extern int MomPeriod=10;
extern int     AlertCandle         = 1;                                                                                                         
extern bool    ShowChartAlerts     = false;                                                                                                     
extern string  AlertEmailSubject   = "Momentum Cross";                                                                                          
                                                                                                                                                
datetime       LastAlertTime       = -999999;                                                                                                   
                                                                                                                                                
string         AlertTextCrossUp    = "Momentum cross UP";                    //
string         AlertTextCrossDown  = "Momentum cross DOWN";                  //
                                                                                                                                               
// ========================================================================= //



//---- buffers
double MomBuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   string short_name;
//---- indicator line
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,MomBuffer);
//---- name for DataWindow and indicator subwindow label
   short_name="Mom("+MomPeriod+")";
   IndicatorShortName(short_name);
   SetIndexLabel(0,short_name);
//----
   SetIndexDrawBegin(0,MomPeriod);
//----
 
   return(0);
  }
//+------------------------------------------------------------------+
//| Momentum                                                         |
//+------------------------------------------------------------------+
int start()
  {
   int i,counted_bars=IndicatorCounted();
//----
   if(Bars<=MomPeriod) return(0);
//---- initial zero
   if(counted_bars<1)
      for(i=1;i<=MomPeriod;i++) MomBuffer[Bars-i]=0.0;
//----
   i=Bars-MomPeriod-1;
   if(counted_bars>=MomPeriod) i=Bars-counted_bars-1;
   while(i>=0)
     {
      MomBuffer[i]=Close[i]*100/Close[i+MomPeriod];
      i--;
     }
     ProcessAlerts();  
   return(0);
  }
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+                                                                          
int ProcessAlerts()   {                                                                                                                         
//+------------------------------------------------------------------+                                                                          
  if (AlertCandle >= 0  &&  Time[0] > LastAlertTime)   {                                                                                        
                                                                                                                                                
    // === Alert processing for crossover UP (indicator line crosses ABOVE signal line) ===                                                     
    if (MomBuffer[AlertCandle] > 100  &&  MomBuffer[AlertCandle+1] <= 100)  {                   //
      string AlertText = Symbol() + "," + TFToStr(Period()) + ": " + AlertTextCrossUp;                                                          
      if (ShowChartAlerts)          Alert(AlertText);                                     //                                                      
      if (AlertEmailSubject > "")   SendMail(AlertEmailSubject,AlertText);                  //                                                    
      LastAlertTime = Time[0];                                                              //
                                                     
     }                                               //                                                                                        
                                                                                                                                                
    // === Alert processing for crossover DOWN (indicator line crosses BELOW signal line) ===                                                   
    if (MomBuffer[AlertCandle] < -100  &&  MomBuffer[AlertCandle+1] >= -100)  {                   //
      AlertText = Symbol() + "," + TFToStr(Period()) + ": " + AlertTextCrossDown;                                                               
      if (ShowChartAlerts)          Alert(AlertText);                                                                                           
      if (AlertEmailSubject > "")   SendMail(AlertEmailSubject,AlertText);                                                                      
      LastAlertTime = Time[0];                                                                                                                  
    }                                                                                                                                                                                                                                                                                           //
                                                                                                                                                
  }                                                                                                                                             
  return(0);                                                                                                                                    
}                                                                                                                                               
                                                                                                                                                
//+------------------------------------------------------------------+                                                                          
string TFToStr(int tf)   {                                                                                                                      
//+------------------------------------------------------------------+                                                                          
  if (tf == 0)        tf = Period();                                                                                                            
  if (tf >= 43200)    return("MN");                                                                                                             
  if (tf >= 10080)    return("W1");                                                                                                             
  if (tf >=  1440)    return("D1");                                                                                                             
  if (tf >=   240)    return("H4");                                                                                                             
  if (tf >=    60)    return("H1");                                                                                                             
  if (tf >=    30)    return("M30");                                                                                                            
  if (tf >=    15)    return("M15");                                                                                                            
  if (tf >=     5)    return("M5");                                                                                                             
  if (tf >=     1)    return("M1");                                                                                                             
  return("");                                                                                                                                   
}                                                                                                                                               
// =======================================================================================//