//------------------------------------------------------------------
#property copyright "mladen"
#property link      "www.forex-station.cm"
//------------------------------------------------------------------
#property indicator_separate_window
#property indicator_buffers 8
#property indicator_color1  clrLimeGreen
#property indicator_color2  clrPaleVioletRed
#property indicator_color3  clrLimeGreen
#property indicator_color4  clrPaleVioletRed
#property indicator_color5  clrLimeGreen
#property indicator_color6  clrPaleVioletRed
#property indicator_color7  clrLimeGreen
#property indicator_color8  clrPaleVioletRed
#property indicator_minimum 0
#property indicator_maximum 5

//
//
//
//
//

extern string              TimeFrame1            = "Current time frame";
extern string              TimeFrame2            = "next1";
extern string              TimeFrame3            = "next2";
extern string              TimeFrame4            = "next3";
extern double              Length                = 20;
extern ENUM_MA_METHOD      MaMethod              = 0;
extern ENUM_APPLIED_PRICE  UpBandPrice           = PRICE_CLOSE;      
extern ENUM_APPLIED_PRICE  LoBandPrice           = PRICE_CLOSE;
extern ENUM_APPLIED_PRICE  StdPrice              = PRICE_CLOSE;    
extern double              Deviation             = 2.0;
extern string              UniqueID              = "4 Time frame BB Stops";
extern bool                alertsOn              = false;
extern int                 alertsLevel           = 3;
extern bool                alertsMessage         = true;
extern bool                alertsSound           = false;
extern bool                alertsEmail           = false;
extern int                 LinesWidth            =  0;
extern color               LabelsColor           = clrDarkGray;
extern int                 LabelsHorizontalShift = 5;
extern double              LabelsVerticalShift   = 1.5;

//
//
//
//
//

double BB1u[];
double BB1d[];
double BB2u[];
double BB2d[];
double BB3u[];
double BB3d[];
double BB4u[];
double BB4d[];

int    timeFrames[4];
bool   returnBars;
bool   calculateValue;
string indicatorFileName;

//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//
//

int init()
{
   SetIndexBuffer(0,BB1u);
   SetIndexBuffer(1,BB1d);
   SetIndexBuffer(2,BB2u);
   SetIndexBuffer(3,BB2d);
   SetIndexBuffer(4,BB3u);
   SetIndexBuffer(5,BB3d);
   SetIndexBuffer(6,BB4u);
   SetIndexBuffer(7,BB4d);
      indicatorFileName = WindowExpertName();
      returnBars        = (TimeFrame1=="returnBars");     if (returnBars)     return(0);
      calculateValue    = (TimeFrame1=="calculateValue"); if (calculateValue) return(0);
      
      //
      //
      //
      //
      //
      
      for (int i=0; i<8; i++) 
      {
         SetIndexStyle(i,DRAW_ARROW,EMPTY,LinesWidth); SetIndexArrow(i,110); 
      }
      timeFrames[0] = stringToTimeFrame(TimeFrame1);
      timeFrames[1] = stringToTimeFrame(TimeFrame2);
      timeFrames[2] = stringToTimeFrame(TimeFrame3);
      timeFrames[3] = stringToTimeFrame(TimeFrame4);
      alertsLevel   = MathMin(MathMax(alertsLevel,3),4);
      IndicatorShortName(UniqueID);
   return(0);
}
int deinit()
{
   for (int t=0; t<4; t++) ObjectDelete(UniqueID+t);
   return(0); 
}

//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//
//

double trend[][2];
#define _up 0
#define _dn 1
int start()
{
   int i,r,counted_bars=IndicatorCounted();
      if(counted_bars < 0) return(-1);
      if(counted_bars>0) counted_bars--;
         int limit = MathMin(Bars-counted_bars,Bars-1);
         if (returnBars) { BB1u[0] = limit+1; return(0); }
         if (calculateValue) { calculateByBB(limit); return(0); }

         if (timeFrames[0] != Period()) limit = MathMax(limit,MathMin(Bars-1,iCustom(NULL,timeFrames[0],indicatorFileName,"returnBars",0,0)*timeFrames[0]/Period()));
         if (timeFrames[1] != Period()) limit = MathMax(limit,MathMin(Bars-1,iCustom(NULL,timeFrames[1],indicatorFileName,"returnBars",0,0)*timeFrames[1]/Period()));
         if (timeFrames[2] != Period()) limit = MathMax(limit,MathMin(Bars-1,iCustom(NULL,timeFrames[2],indicatorFileName,"returnBars",0,0)*timeFrames[2]/Period()));
         if (timeFrames[3] != Period()) limit = MathMax(limit,MathMin(Bars-1,iCustom(NULL,timeFrames[3],indicatorFileName,"returnBars",0,0)*timeFrames[3]/Period()));
         if (ArrayRange(trend,0)!=Bars) ArrayResize(trend,Bars);

         //
         //
         //
         //
         //
         
         bool initialized = false;
         if (!initialized)
         {
            initialized = true;
            int window = WindowFind(UniqueID);
            for (int t=0; t<4; t++)
            {
               string label = timeFrameToString(timeFrames[t]);
                ObjectCreate(UniqueID+t,OBJ_TEXT,window,0,0);
                   ObjectSet(UniqueID+t,OBJPROP_COLOR,LabelsColor);
                   ObjectSet(UniqueID+t,OBJPROP_PRICE1,t+LabelsVerticalShift);
               ObjectSetText(UniqueID+t,label,8,"Arial");
            }               
         }
         for (t=0; t<4; t++) ObjectSet(UniqueID+t,OBJPROP_TIME1,Time[0]+Period()*LabelsHorizontalShift*60);

   //
   //
   //
   //
   //
    
   for(i = limit, r=Bars-i-1; i >= 0; i--,r++)
   {
      trend[r][_up] = 0;
      trend[r][_dn] = 0;
      for (int k=0; k<4; k++)
      {
         int y = iBarShift(NULL,timeFrames[k],Time[i]);
            double spanA = iCustom(NULL,timeFrames[k],indicatorFileName,"calculateValue","","","",Length,MaMethod,UpBandPrice,LoBandPrice,StdPrice,Deviation,0,y);
            bool   isUp  = (spanA>0);
            switch (k)
            {
               case 0 : if (isUp) { BB1u[i] = k+1; BB1d[i] = EMPTY_VALUE;}  else { BB1d[i] = k+1; BB1u[i] = EMPTY_VALUE; } break;
               case 1 : if (isUp) { BB2u[i] = k+1; BB2d[i] = EMPTY_VALUE;}  else { BB2d[i] = k+1; BB2u[i] = EMPTY_VALUE; } break;
               case 2 : if (isUp) { BB3u[i] = k+1; BB3d[i] = EMPTY_VALUE;}  else { BB3d[i] = k+1; BB3u[i] = EMPTY_VALUE; } break;
               case 3 : if (isUp) { BB4u[i] = k+1; BB4d[i] = EMPTY_VALUE;}  else { BB4d[i] = k+1; BB4u[i] = EMPTY_VALUE; } break;
            }
            if (isUp)
                  trend[r][_up] += 1;
            else  trend[r][_dn] += 1;
      }
   }
   manageAlerts();
   return(0);
}


//-------------------------------------------------------------------
//
//-------------------------------------------------------------------
//
//
//
//
//

void calculateByBB(int limit)
{
   for (int i=limit; i>=0; i--) 
   {
      

      double StdDev  = iStdDev(NULL,0,Length,0,MODE_SMA,StdPrice,i);
      double upMA    = iMA(NULL,0,Length,0,MaMethod,UpBandPrice,i);
      double dnMA    = iMA(NULL,0,Length,0,MaMethod,LoBandPrice,i);
             BB4u[i] = upMA + (StdDev*Deviation); //smax
             BB4d[i] = dnMA - (StdDev*Deviation); //smin
             
             BB2u[i] = EMPTY_VALUE;
             BB2d[i] = EMPTY_VALUE;
             BB3u[i] = EMPTY_VALUE;
             BB3d[i] = EMPTY_VALUE;
             BB1u[i] = BB1u[i+1];
   
             //
             //
             //
             //
             //
         
	          if (Close[i] > BB4u[i+1]) BB1u[i]= 1; 
	          if (Close[i] < BB4d[i+1]) BB1u[i]=-1;
	          if (BB1u[i] > 0 && BB4d[i] < BB4d[i+1]) BB4d[i] = BB4d[i+1];
	          if (BB1u[i] < 0 && BB4u[i] > BB4u[i+1]) BB4u[i] = BB4u[i+1];
             if (BB1u[i] == 1)
             {  
               BB2u[i] = BB4d[i]; if (BB1u[i+1]!= 1) BB3u[i] = BB2u[i];
             }
             if (BB1u[i] ==-1)
             {  
               BB2d[i] = BB4u[i]; if (BB1u[i+1]!=-1) BB3d[i] = BB2d[i];
             }
      }
}
//+-------------------------------------------------------------------
//|                                                                  
//+-------------------------------------------------------------------
//
//
//
//
//

void manageAlerts()
{
   if (alertsOn)
   {
      int whichBar = Bars-1;
      if (trend[whichBar][_up] >= alertsLevel || trend[whichBar][_dn] >= alertsLevel)
      {
         if (trend[whichBar][_up] >= alertsLevel) doAlert("up"  ,trend[whichBar][_up]);
         if (trend[whichBar][_dn] >= alertsLevel) doAlert("down",trend[whichBar][_dn]);
      }
   }
}

//
//
//
//
//

void doAlert(string doWhat, int howMany)
{
   static string   previousAlert="nothing";
   static datetime previousTime;
   string message;
   
   if (previousAlert != doWhat || previousTime != Time[0]) {
       previousAlert  = doWhat;
       previousTime   = Time[0];

       //
       //
       //
       //
       //

       message =  Symbol()+" at "+TimeToStr(TimeLocal(),TIME_SECONDS)+" "+howMany+" time frames of BB Stops are aligned "+doWhat;
          if (alertsMessage) Alert(message);
          if (alertsEmail)   SendMail(Symbol()+" 4 time frame BB Stops trend",message);
          if (alertsSound)   PlaySound("alert2.wav");
   }
}

//-------------------------------------------------------------------
//
//-------------------------------------------------------------------
//
//
//
//
//

string sTfTable[] = {"M1","M5","M15","M30","H1","H4","D1","W1","MN"};
int    iTfTable[] = {1,5,15,30,60,240,1440,10080,43200};

//
//
//
//
//

int toInt(double value) { return(value); }
int stringToTimeFrame(string tfs)
{
   tfs = stringUpperCase(tfs);
   int max = ArraySize(iTfTable)-1, add=0;
   int nxt = (StringFind(tfs,"NEXT1")>-1); if (nxt>0) { tfs = ""+Period(); add=1; }
       nxt = (StringFind(tfs,"NEXT2")>-1); if (nxt>0) { tfs = ""+Period(); add=2; }
       nxt = (StringFind(tfs,"NEXT3")>-1); if (nxt>0) { tfs = ""+Period(); add=3; }

      //
      //
      //
      //
      //
         
      for (int i=max; i>=0; i--)
         if (tfs==sTfTable[i] || tfs==""+iTfTable[i]) return(MathMax(iTfTable[toInt(MathMin(max,i+add))],Period()));
                                                      return(Period());
}
string timeFrameToString(int tf)
{
   for (int i=ArraySize(iTfTable)-1; i>=0; i--) 
         if (tf==iTfTable[i]) return(sTfTable[i]);
                              return("");
}

//
//
//
//
//

string stringUpperCase(string str)
{
   string   s = str;

   for (int length=StringLen(str)-1; length>=0; length--)
   {
      int tchar = StringGetChar(s, length);
         if((tchar > 96 && tchar < 123) || (tchar > 223 && tchar < 256))
                     s = StringSetChar(s, length, tchar - 32);
         else if(tchar > -33 && tchar < 0)
                     s = StringSetChar(s, length, tchar + 224);
   }
return(s);
}