//+----------------------------------------------------------------------------------+
//|                                                              Composite index.mq4 |
//|                                                                           mladen |
//+----------------------------------------------------------------------------------+
#property copyright "mladen"
#property link      "mladenfx@gmail.com"

#property indicator_separate_window
#property indicator_buffers 3
#property indicator_color1 Red
#property indicator_color2 Gold
#property indicator_color3 Lime

//
//
//
//
//

extern string TimeFrame      = "Current time frame";
extern int    RSIPrice       = PRICE_CLOSE;
extern int    RSISlowLength  = 14;
extern int    RSIFastLength  =  3;
extern int    MomentumLength =  9;
extern int    SMALength1     =  3;
extern int    SMALength2     = 13;
extern int    SMALength3     = 33;

//
//
//
//
//

double buffer1[];
double buffer2[];
double buffer3[];
double working[][3];

//
//
//
//
//

string indicatorFileName;
bool   returnBars;
bool   calculateValue;
int    timeFrame;

//+----------------------------------------------------------------------------------+
//|                                                                                  |
//+----------------------------------------------------------------------------------+
//
//
//
//
//

int init()
{
   SetIndexBuffer(0,buffer1);
   SetIndexBuffer(1,buffer2);
   SetIndexBuffer(2,buffer3);
   
   //
      //
      //
      //
      //
      
         indicatorFileName = WindowExpertName();
         calculateValue    = TimeFrame=="calculateValue"; if (calculateValue) { return(0); }
         returnBars        = TimeFrame=="returnBars";     if (returnBars)     { return(0); }
         timeFrame         = stringToTimeFrame(TimeFrame);
      
      //
      //
      //
      //
      //
      
       IndicatorShortName(timeFrameToString(timeFrame)+"  Composite index");
   return(0);
}
int deinit()
{
   return(0);
}

//+----------------------------------------------------------------------------------+
//|                                                                                  |
//+----------------------------------------------------------------------------------+
//
//
//
//
//

#define __slowRSI 0
#define __fastRSI 1
#define __composite 2

//
//
//
//
//

int start()
{
   int i,r,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) { buffer1[0] = MathMin(limit+1,Bars-1); return(0); }
         if (ArrayRange(working,0) != Bars) ArrayResize(working,Bars); 

   //
   //
   //
   //
   //
   //
   
   if (calculateValue || timeFrame == Period())
   {    
     for(i=limit, r=Bars-i-1; i >= 0; i--,r++)
     {
        working[r][__slowRSI] = iRSI(NULL,0,RSISlowLength,RSIPrice,i);
        working[r][__fastRSI] = iRSI(NULL,0,RSIFastLength,RSIPrice,i);
      
           double RSIDelta = working[r][__slowRSI]-working[r-MomentumLength][__slowRSI];
           double RSIsma   = iSma(__fastRSI,SMALength1,r);
         
        working[r][__composite] = RSIDelta+RSIsma;
      
        //
        //
        //
        //
        //
      
        buffer1[i] = working[r][__composite];
        buffer2[i] = iSma(__composite,SMALength2,r);
        buffer3[i] = iSma(__composite,SMALength3,r);
     }
   return(0);
   }
   
   //
   //
   //
   //
   //

   limit = MathMax(limit,MathMin(Bars-1,iCustom(NULL,timeFrame,indicatorFileName,"returnBars",0,0)*timeFrame/Period()));
   for (i=limit; i>=0; i--)
   {
      int y = iBarShift(NULL,timeFrame,Time[i]);
         buffer1[i] = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",RSIPrice,RSISlowLength,RSIFastLength,MomentumLength,SMALength1,SMALength2,SMALength3,0,y);
         buffer2[i] = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",RSIPrice,RSISlowLength,RSIFastLength,MomentumLength,SMALength1,SMALength2,SMALength3,1,y);
         buffer3[i] = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",RSIPrice,RSISlowLength,RSIFastLength,MomentumLength,SMALength1,SMALength2,SMALength3,2,y);
   }
return(0);
}

//+----------------------------------------------------------------------------------+
//|                                                                                  |
//+----------------------------------------------------------------------------------+
//
//
//
//
//

double iSma(int forBuffer,int period, int shift)
{
   double sum   =0;
   
   if (shift>=period)
   {
      for (int i=0; i<period; i++) sum += working[shift-i][forBuffer];
      return(sum/period);
   }
   else return(working[shift][forBuffer]);
}

//-------------------------------------------------------------------
//
//-------------------------------------------------------------------
//
//
//
//
//

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)
{
   tfs = stringUpperCase(tfs);
   for (int i=ArraySize(iTfTable)-1; i>=0; i--)
         if (tfs==sTfTable[i] || tfs==""+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("");
}

//
//
//
//
//

string stringUpperCase(string str)
{
   string   s = str;

   for (int length=StringLen(str)-1; length>=0; length--)
   {
      int tchar = StringGetChar(s, length);
         if((tchar > 96 && tchar < 123) || (tchar > 223 && tchar < 256))
                     s = StringSetChar(s, length, tchar - 32);
         else if(tchar > -33 && tchar < 0)
                     s = StringSetChar(s, length, tchar + 224);
   }
return(s);
}


