Global Variable as datetime

 

Hi all, is it possible to save a global variable as datetime format?

I'm trying with the following code, but I see it as a string.

void OnInit()
  {    
  string d     = "TIME_BUY_"  ;
  string e     = Symbol()     ;    
  string TIME_BUY_    = d + e ;
  GlobalVariableSet(TIME_BUY_,datetime(iTime(NULL,PERIOD_CURRENT,0))); 
  
  Print(GlobalVariableGet("TIME_BUY_"+Symbol()));   
  }

 
Alberto Tortella:

Hi all, is it possible to save a global variable as datetime format?

I'm trying with the following code, but I see it as a string.

No global variables are a double
But you can use casting to and from a global variable to achieve what you want 
 
Documentation on MQL5: Conversion Functions / TimeToString
Documentation on MQL5: Conversion Functions / TimeToString
  • www.mql5.com
Converting a value containing time in seconds elapsed since 01.01.1970 into a string of "yyyy.mm.dd hh:mi" format. Parameters value [in]...
 
Convert your datetime to integer and store into the global variable.
 
why not just make the datetime variable global and then use TimeToString function inside GlobalVariableGet
 
Conor Mcnamara #:
why not just make the datetime variable global and then use TimeToString function inside GlobalVariableGet

what? 

global variables are a string name with a double value,  the OP clearly wants to be able to store and recall a datetime and therefore needs to cast to/from the double value, the string value will be needed to set/get the global variable. 

 
Paul Anscombe #:

what? 

global variables are a string name with a double value,  the OP clearly wants to be able to store and recall a datetime and therefore needs to cast to/from the double value, the string value will be needed to set/get the global variable. 

Actually, just make a global string variable. Cast the datetime to a string. Job sorted. I don't know why you need double variable when he needs a date
 
Conor Mcnamara #:
Actually, just make a global string variable. Cast the datetime to a string. Job sorted. I don't know why you need double variable when he needs a date
Because he will need to be able to look it up.  If he uses the date time as the string value  then he will already know the date time to look it up again and it becomes pointless 
 

Read carefully the documentation. There is ONE possibility here using

GlobalVariableSet

Variable Name as String and a Double value... make your magic happen converting the double to what you really want...

OR, make use of saving/reading Terminal text files...

;)

 
Solution for any types.
 
Flavio Javier Jarabeck #:

Read carefully the documentation. There is ONE possibility here using

Variable Name as String and a Double value... make your magic happen converting the double to what you really want...

OR, make use of saving/reading Terminal text files...

;)

This returns only the last access time which will only store a current time and change with every read. If OP wants to store times in Global Variables of the Terminal he will have to int() them first.
And yes of course terminal or common files are always an option, but it requires some more work the first time.