//+------------------------------------------------------------------+
//|                                                          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  4
#property indicator_color2  clrRed
#property indicator_width2  4
#property indicator_color3  clrRed
#property indicator_width3  4
#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?

extern string             button_note1          = "------------------------------";
extern ENUM_BASE_CORNER   btn_corner            = CORNER_LEFT_UPPER; 
extern string             btn_text              = "SSL";
extern string             btn_Font              = "Arial";
extern int                btn_FontSize          = 10;                        
extern color              btn_text_ON_color     = clrRed;
extern color              btn_text_OFF_color    = clrBlack;
extern color              btn_background_color  = clrWhite;
extern color              btn_border_color      = clrBlack;
extern int                button_x              = 400;                                   
extern int                button_y              = 30;                                   
extern int                btn_Width             = 100;                                
extern int                btn_Height            = 20;                               
extern string             button_note2          = "------------------------------";

bool show_data = true;
bool recalc    = true;
string IndicatorName, IndicatorObjPrefix,buttonId;


double val1[],vala[],valb[],valc[];

//------------------------------------------------------------------
//
//------------------------------------------------------------------
//

string GenerateIndicatorName(const string target) 
{
   string name = target;
   int try = 2;
   while (WindowFind(name) != -1)
   {
      name = target + " #" + IntegerToString(try++);
   }
   return name;
   }
//------------------------------------------------------------------
//
//------------------------------------------------------------------


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+

int OnInit()
{


  

IndicatorName = GenerateIndicatorName(btn_text);
   IndicatorObjPrefix = "__" + IndicatorName + "__";
   IndicatorShortName(IndicatorName);
   IndicatorDigits(Digits);
   
   double val;
   if (GlobalVariableGet(IndicatorName + "_visibility", val))
      show_data = val != 0;




   IndicatorBuffers(4);
   SetIndexBuffer(0,val1);  SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(1,vala); SetIndexStyle(1,DRAW_LINE);
   SetIndexBuffer(2,valb); SetIndexStyle(2,DRAW_LINE);
   SetIndexBuffer(3,valc);

//----
   ChartSetInteger(0, CHART_EVENT_MOUSE_MOVE, 1);
   buttonId = IndicatorObjPrefix + (btn_text);
   createButton(buttonId, btn_text, btn_Width, btn_Height, btn_Font, btn_FontSize, btn_background_color, btn_border_color, btn_text_ON_color);
   ObjectSetInteger(0, buttonId, OBJPROP_YDISTANCE, button_y);
   ObjectSetInteger(0, buttonId, OBJPROP_XDISTANCE, button_x);           
            






return(INIT_SUCCEEDED);
}
int deinit() { return(0); }     


 //------------------------------------------------------------------
//
//------------------------------------------------------------------
//

void createButton(string buttonID,string buttonText,int width,int height,string font,int fontSize,color bgColor,color borderColor,color txtColor)
{
      ObjectDelete    (0,buttonID);
      ObjectCreate    (0,buttonID,OBJ_BUTTON,0,0,0);
      ObjectSetInteger(0,buttonID,OBJPROP_COLOR,txtColor);
      ObjectSetInteger(0,buttonID,OBJPROP_BGCOLOR,bgColor);
      ObjectSetInteger(0,buttonID,OBJPROP_BORDER_COLOR,borderColor);
      ObjectSetInteger(0,buttonID,OBJPROP_BORDER_TYPE,BORDER_RAISED);
      ObjectSetInteger(0,buttonID,OBJPROP_XSIZE,width);
      ObjectSetInteger(0,buttonID,OBJPROP_YSIZE,height);
      ObjectSetString (0,buttonID,OBJPROP_FONT,font);
      ObjectSetString (0,buttonID,OBJPROP_TEXT,buttonText);
      ObjectSetInteger(0,buttonID,OBJPROP_FONTSIZE,fontSize);
      ObjectSetInteger(0,buttonID,OBJPROP_SELECTABLE,0);
      ObjectSetInteger(0,buttonID,OBJPROP_CORNER,btn_corner);
      ObjectSetInteger(0,buttonID,OBJPROP_HIDDEN,1);
      ObjectSetInteger(0,buttonID,OBJPROP_XDISTANCE,9999);
      ObjectSetInteger(0,buttonID,OBJPROP_YDISTANCE,9999);
}
//+------------------------------------------------------------------+
void handleButtonClicks()
{
   if (ObjectGetInteger(0, buttonId, OBJPROP_STATE))
   {
      ObjectSetInteger(0, buttonId, OBJPROP_STATE, false);
      show_data = !show_data;
      GlobalVariableSet(IndicatorName + "_visibility", show_data ? 1.0 : 0.0);
      recalc = true;
     // start();
   }
}


void OnChartEvent(const int id, 
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
{
   handleButtonClicks();
   if (id==CHARTEVENT_OBJECT_CLICK && ObjectGet(sparam,OBJPROP_TYPE)==OBJ_BUTTON)
   
  
   for (int banzai=0; banzai<indicator_buffers; banzai++)
     SetIndexStyle(banzai,DRAW_LINE);
     
   if (show_data)
   {
   ObjectSetInteger(ChartID(),buttonId,OBJPROP_COLOR,btn_text_ON_color);
     
      }
      else
      {
       
        ObjectSetInteger(ChartID(),buttonId,OBJPROP_COLOR,btn_text_OFF_color);
       for (int banzai=0; banzai<indicator_buffers; banzai++)
            SetIndexStyle(banzai,DRAW_NONE); 
      }      
   
} 


//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//
//
//
//
//

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;
      val1[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,val1);
   }
   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("");
}

      