//+------------------------------------------------------------------+
//|                                         SymbolChanger- Slave.mq4 |
//|                                    Patrick VIVES, Copyright 2017 |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Patrick VIVES, Copyright 2017"
#property link      "http://www.mql5.com"
#property version   "1.00"
#property strict
#property indicator_chart_window
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   EventSetTimer(1);
   SetText("SCSlave","Slave",10,16,clrWhiteSmoke,7);
   
//---
   return(INIT_SUCCEEDED);
  }
  
void OnDeinit(const int reason)
{
   ObjectDelete("SCSlave");
}  
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
//---
   
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
//| Timer function                                                   |
//+------------------------------------------------------------------+
void OnTimer()
  {
//---
   string Sy=varGetText("SC-");
   
   //Comment(Sy);
  if(Symbol()!=Sy) ChartSetSymbolPeriod(0,Sy,0);
   
  }
//+------------------------------------------------------------------+

string varGetText(string sVarPrefix)
{
    string text;
    int nVar = GlobalVariablesTotal();
    
    for (int jVar = 0; jVar < nVar; jVar++) {
	string sVarName = GlobalVariableName(jVar);
	int x = StringFind(sVarName, sVarPrefix);
	if (x >= 0) {
	    text = StringSubstr(sVarName, x + StringLen(sVarPrefix));
	}
    }
    
    return(text);
}

//---

void SetText(string name,string text,int x,int y,color colour,int fontsize=8)
  {
   if(ObjectCreate(0,name,OBJ_LABEL,0,0,0))
     {
      ObjectSetInteger(0,name,OBJPROP_XDISTANCE,x);
      ObjectSetInteger(0,name,OBJPROP_YDISTANCE,y);
      ObjectSetInteger(0,name,OBJPROP_COLOR,colour);
      ObjectSetInteger(0,name,OBJPROP_FONTSIZE,fontsize);
      ObjectSetInteger(0,name,OBJPROP_CORNER,CORNER_LEFT_LOWER);
     }
   ObjectSetString(0,name,OBJPROP_TEXT,text);
  }
//+------------------------------------------------------------------+