//+------------------------------------------------------------------+
//|                                      DBB Alert.mq4  |
//+------------------------------------------------------------------+


#property indicator_chart_window
#property indicator_buffers 7
#property indicator_color1 Lime
#property indicator_color2 Lime
#property indicator_color3 Lime
#property indicator_color4 Lime
#property indicator_color5 Lime
#property indicator_width1 1
#property indicator_width2 1
#property indicator_width3 0
#property indicator_width4 1
#property indicator_width5 1
#property indicator_width6 1
#property indicator_width7 1
#property indicator_style1 STYLE_SOLID
#property indicator_style2 STYLE_SOLID
#property indicator_style3 STYLE_DOT
#property indicator_style4 STYLE_SOLID
#property indicator_style5 STYLE_SOLID


//---- input parameters
extern int BBandsPeriod = 20;
extern double BBandsDeviation1 = 1.0;
extern double BBandsDeviation2 = 2.0;
extern int MAMode = MODE_SMA;
extern int MAPrice = PRICE_CLOSE;
extern int MAShift = 0;
extern int Arrow_PointGap = 15;
extern bool AlertOn=true;
extern bool SendMailOn = false;
int SignalBar =  1 ; 
bool AlertBuy  = False;
bool AlertSell = False;


//---- indicator buffers
double BBandUpper1[];
double BBandLower1[];
double BBandMiddle[];
double BBandUpper2[];
double BBandLower2[];
double BuySignal[];
double SellSignal[];

//+------------------------------------------------------------------+
//| init()                                                           |
//+------------------------------------------------------------------+
int init()
{
   SetIndexBuffer( 0, BBandUpper1 );
   SetIndexLabel( 0, "BB-Upper1" );
   
   SetIndexBuffer( 1, BBandLower1 );
   SetIndexLabel( 1, "BB-Lower1" );
   
   SetIndexBuffer( 2, BBandMiddle );
   SetIndexLabel( 2, "BB-Middle" );
   
   SetIndexBuffer( 3, BBandUpper2 );
   SetIndexLabel( 3, "BB-Upper2" );
   
   SetIndexBuffer( 4, BBandLower2 );
   SetIndexLabel( 4, "BB-Lower2" );
   
   SetIndexBuffer( 5, BuySignal );
   SetIndexLabel( 5, NULL );
   SetIndexEmptyValue( 3, 0.0 );
   
   SetIndexBuffer( 6, SellSignal );
   SetIndexLabel( 6, NULL );
   SetIndexEmptyValue( 4, 0.0 );

   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 Shift, index, j;
   double BBSum, BBNewRes,  Deviation, MA;

   for( index = Limit; index >= 0; index-- )
   {
      Shift = index + MAShift;
      BBSum = 0.0;
      MA = iMA( Symbol(), Period(), BBandsPeriod, 0, MAMode, MAPrice, Shift );
      
      for ( j = Shift + BBandsPeriod - 1; j >= Shift; j-- )
      {
         BBNewRes = Close[j] - MA;
         BBSum += ( BBNewRes * BBNewRes );
      }
   
      Deviation = MathSqrt( BBSum / BBandsPeriod );
      BBandUpper1[index] = MA + BBandsDeviation1 *Deviation;
      BBandLower1[index] = MA - BBandsDeviation1 *Deviation; 
      BBandMiddle[index] = MA;
      BBandUpper2[index] = MA + BBandsDeviation2 *Deviation;
      BBandLower2[index] = MA - BBandsDeviation2 *Deviation; 
      BuySignal[index] = 0.0;
      SellSignal[index] = 0.0;
      
      // Check for DBB Buy Signal
      if (  Close[index] > BBandUpper1[index] ) 
      {
         if ( Close[index+1] < BBandUpper1[index+1] )
         {
             if ( Close[index+1+1] < BBandUpper1[index+1+1] )    BuySignal[index] = Low[index]-Arrow_PointGap*Point;
         }
      }
      
      // Check for DBB Sell Signal
      if (  Close[index] < BBandLower1[index]  )
      {
         if ( Close[index+1] > BBandLower1[index+1] )
         {
            if ( Close[index+1+1] > BBandLower1[index+1+1] )   SellSignal[index] = High[index]+Arrow_PointGap*Point;
         }
      }   
   }
 //+------------------------------------------------------------------+ 
  string  messageSell  =  StringConcatenate("DBB (", Symbol(), ", ", GetTimeFrameStr(), ")  -  Possible Sell Signal"); 
  string  messageBuy =  StringConcatenate("DBB (", Symbol(), ", ",GetTimeFrameStr(), ")  -  Possible Buy Signal"); 
 
               
        if (SellSignal[SignalBar] != EMPTY_VALUE && SellSignal[SignalBar] != 0 && AlertBuy)
         {
         AlertBuy = False;
           
               if(AlertOn){         
               Alert(messageSell);                             
               if (SendMailOn) SendMail(Symbol(),messageSell); 
            }              
         } 
      if (!AlertBuy && (SellSignal[SignalBar] == EMPTY_VALUE || SellSignal[SignalBar] == 0)) AlertBuy = True; 
      
        if (BuySignal[SignalBar] != EMPTY_VALUE && BuySignal[SignalBar] != 0 && AlertSell)
         {
         AlertSell = False;
            
             if(AlertOn){                    
             Alert(messageBuy);             
             if (SendMailOn) SendMail(Symbol(),messageBuy); 
             }            
         } 
      if (!AlertSell && (BuySignal[SignalBar] == EMPTY_VALUE || BuySignal[SignalBar] == 0)) AlertSell = True;     
   return( 0 );
}

   
string GetTimeFrameStr() {
   switch(Period())
   {
      case 1 : string TimeFrameStr="M1"; break;
      case 5 : TimeFrameStr="M5"; break;
      case 10 : TimeFrameStr="M10"; break;
      case 15 : TimeFrameStr="M15"; break;
      case 30 : TimeFrameStr="M30"; break;
      case 60 : TimeFrameStr="H1"; break;
      case 120 : TimeFrameStr="H2"; break;
      case 240 : TimeFrameStr="H4"; break;
      case 480 : TimeFrameStr="H8"; break;
      case 1440 : TimeFrameStr="D1"; break;
      case 10080 : TimeFrameStr="W1"; break;
      case 43200 : TimeFrameStr="MN1"; break;
      default : TimeFrameStr=Period();
   } 
   return (TimeFrameStr);
   }
extern int     AlertCandle         = 1;                                                                                                         //
extern bool    ShowChartAlerts     = true;                                                                                                     //
extern string  AlertEmailSubject   = "";                                                                                                        //
                                                                                                                                                //
datetime       LastAlertTime       = -999999;                                                                                                   //
                                                                                                                                                //
string         AlertTextCrossUp    = "ZeroLag MACD cross UP";          //---- type your desired text between the quotes                         //
string         AlertTextCrossDown  = "ZeroLag MACD cross DOWN";        //---- type your desired text between the quotes                         //

