//+------------------------------------------------------------------+
//|                                                     SSL fast.mq4 |
//|                                                           mladen |
//|                                                                  |
//| initial SSL for metatrader developed by Kalenzo                  |
//+------------------------------------------------------------------+
#property copyright "www.forex-tsd.com"
#property link      "www.forex-tsd.com"

#property indicator_chart_window
#property indicator_buffers 3
#property indicator_color1  CornflowerBlue
#property indicator_width1  2
#property indicator_color2  Red
#property indicator_width2  2
#property indicator_color3  Red
#property indicator_width3  2

//
//
//
//
//

extern string TimeFrame       = "Current time frame";
extern int    Lb              = 5;
extern int    MaFilterPass    = 2;
extern int    MaShift         = 0;
extern bool   Interpolate     = true;

extern bool   alertsOn        = false;
extern bool   alertsOnCurrent = true;
extern bool   alertsMessage   = true;
extern bool   alertsSound     = false;
extern bool   alertsEmail     = false;

//
//
//
//
//

double ssl[];
double sslDa[];
double sslDb[];
double trend[];

//
//
//
//
//

string indicatorFileName;
int    timeFrame;
bool   returnBars;
bool   calculateValue;

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

int init()
{
    IndicatorBuffers(4);
    SetIndexBuffer(0,ssl);  SetIndexDrawBegin(0,Lb+1);
    SetIndexBuffer(1,sslDa);
    SetIndexBuffer(2,sslDb);
    SetIndexBuffer(3,trend);
    
    //
    //
    //
    //
    //
     
        MaFilterPass      = MathMax(MathMin(MaFilterPass,48),1);
        Lb                = MathMax(Lb,2);
        indicatorFileName = WindowExpertName();
        calculateValue    = (TimeFrame=="calculateValue"); if (calculateValue) return(0);
        returnBars        = (TimeFrame=="returnBars");     if (returnBars)     return(0);
        timeFrame         = stringToTimeFrame(TimeFrame);
 
        for (int i=0;i<4;i++) SetIndexShift(i,MaShift*timeFrame/Period());
   IndicatorShortName(timeFrameToString(timeFrame)+"  MultiPass SSL("+Lb+")");
   return(0);
}
   

//
//
//
//
//

int start()
{
   int counted_bars=IndicatorCounted();
   int i,n,k,limit;

   if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
           limit = MathMin(Bars-counted_bars,Bars-1);
           if (returnBars) { ssl[0] = limit+1; return(0); }

   //
   //
   //
   //
   //
   
   if (trend[limit] == -1) ClearPoint(limit,sslDa,sslDb);

   //
   //
   //
   //
   //
   
   if (calculateValue || timeFrame==Period())
   {

      for(i=limit;i>=0;i--)
      {
         trend[i] = trend[i+1];
         sslDa[i] = EMPTY_VALUE;
         sslDb[i] = EMPTY_VALUE;
         if(Close[i] > iMultiPassMa(iMA(NULL,0,1,0,MODE_SMA,PRICE_HIGH,i+1),Lb,MaFilterPass,i+1,0)) trend[i] =  1;
         if(Close[i] < iMultiPassMa(iMA(NULL,0,1,0,MODE_SMA,PRICE_LOW, i+1),Lb,MaFilterPass,i+1,1)) trend[i] = -1;
         
      
         if(trend[i] == -1)
               ssl[i] = iMultiPassMa(iMA(NULL,0,1,0,MODE_SMA,PRICE_HIGH,i+1),Lb,MaFilterPass,i+1,0);
         else  ssl[i] = iMultiPassMa(iMA(NULL,0,1,0,MODE_SMA,PRICE_LOW, i+1),Lb,MaFilterPass,i+1,1);
         
         if (trend[i] == -1) PlotPoint(i,sslDa,sslDb,ssl);
     }

   manageAlerts();
   return(0);
   }
   
   //
   //
   //
   //
   //
   
   limit = MathMax(limit,MathMin(Bars,iCustom(NULL,timeFrame,indicatorFileName,"returnBars",0,0)*timeFrame/Period()));
   if (trend[limit]==-1) ClearPoint(limit,sslDa,sslDb);
    for (i=limit;i>=0; i--)
    {
       int y = iBarShift(NULL,timeFrame,Time[i]);
          ssl[i]   = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",Lb,MaFilterPass,MaShift,0,y);
          sslDa[i] = EMPTY_VALUE;
          sslDb[i] = EMPTY_VALUE;
          trend[i] = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",Lb,MaFilterPass,MaShift,3,y); 
          
          //
          //
          //
          //
          //
                        
          if (!Interpolate || y==iBarShift(NULL,timeFrame,Time[i-1])) continue;

          //
          //
          //
          //
          //

          datetime time = iTime(NULL,timeFrame,y);
            for(n = 1; i+n < Bars && Time[i+n] >= time; n++) continue;	
            for(k = 1; k < n; k++)
            {
  	           ssl[i+k] = ssl[i]+(ssl[i+n] - ssl[i]) * k/n;

           }  	            
   }
   
   for (i=limit;i>=0;i--) if (trend[i]==-1) PlotPoint(i,sslDa,sslDb,ssl);      
   manageAlerts();
      
   return(0);
}


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

double workMpFilter[][50][2];
double iMultiPassMa(double price, int period, int filterPass, int i, int instanceNo=0)
{ 
   if (ArrayRange(workMpFilter,0) != Bars) ArrayResize(workMpFilter,Bars); i = Bars-i-1; 

   double ma = 0;
   for (int p=0; p<filterPass; p++)
   {
      if (p==0) workMpFilter[i][0][instanceNo] = price;
      if (i>=period)
               ma = workMpFilter[i-1][p+1][instanceNo]+(workMpFilter[i][p][instanceNo]-workMpFilter[i-period][p][instanceNo])/period;
      else {   ma = workMpFilter[i][p][instanceNo]; for (int k=1; k<period && (i-k)>=0; k++) ma += workMpFilter[i-k][p][instanceNo];
                                                                                             ma /= k; }
      workMpFilter[i][p+1][instanceNo] = ma;
   }               
   return(ma);
}      


//+-------------------------------------------------------------------
//|                                                                  
//+-------------------------------------------------------------------
//
//
//
//
//

void manageAlerts()
{
   if (!calculateValue && alertsOn)
   {
      if (alertsOnCurrent)
           int whichBar = 0;
      else     whichBar = 1; whichBar = iBarShift(NULL,0,iTime(NULL,timeFrame,whichBar));
      if (trend[whichBar] != trend[whichBar+1])
      {
         if (trend[whichBar] ==  1) doAlert(whichBar,"up");
         if (trend[whichBar] == -1) doAlert(whichBar,"down");
      }
   }
}

//
//
//
//
//

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 =  StringConcatenate(Symbol()," ",timeFrameToString(timeFrame)," at ",TimeToStr(TimeLocal(),TIME_SECONDS)," ssl changed to ",doWhat);
          if (alertsMessage) Alert(message);
          if (alertsEmail)   SendMail(StringConcatenate(Symbol()," ",timeFrameToString(timeFrame)),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 stringToTimeFrame(string tfs)
{
   tfs = stringUpperCase(tfs);
   for (int i=ArraySize(iTfTable)-1; i>=0; i--)
         if (tfs==sTfTable[i] || tfs==""+iTfTable[i]) return(MathMax(iTfTable[i],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);
}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

void ClearPoint(int i,double& first[],double& second[])
{
   if ((second[i]  != EMPTY_VALUE) && (second[i+1] != EMPTY_VALUE))
        second[i+1] = EMPTY_VALUE;
   else
      if ((first[i] != EMPTY_VALUE) && (first[i+1] != EMPTY_VALUE) && (first[i+2] == EMPTY_VALUE))
          first[i+1] = EMPTY_VALUE;
}

//
//
//
//
//

void PlotPoint(int i,double& first[],double& second[],double& from[])
{
   if (first[i+1] == EMPTY_VALUE)
      {
      if (first[i+2] == EMPTY_VALUE) {
          first[i]    = from[i];
          first[i+1]  = from[i+1];
          second[i]   = EMPTY_VALUE;
         }
      else {
          second[i]   = from[i];
          second[i+1] = from[i+1];
          first[i]    = EMPTY_VALUE;
         }
      }
   else
      {
         first[i]   = from[i];
         second[i]  = EMPTY_VALUE;
      }
}

//
//
//
//
//


