//+------------------------------------------------------------------+
//| Bandwidth Indicator.mq4
//| Copyright © 2007, Fxid10t@yahoo.com
//| http://www.fxstreet.com/education/technical/identifying-budding-trends-with-bollinger-bands/2006-06-14.html
//+------------------------------------------------------------------+
#property copyright "Copyright © 2007, Fxid10t@yahoo.com"
#property link      "http://www.fxstreet.com/education/technical/identifying-budding-trends-with-bollinger-bands/2006-06-14.html"
#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1 Red

//---- input parameters
extern int chart.timeframe=0; 
extern int period=20;
extern int deviation=2;
extern int bands.shift=0;
extern int applied.price=0;

extern double  AlertTriggerValue   = 1;
extern int     AlertCandle         = 1;
extern bool    ShowChartAlerts     = false;
extern string  AlertEmailSubject   = "";

datetime       LastAlertTime       = -999999;

string         AlertTextCross      = "Band breakout";          //---- type your desired text between the quotes

double BWI,ub,lb,mb;
//---- indicator buffers
double BWI.Buffer[];


int init()  {   
   IndicatorBuffers(1);
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0, BWI.Buffer);
   IndicatorShortName("BWI");
   SetIndexDrawBegin(0,period);
   SetIndexLabel(0,"BWI");

return(0); }

int deinit()  {return(0);}

int start() {
   int limit;
   int counted_bars=IndicatorCounted();
//---- check for possible errors
   if(counted_bars<0) return(-1);
//---- the last counted bar will be recounted
   if(counted_bars>0) counted_bars--;
   limit=Bars-counted_bars;
//---- main loop
   for(int i=0; i<limit; i++)  {
      ub=iBands(Symbol(),chart.timeframe,period,deviation,bands.shift,applied.price,1,i);
      lb=iBands(Symbol(),chart.timeframe,period,deviation,bands.shift,applied.price,2,i);
      mb=iBands(Symbol(),chart.timeframe,period,deviation,bands.shift,applied.price,0,i);
      //Print(ub,lb,mb);
      BWI.Buffer[i]=((ub-lb)/mb)*100;}

   ProcessAlerts();
   return(0);}

  
//+------------------------------------------------------------------+
int ProcessAlerts()   {
//+------------------------------------------------------------------+
  if (AlertCandle >= 0  &&  Time[0] > LastAlertTime)   {

    // === Alert processing for crossover UP (indicator line crosses ABOVE signal line) ===
    if (BWI.Buffer[AlertCandle] > AlertTriggerValue  &&  BWI.Buffer[AlertCandle+1] <= AlertTriggerValue)  {
      string AlertText = Symbol() + "," + TFToStr(Period()) + ": " + AlertTextCross;
      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("");
}

