//+------------------------------------------------------------------+
//| T3_RSI.mq4 |
//| MojoFX |
//| http://groups.yahoo.com/group/MetaTrader_Experts_and_Indicators/ |
//+------------------------------------------------------------------+
#property copyright "MojoFX - Conversion only"
#property link "http://groups.yahoo.com/group/MetaTrader_Experts_and_Indicators/"

#property indicator_separate_window
#property indicator_buffers 2
#property indicator_color1 Red
#property indicator_color2 Aqua

#property indicator_level1 50
#property indicator_level2 70
#property indicator_level3 30

extern int RSI_Period = 8;
extern int T3_Period = 8;
extern double T3_Curvature = 0.618;

extern string long_basket= "enter 7 long symbols";
extern string mlong_basket1= "AUDJPY";
extern string mlong_basket2= "CADJPY";
extern string mlong_basket3= "CHFJPY";
extern string mlong_basket4= "EURJPY";
extern string mlong_basket5= "GBPJPY";
extern string mlong_basket6= "NZDUSD";
extern string mlong_basket7= "USDJPY";

extern string short_basket= "enter 7 short symbols";
extern string mshort_basket1= "AUDUSD";
extern string mshort_basket2= "EURCHF";
extern string mshort_basket3= "EURUSD";
extern string mshort_basket4= "GBPCHF";
extern string mshort_basket5= "GBPUSD";
extern string mshort_basket6= "USDCAD";
extern string mshort_basket7= "USDCHF";
string long_basket1, long_basket2, long_basket3, long_basket4, long_basket5, long_basket6, long_basket7;
string short_basket1, short_basket2, short_basket3, short_basket4, short_basket5, short_basket6, short_basket7;

double t3Array[];
double rsiArray[];

double e1,e2,e3,e4,e5,e6;
double c1,c2,c3,c4;
double n,w1,w2,b2,b3;

//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- indicators setting
SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,3);
SetIndexStyle(1,DRAW_LINE,STYLE_SOLID,2);

IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS));
IndicatorShortName("T3 RSI GROUP"+T3_Period);

SetIndexBuffer(0,t3Array);
SetIndexLabel(0,"T3 "+T3_Period);

SetIndexBuffer(1,rsiArray);
SetIndexLabel(1,"RSI "+T3_Period);

   GetCorrectPairs();

//---- variable reset

e1=0; e2=0; e3=0; e4=0; e5=0; e6=0;
c1=0; c2=0; c3=0; c4=0;
n=0;
w1=0; w2=0;
b2=0; b3=0;

b2=T3_Curvature*T3_Curvature;
b3=b2*T3_Curvature;
c1=-b3;
c2=(3*(b2+b3));
c3=-3*(2*b2+T3_Curvature+b3);
c4=(1+3*T3_Curvature+b3+3*b2);
n=T3_Period;

if (n<1) n=1;
n = 1 + 0.5*(n-1);
w1 = 2 / (n + 1);
w2 = 1 - w1;

//----
return(0);
}

//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
int limit=Bars;

//---- indicator calculation

for(int i=limit; i>=0; i--)
{
rsiArray[i] = GetRSIbasket(i);

e1 = w1*rsiArray[i] + w2*e1;
e2 = w1*e1 + w2*e2;
e3 = w1*e2 + w2*e3;
e4 = w1*e3 + w2*e4;
e5 = w1*e4 + w2*e5;
e6 = w1*e5 + w2*e6;

t3Array[i]=c1*e6 + c2*e5 + c3*e4 + c4*e3;
}
//----
return(0);
}

double GetRSIbasket(int pos)
{
   double RSIbasket;
   double r1, r2, r3, r4, r5, r6, r7;
   double s1, s2, s3, s4, s5, s6, s7;
   r1 = iRSI(long_basket1,0,T3_Period,PRICE_CLOSE,pos);
   r2 = iRSI(long_basket2,0,T3_Period,PRICE_CLOSE,pos);
   r3 = iRSI(long_basket3,0,T3_Period,PRICE_CLOSE,pos);
   r4 = iRSI(long_basket4,0,T3_Period,PRICE_CLOSE,pos);
   r5 = iRSI(long_basket5,0,T3_Period,PRICE_CLOSE,pos);
   r6 = iRSI(long_basket6,0,T3_Period,PRICE_CLOSE,pos);
   r7 = iRSI(long_basket7,0,T3_Period,PRICE_CLOSE,pos);
   s1 = iRSI(short_basket1,0,T3_Period,PRICE_CLOSE,pos);
   s2 = iRSI(short_basket2,0,T3_Period,PRICE_CLOSE,pos);
   s3 = iRSI(short_basket3,0,T3_Period,PRICE_CLOSE,pos);
   s4 = iRSI(short_basket4,0,T3_Period,PRICE_CLOSE,pos);
   s5 = iRSI(short_basket5,0,T3_Period,PRICE_CLOSE,pos);
   s6 = iRSI(short_basket6,0,T3_Period,PRICE_CLOSE,pos);
   s7 = iRSI(short_basket7,0,T3_Period,PRICE_CLOSE,pos);
   
   RSIbasket = r1 + r2 + r3 + r4 + r5 + r6 + r7;
   RSIbasket = RSIbasket + s1 + s2 + s3 + s4 + s5 + s6 + s7;
   RSIbasket = RSIbasket / 14.0;
   
   return(RSIbasket);
   
}


void GetCorrectPairs()
{
   long_basket1 = GetCorrectSymbol(mlong_basket7);
   long_basket2 = GetCorrectSymbol(mlong_basket7);
   long_basket3 = GetCorrectSymbol(mlong_basket7);
   long_basket4 = GetCorrectSymbol(mlong_basket7);
   long_basket5 = GetCorrectSymbol(mlong_basket7);
   long_basket6 = GetCorrectSymbol(mlong_basket7);
   long_basket7 = GetCorrectSymbol(mlong_basket7);
   
   short_basket1 = GetCorrectSymbol(mshort_basket1);
   short_basket2 = GetCorrectSymbol(mshort_basket2);
   short_basket3 = GetCorrectSymbol(mshort_basket3);
   short_basket4 = GetCorrectSymbol(mshort_basket4);
   short_basket5 = GetCorrectSymbol(mshort_basket5);
   short_basket6 = GetCorrectSymbol(mshort_basket6);
   short_basket7 = GetCorrectSymbol(mshort_basket7);
}

string GetCorrectSymbol(string pair)
{
   string AddChar, myPair;
   
   myPair = pair;
   if (StringLen(pair) > 0)
   {
   if (StringLen(Symbol()) == 7)
     {
      AddChar = StringSubstr(Symbol(), 6, 1);
      myPair = pair + AddChar;
     }
   }
   return(myPair);
   
}
//+------------------------------------------------------------------+