//+------------------------------------------------------------------+
//|                                                    BBflat_sw.mq4 |
//|                                                          by Raff |
//+------------------------------------------------------------------+
#property indicator_separate_window
#property indicator_buffers     4
#property indicator_color1      clrAqua
#property indicator_color2      clrRed
#property indicator_color3      clrRed
#property indicator_color4      clrYellow
#property indicator_levelcolor  clrSlateGray
#property indicator_level1      0.0004
#property indicator_level2      -0.0004
//----
input int                 period          = 9;
input int                 shift           = 0;
input ENUM_MA_METHOD      method          = MODE_SMA;
input ENUM_APPLIED_PRICE  price           = PRICE_CLOSE;
input double              deviation       = 1.5;
input bool                alertsOn        = true;           // Turn alerts on/off?
input bool                alertsOnCurrent = false;          // Alerts on open bar on/off?
input bool                alertsMessage   = true;           // Alerts message on/off?
input bool                alertsSound     = false;          // Alerts sound on/off?
input bool                alertsNotify    = false;          // Alerts notification on/off?
input bool                alertsEmail     = false;          // Alerts email on/off?
input string              soundFile       = "alert2.wav";   // Sound file
//---- buffers
double buffer1[],buffer2[],buffer3[],buffer4[],diff[],trend[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   IndicatorBuffers(6);
   SetIndexBuffer(0,buffer1); SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(1,buffer2); SetIndexStyle(1,DRAW_LINE);
   SetIndexBuffer(2,buffer3); SetIndexStyle(2,DRAW_LINE);
   SetIndexBuffer(3,buffer4); SetIndexStyle(3,DRAW_LINE);
   SetIndexBuffer(4,diff);
   SetIndexBuffer(5,trend);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
{
   int limit,i,counted_bars = IndicatorCounted();

   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 ima = iMA(NULL,0,period,shift,method,price,i);
      double std = deviation*iStdDev(NULL,0,period,shift,method,price,i);
      buffer1[i] = 0;
      buffer2[i] = std;
      buffer3[i] = -std;
      buffer4[i] = Close[i]-ima;
      diff[i]  = buffer2[i]-buffer3[i];
      trend[i] = trend[i+1];
      if (diff[i]>diff[i+1]) trend[i] = 1; //widening
      if (diff[i]<diff[i+1]) trend[i] =-1; //narrowing
     }
     manageAlerts(); 
return(0);
}

//
//
//
//
//

void manageAlerts()
{
   if (alertsOn)
   {
      int whichBar = 1; if (alertsOnCurrent) whichBar = 0;
      if (trend[whichBar] != trend[whichBar+1])
      {
         if (trend[whichBar] ==  1) doAlert(whichBar,"bands widening");
         if (trend[whichBar] == -1) doAlert(whichBar,"bands narrowing");
      }
   }
}

//
//
//
//
//

void doAlert(int forBar, string doWhat)
{
   static string   previousAlert="nothing";
   static datetime previousTime;
   string message;
   
   if (previousAlert != doWhat || previousTime != Time[forBar]) {
       previousAlert  = doWhat;
       previousTime   = Time[forBar];
       
       //
       //
       //
       //
       //

       message = timeFrameToString(_Period)+" "+_Symbol+" at "+TimeToStr(TimeLocal(),TIME_SECONDS)+" BBflat "+doWhat;
          if (alertsMessage) Alert(message);
          if (alertsNotify)  SendNotification(message);
          if (alertsEmail)   SendMail(_Symbol+" BBflat ",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("");
}

