//+------------------------------------------------------------------+
//|                                              Cronex T RSI BB.mq4 |
//|                                     Copyright � 2008-10, Cronex. |
//|                                       http://www.metaquotes.net/ |
//+------------------------------------------------------------------+
#property  copyright "Copyright � 2008-10, Cronex"
#property  link      "http://www.metaquotes.net/"
//----
#property  indicator_separate_window
#property indicator_buffers 7
#property indicator_color1     DarkOrange
#property indicator_color2     SteelBlue
#property indicator_color3     LightSeaGreen
#property indicator_color4     LightSeaGreen
#property indicator_color5     LightSeaGreen
#property indicator_color6     Blue
#property indicator_color7     Red
#property indicator_width1     2
#property indicator_width6     2
#property indicator_width7     2
#property indicator_style3     STYLE_DOT
#property indicator_style4     STYLE_DOT
#property indicator_style5     STYLE_DOT
#property indicator_levelcolor DarkSlateGray

//
//
//
//
//

extern string  TimeFrame       = "Current time frame";
extern int     RSIPeriod       = 16;
extern int     T3Period        = 10;
extern double  T3Hot           = 0.618;
extern bool    T3Original      = false;
extern int     BandsPeriod     = 20;
extern int     BandsDeviations = 2;
extern int     BandsShift      = 0;
extern int     levelOs         = 20;
extern int     levelMid        = 0;    
extern int     levelOb         = -20;    

//----
//
//
//
//

double RSITArray[];
double RSIArray[];
double BBMDArray[];
double BBUPArray[];
double BBDNArray[];
double MaxArray[];
double MinArray[];
double trend[];

//
//
//
//
//

string IndicatorFileName;
int    timeFrame;
bool   returnBars;
bool   calculateValue;

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

int init()
{

   IndicatorBuffers(8);
   SetIndexBuffer(0,RSITArray);
   SetIndexBuffer(1,RSIArray);
   SetIndexBuffer(2,BBMDArray);
   SetIndexBuffer(3,BBUPArray);
   SetIndexBuffer(4,BBDNArray);
   SetIndexBuffer(5,MaxArray); SetIndexStyle(5,DRAW_HISTOGRAM);  
   SetIndexBuffer(6,MinArray); SetIndexStyle(6,DRAW_HISTOGRAM);  
   SetIndexBuffer(7,trend);
   SetLevelValue(0,levelOs);
   SetLevelValue(1,levelOb);
   SetLevelValue(2,levelMid);
   SetIndexLabel(0,"RSI T");
   SetIndexLabel(1,"RSI");
   SetIndexLabel(2,"BB Middle");
   SetIndexLabel(3,"BB Upper");
   SetIndexLabel(4,"BB Lower");
   SetIndexLabel(5,"Max Point");   
   SetIndexLabel(6,"Min Point"); 
   
      //
      //
      //
      //
      //
      
         IndicatorFileName = WindowExpertName();
         calculateValue    = (TimeFrame=="calculateValue"); if (calculateValue) return(0);
         returnBars        = (TimeFrame=="returnBars");     if (returnBars)     return(0);
         timeFrame         = stringToTimeFrame(TimeFrame); 
  
   IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS));
   IndicatorShortName(timeFrameToString(timeFrame)+"  Cronex T3 RSI BB Signal("+RSIPeriod+")");   
   
return(0);
}

//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+

int start()
{
   int i,limit;
   int counted_bars = IndicatorCounted();

   if(counted_bars < 0) return(-1);
   if(counted_bars > 0) counted_bars--;
             limit=MathMin(Bars-counted_bars,Bars-1);
             if (returnBars) { RSITArray[0] = limit; return(0); }

   //
   //
   //
   //
   //

   if (calculateValue || timeFrame == Period())
   {
      for(i=limit; i>=0; i--)
      {
        RSIArray[i]  =(iRSI(NULL,0,RSIPeriod+4*0,PRICE_WEIGHTED,i)+iRSI(NULL,0,RSIPeriod+4*1,PRICE_WEIGHTED,i)+
                       iRSI(NULL,0,RSIPeriod+4*2,PRICE_WEIGHTED,i)+iRSI(NULL,0,RSIPeriod+4*3,PRICE_WEIGHTED,i))/4-50;      

        RSITArray[i] = iT3(RSIArray[i],T3Period,T3Hot,T3Original,i,0);   
      }

     //================================================================================================ 
     //
     //
     //
     //
     
     for(i=limit; i>=0; i--)
     {
        BBMDArray[i] = iBandsOnArray(RSITArray,0,BandsPeriod,BandsDeviations,BandsShift,MODE_MAIN, i);
        BBUPArray[i] = iBandsOnArray(RSITArray,0,BandsPeriod,BandsDeviations,BandsShift,MODE_UPPER,i);
        BBDNArray[i] = iBandsOnArray(RSITArray,0,BandsPeriod,BandsDeviations,BandsShift,MODE_LOWER,i);
        trend[i]     = 0;
        MaxArray[i]  = EMPTY_VALUE;
        MinArray[i]  = EMPTY_VALUE;
            if(RSIArray[i]<BBDNArray[i])  trend[i] = -1;
            if(RSIArray[i]>BBUPArray[i])  trend[i] =  1;
            if (trend[i] ==  1) MaxArray[i]=RSIArray[i]-BBUPArray[i];
            if (trend[i] == -1) MinArray[i]=RSIArray[i]-BBDNArray[i];  
      
      }
      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]);
            RSITArray[i] = iCustom(NULL,timeFrame,IndicatorFileName,"calculateValue",RSIPeriod,T3Period,T3Hot,T3Original,BandsPeriod,BandsDeviations,BandsShift,0,y);
            RSIArray[i]  = iCustom(NULL,timeFrame,IndicatorFileName,"calculateValue",RSIPeriod,T3Period,T3Hot,T3Original,BandsPeriod,BandsDeviations,BandsShift,1,y);
            BBMDArray[i] = iCustom(NULL,timeFrame,IndicatorFileName,"calculateValue",RSIPeriod,T3Period,T3Hot,T3Original,BandsPeriod,BandsDeviations,BandsShift,2,y);
            BBUPArray[i] = iCustom(NULL,timeFrame,IndicatorFileName,"calculateValue",RSIPeriod,T3Period,T3Hot,T3Original,BandsPeriod,BandsDeviations,BandsShift,3,y);
            BBDNArray[i] = iCustom(NULL,timeFrame,IndicatorFileName,"calculateValue",RSIPeriod,T3Period,T3Hot,T3Original,BandsPeriod,BandsDeviations,BandsShift,4,y);
            trend[i]     = iCustom(NULL,timeFrame,IndicatorFileName,"calculateValue",RSIPeriod,T3Period,T3Hot,T3Original,BandsPeriod,BandsDeviations,BandsShift,7,y);
            MaxArray[i]  = EMPTY_VALUE;
            MinArray[i]  = EMPTY_VALUE;
            if (trend[i] ==  1) MaxArray[i]=RSIArray[i]-BBUPArray[i];
            if (trend[i] == -1) MinArray[i]=RSIArray[i]-BBDNArray[i];
        }                
   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)
{
   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);
}

//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//
//

double workT3[][6];
double workT3Coeffs[][6];
#define _period 0
#define _c1     1
#define _c2     2
#define _c3     3
#define _c4     4
#define _alpha  5

//
//
//
//
//

double iT3(double price, double period, double hot, bool original, int i, int instanceNo=0)
{
   if (ArrayRange(workT3,0) != Bars)                ArrayResize(workT3,Bars);
   if (ArrayRange(workT3Coeffs,0) < (instanceNo+1)) ArrayResize(workT3Coeffs,instanceNo+1);

   if (workT3Coeffs[instanceNo][_period] != period)
   {
     workT3Coeffs[instanceNo][_period] = period;
        double a = hot;
            workT3Coeffs[instanceNo][_c1] = -a*a*a;
            workT3Coeffs[instanceNo][_c2] = 3*a*a+3*a*a*a;
            workT3Coeffs[instanceNo][_c3] = -6*a*a-3*a-3*a*a*a;
            workT3Coeffs[instanceNo][_c4] = 1+3*a+a*a*a+3*a*a;
            if (original)
                 workT3Coeffs[instanceNo][_alpha] = 2.0/(1.0 + period);
            else workT3Coeffs[instanceNo][_alpha] = 2.0/(2.0 + (period-1.0)/2.0);
   }
   
   //
   //
   //
   //
   //
   
   int buffer = instanceNo*6;
   int r = Bars-i-1;
   if (r == 0)
      {
         workT3[r][0+buffer] = price;
         workT3[r][1+buffer] = price;
         workT3[r][2+buffer] = price;
         workT3[r][3+buffer] = price;
         workT3[r][4+buffer] = price;
         workT3[r][5+buffer] = price;
      }
   else
      {
         workT3[r][0+buffer] = workT3[r-1][0+buffer]+workT3Coeffs[instanceNo][_alpha]*(price              -workT3[r-1][0+buffer]);
         workT3[r][1+buffer] = workT3[r-1][1+buffer]+workT3Coeffs[instanceNo][_alpha]*(workT3[r][0+buffer]-workT3[r-1][1+buffer]);
         workT3[r][2+buffer] = workT3[r-1][2+buffer]+workT3Coeffs[instanceNo][_alpha]*(workT3[r][1+buffer]-workT3[r-1][2+buffer]);
         workT3[r][3+buffer] = workT3[r-1][3+buffer]+workT3Coeffs[instanceNo][_alpha]*(workT3[r][2+buffer]-workT3[r-1][3+buffer]);
         workT3[r][4+buffer] = workT3[r-1][4+buffer]+workT3Coeffs[instanceNo][_alpha]*(workT3[r][3+buffer]-workT3[r-1][4+buffer]);
         workT3[r][5+buffer] = workT3[r-1][5+buffer]+workT3Coeffs[instanceNo][_alpha]*(workT3[r][4+buffer]-workT3[r-1][5+buffer]);
      }

   //
   //
   //
   //
   //
   
   return(workT3Coeffs[instanceNo][_c1]*workT3[r][5+buffer] + 
          workT3Coeffs[instanceNo][_c2]*workT3[r][4+buffer] + 
          workT3Coeffs[instanceNo][_c3]*workT3[r][3+buffer] + 
          workT3Coeffs[instanceNo][_c4]*workT3[r][2+buffer]);
}