//+-------------------------------------------------------------------------+
//+-------------------------------------------------------------------------+
#property copyright "www.forex-station.com"
#property link      "www.forex-station.com"

#property indicator_separate_window
#property indicator_buffers 6
#property indicator_color1 clrSilver
#property indicator_color6  clrLightYellow

#property indicator_style6  STYLE_DOT


#property indicator_width1  2
#property indicator_width2  2
#property indicator_width3  2



enum enRsiTypes
{
   rsi_rsi,  // Regular RSI
   rsi_wil,  // Wilders' RSI
   rsi_rap,  // Rapid RSI
   rsi_har,  // Harris RSI
   rsi_rsx,  // RSX
   rsi_cut   // Cuttlers RSI
};
//
//
enum enTimeFrames
{
   tf_cu  = PERIOD_CURRENT, // Current time frame
   tf_m1  = PERIOD_M1,      // 1 minute
   tf_m5  = PERIOD_M5,      // 5 minutes
   tf_m15 = PERIOD_M15,     // 15 minutes
   tf_m30 = PERIOD_M30,     // 30 minutes
   tf_h1  = PERIOD_H1,      // 1 hour
   tf_h4  = PERIOD_H4,      // 4 hours
   tf_d1  = PERIOD_D1,      // Daily
   tf_w1  = PERIOD_W1,      // Weekly
   tf_mn1 = PERIOD_MN1,     // Monthly
   tf_n1  = -1,             // First higher time frame
   tf_n2  = -2,             // Second higher time frame
   tf_n3  = -3              // Third higher time frame
};
//
enum enPrices
{
   pr_close,      // Close
   pr_open,       // Open
   pr_high,       // High
   pr_low,        // Low
   pr_median,     // Median
   pr_typical,    // Typical
   pr_weighted,   // Weighted
   pr_average,    // Average (high+low+open+close)/4
   pr_medianb,    // Average median body (open+close)/2
   pr_tbiased,    // Trend biased price
   pr_tbiased2,   // Trend biased (extreme) price
   pr_haclose,    // Heiken ashi close
   pr_haopen ,    // Heiken ashi open
   pr_hahigh,     // Heiken ashi high
   pr_halow,      // Heiken ashi low
   pr_hamedian,   // Heiken ashi median
   pr_hatypical,  // Heiken ashi typical
   pr_haweighted, // Heiken ashi weighted
   pr_haaverage,  // Heiken ashi average
   pr_hamedianb,  // Heiken ashi median body
   pr_hatbiased,  // Heiken ashi trend biased price
   pr_hatbiased2, // Heiken ashi trend biased (extreme) price
   pr_habclose,   // Heiken ashi (better formula) close
   pr_habopen ,   // Heiken ashi (better formula) open
   pr_habhigh,    // Heiken ashi (better formula) high
   pr_hablow,     // Heiken ashi (better formula) low
   pr_habmedian,  // Heiken ashi (better formula) median
   pr_habtypical, // Heiken ashi (better formula) typical
   pr_habweighted,// Heiken ashi (better formula) weighted
   pr_habaverage, // Heiken ashi (better formula) average
   pr_habmedianb, // Heiken ashi (better formula) median body
   pr_habtbiased, // Heiken ashi (better formula) trend biased price
   pr_habtbiased2 // Heiken ashi (better formula) trend biased (extreme) price
};
//
enum enMaTypes
{
   ma_sma,    // Simple moving average
   ma_ema,    // Exponential moving average
   ma_smma,   // Smoothed MA
   ma_lwma,   // Linear weighted MA
   ma_tema    // Tripple exponential moving average
};
//

enum enArrowOn
{
   cc_onRSIcrosslevel,   // Color on RSI cross level
   cc_RSIcrossMA,  // Color on RSI cross MA 
   cc_onSLOPE,   // Color on RSI slope
  
};



 extern enTimeFrames TimeFrame               = tf_cu;      // Time frame
extern int             RsiPeriod      = 14;             // RSI period
extern enRsiTypes      RsiType            = rsi_rsi;
extern enPrices        RsiPrice        = pr_close;       // Price to use
extern int                AveragePeriod      = 21;             // Average period
extern enMaTypes          AverageType        = ma_ema;         // Average type for RSI signal line
extern bool              ShowAverage = true;            // MA are visible?



 extern bool              ShowHorlev = true;            // Horizontal Levels are visible?
extern color           Level_color_lines                = clrGray;      // Levels color
extern double LevelUp                =  60;
extern double LevelDown              = 40;

extern color           Up_color_lines                = clrSeaGreen;      // Up color
extern color           Dn_color_lines                = clrCrimson;      // Down  color
extern int             LineWidth    =   2;                // Lines width
extern enArrowOn       ColorOn      = cc_RSIcrossMA;     // Arrows on:
extern bool   Interpolate      = true;

extern bool               alertsOn         = false;
extern bool               alertsOnCurrent  = true;
extern bool               alertsMessage    = true;
extern bool               alertsSound      = false;

extern bool               alertsEmail      = false;

input bool            arrowsVisible             = false;              // Arrows visible true/false?
input string          arrowsIdentifier          = " RSI X";     // Unique ID for arrows
input bool            arrowsOnNewest            = false;
input double          arrowsUpperGap            = 2;                // Upper arrow gap
input double          arrowsLowerGap            = 0.5;                // Lower arrow gap
input color           arrowsUpColor             = clrMediumSeaGreen;       // Up arrow color
input color           arrowsDnColor             = clrCrimson;          // Down arrow color
input int             arrowsUpCode              = 159;                // Up arrow code
input int             arrowsDnCode              = 159;                // Down arrow code
input int             arrowsUpSize              = 2;                  // Up arrow size
input int             arrowsDnSize              = 2;                  // Down arrow size

input bool            ShowZones_MC         = false;             // Display the background zones on the main chart
input bool            ShowZones_SW1        = false;            // Display the background zones in the first separated window
input bool            ShowZones_SW2        = false;            // Display the background zones in the second separated window
input bool            ShowZones_SW3        = false;            // Display the background zones in the third separated window
input color           ZoneColorUp          = clrMediumSeaGreen;  // Uptrend Zone color
input color           ZoneColorDn          = clrSalmon;        // Downtrend Zone color
input color           ZoneColorNe         = clrGray;        // Downtrend Zone color

input string          UniqueZoneID         = "RSI Xzones1";      // Unique ID for the zones


//
extern string             button_note1          = "------------------------------";
extern ENUM_BASE_CORNER   btn_corner            = CORNER_LEFT_UPPER; 
extern string             btn_text              = "RSI  X";
extern string             btn_Font              = "Arial";
extern int                btn_FontSize          = 10;                            
extern color              btn_text_ON_color     = clrLime;
extern color              btn_text_OFF_color    = clrRed;
extern string             btn_pressed           = "RSI X OFF";            
extern string             btn_unpressed         = "RSI X ON";
extern color              btn_background_color  = clrDimGray;
extern color              btn_border_color      = clrBlack;
extern int                button_x              = 900;                                 
extern int                button_y              = 0;                                   
extern int                btn_Width             = 85;                                 
extern int                btn_Height            = 20;                                
extern string             soundBT               = "tick.wav";  
extern string             button_note2          = "------------------------------";

bool                      show_data             = true;
string IndicatorName, IndicatorObjPrefix ,buttonId ;
//
string _avgNames[]={"SMA","EMA","SMMA","LWMA","TEMA"};
//
//

//
//
double rsi[];
double maDa[],maDb[],maUa[],maUb[],avg[];



double trend[],count[];

//
//
//
//
//
int WhatSW1 = 1,WhatSW2 = 2,WhatSW3 = 3;
double Dummy = -1;


string indicatorFileName;
#define _mtfCall(_buff,_ind) iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,RsiPeriod,RsiType,RsiPrice,AveragePeriod,AverageType,ShowAverage,ShowHorlev,Level_color_lines,LevelUp,LevelDown,Up_color_lines,Dn_color_lines,LineWidth,ColorOn,Interpolate,alertsOn,alertsOnCurrent,alertsMessage,alertsSound,alertsEmail,arrowsVisible,arrowsIdentifier,arrowsOnNewest,arrowsUpperGap,arrowsLowerGap,arrowsUpColor,arrowsDnColor,arrowsUpCode,arrowsDnCode,arrowsUpSize,arrowsDnSize,ShowZones_MC,ShowZones_SW1,ShowZones_SW2,ShowZones_SW3,ZoneColorUp,ZoneColorDn,ZoneColorNe,UniqueZoneID,button_note1,btn_corner,btn_text,btn_Font,btn_FontSize,btn_text_ON_color,btn_text_OFF_color,btn_pressed,btn_unpressed,btn_background_color,btn_border_color,button_x,button_y,btn_Width,btn_Height,soundBT,button_note2,_buff,_ind)
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
string GenerateIndicatorName(const string target)
{
   string name = target;
   int try = 2;
   while (WindowFind(name) != -1)
   {
      name = target + " #" + IntegerToString(try++);
   }
   return name;
}
//
//
//

int init()
{
   IndicatorBuffers(8);
   SetIndexBuffer(0,rsi); SetIndexStyle(0,DRAW_LINE,EMPTY,LineWidth);
   SetIndexBuffer(1,maUa); SetIndexStyle(1,DRAW_LINE,EMPTY,LineWidth,Up_color_lines);
   SetIndexBuffer(2,maUb); SetIndexStyle(2,DRAW_LINE,EMPTY,LineWidth,Up_color_lines);
   SetIndexBuffer(3,maDa); SetIndexStyle(3,DRAW_LINE,EMPTY,LineWidth,Dn_color_lines);
   SetIndexBuffer(4,maDb); SetIndexStyle(4,DRAW_LINE,EMPTY,LineWidth,Dn_color_lines);
   
                  if (ShowAverage)
   { 
    SetIndexBuffer(5,avg,INDICATOR_DATA);SetIndexStyle(5,DRAW_LINE);
    }
    else
   {
    SetIndexBuffer(5,avg,INDICATOR_DATA);SetIndexStyle(5,DRAW_NONE);
   }
   
   
   
   
    SetIndexBuffer(6,trend);
   SetIndexBuffer(7,count);
   
    RsiPeriod = fmax(RsiPeriod ,1);
      //
      //
      //
      //
      
      SetLevelValue(0,LevelUp);  
      SetLevelValue(1,LevelDown);
      
                  if (ShowHorlev)
   { 
   SetLevelStyle(STYLE_DOT,1,Level_color_lines);
    }
    else
   {
   SetLevelStyle(STYLE_DOT,1,clrNONE);
   }
   
        indicatorFileName = WindowExpertName();
         TimeFrame         = (enTimeFrames)timeFrameValue(TimeFrame);      
  
   
   IndicatorShortName(timeFrameToString(TimeFrame)+"  RSI 2X ("+RsiPeriod+"),("+AveragePeriod+")");
   
                IndicatorName = GenerateIndicatorName(btn_text);
   IndicatorObjPrefix = "__" + IndicatorName + "__";
   IndicatorShortName(WindowExpertName());
   IndicatorDigits(Digits);
      double val;
   if (GlobalVariableGet(IndicatorName + "_visibility", val))
   show_data = val != 0;

   ChartSetInteger(ChartID(), 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(ChartID(), buttonId, OBJPROP_YDISTANCE, button_y);
   ObjectSetInteger(ChartID(), buttonId, OBJPROP_XDISTANCE, button_x);        
    
    return(0);
}

void createButton(string buttonID,string buttonText,int width,int height,string font,int fontSize,color bgColor,color borderColor,color txtColor)
{
    //  ObjectDelete    (ChartID(),buttonID);
      ObjectCreate (ChartID(),buttonID,OBJ_BUTTON,WindowOnDropped(),0,0);
      ObjectSetInteger(ChartID(),buttonID,OBJPROP_COLOR,txtColor);
      ObjectSetInteger(ChartID(),buttonID,OBJPROP_BGCOLOR,bgColor);
      ObjectSetInteger(ChartID(),buttonID,OBJPROP_BORDER_COLOR,borderColor);
      ObjectSetInteger(ChartID(),buttonID,OBJPROP_XSIZE,width);
      ObjectSetInteger(ChartID(),buttonID,OBJPROP_YSIZE,height);
      ObjectSetString (ChartID(),buttonID,OBJPROP_FONT,font);
      ObjectSetString (ChartID(),buttonID,OBJPROP_TEXT,buttonText);
      ObjectSetInteger(ChartID(),buttonID,OBJPROP_FONTSIZE,fontSize);
      ObjectSetInteger(ChartID(),buttonID,OBJPROP_SELECTABLE,0);
      ObjectSetInteger(ChartID(),buttonID,OBJPROP_CORNER,btn_corner);
      ObjectSetInteger(ChartID(),buttonID,OBJPROP_HIDDEN,1);
      ObjectSetInteger(ChartID(),buttonID,OBJPROP_XDISTANCE,9999);
      ObjectSetInteger(ChartID(),buttonID,OBJPROP_YDISTANCE,9999);
}

int deinit()
{
 deleteArrows(); 

string lookFor       = UniqueZoneID+":";
   int    lookForLength = StringLen(lookFor);
   for (int i=ObjectsTotal()-1; i>=0; i--)
   {
      string objectName = ObjectName(i);
      if (StringSubstr(objectName,0,lookForLength) == lookFor) ObjectDelete(objectName);
   }
   GlobalVariableDel(ChartID()+":"+UniqueZoneID); 
   
    
  ObjectsDeleteAll(0,"RSI  X");
   ObjectsDeleteAll(ChartID(), IndicatorObjPrefix);
   return(0);
}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
 bool recalc = true;
 void handleButtonClicks()
{
   if (ObjectGetInteger(ChartID(), buttonId, OBJPROP_STATE))
   {
      ObjectSetInteger(ChartID(), 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)
   {
   if (soundBT!="") PlaySound(soundBT);     
   }
}
//
//
//

int start()
{
handleButtonClicks();
   recalc = false;
   start2();
   int counted_bars=IndicatorCounted();
   int i,limit;

   if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
         limit = MathMin(Bars-counted_bars,Bars-1);count[0] = limit;
         
         
    SetIndexStyle(0,DRAW_LINE);
    SetIndexStyle(1,DRAW_LINE);
    SetIndexStyle(2,DRAW_LINE);
    SetIndexStyle(3,DRAW_LINE);
    SetIndexStyle(4,DRAW_LINE);
                  if (ShowAverage)
   { 
    SetIndexStyle(5,DRAW_LINE);
    }
    else
   {
    SetIndexStyle(5,DRAW_NONE);
   }
   
                 if (ShowHorlev)
   { 
   SetLevelStyle(STYLE_DOT,1,Level_color_lines);
    }
    else
   {
   SetLevelStyle(STYLE_DOT,1,clrNONE);
   }
   
   start2();
   
  
   
      
   
   if (show_data)
      {
      ObjectSetInteger(ChartID(),buttonId,OBJPROP_COLOR,btn_text_ON_color);
      ObjectSetString(ChartID(),buttonId,OBJPROP_TEXT,btn_unpressed);
     
      }
      else
      {
      ObjectSetInteger(ChartID(),buttonId,OBJPROP_COLOR,btn_text_OFF_color);
      ObjectSetString(ChartID(),buttonId,OBJPROP_TEXT,btn_pressed);
        SetIndexStyle(0,DRAW_NONE);
     SetIndexStyle(1,DRAW_NONE);
     SetIndexStyle(2,DRAW_NONE);
   
    SetIndexStyle(3,DRAW_NONE);
     SetIndexStyle(4,DRAW_NONE);
   SetIndexStyle(5,DRAW_NONE);
   SetLevelStyle(STYLE_DOT,1,clrNONE);
   
   
  string lookFor       = UniqueZoneID+":";
   int    lookForLength = StringLen(lookFor);
   for ( i=ObjectsTotal()-1; i>=0; i--)
   {
      string objectName = ObjectName(i);
      if (StringSubstr(objectName,0,lookForLength) == lookFor) ObjectDelete(objectName);
   }
   GlobalVariableDel(ChartID()+":"+UniqueZoneID);   
    deleteArrows();    
  
      
   
      //template code     
      }
       return(0);
      }
      
       int start2()
  {
  
    int i, limit = Bars;
  datetime tDummy = GlobalVariableGet(ChartID()+":"+UniqueZoneID); if (tDummy==0) tDummy = Time[0];  
        
           
            if (TimeFrame != _Period)
            {
               limit = (int)MathMax(limit,MathMin(Bars-1,_mtfCall(12,0)*TimeFrame/_Period));
              
              if (trend[limit]== 1) CleanPoint(limit,maUa,maUb);
               if (trend[limit]==-1) CleanPoint(limit,maDa,maDb);
             
             
   
               for( i=limit; i>=0 && !_StopFlag; i--)
               {
                  int y = iBarShift(NULL,TimeFrame,Time[i]);
                 
                      rsi[i]    = _mtfCall(0,y);
                     
                    
                      avg[i] = _mtfCall(5,y);
                     
                      trend[i] = _mtfCall(6,y);
                  
                        
                        maUa[i] = EMPTY_VALUE;
                     maUb[i] = EMPTY_VALUE;
                     maDa[i] = EMPTY_VALUE;
                     maDb[i] = EMPTY_VALUE;  
                       
                              
                     //
                     //
                     //
                     //
                     //
                  
                     if (!Interpolate || (i>0 && y==iBarShift(NULL,TimeFrame,Time[i-1]))) continue;
                        #define _interpolate(buff) buff[i+k] = buff[i]+(buff[i+n]-buff[i])*k/n
                        int n,k; datetime ctime = iTime(NULL,TimeFrame,y);
                           for(n = 1; (i+n)<Bars && Time[i+n] >= ctime; n++) continue;
                           for(k = 1; k<n && (i+n)<Bars && (i+k)<Bars; k++) 
                           {
                              
                              _interpolate(rsi);
                              _interpolate(avg);
                              
                              
                              
                           }                              
               }
                     
                      for(i=limit;i>=0; i--)
         {      
            
            
              if (trend[i]== 1) PlotPoint(i,maUa,maUb,rsi);
                  if (trend[i]==-1) PlotPoint(i,maDa,maDb,rsi);
             
             
         
                  
         }
         
                
               return(0);
            }
            
            
   //
   //
   //
   //
   //
   
 
              if (trend[limit]== 1) CleanPoint(limit,maUa,maUb);
   if (trend[limit]==-1) CleanPoint(limit,maDa,maDb);
             
              
      for(i=limit; i>=0; i--)
      { 
       double _price = getPrice(RsiPrice,Open,Close,High,Low,i,Bars);
        double MABuffer1 = iCustomMa(AverageType,_price,RsiPeriod,i,Bars,0); 
         rsi[i] = iRsi(MABuffer1,RsiPeriod,RsiType,i);
         avg[i]   = iCustomMa(AverageType,rsi[i],AveragePeriod,i,Bars,1);
                                        
     
    
    
    switch(ColorOn)
     {
     case cc_onRSIcrosslevel:  trend[i] = (rsi[i] > LevelUp) ? 1 : (rsi[i] < LevelDown) ? -1 :  0;  break;
     case cc_RSIcrossMA    :  trend[i] = (i<Bars-1) ? (rsi[i] > avg[i]) ? 1 : (rsi[i] < avg[i]) ? -1 : trend[i+1] : 0;  break;
     default   : if (i<Bars-1) trend[i] = (i<Bars-1) ? (rsi[i] > rsi[i+1]) ? 1 : (rsi[i] < rsi[i+1]) ? -1 : trend[i+1] : 0; 
     }
         maDa[i] = EMPTY_VALUE;
         maDb[i] = EMPTY_VALUE;
         maUa[i] = EMPTY_VALUE;
         maUb[i] = EMPTY_VALUE;
         if (trend[i]==-1) PlotPoint(i,maDa,maDb,rsi);
         if (trend[i]== 1) PlotPoint(i,maUa,maUb,rsi);
             
                          
     
     
    
     
               if (arrowsVisible)
        {
            string lookFor = arrowsIdentifier+":"+(string)Time[i]; ObjectDelete(lookFor);            
            if (i<(Bars-1) && trend[i] != trend[i+1])
            {
              if (trend[i] == 1) drawArrow(i,arrowsUpColor,arrowsUpCode,arrowsUpSize,false);
              if (trend[i] ==-1) drawArrow(i,arrowsDnColor,arrowsDnCode,arrowsDnSize, true);  
      }
       }
       
         int    index=0;
         for (index=i; index<Bars; index++) if (trend[index]!= trend[index+1]) break;
         if (ShowZones_MC)
         {
            string   name     = UniqueZoneID+":MainChart"+Time[index+1]; ObjectDelete(name); ObjectDelete(UniqueZoneID+":"+Time[1]);
          datetime   lastTime = Time[i-1]; if (i==0) { lastTime = tDummy; if (Time[index]==lastTime) lastTime = Time[0]+Period()*60;}
            ObjectCreate(name,OBJ_RECTANGLE,0,Time[index],0,lastTime,WindowPriceMax()*3.0);
            ObjectSet(name,OBJPROP_BACK,true);
            if (trend[i]==-1) ObjectSet(name,OBJPROP_COLOR,ZoneColorDn);
            if (trend[i]==1) ObjectSet(name,OBJPROP_COLOR,ZoneColorUp);
            if (trend[i]==0) ObjectSet(name,OBJPROP_COLOR,ZoneColorNe);   
          }
          if (ShowZones_SW1)
          {
          string   name2 = UniqueZoneID+":aboveZero1"+Time[index+1]; ObjectDelete(name2); ObjectDelete(UniqueZoneID+":"+Time[1]);             
          datetime   lastTime2 = Time[i-1]; if (i==0) { lastTime2 = tDummy; if (Time[index]==lastTime2) lastTime2 = Time[0]+Period()*60;}
            ObjectCreate(name2,OBJ_RECTANGLE,WhatSW1,Time[index],0,lastTime2,1000);
               ObjectSet(name2,OBJPROP_BACK,true);
            if (trend[i]==-1) ObjectSet(name2,OBJPROP_COLOR,ZoneColorDn);
            if (trend[i]==1) ObjectSet(name2,OBJPROP_COLOR,ZoneColorUp);
            if (trend[i]==0) ObjectSet(name2,OBJPROP_COLOR,ZoneColorNe);   

           string  name3 = UniqueZoneID+":belowZero1"+Time[index+1]; ObjectDelete(name3); ObjectDelete(UniqueZoneID+":"+Time[1]);             
           datetime  lastTime3 = Time[i-1]; if (i==0) { lastTime3 = tDummy; if (Time[index]==lastTime3) lastTime3 = Time[0]+Period()*60;}
            ObjectCreate(name3,OBJ_RECTANGLE,WhatSW1,Time[index],0,lastTime3,-1000);
               ObjectSet(name3,OBJPROP_BACK,true);
            if (trend[i]==-1) ObjectSet(name3,OBJPROP_COLOR,ZoneColorDn);
            if (trend[i]==1) ObjectSet(name3,OBJPROP_COLOR,ZoneColorUp);
            if (trend[i]==0) ObjectSet(name3,OBJPROP_COLOR,ZoneColorNe);   
           }
           if (ShowZones_SW2)
           {
            string   name4 = UniqueZoneID+":aboveZero2"+Time[index+1]; ObjectDelete(name4); ObjectDelete(UniqueZoneID+":"+Time[1]);             
             datetime  lastTime4 = Time[i-1]; if (i==0) { lastTime4 = tDummy; if (Time[index]==lastTime4) lastTime4 = Time[0]+Period()*60;}
              ObjectCreate(name4,OBJ_RECTANGLE,WhatSW2,Time[index],0,lastTime4,1000);
                 ObjectSet(name4,OBJPROP_BACK,true);
              if (trend[i]==-1) ObjectSet(name4,OBJPROP_COLOR,ZoneColorDn);
            if (trend[i]==1) ObjectSet(name4,OBJPROP_COLOR,ZoneColorUp);
            if (trend[i]==0) ObjectSet(name4,OBJPROP_COLOR,ZoneColorNe);   

            string   name5 = UniqueZoneID+":belowZero2"+Time[index+1]; ObjectDelete(name5); ObjectDelete(UniqueZoneID+":"+Time[1]);             
            datetime   lastTime5 = Time[i-1]; if (i==0) { lastTime5 = tDummy; if (Time[index]==lastTime5) lastTime5 = Time[0]+Period()*60;}
              ObjectCreate(name5,OBJ_RECTANGLE,WhatSW2,Time[index],0,lastTime5,-1000);
                 ObjectSet(name5,OBJPROP_BACK,true);
              if (trend[i]==-1) ObjectSet(name5,OBJPROP_COLOR,ZoneColorDn);
            if (trend[i]==1) ObjectSet(name5,OBJPROP_COLOR,ZoneColorUp);
            if (trend[i]==0) ObjectSet(name5,OBJPROP_COLOR,ZoneColorNe);   
             }
             if (ShowZones_SW3)
             {
             string   name6 = UniqueZoneID+":aboveZero3"+Time[index+1]; ObjectDelete(name6); ObjectDelete(UniqueZoneID+":"+Time[1]);             
             datetime   lastTime6 = Time[i-1]; if (i==0) { lastTime6 = tDummy; if (Time[index]==lastTime6) lastTime6 = Time[0]+Period()*60;}
               ObjectCreate(name6,OBJ_RECTANGLE,WhatSW3,Time[index],0,lastTime6,1000);
                  ObjectSet(name6,OBJPROP_BACK,true);
               if (trend[i]==-1) ObjectSet(name6,OBJPROP_COLOR,ZoneColorDn);
            if (trend[i]==1) ObjectSet(name6,OBJPROP_COLOR,ZoneColorUp);
            if (trend[i]==0) ObjectSet(name6,OBJPROP_COLOR,ZoneColorNe);   

             string   name7 = UniqueZoneID+":belowZero3"+Time[index+1]; ObjectDelete(name7); ObjectDelete(UniqueZoneID+":"+Time[1]);             
             datetime   lastTime7 = Time[i-1]; if (i==0) { lastTime7 = tDummy; if (Time[index]==lastTime7) lastTime7 = Time[0]+Period()*60;}
               ObjectCreate(name7,OBJ_RECTANGLE,WhatSW3,Time[index],0,lastTime7,-1000);
                  ObjectSet(name7,OBJPROP_BACK,true);
               if (trend[i]==-1) ObjectSet(name7,OBJPROP_COLOR,ZoneColorDn);
            if (trend[i]==1) ObjectSet(name7,OBJPROP_COLOR,ZoneColorUp);
            if (trend[i]==0) ObjectSet(name7,OBJPROP_COLOR,ZoneColorNe);   
            }  


         manageAlerts();
       
        
            
            
           
      }   
     
      return(0);
   }
   
   //
   //
   //
   //
   //
   
  
 

   //
   //
   //
   //
   //
   
  

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//


#define _maInstances 2
#define _maWorkBufferx1 _maInstances
#define _maWorkBufferx3 _maInstances*3
double iCustomMa(int mode, double price, double length, int r, int bars, int instanceNo=0)
{
   bars = Bars; r=bars-r-1;
   switch (mode)
   {
      case ma_sma   : return(iSma(price,(int)length,r,bars,instanceNo));
      case ma_ema   : return(iEma(price,length,r,bars,instanceNo));
      case ma_smma  : return(iSmma(price,(int)length,r,bars,instanceNo));
      case ma_lwma  : return(iLwma(price,(int)length,r,bars,instanceNo));
      case ma_tema  : return(iTema(price,(int)length,r,bars,instanceNo));
      default       : return(price);
   }
}

//
//
//
//
//

double workSma[][_maWorkBufferx1];
double iSma(double price, int period, int r, int _bars, int instanceNo=0)
{
   if (ArrayRange(workSma,0)!= _bars) ArrayResize(workSma,_bars);

   workSma[r][instanceNo+0] = price;
   double sma = price;  int k=1; for(; k<period && (r-k)>=0; k++) sma += workSma[r-k][instanceNo+0];  sma /= (double)k;
   return(sma);
}

//
//
//
//
//

double workEma[][_maWorkBufferx1];
double iEma(double price, double period, int r, int _bars, int instanceNo=0)
{
   if (ArrayRange(workEma,0)!= _bars) ArrayResize(workEma,_bars);

   workEma[r][instanceNo] = price;
   if (r>0 && period>1)
          workEma[r][instanceNo] = workEma[r-1][instanceNo]+(2.0/(1.0+period))*(price-workEma[r-1][instanceNo]);
   return(workEma[r][instanceNo]);
}

//
//
//
//
//

double workSmma[][_maWorkBufferx1];
double iSmma(double price, double period, int r, int _bars, int instanceNo=0)
{
   if (ArrayRange(workSmma,0)!= _bars) ArrayResize(workSmma,_bars);

   workSmma[r][instanceNo] = price;
   if (r>1 && period>1)
          workSmma[r][instanceNo] = workSmma[r-1][instanceNo]+(price-workSmma[r-1][instanceNo])/period;
   return(workSmma[r][instanceNo]);
}

//
//
//
//
//

double workLwma[][_maWorkBufferx1];
double iLwma(double price, double period, int r, int _bars, int instanceNo=0)
{
   if (ArrayRange(workLwma,0)!= _bars) ArrayResize(workLwma,_bars);
   
   workLwma[r][instanceNo] = price; if (period<1) return(price);
      double sumw = period;
      double sum  = period*price;

      for(int k=1; k<period && (r-k)>=0; k++)
      {
         double weight = period-k;
                sumw  += weight;
                sum   += weight*workLwma[r-k][instanceNo];  
      }             
      return(sum/sumw);
}

//
//
//
//
//

double workTema[][_maWorkBufferx3];
#define _tema1 0
#define _tema2 1
#define _tema3 2

double iTema(double price, double period, int r, int bars, int instanceNo=0)
{
   if (ArrayRange(workTema,0)!= bars) ArrayResize(workTema,bars); instanceNo*=3;

   //
   //
   //
   //
   //
      
   workTema[r][_tema1+instanceNo] = price;
   workTema[r][_tema2+instanceNo] = price;
   workTema[r][_tema3+instanceNo] = price;
   if (r>0 && period>1)
   {
      double alpha = 2.0 / (1.0+period);
          workTema[r][_tema1+instanceNo] = workTema[r-1][_tema1+instanceNo]+alpha*(price                         -workTema[r-1][_tema1+instanceNo]);
          workTema[r][_tema2+instanceNo] = workTema[r-1][_tema2+instanceNo]+alpha*(workTema[r][_tema1+instanceNo]-workTema[r-1][_tema2+instanceNo]);
          workTema[r][_tema3+instanceNo] = workTema[r-1][_tema3+instanceNo]+alpha*(workTema[r][_tema2+instanceNo]-workTema[r-1][_tema3+instanceNo]); }
   return(workTema[r][_tema3+instanceNo]+3.0*(workTema[r][_tema1+instanceNo]-workTema[r][_tema2+instanceNo]));
}  
 
//
//
//
//
//

 string rsiMethodNames[] = {"RSI","Wilders' RSI","Rapid RSI","Harris RSI","RSX","Cuttler RSI"};
string getRsiName(int method)
{
   int max = ArraySize(rsiMethodNames)-1;
      method=MathMax(MathMin(method,max),0); return(rsiMethodNames[method]);
}

//
//
//
//
//

double workRsi[][26];
#define _price  0
#define _change 1
#define _changa 2
#define _rsival 1
#define _rsval  1

double iRsi(double price, double period, int rsiMode, int i, int instanceNo=0)
{
   if (ArrayRange(workRsi,0)!=Bars) ArrayResize(workRsi,Bars);
      int z = instanceNo*13; 
      int r = Bars-i-1;
   
   //
   //
   //
   //
   //
   
   workRsi[r][z+_price] = price;
   switch (rsiMode)
   {
      case 0:
         double alpha = 1.0/period; 
         if (r<period)
            {
               int k; double sum = 0; for (k=0; k<period && (r-k-1)>=0; k++) sum += MathAbs(workRsi[r-k][z+_price]-workRsi[r-k-1][z+_price]);
                  workRsi[r][z+_change] = (workRsi[r][z+_price]-workRsi[0][z+_price])/MathMax(k,1);
                  workRsi[r][z+_changa] =                                         sum/MathMax(k,1);
            }
         else
            {
               double change = workRsi[r][z+_price]-workRsi[r-1][z+_price];
                               workRsi[r][z+_change] = workRsi[r-1][z+_change] + alpha*(        change  - workRsi[r-1][z+_change]);
                               workRsi[r][z+_changa] = workRsi[r-1][z+_changa] + alpha*(MathAbs(change) - workRsi[r-1][z+_changa]);
            }
         if (workRsi[r][z+_changa] != 0)
               return(50.0*(workRsi[r][z+_change]/workRsi[r][z+_changa]+1));
         else  return(50.0);
         
      //
      //
      //
      //
      //
      
      case 1 :
         double up = 0;
         double dn = 0;
         for(k=0; k<(int)period && (r-k-1)>=0; k++)
         {
            double diff = workRsi[r-k][z+_price]- workRsi[r-k-1][z+_price];
            if(diff>0)
                  up += diff;
            else  dn -= diff;
         }
         if(up + dn == 0)
               workRsi[r][z+_rsival] = workRsi[r-1][z+_rsival]+(1/period)*(50            -workRsi[r-1][z+_rsival]);
         else  workRsi[r][z+_rsival] = workRsi[r-1][z+_rsival]+(1/period)*(100*up/(up+dn)-workRsi[r-1][z+_rsival]);
         return(workRsi[r][z+_rsival]);      

      //
      //
      //
      //
      //

      case 2 :
         up = 0;
         dn = 0;
         for(k=0; k<(int)period && (r-k-1)>=0; k++)
         {
            diff = workRsi[r-k][z+_price]- workRsi[r-k-1][z+_price];
            if(diff>0)
                  up += diff;
            else  dn -= diff;
         }
         if(up + dn == 0)
               return(50);
         else  return(100 * up / (up + dn));      

      //
      //
      //
      //
      //

      
      case 3 :
         double avgUp=0,avgDn=0; up=0;dn=0;
         for(k=0; k<(int)period && (r-k-1)>=0; k++)
         {
            diff = workRsi[r-k][instanceNo+_price]- workRsi[r-k-1][instanceNo+_price];
            if(diff>0)
                  { avgUp += diff; up++; }
            else  { avgDn -= diff; dn++; }
         }
         if (up!=0) avgUp /= up;
         if (dn!=0) avgDn /= dn;
         double rs = 1;
            if (avgDn!=0) rs = avgUp/avgDn;
            return(100-100/(1.0+rs));

      //
      //
      //
      //
      //
      
      case 4 :     
         double Kg = (3.0)/(2.0+period), Hg = 1.0-Kg;
         if (r<period) { for (k=1; k<13; k++) workRsi[r][k+z] = 0; return(50); }  

         //
         //
         //
         //
         //
      
         double mom = workRsi[r][_price+z]-workRsi[r-1][_price+z];
         double moa = MathAbs(mom);
         for (k=0; k<3; k++)
         {
            int kk = k*2;
            workRsi[r][z+kk+1] = Kg*mom                + Hg*workRsi[r-1][z+kk+1];
            workRsi[r][z+kk+2] = Kg*workRsi[r][z+kk+1] + Hg*workRsi[r-1][z+kk+2]; mom = 1.5*workRsi[r][z+kk+1] - 0.5 * workRsi[r][z+kk+2];
            workRsi[r][z+kk+7] = Kg*moa                + Hg*workRsi[r-1][z+kk+7];
            workRsi[r][z+kk+8] = Kg*workRsi[r][z+kk+7] + Hg*workRsi[r-1][z+kk+8]; moa = 1.5*workRsi[r][z+kk+7] - 0.5 * workRsi[r][z+kk+8];
         }
         if (moa != 0)
              return(MathMax(MathMin((mom/moa+1.0)*50.0,100.00),0.00)); 
         else return(50);
            
      //
      //
      //
      //
      //
      
      case 5 :
         double sump = 0;
         double sumn = 0;
         for (k=0; k<(int)period; k++)
         {
            diff = workRsi[r-k][z+_price]-workRsi[r-k-1][z+_price];
               if (diff > 0) sump += diff;
               if (diff < 0) sumn -= diff;
         }
         if (sumn > 0)
               return(100.0-100.0/(1.0+sump/sumn));
         else  return(50);
   } 
   return(0);
}
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//

//
//
#define _prHABF(_prtype) (_prtype>=pr_habclose && _prtype<=pr_habtbiased2)
#define _priceInstances     1
#define _priceInstancesSize 4
double workHa[][_priceInstances*_priceInstancesSize];
double getPrice(int tprice, const double& open[], const double& close[], const double& high[], const double& low[], int i, int bars, int instanceNo=0)
{
  if (tprice>=pr_haclose)
   {
      if (ArrayRange(workHa,0)!= bars) ArrayResize(workHa,bars); instanceNo*=_priceInstancesSize; int r = bars-i-1;
         
         //
         //
         //
         //
         //
         
         double haOpen  = (r>0) ? (workHa[r-1][instanceNo+2] + workHa[r-1][instanceNo+3])/2.0 : (open[i]+close[i])/2;;
         double haClose = (open[i]+high[i]+low[i]+close[i]) / 4.0;
         if (_prHABF(tprice))
               if (high[i]!=low[i])
                     haClose = (open[i]+close[i])/2.0+(((close[i]-open[i])/(high[i]-low[i]))*fabs((close[i]-open[i])/2.0));
               else  haClose = (open[i]+close[i])/2.0; 
         double haHigh  = fmax(high[i], fmax(haOpen,haClose));
         double haLow   = fmin(low[i] , fmin(haOpen,haClose));

         //
         //
         //
         //
         //
         
         if(haOpen<haClose) { workHa[r][instanceNo+0] = haLow;  workHa[r][instanceNo+1] = haHigh; } 
         else               { workHa[r][instanceNo+0] = haHigh; workHa[r][instanceNo+1] = haLow;  } 
                              workHa[r][instanceNo+2] = haOpen;
                              workHa[r][instanceNo+3] = haClose;
         //
         //
         //
         //
         //
         
         switch (tprice)
         {
            case pr_haclose:
            case pr_habclose:    return(haClose);
            case pr_haopen:   
            case pr_habopen:     return(haOpen);
            case pr_hahigh: 
            case pr_habhigh:     return(haHigh);
            case pr_halow:    
            case pr_hablow:      return(haLow);
            case pr_hamedian:
            case pr_habmedian:   return((haHigh+haLow)/2.0);
            case pr_hamedianb:
            case pr_habmedianb:  return((haOpen+haClose)/2.0);
            case pr_hatypical:
            case pr_habtypical:  return((haHigh+haLow+haClose)/3.0);
            case pr_haweighted:
            case pr_habweighted: return((haHigh+haLow+haClose+haClose)/4.0);
            case pr_haaverage:  
            case pr_habaverage:  return((haHigh+haLow+haClose+haOpen)/4.0);
            case pr_hatbiased:
            case pr_habtbiased:
               if (haClose>haOpen)
                     return((haHigh+haClose)/2.0);
               else  return((haLow+haClose)/2.0);        
            case pr_hatbiased2:
            case pr_habtbiased2:
               if (haClose>haOpen)  return(haHigh);
               if (haClose<haOpen)  return(haLow);
                                    return(haClose);        
         }
   }
   
   //
   //
   //
   //
   //
   
   switch (tprice)
   {
      case pr_close:     return(close[i]);
      case pr_open:      return(open[i]);
      case pr_high:      return(high[i]);
      case pr_low:       return(low[i]);
      case pr_median:    return((high[i]+low[i])/2.0);
      case pr_medianb:   return((open[i]+close[i])/2.0);
      case pr_typical:   return((high[i]+low[i]+close[i])/3.0);
      case pr_weighted:  return((high[i]+low[i]+close[i]+close[i])/4.0);
      case pr_average:   return((high[i]+low[i]+close[i]+open[i])/4.0);
      case pr_tbiased:   
               if (close[i]>open[i])
                     return((high[i]+close[i])/2.0);
               else  return((low[i]+close[i])/2.0);        
      case pr_tbiased2:   
               if (close[i]>open[i]) return(high[i]);
               if (close[i]<open[i]) return(low[i]);
                                     return(close[i]);        
   }
   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("");
}
int timeFrameValue(int _tf)
{
   int add  = (_tf>=0) ? 0 : MathAbs(_tf);
   if (add != 0) _tf = _Period;
   int size = ArraySize(iTfTable); 
      int i =0; for (;i<size; i++) if (iTfTable[i]==_tf) break;
                                   if (i==size) return(_Period);
                                                return(iTfTable[(int)MathMin(i+add,size-1)]);
}


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//

//
//
void manageAlerts()
{
   if (alertsOn)
   {
      if (alertsOnCurrent)
           int whichBar = 0;
      else     whichBar = 1;
      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 =  Symbol()+" at "+TimeToStr(TimeLocal(),TIME_SECONDS)+" RSI 2X  "+doWhat;
          if (alertsMessage) Alert(message);
          if (alertsEmail)   SendMail(Symbol()+" RSI 2X ",message);
          if (alertsSound)   PlaySound("alert2.wav");
   }
}



void drawArrow(int i,color theColor,int theCode, int theSize, bool up)
{
   string name = arrowsIdentifier+":"+(string)Time[i];
   double gap  = iATR(NULL,0,20,i);   
   
      //
      //
      //
      //
      //

      datetime time = Time[i]; if (arrowsOnNewest) time += _Period*60-1;      
      ObjectCreate(name,OBJ_ARROW,0,Time[i],0);
         ObjectSet(name,OBJPROP_ARROWCODE,theCode);
         ObjectSet(name,OBJPROP_WIDTH,    theSize);
         ObjectSet(name,OBJPROP_COLOR,    theColor);
         if (up)
               ObjectSet(name,OBJPROP_PRICE1,High[i] + arrowsUpperGap * gap);
         else  ObjectSet(name,OBJPROP_PRICE1,Low[i]  - arrowsLowerGap * gap);
}

void deleteArrows()
{
   string lookFor       = arrowsIdentifier+":";
   int    lookForLength = StringLen(lookFor);
   for (int i=ObjectsTotal()-1; i>=0; i--)
   {
      string objectName = ObjectName(i);
         if (StringSubstr(objectName,0,lookForLength) == lookFor) ObjectDelete(objectName);
   }
}

void CleanPoint(int i,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,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; }
}
//
//
//
//
//



//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

      

//
//
//
//
//



//
//
//
//
//

