//+------------------------------------------------------------------+
//|                                                 ZZ MTF RSI_5.mq4 |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "  ZZ MTF RSI_5 "
#property link      "  ZZ MTF RSI_5 "

#property indicator_separate_window
#property indicator_buffers 8
#property indicator_color1 MediumSeaGreen
#property indicator_color2 Red
#property indicator_color3 MediumSeaGreen
#property indicator_color4 Red
#property indicator_color5 MediumSeaGreen 
#property indicator_color6 Red      
#property indicator_color7 MediumSeaGreen
#property indicator_color8 Red

#property indicator_width1 3
#property indicator_width2 3
#property indicator_width3 3
#property indicator_width4 3
#property indicator_width5 3
#property indicator_width6 3
#property indicator_width7 3
#property indicator_width8 3

#property indicator_level1 6
#property indicator_level2 -6

#property indicator_levelcolor Lime  
#property indicator_levelstyle STYLE_SOLID
#property indicator_levelwidth 1

//extern int   LineSize1=3;
//extern int   LineSize2=2;//1
extern int CountBars=200;

//#property indicator_maximum 5                     
//#property indicator_minimum -5

                          
extern int TF1=5;      
extern int TF2=5;//<<<<<<<<<<<<<<<<<<<<<<<<<<<<
extern int TF3=60;//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<,
extern int TF4=60;


extern int RSIPeriod1=5;
extern int RSIPeriod2=14;//5<<<<<<<
extern int RSIPeriod3=5;
extern int RSIPeriod4=14;//5 <<<<<<<<<<<<<<<<


double rsiup1, rsidn1, rsiup2, rsidn2, rsiup3, rsidn3, rsiup4, rsidn4 ;


double ExtMapBuffer1[];
double ExtMapBuffer2[];
double ExtMapBuffer3[];
double ExtMapBuffer4[];
double ExtMapBuffer5[];
double ExtMapBuffer6[];
double ExtMapBuffer7[];
double ExtMapBuffer8[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {

//---- indicators
   SetIndexStyle(0,DRAW_HISTOGRAM,STYLE_SOLID);//,LineSize1
   SetIndexBuffer(0,ExtMapBuffer1);
   SetIndexLabel(0, NULL);
   SetIndexStyle(1,DRAW_HISTOGRAM,STYLE_SOLID);
   SetIndexBuffer(1,ExtMapBuffer2);
   SetIndexLabel(1, NULL);
   SetIndexStyle(2,DRAW_HISTOGRAM,STYLE_SOLID);
   SetIndexBuffer(2,ExtMapBuffer3);
   SetIndexLabel(2, NULL);
   SetIndexStyle(3,DRAW_HISTOGRAM,STYLE_SOLID);
   SetIndexBuffer(3,ExtMapBuffer4);
   SetIndexLabel(3, NULL);
   SetIndexStyle(4,DRAW_HISTOGRAM,STYLE_SOLID);
   SetIndexBuffer(4,ExtMapBuffer5);
   SetIndexLabel(4, NULL);
   SetIndexStyle(5,DRAW_HISTOGRAM,STYLE_SOLID);
   SetIndexBuffer(5,ExtMapBuffer6);
   SetIndexLabel(5, NULL);
   SetIndexStyle(6,DRAW_HISTOGRAM,STYLE_SOLID);
   SetIndexBuffer(6,ExtMapBuffer7);
   SetIndexLabel(6, NULL);
   SetIndexStyle(7,DRAW_HISTOGRAM,STYLE_SOLID);
   SetIndexBuffer(7,ExtMapBuffer8);
   SetIndexLabel(7, NULL);   
   

 
   string short_name;
   short_name=(" TF1  ("+TF1+","+RSIPeriod1+"), TF2 ("+TF2+","+RSIPeriod2+"), TF3 ("+TF3+","+RSIPeriod3+"), TF4 ("+TF4+","+RSIPeriod4+" )");
   IndicatorShortName(short_name);
   SetIndexLabel(0,short_name); 
   return(0); 
  }
//----
   
 
//+------------------------------------------------------------------+
//| MTF Parabolic Sar                                         |
//+------------------------------------------------------------------+
int start()
  {
   int    i,limit,y=0,counted_bars=IndicatorCounted();
   
   limit=Bars-counted_bars;
   limit=Bars-counted_bars;
   limit=Bars-counted_bars;
   limit=Bars-counted_bars;
   limit=Bars-counted_bars;
   
    if(counted_bars>0) limit++; else if (limit>100) limit=CountBars;  
     SetIndexDrawBegin(0,Bars-CountBars);
     SetIndexDrawBegin(1,Bars-CountBars);
     SetIndexDrawBegin(2,Bars-CountBars);
     SetIndexDrawBegin(3,Bars-CountBars);
     SetIndexDrawBegin(4,Bars-CountBars);
     SetIndexDrawBegin(5,Bars-CountBars);
     SetIndexDrawBegin(6,Bars-CountBars);
     SetIndexDrawBegin(7,Bars-CountBars);
     
   int lastp15=0, lastp30=0, lastp60=0;
   for(i=0,y=0;i<limit;i++)
   {
      int p1,p2,p3,p4,p5;
      
      p1 = iBarShift( NULL, TF1, Time[i], false );
      p2 = iBarShift( NULL, TF2, Time[i], false );
      p3 = iBarShift( NULL, TF3, Time[i], false );
      p4 = iBarShift( NULL, TF4, Time[i], false );
 
       double A1_rsia = iRSI(NULL,TF1,RSIPeriod1,PRICE_CLOSE,p1);
         if ((A1_rsia > 50)){ rsiup1 = 1; rsidn1 = 0; }
         if ((A1_rsia < 50)){ rsiup1 = 0; rsidn1 = -1; }
         
       double A2_rsia = iRSI(NULL,TF2,RSIPeriod2,PRICE_CLOSE,p2);
         if ((A2_rsia > 50)){ rsiup2 = 1; rsidn2 = 0; }
         if ((A2_rsia < 50)){ rsiup2 = 0; rsidn2 = -1; }
         
       double A3_rsia = iRSI(NULL,TF3,RSIPeriod3,PRICE_CLOSE,p3);
         if ((A3_rsia > 50)){ rsiup3 = 1; rsidn3 = 0; }
         if ((A3_rsia < 50)){ rsiup3 = 0; rsidn3 = -1; }
         
       double A4_rsia = iRSI(NULL,TF4,RSIPeriod4,PRICE_CLOSE,p4);
         if ((A4_rsia > 50)){ rsiup4 = 1; rsidn4 = 0; }
         if ((A4_rsia < 50)){ rsiup4 = 0; rsidn4 = -1; }
         


          //============================================================================================   
       double totalUp1   = 0.0;
       double totalDn1   = 0.0;
       double totalUp2   = 0.0;
       double totalDn2   = 0.0;
       double totalUp3   = 0.0;
       double totalDn3   = 0.0;
       double totalUp4   = 0.0;
       double totalDn4   = 0.0;
 
      double TF5Up     = 0.0;
      double TF5Down   = 0.0;
      


  if  (( rsiup1 + rsiup2 + rsiup3 + rsiup4  ) ==  1)  totalUp1  =   2 ;
  if  (( rsiup1 + rsiup2 + rsiup3 + rsiup4  ) ==  2)  totalUp2  =   4 ;
  if  (( rsiup1 + rsiup2 + rsiup3 + rsiup4  ) ==  3)  totalUp3  =   6 ;
  if  (( rsiup1 + rsiup2 + rsiup3 + rsiup4  ) ==  4)  totalUp4  =   8 ;

  if  (( rsidn1 + rsidn2 + rsidn3 + rsidn4  ) ==  -1)  totalDn1  =   -2 ;
  if  (( rsidn1 + rsidn2 + rsidn3 + rsidn4  ) ==  -2)  totalDn2  =   -4 ;
  if  (( rsidn1 + rsidn2 + rsidn3 + rsidn4  ) ==  -3)  totalDn3  =   -6 ;
  if  (( rsidn1 + rsidn2 + rsidn3 + rsidn4  ) ==  -4)  totalDn4  =   -8 ;



 //=============================================================================================
 
  //    TF5Up    =  rsiup5; 
  //    TF5Down  =  rsidn5; 

      ExtMapBuffer1[i] = 0;
      ExtMapBuffer2[i] = 0;
      ExtMapBuffer3[i] = 0;
      ExtMapBuffer4[i] = 0;
      ExtMapBuffer5[i] = 0;
      ExtMapBuffer6[i] = 0;
      ExtMapBuffer7[i] = 0;
      ExtMapBuffer8[i] = 0;
      
     if ( totalUp1 == 2 || totalDn1 == - 2 )
           {
             ExtMapBuffer1[i] = totalUp1;
             ExtMapBuffer2[i] = totalDn1;
           }
   if ( totalUp2 == 4 || totalDn2 == - 4 )
          {
            ExtMapBuffer3[i] = totalUp2;
            ExtMapBuffer4[i] = totalDn2;
          }
   if ( totalUp3 == 6 || totalDn3 == - 6 )
          {
            ExtMapBuffer5[i] = totalUp3;
            ExtMapBuffer6[i] = totalDn3;
          }
   if ( totalUp4 == 8 || totalDn4 == - 8 )
          {
            ExtMapBuffer7[i] = totalUp4;
            ExtMapBuffer8[i] = totalDn4;
          } 

   }  
   return(0);
  }
//+------------------------------------------------------------------+