//+------------------------------------------------------------------+
//|                                                  OnChart WPR.mq4 |
//|                                                           mladen |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "mladen"
#property link      ""

#property indicator_chart_window
#property indicator_buffers 8
#property indicator_color1  Crimson
#property indicator_color2  Goldenrod
#property indicator_color3  Goldenrod
#property indicator_color4  DeepSkyBlue
#property indicator_color5  PaleVioletRed
#property indicator_color6  PaleVioletRed
#property indicator_color7  DodgerBlue
#property indicator_color8  Magenta
#property indicator_width4  2
#property indicator_width5  2
#property indicator_width6  2
#property indicator_style1  STYLE_DASH

//
//
//
//
//

extern ENUM_TIMEFRAMES    TimeFrame        = PERIOD_CURRENT;
extern int                RsxLength        = 14;
extern ENUM_APPLIED_PRICE RsxPrice         = PRICE_CLOSE;
extern int                MinMaxPeriod     = 49;
extern int                overBought       = 10;
extern int                overSold         = 90;
extern bool               alertsOn         = false;
extern bool               alertsOnCurrent  = true;
extern bool               alertsMessage    = true;
extern bool               alertsSound      = false;
extern bool               alertsNotify     = false;
extern bool               alertsEmail      = false;
extern string             soundFile        = "alert2.wav";
extern bool               Interpolate      = true;

extern bool               ShowArrows       = false;
extern bool               ArrowsOnFirstBar = true;
extern int                arrowSize        = 2;


//
//
//
//
//

double Buffer1[];
double Buffer2[];
double Buffer3[];
double Buffer4[];
double Buffer4Da[];
double Buffer4Db[];
double arrowUp[];
double arrowDn[];
double slope[];
double wrkBuffer[][13];


//
//
//
//
//

string indicatorFileName;
bool   returnBars;

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+

int init()
{
   IndicatorBuffers(9);
   SetIndexBuffer(0,Buffer1);
   SetIndexBuffer(1,Buffer2);
   SetIndexBuffer(2,Buffer3);
   SetIndexBuffer(3,Buffer4);
   SetIndexBuffer(4,Buffer4Da);
   SetIndexBuffer(5,Buffer4Db);
   SetIndexBuffer(6,arrowUp); 
   SetIndexBuffer(7,arrowDn); 
   SetIndexBuffer(8,slope);
   
   if (ShowArrows)
   {
      SetIndexStyle(6,DRAW_ARROW,0,arrowSize); SetIndexArrow(6,233);  
      SetIndexStyle(7,DRAW_ARROW,0,arrowSize); SetIndexArrow(7,234);
   }
   else
   {
      SetIndexStyle(6,DRAW_NONE);  
      SetIndexStyle(7,DRAW_NONE);
   }
   
   
   indicatorFileName = WindowExpertName();
   returnBars        = TimeFrame==-99;
   TimeFrame         = MathMax(TimeFrame,_Period);
        
   IndicatorShortName(timeFrameToString(TimeFrame)+"   on chart rsx");
return(0);
}
int deinit() { return(0); }

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//
//

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) { Buffer1[0] = MathMin(limit+1,Bars-1); return(0); }
      
   //
   //
   //
   //
   //
   
   if (TimeFrame == Period())
   {
      if (ArrayRange(wrkBuffer,0) != Bars) ArrayResize(wrkBuffer,Bars);
      if (slope[limit]==-1) CleanPoint(limit,Buffer4Da,Buffer4Db);
      double Kg = (3.0)/(2.0+RsxLength);
      double Hg = 1.0-Kg;
      for(i=limit, r=Bars-i-1; i>=0; i--, r++)
      {  
         double rsxValue;
         wrkBuffer[r][12] = iMA(NULL,0,1,0,MODE_SMA,RsxPrice,i);
         if (i==(Bars-1)) { for (int c=0; c<12; c++) wrkBuffer[r][c] = 0; continue; }  

         //
         //
         //
         //
         //
      
         double roc = wrkBuffer[r][12]-wrkBuffer[r-1][12];
         double roa = MathAbs(roc);
         for (int k=0; k<3; k++)
         {
            int kk = k*2;
               wrkBuffer[r][kk+0] = Kg*roc                + Hg*wrkBuffer[r-1][kk+0];
               wrkBuffer[r][kk+1] = Kg*wrkBuffer[r][kk+0] + Hg*wrkBuffer[r-1][kk+1]; roc = 1.5*wrkBuffer[r][kk+0] - 0.5 * wrkBuffer[r][kk+1];
               wrkBuffer[r][kk+6] = Kg*roa                + Hg*wrkBuffer[r-1][kk+6];
               wrkBuffer[r][kk+7] = Kg*wrkBuffer[r][kk+6] + Hg*wrkBuffer[r-1][kk+7]; roa = 1.5*wrkBuffer[r][kk+6] - 0.5 * wrkBuffer[r][kk+7];
         }
         if (roa != 0)
              rsxValue = MathMax(MathMin((roc/roa+1.0)*50.0,100.00),0.00);
         else rsxValue = 50.0;
            double max = iHigh(NULL,0,iHighest(NULL,0,MODE_HIGH,MinMaxPeriod,i));
            double min = iLow (NULL,0,iLowest (NULL,0,MODE_LOW ,MinMaxPeriod,i));
            double rng = max-min;
            Buffer1[i] = (min+max)/2;
            Buffer2[i] = min+rng*overBought/100;
            Buffer3[i] = min+rng*overSold  /100;
            Buffer4[i] = min+rng*rsxValue  /100;
            
            //
            //
            //
            //
            //
            
            Buffer4Da[i] = EMPTY_VALUE;
            Buffer4Db[i] = EMPTY_VALUE;
            slope[i] = slope[i+1];
            if (Buffer4[i]<Buffer4[i+1]) slope[i] = -1;
            if (Buffer4[i]>Buffer4[i+1]) slope[i] =  1;
            if (slope[i] == -1) PlotPoint(i,Buffer4Da,Buffer4Db,Buffer4);
           
            //
            //
            //
            //
            //
      
            arrowUp[i] = EMPTY_VALUE;
            arrowDn[i] = EMPTY_VALUE;
            if (slope[i]!= slope[i+1])
            if (slope[i] == 1)
                  arrowUp[i] = Buffer4[i] - iATR(NULL,0,20,i)/2.0;
            else  arrowDn[i] = Buffer4[i] + iATR(NULL,0,20,i)/2.0;
     }
     
     //
     //
     //
     //
     //
      
     if (alertsOn)
     {
       if (alertsOnCurrent)
            int whichBar = 0;
       else     whichBar = 1;
       if (slope[whichBar] != slope[whichBar+1])
       if (slope[whichBar] == 1)
             doAlert("sloping up");
       else  doAlert("sloping down");                   
     }
   return(0);
   } 
        
   //
   //
   //
   //
   //
   
   int shift = -1; if (ArrowsOnFirstBar) shift=1;
   limit = MathMax(limit,MathMin(Bars-1,iCustom(NULL,TimeFrame,indicatorFileName,-99,0,0)*TimeFrame/Period()));
   if (slope[limit]==-1) CleanPoint(limit,Buffer4Da,Buffer4Db);
   for (i=limit; i>=0; i--)
   {
      int y = iBarShift(NULL,TimeFrame,Time[i]);
      int x = iBarShift(NULL,TimeFrame,Time[i+shift]);
         Buffer1[i]   = iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,RsxLength,RsxPrice,MinMaxPeriod,overBought,overSold,alertsOn,alertsOnCurrent,alertsMessage,alertsSound,alertsNotify,alertsEmail,soundFile,0,y);
         Buffer2[i]   = iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,RsxLength,RsxPrice,MinMaxPeriod,overBought,overSold,alertsOn,alertsOnCurrent,alertsMessage,alertsSound,alertsNotify,alertsEmail,soundFile,1,y);
         Buffer3[i]   = iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,RsxLength,RsxPrice,MinMaxPeriod,overBought,overSold,alertsOn,alertsOnCurrent,alertsMessage,alertsSound,alertsNotify,alertsEmail,soundFile,2,y);
         Buffer4[i]   = iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,RsxLength,RsxPrice,MinMaxPeriod,overBought,overSold,alertsOn,alertsOnCurrent,alertsMessage,alertsSound,alertsNotify,alertsEmail,soundFile,3,y); 
         slope[i]     = iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,RsxLength,RsxPrice,MinMaxPeriod,overBought,overSold,alertsOn,alertsOnCurrent,alertsMessage,alertsSound,alertsNotify,alertsEmail,soundFile,8,y); 
         Buffer4Da[i] = EMPTY_VALUE;
         Buffer4Db[i] = EMPTY_VALUE; 
       if(x!=y)
       {
         arrowUp[i]   = iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,RsxLength,RsxPrice,MinMaxPeriod,overBought,overSold,alertsOn,alertsOnCurrent,alertsMessage,alertsSound,alertsNotify,alertsEmail,soundFile,6,y); 
         arrowDn[i]   = iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,RsxLength,RsxPrice,MinMaxPeriod,overBought,overSold,alertsOn,alertsOnCurrent,alertsMessage,alertsSound,alertsNotify,alertsEmail,soundFile,7,y);
       }
       else
       {
         arrowUp[i]   = EMPTY_VALUE;
         arrowDn[i]   = EMPTY_VALUE;
       }
           
         //
         //
         //
         //
         //
               
         if (!Interpolate || y==iBarShift(NULL,TimeFrame,Time[i-1])) continue;

         //
         //
         //
         //
         //
 
         datetime time = iTime(NULL,TimeFrame,y);
            for(int n = 1; i+n < Bars && Time[i+n] >= time; n++) continue;	
            for(int j = 1; j < n; j++)
            {
               Buffer1[i+j] = Buffer1[i] + (Buffer1[i+n] - Buffer1[i]) * j/n;
               Buffer2[i+j] = Buffer2[i] + (Buffer2[i+n] - Buffer2[i]) * j/n;
               Buffer3[i+j] = Buffer3[i] + (Buffer3[i+n] - Buffer3[i]) * j/n;
               Buffer4[i+j] = Buffer4[i] + (Buffer4[i+n] - Buffer4[i]) * j/n;             
           }          
    }
    for (i=limit;i>=0;i--) if (slope[i] == -1) PlotPoint(i,Buffer4Da,Buffer4Db,Buffer4);  
return(0);
}

//+-------------------------------------------------------------------
//|                                                                  
//+-------------------------------------------------------------------
//
//
//
//
//

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("");
}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

void CleanPoint(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;
      }
}

//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//
//

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 =  StringConcatenate(Symbol()," ",timeFrameToString(TimeFrame)," at ",TimeToStr(TimeLocal(),TIME_SECONDS)," OnChart Rsx ",doWhat);
             if (alertsMessage) Alert(message);
             if (alertsNotify)  SendNotification(message);
             if (alertsEmail)   SendMail(StringConcatenate(Symbol()," OnChart Rsx "),message);
             if (alertsSound)   PlaySound(soundFile);
      }
}

  