//+------------------------------------------------------------------+
//|                                                   Bands_Stop.mq4 |
//|                                                                  |
//|                                                                  |
//+------------------------------------------------------------------+
#property indicator_chart_window
#property indicator_buffers 7
// no colors on the first 6 buffers when the EA is the only user
// restore colors if you want to
#property indicator_color1 Lime //CLR_NONE
#property indicator_color2 Red  //CLR_NONE
#property indicator_color3 Lime //CLR_NONE
#property indicator_color4 Red  //CLR_NONE
#property indicator_color5 CLR_NONE //Lime
#property indicator_color6 CLR_NONE //Red
#property indicator_color7 White    // any color except background
 // ===
//---- input parameters
extern int Length=20;     // Bollinger Bands Period
extern int Deviation=2;  // Deviation was 2
// ===
string sVer = "0604";
// = = =
double MoneyRisk=1.00; // Offset Factor
int    Signal=1;       // Display signals mode: 1-Signals & Stops; 0-only Stops; 2-only Signals;
//int    Line=1;         // Display line mode: 0-no,1-yes  
//---- indicator buffers
double UpTrendBuffer[];
double DownTrendBuffer[];
double UpTrendSignal[];
double DownTrendSignal[];
double UpTrendLine[];
double DownTrendLine[];
double FinalTrendLine[];
//bool SoundON = false;  // alerts disabled 
//bool TurnedUp = false;
//bool TurnedDown = false;
double dSmaxThis,dSminThis,dBsmaxThis,dBsminThis;
double dSmaxPrev,dSminPrev,dBsmaxPrev,dBsminPrev;
// = = =
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
void init()
{
   //---- indicator line
   SetIndexStyle(0,DRAW_ARROW,0,1);
   SetIndexBuffer(0,UpTrendBuffer);
   SetIndexArrow(0,159);  // dot
   SetIndexLabel(0,"UpTrend Stop");
   
   SetIndexStyle(1,DRAW_ARROW,0,1);
   SetIndexBuffer(1,DownTrendBuffer);
   SetIndexArrow(1,159);  // dot
   SetIndexLabel(1,"DownTrend Stop");   
   
   SetIndexStyle(2,DRAW_ARROW,0,1);
   SetIndexBuffer(2,UpTrendSignal);
   SetIndexArrow(2,241);  // up arrow
   SetIndexLabel(2,"UpTrend Signal");   
   
   SetIndexStyle(3,DRAW_ARROW,0,1);
   SetIndexBuffer(3,DownTrendSignal);
   SetIndexArrow(3,242);  // down arrow
   SetIndexLabel(3,"DownTrend Signal");   
   
   SetIndexStyle(4,DRAW_LINE);
   SetIndexBuffer(4,UpTrendLine);
   SetIndexLabel(4,"UpTrend Line");
   
   SetIndexStyle(5,DRAW_LINE);
   SetIndexBuffer(5,DownTrendLine);
   SetIndexLabel(5,"DownTrend Line");
   
   SetIndexStyle(6,DRAW_LINE,0,2);
   SetIndexBuffer(6,FinalTrendLine);
   SetIndexLabel(6,"FinalTrend Line");

   IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS));
   //----
   //---- name for DataWindow and indicator subwindow label
   string short_name;
   short_name = "BBandsSAR_v2("+Length+","+Deviation+")";
   //IndicatorShortName(short_name);  // for sub windows only
   Print(short_name + ", ver: " + sVer);
   return;
}
//+------------------------------------------------------------------+
//| Bollinger Bands_Stop                                             |
//+------------------------------------------------------------------+
void start()
{
   static int iFirstStart;
   static int iBarsPrev;
   if(iFirstStart == 0)
   {
      iFirstStart = 1;
      Print("start first entry - do back fill.");
      DoBackFill();
      Print("processing on bar close.");
   }
   if(Bars == iBarsPrev) return;
   DoProcess(1);
   iBarsPrev = Bars;
   return;
}
// = = = 
void DoBackFill()
{
   for(int shift=Bars-Length-1;shift>=1;shift--)
   {   DoProcess(shift);  }
   return;
}
// = = = 
void DoProcess(int shift)
{
   static int iFirstProcess;
   static int iTrend;
   UpTrendBuffer[shift]=0;
   DownTrendBuffer[shift]=0;
   UpTrendSignal[shift]=0;
   DownTrendSignal[shift]=0;
   UpTrendLine[shift]=EMPTY_VALUE;
   DownTrendLine[shift]=EMPTY_VALUE;
   FinalTrendLine[shift]=EMPTY_VALUE;

   dSmaxPrev = dSmaxThis;
   dSminPrev = dSminThis;
   dBsmaxPrev = dBsmaxThis;
   dBsminPrev = dBsminThis;
   
   if(iFirstProcess == 0)
   {
      iFirstProcess = 1;
   	dSmaxPrev = iBands(NULL,0,Length,Deviation,0,PRICE_CLOSE,MODE_UPPER,shift+1);
   	dSminPrev = iBands(NULL,0,Length,Deviation,0,PRICE_CLOSE,MODE_LOWER,shift+1);
   }
   
   dSmaxThis=iBands(NULL,0,Length,Deviation,0,PRICE_CLOSE,MODE_UPPER,shift);
	dSminThis=iBands(NULL,0,Length,Deviation,0,PRICE_CLOSE,MODE_LOWER,shift);
	
	if(Close[shift] > dSmaxPrev) iTrend=1; 
	if(Close[shift] < dSminPrev) iTrend=-1;
		 	
	if(iTrend>0 && dSminThis < dSminPrev) dSminThis=dSminPrev;
	if(iTrend<0 && dSmaxThis > dSmaxPrev) dSmaxThis=dSmaxPrev;
	  	  
	dBsmaxThis = dSmaxThis+0.5*(MoneyRisk-1)*(dSmaxThis-dSminThis);
	dBsminThis = dSminThis-0.5*(MoneyRisk-1)*(dSmaxThis-dSminThis);
		
	if(iTrend>0 && dBsminThis < dBsminPrev) dBsminThis = dBsminPrev;
	if(iTrend<0 && dBsmaxThis > dBsmaxPrev) dBsmaxThis = dBsmaxPrev;
	  
	if (iTrend>0) 
	{
	   if (Signal>0 && UpTrendBuffer[shift+1]==-1.0)
	   {
	      UpTrendSignal[shift] = dBsminThis;
	      UpTrendBuffer[shift] = dBsminThis;
	      //if(Line>0) UpTrendLine[shift] = dBsminThis;
	      UpTrendLine[shift] = dBsminThis;
	      FinalTrendLine[shift] = dBsminThis;
         //if (SoundON==true && shift==0 && !TurnedUp)
         //{
         //   //Alert("BBands going Up on ",Symbol(),"-",Period());
         //   Print("BBands going Up on " + Symbol() + "-" + Period());
         //   TurnedUp = true;
         //   TurnedDown = false;
         //}
	   }
	   else
	   {
	      UpTrendBuffer[shift] = dBsminThis;
	      //if(Line>0) UpTrendLine[shift] = dBsminThis;
	      UpTrendLine[shift] = dBsminThis;
	      FinalTrendLine[shift] = dBsminThis;
	      UpTrendSignal[shift]=-1;
	   }
	   if (Signal==2) UpTrendBuffer[shift]=0;   
	   DownTrendSignal[shift]=-1;
	   DownTrendBuffer[shift]=-1.0;
	   DownTrendLine[shift]=EMPTY_VALUE;
	}
	if (iTrend<0) 
	{
	   if (Signal>0 && DownTrendBuffer[shift+1]==-1.0)
	   {
	      DownTrendSignal[shift] = dBsmaxThis;
	      DownTrendBuffer[shift] = dBsmaxThis;
	      //if(Line>0) DownTrendLine[shift] = dBsmaxThis;
	      DownTrendLine[shift] = dBsmaxThis;
	      FinalTrendLine[shift] = dBsmaxThis;
         //if (SoundON==true && shift==0 && !TurnedDown)
         //{
         //   //Alert("BBands going Down on ",Symbol(),"-",Period());
         //   Print("BBands going Down on " + Symbol() + "-" + Period());
         //   TurnedDown = true;
         //   TurnedUp = false;
         //}
	   }
	   else
	   {
	      DownTrendBuffer[shift] = dBsmaxThis;
	      //if(Line>0)DownTrendLine[shift] = dBsmaxThis;
	      DownTrendLine[shift] = dBsmaxThis;
	      FinalTrendLine[shift] = dBsmaxThis;
	      DownTrendSignal[shift]=-1;
	   }
	   if (Signal==2) DownTrendBuffer[shift]=0;    
	   UpTrendSignal[shift]=-1;
	   UpTrendBuffer[shift]=-1.0;
	   UpTrendLine[shift]=EMPTY_VALUE;
	}
   return;	
}
//+------------------------------------------------------------------+

