

//*********************************************************************
          
//                 RSI with trend, colours and levels
//                     by ANGUS
                  
//*********************************************************************
#property  copyright "Copyright © 2010, ANGUS"
#property  link      "http://www.forexfactory.com/"

#property indicator_separate_window
#property indicator_buffers 6

#property indicator_color1 White
#property indicator_color2 Lime
#property indicator_color3 Red
#property indicator_color4 Lime
#property indicator_color5 Red
#property indicator_color6 White

extern string TimeFrame          = "Current time frame";
extern int       RSIPeriod=20;
extern int       DotSize =2;
extern int       UpLineValue=55;
extern int       DownLineValue=45;
extern color     UpLineColour=Black;
extern color     DownLineColour=Black;
extern color     MidLineColour=Silver;
extern int       LineStyle = 2;


double UpRSI[];
double DnRSI[];
double NoRSI[];
double UpLnRSI[];
double DnLnRSI[];
double NoLnRSI[];

string indicatorFileName;
bool   returnBars;
int    timeFrame;

int init() 
{

   IndicatorBuffers(6);
   
   
   SetIndexStyle(0,DRAW_LINE,0,1);
   SetIndexBuffer(0,NoLnRSI);
   
   SetIndexStyle(1,DRAW_LINE,0,1);
   SetIndexBuffer(1,UpLnRSI);
   
   SetIndexStyle(2,DRAW_LINE,0,1);
   SetIndexBuffer(2,DnLnRSI);
   
   SetIndexStyle(3,DRAW_ARROW,0,DotSize);
   SetIndexBuffer(3,UpRSI);
   SetIndexArrow(3,159);
   
   SetIndexStyle(4,DRAW_ARROW,0,DotSize);
   SetIndexBuffer(4,DnRSI);
   SetIndexArrow(4,159);
   
   SetIndexStyle(5,DRAW_ARROW,0,DotSize);
   SetIndexBuffer(5,NoRSI);
   SetIndexArrow(5,159);
   
   
          
   IndicatorShortName("RSI-TREND("+RSIPeriod+")");
   IndicatorDigits(2);
   
     indicatorFileName = WindowExpertName();
     timeFrame         = stringToTimeFrame(TimeFrame);
     returnBars        = (TimeFrame=="returnBars");
   
   return(0);
   
}


int start() 
{

   int 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)  { NoLnRSI[0] = limit+1; return(0); }
         if (timeFrame!=Period())
         {
               limit = (int)MathMax(limit,MathMin(Bars-1,iCustom(NULL,timeFrame,indicatorFileName,"returnBars",0,0)*timeFrame/Period()));
               for(int i=limit; i>=0; i--)
               {
                  int y = iBarShift(NULL,timeFrame,Time[i]);
                  int x = iBarShift(NULL,timeFrame,Time[i+1]);
                     NoLnRSI[i] = iCustom(NULL,timeFrame,indicatorFileName,"",RSIPeriod,DotSize,UpLineValue,DownLineValue,UpLineColour,DownLineColour,MidLineColour,LineStyle,0,y);
                     UpLnRSI[i] = iCustom(NULL,timeFrame,indicatorFileName,"",RSIPeriod,DotSize,UpLineValue,DownLineValue,UpLineColour,DownLineColour,MidLineColour,LineStyle,1,y);
                     DnLnRSI[i] = iCustom(NULL,timeFrame,indicatorFileName,"",RSIPeriod,DotSize,UpLineValue,DownLineValue,UpLineColour,DownLineColour,MidLineColour,LineStyle,2,y);
                     if (x!=y)
                     {
                        UpRSI[i] = iCustom(NULL,timeFrame,indicatorFileName,"",RSIPeriod,DotSize,UpLineValue,DownLineValue,UpLineColour,DownLineColour,MidLineColour,LineStyle,3,y);
                        DnRSI[i] = iCustom(NULL,timeFrame,indicatorFileName,"",RSIPeriod,DotSize,UpLineValue,DownLineValue,UpLineColour,DownLineColour,MidLineColour,LineStyle,4,y);
                        NoRSI[i] = iCustom(NULL,timeFrame,indicatorFileName,"",RSIPeriod,DotSize,UpLineValue,DownLineValue,UpLineColour,DownLineColour,MidLineColour,LineStyle,5,y);
                     }
                     else
                     {
                        UpRSI[i] = EMPTY_VALUE;
                        DnRSI[i] = EMPTY_VALUE;
                        NoRSI[i] = EMPTY_VALUE;
                     }
            }
            return(0);
         }
         
            
   
   //
   //
   //
   //
   //
   
   int WindowIndex=WindowFind("RSI-TREND("+RSIPeriod+")");
   double RSIVal;
   static datetime prevtime = 0;
   
   for (int shift=limit;shift>=0;shift--)   
   {
            
      RSIVal=iRSI(NULL,0,RSIPeriod,PRICE_CLOSE,shift);
      
      /// Colour coding
   
 UpRSI[shift]=RSIVal;//Green
 DnRSI[shift]=RSIVal;//Red
 NoRSI[shift]=RSIVal;//White  
 UpLnRSI[shift]=RSIVal;//Green
 DnLnRSI[shift]=RSIVal;//Red
 NoLnRSI[shift]=RSIVal;//White 
      
      if (RSIVal > UpLineValue)
       {
       DnRSI[shift]=EMPTY_VALUE;NoRSI[shift]=EMPTY_VALUE;
       DnLnRSI[shift]=EMPTY_VALUE;//NoLnRSI[shift]=EMPTY_VALUE;
       }
       
 else if (RSIVal < UpLineValue && RSIVal > DownLineValue)
       {
       DnRSI[shift]=EMPTY_VALUE;UpRSI[shift]=EMPTY_VALUE;
       DnLnRSI[shift]=EMPTY_VALUE;UpLnRSI[shift]=EMPTY_VALUE;
       }
 else 
       { 
       NoRSI[shift]=EMPTY_VALUE;UpRSI[shift]=EMPTY_VALUE;
       UpLnRSI[shift]=EMPTY_VALUE;//NoLnRSI[shift]=EMPTY_VALUE;
       }
      

   }    /// Lines and levels
    
     ObjectCreate( "RSI-Up", OBJ_HLINE, WindowIndex, 0, UpLineValue );
    ObjectSet( "RSI-Up", OBJPROP_COLOR, UpLineColour );
    ObjectSet( "RSI-Up", OBJPROP_STYLE, LineStyle);
    ObjectSet( "RSI-Up", OBJPROP_BACK, true );  
    
     ObjectCreate( "RSI-Mid", OBJ_HLINE, WindowIndex, 0, 50 );
    ObjectSet( "RSI-Mid", OBJPROP_COLOR, MidLineColour );
    ObjectSet( "RSI-Mid", OBJPROP_STYLE, LineStyle);
    ObjectSet( "RSI-Mid", OBJPROP_BACK, true );    
    
     ObjectCreate( "RSI-Low", OBJ_HLINE, WindowIndex, 0, DownLineValue );
    ObjectSet( "RSI-Low", OBJPROP_COLOR, DownLineColour );
    ObjectSet( "RSI-Low", OBJPROP_STYLE, LineStyle);
    ObjectSet( "RSI-Low", OBJPROP_BACK, true );      
      
   return(0);
   
}     /// Delete all lines

int deinit()
  {

   int WindowIndex=WindowFind("RSI-TREND("+RSIPeriod+")");
   
   ObjectsDeleteAll(WindowIndex,OBJ_HLINE); 

   return(0);
   
  } 
  //-------------------------------------------------------------------
//
//-------------------------------------------------------------------
//
//
//
//
//

string sTfTable[] = {"M1","M5","M15","M30","H1","H4","D1","W1","MN"};
int    iTfTable[] = {1,5,15,30,60,240,1440,10080,43200};

//
//
//
//
//

int stringToTimeFrame(string tfs)
{
   StringToUpper(tfs);
   for (int i=ArraySize(iTfTable)-1; i>=0; i--)
         if (tfs==sTfTable[i] || tfs==""+(string)iTfTable[i]) return(MathMax(iTfTable[i],Period()));
                                                      return(Period());
}
string timeFrameToString(int tf)
{
   for (int i=ArraySize(iTfTable)-1; i>=0; i--) 
         if (tf==iTfTable[i]) return(sTfTable[i]);
                              return("");
}
