//+------------------------------------------------------------------+
//|                               Top 8 Trenders all brokers v3.mq4  |
//|                                                    Edward Munroe |
//|                                                       Hugh Briss |
//|                                                        GodsClown |
//+------------------------------------------------------------------+
#property copyright "Edward Munroe"

#property indicator_chart_window

extern bool   ShowIndicator         = true;
extern string note1                 = "change color/symbol for top?";
extern bool   ChangeColor           = false;
extern bool   ChangeChartSymbol     = false;
extern int    topPairs              = 8;
extern color  ForG_Color            = WhiteSmoke;
//extern color  BackG_Color           = C'15,7,0';
extern color  Bull_Color            = Lime;
extern color  Bear_Color            = Red;
//extern bool   SendeMail             = false;
extern string note2                 = "MA tf1 settings";
extern int    tf1                   = 1;
extern int    tf1malength           = 100;
extern int    tf1matype             = 3;
extern int    tf1maprice            = 0;
extern int    tf1lookback           = 15;
extern string note3                 = "MA tf2 settings";
extern int    tf2                   = 5;
extern int    tf2malength           = 100;
extern int    tf2matype             = 3;
extern int    tf2maprice            = 0;
extern int    tf2lookback           = 12;
extern string note4                 = "CCi settings";
extern bool   CCi_Alert             = false;
extern int    CCi_Period_tf1        = 14;
//extern int    CCi_Period_tf2        = 14;
extern int    CCi_Level             = 100;
//extern bool   AlertMessage          = false;
//extern bool   SoundOn               = false;
//extern string soundfile_tf1         = "alert.wav";
//extern string soundfile_tf2         = "alert2.wav";
extern bool   OpenNewChart          = false;
extern string note5                 = "Other settings";
extern int    xgap                  = 100;
extern int    ygap                  = 20;
extern int    x                     = 5;
extern int    y                     = 20;
extern int    corner                = 4;
extern int    fontsize              = 8;
extern string prefix                = "";
extern string suffix                = "";

string pairss[27];
double currsma1[27], prevsma1[27], currsma2[27], prevsma2[27],currsma1a[27], prevsma1a[27], currsma2a[27], prevsma2a[27];
double smadifftf1[27], smadiffpercenttf1[27], smadifftf2[27], smadiffpercenttf2[27], smadifftf1a[27], smadiffpercenttf1a[27], smadifftf2a[27], smadiffpercenttf2a[27];
color trendslopecoltf1[27], trendslopecoltf2[27];
double smadiffpercentarraytf1[27][2], smadiffpercentarraytf2[27][2];
string tf1s, tf2s;
double Buffer1[27];
double Buffer2[27];
int ThisAlert[27];
int ThisAlert2[27];

double currCCi1[27],prevCCi1[27],currCCi2[27],prevCCi2[27];
int CCi1[27],CCi2[27];

int init()
{  IndicatorBuffers(2); 
   SetIndexBuffer(0, Buffer1);
   SetIndexBuffer(1, Buffer2);
   
   EventSetTimer(1);
   return(0);
}

int deinit()
  { 
   long currChartd,prevChartd=ChartFirst();
   for (int z=0;z<=127;z++)//change color of every symbol
   {
        currChartd=ChartNext(prevChartd); // Get the new chart ID by using the previous chart ID
        if (ChartGetInteger(prevChartd,CHART_COLOR_FOREGROUND) != ForG_Color) ChartSetInteger(prevChartd,CHART_COLOR_FOREGROUND, ForG_Color); 
        if(currChartd<0) break;          // Have reached the end of the chart list
        prevChartd=currChartd;// let's save the current chart ID for the ChartNext()  
   }
    
   int i = 0; 
   int ObjectsTotal_ = ObjectsTotal();
   string ObjName;
   for(i=ObjectsTotal()-1; i>=0; i--) {
       ObjName = ObjectName(i);
       if(StringFind(ObjectName(i),"tf1_top8")==0) ObjectDelete(ObjectName(i)); 
       if(StringFind(ObjectName(i),"tf2_top8")==0) ObjectDelete(ObjectName(i));      
       if(StringFind(ObjectName(i),"tf1_strongesttrend")==0) ObjectDelete(ObjectName(i));
       if(StringFind(ObjectName(i),"tf2_strongesttrend")==0) ObjectDelete(ObjectName(i));    
       if(StringFind(ObjectName(i),"tf1_settings")==0) ObjectDelete(ObjectName(i));
       if(StringFind(ObjectName(i),"tf2_settings")==0) ObjectDelete(ObjectName(i));   
          continue;    
       }
       ObjectDelete(ObjName);
          
  return(0);
  }

int start() 
{
  // Pause and refresh
  Sleep(200);
  
  // Define strings
  
  pairss[0] = prefix + "EURUSD" + suffix; pairss[1] = prefix + "EURGBP" + suffix; pairss[2] = prefix + "EURJPY" + suffix; 
  pairss[3] = prefix + "EURCAD" + suffix; pairss[4] = prefix + "EURAUD" + suffix; pairss[5] = prefix + "EURNZD" + suffix; 
  pairss[6] = prefix + "GBPUSD" + suffix; pairss[7] = prefix + "GBPCHF" + suffix; pairss[8] = prefix + "GBPJPY" + suffix; 
  pairss[9] = prefix + "GBPCAD" + suffix; pairss[10] = prefix + "GBPAUD" + suffix; pairss[11] = prefix + "GBPNZD" + suffix; 
  pairss[12] = prefix + "AUDUSD" + suffix; pairss[13] = prefix + "AUDCHF" + suffix; pairss[14] = prefix + "AUDJPY" + suffix; 
  pairss[15] = prefix + "AUDCAD" + suffix; pairss[16] = prefix + "AUDNZD" + suffix; pairss[17] = prefix + "NZDUSD" + suffix; 
  pairss[18] = prefix + "NZDCHF" + suffix; pairss[19] = prefix + "NZDJPY" + suffix; pairss[20] = prefix + "NZDCAD" + suffix;
  pairss[21] = prefix + "USDCHF" + suffix; pairss[22] = prefix + "USDJPY" + suffix; pairss[23] = prefix + "USDCAD" + suffix;
  pairss[24] = prefix + "CADCHF" + suffix; pairss[25] = prefix + "CADJPY" + suffix; pairss[26] = prefix + "CHFJPY" + suffix;
  
  if(tf1 == 1) tf1s = "1 Min"; if(tf1 == 5) tf1s = "5 Min"; if(tf1 == 15) tf1s = "15 Min"; if(tf1 == 30) tf1s = "30 Min";
  if(tf1 == 60) tf1s = "Hourly"; if(tf1 == 240) tf1s = "4 Hour"; if(tf1 == 1440) tf1s = "Daily"; if(tf1 == PERIOD_W1) tf1s = "Weekly"; if(tf1 == PERIOD_MN1) tf1s = "Monthly";
  
  if(tf2 == 1) tf2s = "1 Min"; if(tf2 == 5) tf2s = "5 Min"; if(tf2 == 15) tf2s = "15 Min"; if(tf2 == 30) tf2s = "30 Min"; if(tf2 == 60) tf2s = "Hourly"; 
  if(tf2 == 240) tf2s = "4 Hour"; if(tf2 == 1440) tf2s = "Daily"; if(tf2 == PERIOD_W1) tf2s = "Weekly"; if(tf2 == PERIOD_MN1) tf2s = "Monthly";
  
 // Calculate indicators
  

  

  for(int counter1=0;counter1<27;counter1++)
  {
  int tf1lookbacka = tf1lookback + 1;
  int tf2lookbacka = tf2lookback + 1;
  
  if (tf1matype <= 3){
  currsma1[counter1] = iMA(pairss[counter1],tf1,tf1malength,0,tf1matype,tf1maprice,1);
  prevsma1[counter1] = iMA(pairss[counter1],tf1,tf1malength,0,tf1matype,tf1maprice,1+tf1lookback);
  }
  if (tf2matype <= 3){
  currsma2[counter1] = iMA(pairss[counter1],tf2,tf2malength,0,tf2matype,tf2maprice,1);
  prevsma2[counter1] = iMA(pairss[counter1],tf2,tf2malength,0,tf2matype,tf2maprice,1+tf2lookback);
  }
  if (tf1matype > 3){
  currsma1[counter1]  = iCustom(pairss[counter1],tf1,"My System\\AllAverages_v3.3",tf1,tf1maprice,tf1malength,0,tf1matype,0,0,0,"","",0,1);
  prevsma1[counter1]  = iCustom(pairss[counter1],tf1,"My System\\AllAverages_v3.3",tf1,tf1maprice,tf1malength,0,tf1matype,0,0,0,"","",0,1+tf1lookback);
  }
  if (tf2matype > 3){
  currsma2[counter1]  = iCustom(pairss[counter1],tf2,"My System\\AllAverages_v3.3",tf2,tf2maprice,tf2malength,0,tf2matype,0,0,0,"","",0,1);
  prevsma2[counter1]  = iCustom(pairss[counter1],tf2,"My System\\AllAverages_v3.3",tf2,tf2maprice,tf2malength,0,tf2matype,0,0,0,"","",0,1+tf2lookback);
  }
  //currsma1a[counter1] = iCustom(pairss[counter1],tf1,"AllAverages_v3.3",tf1,tf1maprice,tf1malength,0,tf1matype,0,0,0,"","",0,2);
  //prevsma1a[counter1] = iCustom(pairss[counter1],tf1,"AllAverages_v3.3",tf1,tf1maprice,tf1malength,0,tf1matype,0,0,0,"","",0,2+tf1lookback);
  //currsma2a[counter1] = iCustom(pairss[counter1],tf2,"AllAverages_v3.3",tf2,tf2maprice,tf2malength,0,tf2matype,0,0,0,"","",0,2);
  //prevsma2a[counter1] = iCustom(pairss[counter1],tf2,"AllAverages_v3.3",tf2,tf2maprice,tf2malength,0,tf2matype,0,0,0,"","",0,2+tf2lookback);
 


  // Calculate trend percent
  if(currsma1[counter1] >= prevsma1[counter1]) smadifftf1[counter1] = currsma1[counter1] - prevsma1[counter1];
  if(currsma1[counter1] < prevsma1[counter1]) smadifftf1[counter1] = prevsma1[counter1] - currsma1[counter1];
  if(currsma2[counter1] >= prevsma2[counter1]) smadifftf2[counter1] = currsma2[counter1] - prevsma2[counter1];
  if(currsma2[counter1] < prevsma2[counter1]) smadifftf2[counter1] = prevsma2[counter1] - currsma2[counter1];
  //if(currsma1a[counter1] >= prevsma1a[counter1]) smadifftf1a[counter1] = currsma1a[counter1] - prevsma1a[counter1];
  //if(currsma1a[counter1] < prevsma1a[counter1]) smadifftf1a[counter1] = prevsma1a[counter1] - currsma1a[counter1];
  //if(currsma2a[counter1] >= prevsma2a[counter1]) smadifftf2a[counter1] = currsma2a[counter1] - prevsma2a[counter1];
  //if(currsma2a[counter1] < prevsma2a[counter1]) smadifftf2a[counter1] = prevsma2a[counter1] - currsma2a[counter1];
    
  

  
  int pipMult = 1000000;
  if(StringFind(pairss[counter1],"JPY",0) != -1) pipMult = 10000;
  
  smadiffpercenttf1[counter1] = (smadifftf1[counter1]/tf1lookback) * pipMult;
  smadiffpercenttf2[counter1] = (smadifftf2[counter1]/tf2lookback) * pipMult;
  //smadiffpercenttf1a[counter1] = (smadifftf1a[counter1]/tf1lookback) * pipMult;
  //smadiffpercenttf2a[counter1] = (smadifftf2a[counter1]/tf2lookback) * pipMult;

  
  trendslopecoltf1[counter1] = Blue;
  if(currsma1[counter1] > prevsma1[counter1]) trendslopecoltf1[counter1] = Lime; 
  if(currsma1[counter1] < prevsma1[counter1]) trendslopecoltf1[counter1] = Red;
  trendslopecoltf2[counter1] = Blue;
  if(currsma2[counter1] > prevsma2[counter1]) trendslopecoltf2[counter1] = Lime; 
  if(currsma2[counter1] < prevsma2[counter1]) trendslopecoltf2[counter1] = Red;
  
  smadiffpercentarraytf1[counter1][0] = smadiffpercenttf1[counter1];
  smadiffpercentarraytf1[counter1][1] = counter1;
  smadiffpercentarraytf2[counter1][0] = smadiffpercenttf2[counter1];
  smadiffpercentarraytf2[counter1][1] = counter1;
    
  }

  
  
  
  
  // Sort diff array and display strongest 8 pairs
  ArraySort(smadiffpercentarraytf1,WHOLE_ARRAY,0,MODE_DESCEND);
  ArraySort(smadiffpercentarraytf2,WHOLE_ARRAY,0,MODE_DESCEND);
  

  int xt, yt, xt2, yt2,xd, yd, xd2, yd2;
  string tf1_Mode1, tf2_Mode1;
  string tf1_Mode = GetPeriodAndMAtf1(tf1_Mode1);
  string tf2_Mode = GetPeriodAndMAtf2(tf2_Mode1);
  
  
  
  if (ShowIndicator == true)
  {
  for(int counterm=0;counterm<8;counterm++)
  { 
   
  if (corner==1) {xt = x+xgap; xt2 = x;      yt  = y;                                        yt2 = y;                                        xd  = x+xgap; xd2 = x;      yd = y+15 + ((ygap * counterm)-20) + (ygap*2); yd2 = y+15 + ((ygap * counterm)-20) + (ygap*2);}
  if (corner==2) {xt = x;      xt2 = x+xgap; yt  = y+90+ ((ygap)* counterm -100) + (ygap*2); yt2 = y+90+ ((ygap)* counterm -100) + (ygap*2); xd  = x;      xd2 = x+xgap; yd = y+90 - ((ygap * counterm)-20) + (ygap*2); yd2 = y+90 - ((ygap * counterm)-20) + (ygap*2);}
  if (corner==3) {xt = x+xgap; xt2 = x;      yt  = y+90+ ((ygap)* counterm -100) + (ygap*2); yt2 = y+90+ ((ygap)* counterm -100) + (ygap*2); xd  = x+xgap; xd2 = x;      yd = y+90 - ((ygap * counterm)-20) + (ygap*2); yd2 = y+90 - ((ygap * counterm)-20) + (ygap*2);}
  if (corner==4) {xt = x;      xt2 = x+xgap; yt  = y;                                        yt2 = y;                                        xd  = x;      xd2 = x+xgap; yd = y+15 + ((ygap * counterm)-20) + (ygap*2); yd2 = y+15 + ((ygap * counterm)-20) + (ygap*2);}

  
  Display("tf1_top8"+tf1, xt,yt);
  ObjectSetText("tf1_top8"+tf1,"Top 8 - " + tf1s,8,"Verdana",Gainsboro);
  Display("tf1_top8under"+tf1,xt,yt);
  ObjectSetText("tf1_top8under"+tf1,"_____",8,"Verdana",Gainsboro);
  Display("tf1_settings"+tf1, xt,yt+15);
  ObjectSetText("tf1_settings"+tf1,tf1malength+" "+tf1_Mode,8,"Verdana",Gainsboro);
  
  Display("tf2_top8"+tf2,xt2,yt2);
  ObjectSetText("tf2_top8"+tf2,"Top 8 - " + tf2s,8,"Verdana",Gainsboro);
  Display("tf2_top8under"+tf2,xt2,yt2);
  ObjectSetText("tf2_top8under"+tf2,"_____",8,"Verdana",Gainsboro);
  Display("tf2_settings"+tf2, xt2,yt2+15);
  ObjectSetText("tf2_settings"+tf2,tf2malength+" "+tf2_Mode,8,"Verdana",Gainsboro);

  
  int pair1 = smadiffpercentarraytf1[counterm][1];
  int pair2 = smadiffpercentarraytf2[counterm][1];
  

  Display("tf1_strongesttrends" + counterm + tf1, xd,yd);
  ObjectSetText("tf1_strongesttrends" + counterm + tf1,pairss[pair1] + " / " + NormalizeDouble(smadiffpercenttf1[pair1],0),fontsize,"Verdana",trendslopecoltf1[pair1]);
  Display("tf2_strongesttrends" + counterm + tf2, xd2,yd2); 
  ObjectSetText("tf2_strongesttrends" + counterm + tf2,pairss[pair2] + " / " + NormalizeDouble(smadiffpercenttf2[pair2],0),fontsize,"Verdana",trendslopecoltf2[pair2]);

  }
  }
           

  if (ChangeChartSymbol == true) 
  {
      long currChart,prevChart=ChartFirst();
      int charts=127;
     
      for (int counter2=0;counter2<topPairs;counter2++)// We have certainly not more than 100 open charts
     {
           int PAIr1 = smadiffpercentarraytf1[counter2][1];
           int PAIr2 = smadiffpercentarraytf2[counter2][1];
     
           if (pairss[PAIr1] != ChartSymbol(prevChart)) ChartSetSymbolPeriod(prevChart,pairss[PAIr1],tf1); 
           if (prevChart<0 ) ChartOpen(pairss[PAIr1],tf1);
           currChart=ChartNext(prevChart);// Get the new chart ID by using the previous chart ID
           prevChart=currChart;// let's save the current chart ID for the ChartNext()
     }
   }
 
  
  if (ChangeColor == true)
  {   
      string backg = CHART_COLOR_BACKGROUND;
      string forg = CHART_COLOR_FOREGROUND;
      
      for (int counter3=0;counter3<=27;counter3++)
      {
           for (int counter4=0;counter4<=topPairs-1;counter4++)  //change color of every symbol in top(topPairs)
           {
           int Pair1 = smadiffpercentarraytf1[counter4][1];
           int Pair2 = smadiffpercentarraytf2[counter4][1];
  
           Change_Color(pairss[Pair1],ForG_Color,forg);
           if (currsma1[Pair1] > prevsma1[Pair1]) Change_Color(pairss[Pair1],Bull_Color,forg);// Change_Color(pairss[Pair1],Bull_Color,backg);}
           if (currsma1[Pair1] < prevsma1[Pair1]) Change_Color(pairss[Pair1],Bear_Color,forg);// Change_Color(pairss[Pair1],Bull_Color,backg);}
           //if (currsma2[Pair1] > prevsma2[Pair1]) Change_Color(pairss[Pair2],Bull_Color,forg);// Change_Color(pairss[Pair1],Bear_Color,backg);}
           //if (currsma2[Pair2] < prevsma2[Pair2]) Change_Color(pairss[Pair2],Bear_Color,forg);// Change_Color(pairss[Pair1],Bear_Color,backg);}
            
           if (pairss[counter3] != pairss[Pair1]) Change_Color(pairss[counter3],ForG_Color,forg);//Change_Color(pairss[counters],ForG_Color,backg);}
           }
       }
  }  
   
   
return(0);
  }
  
void Display(string name, int x2, int y2)
   {
   ObjectCreate(name, OBJ_LABEL, 0, 0, 0);
   ObjectSet(name, OBJPROP_CORNER, corner);
   ObjectSet(name, OBJPROP_XDISTANCE, x2);
   ObjectSet(name, OBJPROP_YDISTANCE, y2);
   ObjectSet(name, OBJPROP_BACK, FALSE);
   } 
   
string GetPeriodAndMAtf1(string tf1_Mode1)
   {
// List of MAs:
   if (tf1matype == 0) tf1_Mode1 = "SMA";        // Simple Moving Average
   if (tf1matype == 1) tf1_Mode1 = "EMA";        // Exponential Moving Average
   if (tf1matype == 2) tf1_Mode1 = "Wilder";     // Wilder Exponential Moving Average
   if (tf1matype == 3) tf1_Mode1 = "LWMA";       // Linear Weighted Moving Average 
   if (tf1matype == 4) tf1_Mode1 = "SineWMA";    // Sine Weighted Moving Average
   if (tf1matype == 5) tf1_Mode1 = "TriMA";      // Triangular Moving Average
   if (tf1matype == 6) tf1_Mode1 = "LSMA";       // Least Square Moving Average (or EPMA, Linear Regression Line)
   if (tf1matype == 7) tf1_Mode1 = "SMMA";       // Smoothed Moving Average
   if (tf1matype == 8) tf1_Mode1 = "HMA";        // Hull Moving Average by Alan Hull
   if (tf1matype == 9) tf1_Mode1 = "0LagEMA"; // Zero//Lag Exponential Moving Average
   if (tf1matype ==10) tf1_Mode1 = "DEMA";       // Double Exponential Moving Average by Patrick Mulloy
   if (tf1matype ==11) tf1_Mode1 = "T3_basic";   // T3 by T.Tillson (original version)
   if (tf1matype ==12) tf1_Mode1 = "ITrend";     // Instantaneous Trendline by J.Ehlers
   if (tf1matype ==13) tf1_Mode1 = "Median";     // Moving Median
   if (tf1matype ==14) tf1_Mode1 = "GeoMean";    // Geometric Mean
   if (tf1matype ==15) tf1_Mode1 = "REMA";       // Regularized EMA by Chris Satchwell
   if (tf1matype ==16) tf1_Mode1 = "ILRS";       // Integral of Linear Regression Slope 
   if (tf1matype ==17) tf1_Mode1 = "IE/2";       // Combination of LSMA and ILRS 
   if (tf1matype ==18) tf1_Mode1 = "TriMAgen";   // Triangular Moving Average generalized by J.Ehlers
   if (tf1matype ==19) tf1_Mode1 = "VWMA";       // Volume Weighted Moving Average 
   if (tf1matype ==20) tf1_Mode1 = "JSmooth";    // Smoothing by Mark Jurik
   if (tf1matype ==21) tf1_Mode1 = "SMA_eq";     // Simplified SMA
   if (tf1matype ==22) tf1_Mode1 = "ALMA";       // Arnaud Legoux Moving Average
   if (tf1matype ==23) tf1_Mode1 = "TEMA";       // Triple Exponential Moving Average by Patrick Mulloy
   if (tf1matype ==24) tf1_Mode1 = "T3";         // T3 by T.Tillson (correct version)
   if (tf1matype ==25) tf1_Mode1 = "Laguerre";   // Laguerre filter by J.Ehlers
   if (tf1matype ==26) tf1_Mode1 = "MD";         // McGinley Dynamic
   
   return(tf1_Mode1);
   }
   
string GetPeriodAndMAtf2(string tf2_Mode1)
   {
// List of MAs:
   if (tf2matype == 0) tf2_Mode1 = "SMA";      
   if (tf2matype == 1) tf2_Mode1 = "EMA";       
   if (tf2matype == 2) tf2_Mode1 = "Wilder"; 
   if (tf2matype == 3) tf2_Mode1 = "LWMA";      
   if (tf2matype == 4) tf2_Mode1 = "SineWMA";  
   if (tf2matype == 5) tf2_Mode1 = "TriMA";     
   if (tf2matype == 6) tf2_Mode1 = "LSMA";     
   if (tf2matype == 7) tf2_Mode1 = "SMMA";     
   if (tf2matype == 8) tf2_Mode1 = "HMA";        
   if (tf2matype == 9) tf2_Mode1 = "0LagEMA";
   if (tf2matype ==10) tf2_Mode1 = "DEMA";      
   if (tf2matype ==11) tf2_Mode1 = "T3_basic";  
   if (tf2matype ==12) tf2_Mode1 = "ITrend";   
   if (tf2matype ==13) tf2_Mode1 = "Median";    
   if (tf2matype ==14) tf2_Mode1 = "GeoMean";   
   if (tf2matype ==15) tf2_Mode1 = "REMA";      
   if (tf2matype ==16) tf2_Mode1 = "ILRS";       
   if (tf2matype ==17) tf2_Mode1 = "IE/2";      
   if (tf2matype ==18) tf2_Mode1 = "TriMAgen";  
   if (tf2matype ==19) tf2_Mode1 = "VWMA";       
   if (tf2matype ==20) tf2_Mode1 = "JSmooth";   
   if (tf2matype ==21) tf2_Mode1 = "SMA_eq";    
   if (tf2matype ==22) tf2_Mode1 = "ALMA";      
   if (tf2matype ==23) tf2_Mode1 = "TEMA";      
   if (tf2matype ==24) tf2_Mode1 = "T3";        
   if (tf2matype ==25) tf2_Mode1 = "Laguerre";  
   if (tf2matype ==26) tf2_Mode1 = "MD";  
   
   return(tf2_Mode1);
   }
  

void OpenChart (string symbol,int TF)
{ 
     long currChart,prevChart=ChartFirst();
     int charts=127;
     int Counter=0,divCounter=0;
          
     for (int f=0;f<charts;f++)// We have certainly not more than 100 open charts
     {
          Counter++;
          if (symbol == ChartSymbol(prevChart)) divCounter++;
          currChart=ChartNext(prevChart);// Get the new chart ID by using the previous chart ID
          if (currChart<0) {if ((Counter - divCounter) == Counter) {ChartOpen(symbol,TF);break;}// Have reached the end of the chart list
                            else break;}
          prevChart=currChart;// let's save the current chart ID for the ChartNext()
     }
}


void Change_Color(string symbol,color G_Color,string place)
{
     long currChart,prevChart=ChartFirst();
     int charts=127;
     
     for (int i=0;i<charts;i++)// We have certainly not more than 100 open charts
     {
          if (symbol == ChartSymbol(prevChart)) ChartSetInteger(prevChart,place,G_Color);
          currChart=ChartNext(prevChart); // Get the new chart ID by using the previous chart ID
          if (currChart<0) break;
          prevChart=currChart;// let's save the current chart ID for the ChartNext()
     }
}


void ALERT(string symbol, string TF, string text)
{     
       Alert(symbol+" "+TF+" / "+text);
   //  if (SoundOn) 
   //      PlaySound(soundfile_tf1);
     if (OpenNewChart==true) OpenChart(symbol,TF);
   //if (ChangeColor == 1) 
   //   {if (currsma1[counter1] > prevsma1[counter1]) Change_Color(pairss[counter1],Bull_Color); 
   //    if (currsma1[counter1] < prevsma1[counter1]) Change_Color(pairss[counter1],Bear_Color);}
   //   if (SendeMail) 
   //       SendMail(tf1s+" / TTindy over "+StringConcatenate(AlertValue_tf1),"On timeframe "+tf1s+" TTindicator is over "+StringConcatenate(AlertValue_tf1)+" on "+pairss[counter1]+" / "+StringConcatenate(MathRound(smadiffpercenttf1[counter1])));
}

void OnTimer()
{

     for(int counter1=0;counter1<27;counter1++)
     {

     if (CCi_Alert==true){
     currCCi1[counter1]  = iCCI(pairss[counter1],tf1,CCi_Period_tf1,PRICE_TYPICAL,1);
     prevCCi1[counter1]  = iCCI(pairss[counter1],tf1,CCi_Period_tf1,PRICE_TYPICAL,2);
     //currCCi2[counter1]  = iCCI(pairss[counter1],tf2,CCi_Period_tf2,PRICE_TYPICAL,1);
     //prevCCi2[counter1]  = iCCI(pairss[counter1],tf2,CCi_Period_tf2,PRICE_TYPICAL,2);
  
     CCi1[counter1] = 0;
     if (currCCi1[counter1] > ((-1)*CCi_Level) && prevCCi1[counter1] <= ((-1)*CCi_Level)) CCi1[counter1] =  1;
     if (currCCi1[counter1] < CCi_Level        && prevCCi1[counter1] >= CCi_Level)        CCi1[counter1] = -1;
     CCi2[counter1] = 0;
     //if (currCCi2[counter1] >= (-1*CCi_Level) && prevCCi2[counter1] < (-1*CCi_Level)) CCi2[counter1] =  1;
     //if (currCCi2[counter1] <= CCi_Level      && prevCCi2[counter1] > CCi_Level)      CCi2[counter1] = -1; 
     }
     }
       
     for (int counter5=0;counter5<=topPairs-1;counter5++)
     {
           int PAIR1 = smadiffpercentarraytf1[counter5][1];
           int PAIR2 = smadiffpercentarraytf2[counter5][1];
      
           if (ThisAlert[PAIR1] != Time[0])
                {if ((currsma1[PAIR1] > prevsma1[PAIR1]) && CCi1[PAIR1] ==  1) ALERT(pairss[PAIR1],tf1s,"CCi crosses over -100");
                 if ((currsma1[PAIR1] < prevsma1[PAIR1]) && CCi1[PAIR1] == -1) ALERT(pairss[PAIR1],tf1s,"CCi crosses under 100");
               /* {
                    if (AlertMessage == true) 
                        Alert(pairss[PAIR1]+" "+tf1s+" / "+"CCi crosses over "+ -CCi_Level);
                    if (SoundOn) 
                        PlaySound(soundfile_tf1);
                    if (OpenNewChart==true) OpenChart(pairss[PAIR1],tf1);
                    //if (ChangeColor == 1) 
                    //   {if (currsma1[counter1] > prevsma1[counter1]) Change_Color(pairss[counter1],Bull_Color); 
                    //    if (currsma1[counter1] < prevsma1[counter1]) Change_Color(pairss[counter1],Bear_Color);}
                    if (SendeMail) 
                        SendMail(tf1s+" / TTindy over "+StringConcatenate(AlertValue_tf1),"On timeframe "+tf1s+" TTindicator is over "+StringConcatenate(AlertValue_tf1)+" on "+pairss[counter1]+" / "+StringConcatenate(MathRound(smadiffpercenttf1[counter1])));
                    */
                ThisAlert[PAIR1] = Time[0];}
       }
}