How do I protect a variable from changing?

 
int OnInit()   {
   TF = Period();
   drawLabel();
   return(INIT_SUCCEEDED);
}

void OnDeinit(const int reason)  {
   if (ObjectFind("LabelDebug") != -1)         {ObjectDelete("LabelDebug");}
}

int   TF;

void drawLabel()  {
   string name = "LabelDebug";
   string text = "TF: " + TF;
   if (ObjectFind(name) != -1) {ObjectDelete(name);}
   ObjectCreate(name, OBJ_LABEL, 0, 0, 0, 0, 0);
   ObjectSetText(name, text, 14, "Times New Roman", Black);
   ObjectSet(name, OBJPROP_CORNER, 1);
   ObjectSet(name, OBJPROP_XDISTANCE, 10);
   ObjectSet(name, OBJPROP_YDISTANCE, 60);
}

void RunLoop()   {drawLabel();}

void OnTick()  {RunLoop();}


In the code above, TF contains the output of Period(). Of course, it stands for "Timeframe".

My problem is, when I change the TF on the chart, OnInit() is run again and the TF changes accordingly.

What I want is for the variable to be evaluated only once, when the EA or indicator is loaded, and commit to that value forever.

How can I do that?

 

OK, it seems the trick is NOT to set the variable within OnInit().

I shall declare it and set it outside OnInit().


int TF = Period();


Problem solved. Sorry about the trouble.
 
whoowl #:

OK, it seems the trick is NOT to set the variable within OnInit().

I shall declare it and set it outside OnInit().


Problem solved. Sorry about the trouble.
int TF = PERIOD_H1;
Reason: