//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "www,forex-tsd.com"
#property link      "www,forex-tsd.com"

#property indicator_chart_window
#property indicator_buffers 4
#property indicator_color1 DeepSkyBlue
#property indicator_color2 PaleVioletRed
#property indicator_color3 DeepSkyBlue
#property indicator_color4 PaleVioletRed
#property indicator_width3 3
#property indicator_width4 3

//
//
//
//
//
extern double Length          = 6;
extern int    MaMethod        = 0;
extern int    Price           = 0;
extern double Deviation       = 1.0;
extern bool   ShowLines       = true;

extern bool   alertsOn        = true;
extern bool   alertsOnCurrent = false;
extern bool   alertsMessage   = true;
extern bool   alertsSound     = true;
extern bool   alertsEmail     = true;
extern string soundfile       = "alert2.wav";

//
//
//
//
//

double UpLine[];
double DnLine[]; 
double UpArrow[];
double DnArrow[]; 
double smax[];
double smin[];
double trend[];

//
//
//
//
//


int init()
{
   IndicatorBuffers(7);
      SetIndexBuffer(0,UpLine);  
      SetIndexBuffer(1,DnLine);  
      SetIndexBuffer(2,UpArrow); SetIndexStyle(2,DRAW_ARROW); SetIndexArrow(2,159);
      SetIndexBuffer(3,DnArrow); SetIndexStyle(3,DRAW_ARROW); SetIndexArrow(3,159);
      SetIndexBuffer(4,smax);
      SetIndexBuffer(5,smin);
      SetIndexBuffer(6,trend);
      
      if(ShowLines)
      {
      SetIndexStyle(0,DRAW_LINE);
      SetIndexStyle(1,DRAW_LINE);
      }
      else
      {
      SetIndexStyle(0,DRAW_ARROW);SetIndexArrow(0,159);
      SetIndexStyle(1,DRAW_ARROW);SetIndexArrow(1,159);
      }
      SetIndexStyle(2,DRAW_ARROW);SetIndexArrow(2,108);
      SetIndexStyle(3,DRAW_ARROW);SetIndexArrow(3,108);
   
      
 
         
   IndicatorShortName("BBands Stop("+Length+","+Deviation+")");
   return(0);
}
 
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

int start()
{
   int counted_bars=IndicatorCounted();
   int i,limit;

   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 StdDev  = iStdDev(NULL,0,Length,0,MODE_SMA,Price,i);
      double ma      = iMA(NULL,0,Length,0,MaMethod,Price,i);
             smax[i] = ma + (StdDev*Deviation);
             smin[i] = ma - (StdDev*Deviation);
             
             UpLine[i]  = EMPTY_VALUE;
             DnLine[i]  = EMPTY_VALUE;
             UpArrow[i] = EMPTY_VALUE;
             DnArrow[i] = EMPTY_VALUE;
             trend[i]   = trend[i+1]; 
   
             //
             //
             //
             //
             //
         
	          if (Close[i] > smax[i+1]) trend[i]= 1; 
	          if (Close[i] < smin[i+1]) trend[i]=-1;
	          if (trend[i] > 0 && smin[i] < smin[i+1]) smin[i] = smin[i+1];
	          if (trend[i] < 0 && smax[i] > smax[i+1]) smax[i] = smax[i+1];
             if (trend[i] == 1)
             {  
               UpLine[i] = smin[i]; if (trend[i+1]!= 1) UpArrow[i] = UpLine[i];
             }
             if (trend[i] ==-1)
             {  
               DnLine[i] = smax[i]; if (trend[i+1]!=-1) DnArrow[i] = DnLine[i];
             }
      }
       
      //
      //
      //
      //
      //
               
      if (alertsOn)
      {
      if (alertsOnCurrent)
           int whichBar = 0;
      else     whichBar = 1;

      //
      //
      //
      //
      //
         
      if (trend[whichBar] != trend[whichBar+1])
      if (trend[whichBar] == 1)
            doAlert("buy");
      else  doAlert("sell");    
      }
return(0);           
}

//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//
//

void doAlert(string doWhat)
{
   static string   previousAlert="nothing";
   static datetime previousTime;
   string message;
   
      if (previousAlert != doWhat || previousTime != Time[0]) {
          previousAlert  = doWhat;
          previousTime   = Time[0];

          //
          //
          //
          //
          //

          message =  StringConcatenate(Symbol()," at ",TimeToStr(TimeLocal(),TIME_SECONDS)," bbands stop ",doWhat);
             if (alertsMessage) Alert(message);
             if (alertsEmail)   SendMail(StringConcatenate(Symbol()," bbands stop "),message);
             if (alertsSound)   PlaySound(soundfile);
      }
}




