//+------------------------------------------------------------------+
//|                                                     MTF_RSI.mq4  |
//|                                      Copyright ?2006, Keris2112 |
//|                                            recoded by FOREXflash |
//+------------------------------------------------------------------+
#property copyright "Copyright ?2006, Keris2112, recoded by FOREXflash 2009."
#property link      "http://www.forex-tsd.com"

#property indicator_separate_window
#property indicator_buffers 3
#property indicator_color1 Yellow 
#property indicator_color2 Red
#property indicator_color3 DeepSkyBlue
#property indicator_level1 50
#property indicator_minimum 0
#property indicator_maximum 100

//extern string _____RSI_TF1_____; 
extern int TimeFrame1=0;//5;
extern int RSIperiod1=9;//14;
extern int applied_price1=4;

//extern string _____RSI_TF2_____; 
extern int TimeFrame2=0;//15;
extern int RSIperiod2=14;//14;
extern int applied_price2=4;

//extern string _____RSI_TF3_____; 
extern int TimeFrame3=0;//30;
extern int RSIperiod3=20;//14;
extern int applied_price3=4;//0;

extern int ShiftPrevious = 4;//This Option Must >=2

double r1[];
double r2[];
double r3[];
datetime t0;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   
//---- indicator line
   SetIndexBuffer(0,r1);
   SetIndexStyle(0,DRAW_LINE);
   
   switch(TimeFrame1)
   {
      case 1 : string TimeFrameStr1="Period_M1"; break;
      case 5 : TimeFrameStr1="Period_M5"; break;
      case 15 : TimeFrameStr1="Period_M15"; break;
      case 30 : TimeFrameStr1="Period_M30"; break;
      case 60 : TimeFrameStr1="Period_H1"; break;
      case 240 : TimeFrameStr1="Period_H4"; break;
      case 1440 : TimeFrameStr1="Period_D1"; break;
      case 10080 : TimeFrameStr1="Period_W1"; break;
      case 43200 : TimeFrameStr1="Period_MN1"; break;
      default : TimeFrameStr1="Current Timeframe";
   }
   
   SetIndexBuffer(1,r2);
   SetIndexStyle(1,DRAW_LINE);
   
   switch(TimeFrame2)
   {
      case 1 : string TimeFrameStr2="Period_M1"; break;
      case 5 : TimeFrameStr2="Period_M5"; break;
      case 15 : TimeFrameStr2="Period_M15"; break;
      case 30 : TimeFrameStr2="Period_M30"; break;
      case 60 : TimeFrameStr2="Period_H1"; break;
      case 240 : TimeFrameStr2="Period_H4"; break;
      case 1440 : TimeFrameStr2="Period_D1"; break;
      case 10080 : TimeFrameStr2="Period_W1"; break;
      case 43200 : TimeFrameStr2="Period_MN1"; break;
      default : TimeFrameStr2="Current Timeframe";
   }
   
   SetIndexBuffer(2,r3);
   SetIndexStyle(2,DRAW_LINE);
   
   switch(TimeFrame3)
   {
      case 1 : string TimeFrameStr3="Period_M1"; break;
      case 5 : TimeFrameStr3="Period_M5"; break;
      case 15 : TimeFrameStr3="Period_M15"; break;
      case 30 : TimeFrameStr3="Period_M30"; break;
      case 60 : TimeFrameStr3="Period_H1"; break;
      case 240 : TimeFrameStr3="Period_H4"; break;
      case 1440 : TimeFrameStr3="Period_D1"; break;
      case 10080 : TimeFrameStr3="Period_W1"; break;
      case 43200 : TimeFrameStr3="Period_MN1"; break;
      default : TimeFrameStr3="Current Timeframe";
   }
   IndicatorShortName("3_RSI("+RSIperiod1+","+RSIperiod2+","+RSIperiod3+")");
   
   SetIndexLabel(0,"R1("+RSIperiod1+")");
   SetIndexLabel(1,"R2("+RSIperiod2+")");
   SetIndexLabel(2,"R3("+RSIperiod3+")");
   
   return(0);
  }
 
//+------------------------------------------------------------------+
//| MTF RSI                                                          |
//+------------------------------------------------------------------+
int start()
  {
   start2();
   start3();
   
   datetime TimeArray1[];
   int    i,limit,y=0,counted_bars=IndicatorCounted();
 
// Plot defined time frame on to current time frame
   ArrayCopySeries(TimeArray1,MODE_TIME,Symbol(),TimeFrame1); 
   
   limit=Bars-counted_bars;
   for(i=0,y=0;i<limit;i++)
   {
   if (Time[i]<TimeArray1[y]) y++;

/***********************************************************   
   Add your main indicator loop below.  You can reference an existing
      indicator with its iName  or iCustom.
   Rule 1:  Add extern inputs above for all neccesary values   
   Rule 2:  Use 'TimeFrame' for the indicator time frame
   Rule 3:  Use 'y' for your indicator's shift value
 **********************************************************/  
 
    r1[i]=iRSI(NULL,TimeFrame1,RSIperiod1,applied_price1,y) ; 
   }
      
   if(t0!=Time[0])
     {
      int cup=0,cdn=0;
      for(i=2; i<=ShiftPrevious; i++)
         {
          double
          r1_2 =r1[i], r2_2 =r2[i], r3_2 =r3[i];
          if(cup==0 && r1_2<50 && r2_2<50 && r3_2<50) cup =1;
          if(cdn==0 && r1_2>50 && r2_2>50 && r3_2>50) cdn =1;
         }
      
      bool rsiCrossUp = r1[1]>50 && r2[1]>50 && r3[i]>50  &&  cup==1;
      bool rsiCrossDn = r1[1]<50 && r2[1]<50 && r3[i]<50  &&  cdn==1;   
   
      if(rsiCrossUp)
        {
         Alert("**** "+Symbol()+": 3 RSI Cross 50 Up");
         draw(clrLime);
        }
      if(rsiCrossDn)
        {
         Alert("**** "+Symbol()+": 3 RSI Cross 50 Dn");
         draw(clrMagenta);
        }
      t0=Time[0];
     }  
     
   return(0);
  }

//+------------------------------------------------------------------+
//| MTF RSI                                                          |
//+------------------------------------------------------------------+
int start2()
  {
   datetime TimeArray2[];
   int    i,limit,y=0,counted_bars=IndicatorCounted();
 
// Plot defined time frame on to current time frame
   ArrayCopySeries(TimeArray2,MODE_TIME,Symbol(),TimeFrame2); 
   
   limit=Bars-counted_bars;
   for(i=0,y=0;i<limit;i++)
   {
   if (Time[i]<TimeArray2[y]) y++;

/***********************************************************   
   Add your main indicator loop below.  You can reference an existing
      indicator with its iName  or iCustom.
   Rule 1:  Add extern inputs above for all neccesary values   
   Rule 2:  Use 'TimeFrame' for the indicator time frame
   Rule 3:  Use 'y' for your indicator's shift value
 **********************************************************/  
 
   r2[i]=iRSI(NULL,TimeFrame2,RSIperiod2,applied_price2,y) ; 
   
   }  
   return(0);
  }
//+------------------------------------------------------------------+
//| MTF RSI                                                          |
//+------------------------------------------------------------------+
int start3()
  {
   datetime TimeArray3[];
   int    i,limit,y=0,counted_bars=IndicatorCounted();
 
// Plot defined time frame on to current time frame
   ArrayCopySeries(TimeArray3,MODE_TIME,Symbol(),TimeFrame3); 
   
   limit=Bars-counted_bars;
   for(i=0,y=0;i<limit;i++)
   {
   if (Time[i]<TimeArray3[y]) y++;

/***********************************************************   
   Add your main indicator loop below.  You can reference an existing
      indicator with its iName  or iCustom.
   Rule 1:  Add extern inputs above for all neccesary values   
   Rule 2:  Use 'TimeFrame' for the indicator time frame
   Rule 3:  Use 'y' for your indicator's shift value
 **********************************************************/  
 
   r3[i]=iRSI(NULL,TimeFrame3,RSIperiod3,applied_price3,y) ; 
   
   }  
   return(0);
  }
//+------------------------------------------------------------------+
void draw(color cr)
  {
   string name =TimeToStr(Time[1],TIME_MINUTES);
   ObjectCreate(name, OBJ_VLINE,1,Time[1],0);
   ObjectSet   (name, OBJPROP_COLOR, cr);
   ObjectSet   (name, OBJPROP_BACK, true);
   ObjectSet   (name, OBJPROP_STYLE, STYLE_DOT);
  }