#property indicator_separate_window
#property indicator_buffers 0
#property strict

//extern string  Symbols       = "EURJPY;EURUSD;GBPJPY;GBPUSD;USDJPY;XAUUSD;XAGUSD;XBRUSD;DE30;US500;AUDJPY;EURAUD;GBPAUD;USDCAD;"; // List of symbols (separated by ";")
extern string  Symbols       = "EURJPY;EURUSD;GBPJPY;GBPUSD;USDJPY;AUDJPY;EURAUD;GBPAUD;USDCAD"; // List of symbols (separated by ";")

extern string  UniqueID      = "SymbolChanger1 Sep"; // Indicator unique ID
extern int     ButtonsInARow = 14;                // Buttons in a horizontal row
extern bool    ShowTimeFrames= true;
extern int     XShift        = 10;               // Horizontal shift
extern int     YShift        = 20;               // Vertical shift
extern int     XSize         = 55;               // Width of buttons
extern int     YSize         = 17;               // Height of buttons
extern bool    FontBold      = false;
extern int     FSize         = 8;               // Fort size
extern color   Bcolor        = clrBlack;     // Button color
extern color   Dcolor        = clrDimGray;       // Button border color
extern color   Tncolor       = clrWhite;         // Text color - normal
extern color   Sncolor       = clrLime;           // Text color - selected
extern bool    Transparent   = false;            // Transparent buttons?

string aSymbols[];
int OnInit() 
{
    IndicatorShortName(UniqueID); 
   Symbols = StringTrimLeft(StringTrimRight(Symbols));
   if (StringSubstr(Symbols,StringLen(Symbols)-1,1) != ";")
                    Symbols = StringConcatenate(Symbols,";");
   
         int s=0,i=StringFind(Symbols,";",s);
         string current;
         while (i > 0)
         {
            current = StringSubstr(Symbols,s,i-s);
               ArrayResize(aSymbols,ArraySize(aSymbols)+1);
                           aSymbols[ArraySize(aSymbols)-1] = current;
                           s = i + 1;
                           i = StringFind(Symbols,";",s);
         }
            
   int xpos=0,ypos=0,maxx=0,maxy=0; 
         for (i = 0; i<ArraySize(aSymbols); i++)
            { if (i>0 && MathMod(i,ButtonsInARow)==0) { xpos=0; ypos+=YSize+1; } createButton(UniqueID+":symbol:"+string(i),aSymbols[i],XShift+xpos,YShift+ypos); xpos +=XSize+1; }                     
   xpos = 0; ypos += YSize*2;     
         if(ShowTimeFrames)
         {
         
         for (i = 0; i<ArraySize(sTfTable); i++)
            { if (i>0 && MathMod(i,ButtonsInARow)==0) { xpos=0; ypos+=YSize+1; } createButton(UniqueID+":time:"+string(i),sTfTable[i],XShift+xpos,YShift+ypos); xpos +=XSize+1; }}                     
               
   setSymbolButtonColor();
   setTimeFrameButtonColor();
   return(0);   
}
void OnDeinit(const int reason)
{ 
   switch(reason)
   {
      case REASON_CHARTCHANGE :
      case REASON_RECOMPILE   :
      case REASON_CLOSE       : break;
      default :
      {
         string lookFor       = UniqueID+":";
         int    lookForLength = StringLen(lookFor);
         for (int i=ObjectsTotal()-1; i>=0; i--)
         {
               string objectName = ObjectName(i);  if (StringSubstr(objectName,0,lookForLength) == lookFor) ObjectDelete(objectName);
         }
      }                  
   }
}

void createButton(string name, string caption, int xpos, int ypos)
{
    int window = WindowFind(UniqueID);
   if (ObjectFind(name)!=0)
       ObjectCreate(name,OBJ_BUTTON,window,0,0);
          ObjectSet(name,OBJPROP_CORNER   ,0);
          ObjectSet(name,OBJPROP_XDISTANCE,xpos);
          ObjectSet(name,OBJPROP_YDISTANCE,ypos);
          ObjectSet(name,OBJPROP_XSIZE,XSize);
          ObjectSet(name,OBJPROP_YSIZE,YSize);
          ObjectSetText(name,caption,FSize,"Arial",Tncolor);
              ObjectSet(name,OBJPROP_FONTSIZE,FSize);
              ObjectSet(name,OBJPROP_BORDER_TYPE,BORDER_FLAT);
              ObjectSet(name,OBJPROP_COLOR,Tncolor); 
              ObjectSet(name,OBJPROP_BGCOLOR,Bcolor); 
              ObjectSet(name,OBJPROP_BACK,Transparent); 
              ObjectSet(name,OBJPROP_BORDER_COLOR,Dcolor); 
              ObjectSet(name,OBJPROP_STATE,false);
              ObjectSet(name,OBJPROP_HIDDEN,true);
}

void setSymbolButtonColor()
{
   string lookFor       = UniqueID+":symbol:";
   int    lookForLength = StringLen(lookFor);
   for (int i=ObjectsTotal()-1; i>=0; i--)
   {
      string objectName = ObjectName(i);  
      if (StringSubstr(objectName,0,lookForLength) == lookFor)
      {
         string symbol = ObjectGetString(0,objectName,OBJPROP_TEXT);
         if (symbol != _Symbol)
               ObjectSet(objectName,OBJPROP_COLOR,Tncolor); 
         else  ObjectSet(objectName,OBJPROP_COLOR,Sncolor);
         switch(FontBold)
            {
              case 0: ObjectSetString(0,objectName,OBJPROP_FONT,"Arial"); break;
              case 1: ObjectSetString(0,objectName,OBJPROP_FONT,"Arial Bold");
            }  
      }         
   }
}
void setTimeFrameButtonColor()
{
   string lookFor       = UniqueID+":time:";
   int    lookForLength = StringLen(lookFor);
   for (int i=ObjectsTotal()-1; i>=0; i--)
   {
      string objectName = ObjectName(i);  
      if (StringSubstr(objectName,0,lookForLength) == lookFor)
      {
         int time = stringToTimeFrame(ObjectGetString(0,objectName,OBJPROP_TEXT));
         if (time != _Period)
               ObjectSet(objectName,OBJPROP_COLOR,Tncolor); 
         else  ObjectSet(objectName,OBJPROP_COLOR,Sncolor);
      }         
   }
}

string sTfTable[] = {"M1","M5","M15","M30","H1","H4","D1","W1","MN"};
int    iTfTable[] = {1,5,15,30,60,240,1440,10080,43200};

string timeFrameToString(int tf)
{
   for (int i=ArraySize(iTfTable)-1; i>=0; i--) 
         if (tf==iTfTable[i]) return(sTfTable[i]);
                              return("");
}
int stringToTimeFrame(string tf)
{
   for (int i=ArraySize(sTfTable)-1; i>=0; i--) 
         if (tf==sTfTable[i]) return(iTfTable[i]);
                              return(0);
}


void OnChartEvent(const int id, const long& lparam, const double& dparam, const string& sparam)
{
   if (id==CHARTEVENT_OBJECT_CLICK && ObjectGet(sparam,OBJPROP_TYPE)==OBJ_BUTTON)
   {
      if (StringFind(sparam,UniqueID+":symbol:",0)==0) ChartSetSymbolPeriod(0,ObjectGetString(0,sparam,OBJPROP_TEXT),_Period);
      if (StringFind(sparam,UniqueID+":time:"  ,0)==0) ChartSetSymbolPeriod(0,_Symbol,stringToTimeFrame(ObjectGetString(0,sparam,OBJPROP_TEXT)));
      if (StringFind(sparam,UniqueID+":back:"  ,0)==0) ObjectSet(sparam,OBJPROP_STATE,false);
   }
}  

int start() { return(0); }