//+------------------------------------------------------------------+
//|                                                                  |
//|     *Original author header missing                              |
//|                                                                  |
//+------------------------------------------------------------------+

//Modified, 09/Mar/2022, by jeanlouie, www.forexfactory.com/jeanlouie
// - added author header template
// - buffer label
// - rsi params as inputs
// - mfi added
// - input choice for rsi/mfi


#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1  Crimson
#property indicator_width1  2
#property indicator_level1  50

enum e1{
   RSI,
   MFI,
};

extern int Tframe1=240;
extern int Tframe2=1440;
extern int Tframe3=10080;
extern int Tframe4=0;
extern int NumBars=500;

extern e1 Indicator_type = RSI;

extern int RSI_short_period = 2;
extern ENUM_APPLIED_PRICE RSI_short_price = PRICE_CLOSE;
extern int RSI_long_period = 9;
extern ENUM_APPLIED_PRICE RSI_long_price = PRICE_CLOSE;

extern int MFI_short_period = 2;
extern int MFI_long_period = 9;

double Crsi[];
double four2, four9, day2, day9, week2, week9, mth2, mth9;
double RSItot;
int RSInum;
int fouri, dayi, weeki, monthi, i;

int init()
{
   IndicatorBuffers(1);
   SetIndexBuffer(0,Crsi); SetIndexStyle(0,DRAW_LINE); 
   
   string lbl = Indicator_type==RSI ? "Comp-RSI" : "Comp-MFI";
   SetIndexLabel(0,lbl);
   
   return(0);
}
int deinit() { return(0); }


int start()
{

   for ( i=NumBars; i>=0; i--)
   {
       fouri = iBarShift(Symbol(), Tframe1, Time[i], false); 
       dayi = iBarShift(Symbol(), Tframe2, Time[i], false);
       weeki = iBarShift(Symbol(), Tframe3, Time[i], false); 
       monthi = iBarShift(Symbol(), Tframe4, Time[i], false);
      
      RSInum=0;  RSItot=0;
      
      if(Tframe1>0){
         if(Indicator_type==RSI){
            //four2 = iRSI(NULL,Tframe1,2,PRICE_CLOSE,fouri);   
            //four9 = iRSI(NULL,Tframe1,9,PRICE_CLOSE,fouri); 
            four2 = iRSI(NULL,Tframe1,RSI_short_period,RSI_short_price,fouri);   
            four9 = iRSI(NULL,Tframe1,RSI_long_period,RSI_long_price,fouri); 
         }
         else if(Indicator_type==MFI){
            four2 = iMFI(_Symbol,Tframe1,MFI_short_period,fouri);
            four9 = iMFI(_Symbol,Tframe1,MFI_long_period,fouri);
         }
         RSInum=RSInum+2;  RSItot=RSItot+four2+four9;
      }  
      
      if(Tframe2>0){
         if(Indicator_type==RSI){
            day2 = iRSI(NULL,Tframe2,RSI_short_period,RSI_short_price,dayi);   
            day9 = iRSI(NULL,Tframe2,RSI_long_period,RSI_long_price,dayi); 
         }
         else if(Indicator_type==MFI){
            day2 = iMFI(_Symbol,Tframe2,MFI_short_period,dayi);
            day9 = iMFI(_Symbol,Tframe2,MFI_long_period,dayi);
         }
         RSInum=RSInum+2;  RSItot=RSItot+day2+day9;
      }  
        
      if(Tframe3>0){
         if(Indicator_type==RSI){
            week2 = iRSI(NULL,Tframe3,RSI_short_period,RSI_short_price,weeki);   
            week9 = iRSI(NULL,Tframe3,RSI_long_period,RSI_long_price,weeki); 
         }
         else if(Indicator_type==MFI){
            week2 = iMFI(_Symbol,Tframe3,MFI_short_period,weeki);
            week9 = iMFI(_Symbol,Tframe3,MFI_long_period,weeki);
         }
         RSInum=RSInum+2;  RSItot=RSItot+week2+week9;
      }  
        
      if(Tframe4>0){
         if(Indicator_type==RSI){
            mth2 = iRSI(NULL,Tframe4,RSI_short_period,RSI_short_price,monthi);   
            mth9 = iRSI(NULL,Tframe4,RSI_long_period,RSI_long_price,monthi); 
         }
         else if(Indicator_type==MFI){
            mth2 = iMFI(_Symbol,Tframe4,MFI_short_period,monthi);
            mth9 = iMFI(_Symbol,Tframe4,MFI_long_period,monthi);
         }
         RSInum=RSInum+2;  RSItot=RSItot+mth2+mth9;
      }  

      Crsi[i]=RSItot/RSInum;
      
   }     

   return(0);
}  

