//+------------------------------------------------------------------+
//|                                              rsx on jurik smooth |
//|                                                           mladen |
//+------------------------------------------------------------------+
#property copyright "mladen"
#property link      "mladenfx@gmail.com"

#property indicator_separate_window
#property indicator_buffers  3
#property indicator_color1   clrBlue
#property indicator_color2   clrMagenta
#property indicator_color3   clrGray
#property strict

//
//
//

input int                RsiPeriod             = 14;                // Rsx length
input ENUM_APPLIED_PRICE RsiPrice              = PRICE_CLOSE;       // Rsx price
input bool               alertsOn              = true;              // Alerts?
input bool               alertsOnCurrent       = false;             // Alerts open bar?
input bool               alertsMessage         = true;              // Alerts popup message?
input bool               alertsSound           = false;             // Alerts sound?
input bool               alertsEmail           = false;             // Alerts email?
input bool               alertsNotify          = false;             // Alerts notification?

double UpH[],DnH[],val[],vals[];

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+

int OnInit()
{
   IndicatorBuffers(4);
   SetIndexBuffer(0,UpH,INDICATOR_DATA);  SetIndexStyle(0,DRAW_HISTOGRAM);
   SetIndexBuffer(1,DnH,INDICATOR_DATA);  SetIndexStyle(1,DRAW_HISTOGRAM);
   SetIndexBuffer(2,val,INDICATOR_DATA);  SetIndexStyle(2,DRAW_LINE);
   SetIndexBuffer(3,vals); 
    
   IndicatorSetString(INDICATOR_SHORTNAME,"Rsi ("+(string)RsiPeriod+")");
return(INIT_SUCCEEDED);
}
void OnDeinit(const int reason)  { }

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+

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;
   
   //
   //
   //
   
   for (; i>=0 && !_StopFlag; i--)
   {
      val[i]  = iRSI(_Symbol,_Period,RsiPeriod,RsiPrice,i)-50;
      vals[i] = (i<rates_total-1) ? (val[i]>val[i+1]) ? 1 : (val[i]<val[i+1]) ? -1 : vals[i+1] : 0;  
      UpH[i] = (vals[i] == 1) ? val[i] : EMPTY_VALUE;
      DnH[i] = (vals[i] ==-1) ? val[i] : EMPTY_VALUE;
   }
   if (alertsOn)
   {
      int whichBar = 1; if (alertsOnCurrent) whichBar = 0;
      if (vals[whichBar] != vals[whichBar+1])
      if (vals[whichBar] == 1)
            doAlert("up");
      else  doAlert("down");       
   }     
return(rates_total);
}

//------------------------------------------------------------------
//                                                                  
//------------------------------------------------------------------

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)+" Rsi "+doWhat;
             if (alertsMessage) Alert(message);
             if (alertsNotify)  SendNotification(message);
             if (alertsEmail)   SendMail(_Symbol+" Rsi ",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("");
}

