Download MetaTrader 5
To add comments, please log in or register
Publish your article. We will translate it into other languages!
winterz
104
winterz 2015.02.03 16:03 
...

Set_D1_Bar = GlobalVariableSet("D1_Bar",iTime(NULL,1440,1));
D1_Bar     = GlobalVariableGet("D1_Bar");
D1_Bias    = "Bearish";

Comment("D1 Bias is: "+D1_Bias+" since: "+DoubleToStr(D1_Bar,TIME_DATE|TIME_MINUTES),
        "\nH4 Bias is: "+H4_Bias,
        "\nH1 Bias is: ",H1_Bias);

...

How is it I can display under the Comment what is a Double, into a human readable DateTime?
honest_knave
969
honest_knave 2015.02.03 16:17  

TimeToStr() is the easy option.

GlobalVariableSet("D1_Bar",iTime(NULL,1440,1));
Print(TimeToStr(GlobalVariableGet("D1_Bar")));

 

Alternatively, you can get imaginative combining the components into a string of your format choice.

winterz
104
winterz 2015.02.03 17:21  

But if you do it this way, GlobalVariableSet is a datetime and GlobalVariableGet is a double.

Using TimeToStr would give a "possible loss of data due to type conversion" which i'd rather not use this way? 

Carl Schreiber
5959
Carl Schreiber 2015.02.03 17:45  

datetime is a ulong variable double variable aren't that exact so if you store a date as double and reconvert back to datetime you risk to get another date+time than the one you've originally saved,

but may be the date you save could be 'small' enough to get back the same date+time.

winterz
104
winterz 2015.02.03 17:57  

Thats interesting thanks gooly.

I think I may test this under the circumstances in which I use double and datetime. I really want to store the datetime so that if MT4 crashes or restart happens, it will continue from where it left off (as it uses datetime to the hour precision - not minutes or seconds etc.).

How could I go about properly displaying this in the Comment so that I can read it then without having:  "possible loss of data due to type conversion"

honest_knave
969
honest_knave 2015.02.03 18:25  
winterz:

How could I go about properly displaying this in the Comment so that I can read it then without having:  "possible loss of data due to type conversion"

 Typecast it:

GlobalVariableSet("D1_Bar",iTime(NULL,1440,1));
Print(TimeToStr((datetime)GlobalVariableGet("D1_Bar")));

You can find out more about typecasting, and the limitations that Gooly has raised, here

whroeder1
13641
whroeder1 2015.02.03 19:47  
gooly: date you save could be 'small' enough to get back the same date+time.
A double has 15-17 digits 1417305600 (Sun, 30 Nov 2014 00:00:00 GMT) has 10. No problem going from datatime -> double -> datetime. Going to an int fails on 2147483647 (Tue, 19 Jan 2038 03:14:07 GMT)
honest_knave
969
honest_knave 2015.02.03 19:54  
winterz:

But if you do it this way, GlobalVariableSet is a datetime and GlobalVariableGet is a double.

Using TimeToStr would give a "possible loss of data due to type conversion" which i'd rather not use this way? 

Just a minor point... the return value of GlobalVariableSet is indeed a datetime because it is returning the modification time of the variable. It is not setting a datetime.

I hope that makes sense - it can be confusing. 

winterz
104
winterz 2015.02.03 21:43  
Thank you kindly for everyones input. It's been very helpful!
winterz
104
winterz 2015.03.04 19:37  
winterz:

How is it I can display under the Comment what is a Double, into a human readable DateTime?
Set_D1_Bar = GlobalVariableSet("D1_Bar",iTime(NULL,1440,1));
D1_Bar     = GlobalVariableGet("D1_Bar");

...

Resurrecting this thread; slightly different question: how would I go about converting the D1_Bar (Global variable Get()) into a datetime? I am trying to use this in the third parameter of:

int  iBarShift(
   string           symbol,          // symbol
   int              timeframe,       // timeframe
   datetime         time,            // time
   bool             exact=false      // mode
   );
winterz
104
winterz 2015.03.04 20:58  
(datetime)D1_Bar;

...
Fixed it :)
/
To add comments, please log in or register