//+------------------------------------------------------------------+
//|                                                          SSL.mq4 |
//|                                     This version by Mladen Rakic |
//|                                                                  |
//| initial SSL for metatrader developed by Kalenzo                  |
//+------------------------------------------------------------------+
#property copyright "www.forex-station.com"
#property link      "www.forex-station.com"

#property indicator_chart_window
#property indicator_buffers 3
#property indicator_color1  clrCornflowerBlue
#property indicator_width1  2
#property indicator_color2  clrRed
#property indicator_width2  2
#property indicator_color3  clrRed
#property indicator_width3  2
#property strict

//
//
//

input int    Lb              = 10;           // Look back period
input bool   alertsOn        = true;         // Alerts?
input bool   alertsOnCurrent = true;         // Alerts open bar?
input bool   alertsMessage   = true;         // Alerts message?
input bool   alertsSound     = true;         // Alerts sound?
input bool   alertsEmail     = false;        // Alerts email?
input bool   alertsNotify    = false;        // Alerts phone notification?

double val[],vala[],valb[],valc[];

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+

int OnInit()
{
   IndicatorBuffers(4);
   SetIndexBuffer(0,val);  SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(1,vala); SetIndexStyle(1,DRAW_LINE);
   SetIndexBuffer(2,valb); SetIndexStyle(2,DRAW_LINE);
   SetIndexBuffer(3,valc);
return(INIT_SUCCEEDED);
}

//
//
//

int  OnCalculate(const int rates_total,const int prev_calculated,const datetime &time[],
                 const double &open[],
                 const double &high[],
                 const double &low[],
                 const double &close[],
                 const long &tick_volume[],
                 const long &volume[],
                 const int &spread[])
{
   int i=rates_total-prev_calculated+1; if (i>=rates_total) i=rates_total-1; 

   //
   //
   //
   
   if (valc[i] == -1) CleanPoint(i,rates_total,vala,valb);
   for (; i>=0 && !_StopFlag; i--)
   {
      vala[i] = valb[i] = EMPTY_VALUE;
      valc[i] = (i<rates_total-1) ? (close[i]>iMA(_Symbol,_Period,Lb,0,MODE_SMA,PRICE_HIGH,i+1)) ? 1 : 
                                    (close[i]<iMA(_Symbol,_Period,Lb,0,MODE_SMA,PRICE_LOW ,i+1)) ? -1 : valc[i+1] : 0;
      val[i]  = (valc[i] == -1) ? iMA(_Symbol,_Period,Lb,0,MODE_SMA,PRICE_HIGH,i+1) : 
                                  iMA(_Symbol,_Period,Lb,0,MODE_SMA,PRICE_LOW ,i+1);  
      if (valc[i] == -1) PlotPoint(i,rates_total,vala,valb,val);
   }
   if (alertsOn)
   {
      int whichBar = (alertsOnCurrent) ? 0 : 1;
      if (valc[whichBar] != valc[whichBar+1])
      if (valc[whichBar] == 1)
            doAlert(" up");
      else  doAlert(" down");       
   }
return(rates_total);
}

//
//
//

void CleanPoint(int i,int bars,double& first[],double& second[])
{
   if (i>=bars-3) return;
   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,int bars,double& first[],double& second[],double& from[])
{
   if (i>=bars-2) return;
   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; }
}

//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//
//

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 = timeFrameToString(_Period)+" "+_Symbol+" at "+TimeToStr(TimeLocal(),TIME_SECONDS)+" SSL "+doWhat;
             if (alertsMessage) Alert(message);
             if (alertsNotify)  SendNotification(message);
             if (alertsEmail)   SendMail(_Symbol+" SSL ",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};

string timeFrameToString(int tf)
{
   for (int i=ArraySize(iTfTable)-1; i>=0; i--) 
         if (tf==iTfTable[i]) return(sTfTable[i]);
                              return("");
}

      