//+------------------------------------------------------------------+
//|                                      DBB Alert.mq4  |
//+------------------------------------------------------------------+


#property indicator_chart_window
#property indicator_buffers 3
#property indicator_color1 BlueViolet
#property indicator_color2 Red
#property indicator_color3 Aqua
#property indicator_width1 2
#property indicator_width2 2
#property indicator_width3 2
#property indicator_style1 STYLE_SOLID
#property indicator_style2 STYLE_SOLID
#property indicator_style3 STYLE_SOLID


//---- input parameters
extern int BBandsPeriod = 20;
extern double BBandsDeviation = 2.0;
extern int MAPeriod = 20;
extern int MACD_Fast = 12;
extern int MACD_Slow = 26;
extern int MACD_SMA = 9;
extern int MAMode = MODE_SMA;
extern int MAPrice = PRICE_CLOSE;
extern int MAShift = 0;
input bool Platform_Alert = true;
input bool Mobile_Alert = true;
input bool Email_Alert = true;
input int Alert_Repeat = 1;
input int Alert_Interval = 1;

//---- indicator buffers
double BBandMiddle[];
double MACD[];
double SMA50[];


datetime SignalTime;
int AlertType=0, AlertCounter=0;
string AlertMSG, TF;
//+------------------------------------------------------------------+
//| init()                                                           |
//+------------------------------------------------------------------+
int init()
{
   EventSetTimer(Alert_Interval);
   SignalTime=Time[1];
   if(Period()==1) TF="M1";
   else if(Period()==5) TF="M5";
   else if(Period()==15) TF="M15";
   else if(Period()==30) TF="M30";
   else if(Period()==60) TF="H1";
   else if(Period()==240) TF="H4";
   else if(Period()==1440) TF="D1";
   else if(Period()==10080) TF="W1";
   else if(Period()==43200) TF="MN1";
   else TF="N/A";
   
   SetIndexBuffer( 0, SMA50 );
   SetIndexLabel( 0, "Moving Average" );
   
   SetIndexBuffer( 1, MACD );
   SetIndexLabel( 1, "MACD" );
   
   SetIndexBuffer( 2, BBandMiddle );
   SetIndexLabel( 2, "BB-Middle" );
   
   
   
   IndicatorShortName( "DBB" );
   IndicatorDigits( Digits );
   
   return( 0 );
}

//+------------------------------------------------------------------+
//| deinit()                                                         |
//+------------------------------------------------------------------+
int deinit()
{
   return( 0 );
}

//+------------------------------------------------------------------+
//| start()                                                          |
//+------------------------------------------------------------------+
int start()
 {
   int counted_bars = IndicatorCounted();
   
   if (counted_bars < 0) return (-1);
   if (counted_bars > 0) counted_bars--;
   int Limit = Bars - counted_bars;
        
   int index;
   for( index = Limit; index >= 0; index-- )
   {
      BBandMiddle[index]=iMA(NULL,0, BBandsPeriod, 0, MAMode, MAPrice, index);
      SMA50[index]=iMA(NULL,0, MAPeriod , 0, MAMode, MAPrice, index);
      MACD[index]=SMA50[index]+iMACD(NULL,0,MACD_Fast,MACD_Slow,MACD_SMA,MAPrice,MODE_MAIN,index);
    
   }

      if(MACD[0]<=SMA50[0] && MACD[1]> SMA50[1] && SignalTime!=Time[1])
      {
         AlertType=1;
         SignalTime=Time[1];
         AlertCounter=Alert_Repeat;
         if(Platform_Alert) Alert(StringConcatenate(Symbol(),"(",TF,") : R Cross Down V"));
        
         Comment(AlertMSG);
      }
      else if(MACD[0]>=SMA50[0] && MACD[1]<SMA50[1] && SignalTime!=Time[1])
      {
         AlertType=2;
         SignalTime=Time[1];
         AlertCounter=Alert_Repeat;
         if(Platform_Alert) Alert(StringConcatenate(Symbol(),"(",TF,") : R Cross Up V"));
        
         Comment(AlertMSG);
         
      }
      
          
            
      
 //+------------------------------------------------------------------+     
   return( 0 );
}

void OnTimer()
{
   if(AlertType==1) AlertMSG=StringConcatenate(Symbol(),"(",TF,") : R Cross Down V");
   else if(AlertType==2) AlertMSG=StringConcatenate(Symbol(),"(",TF,") : R Cross Up V");
  
   else return;
   
   if(AlertCounter>0)
   {
      Alert(AlertMSG);
      AlertCounter--;
   }
}