25.10.2016 Views

Expert Advisor Programming by Andrew R. Young

Expert Advisor Programming by Andrew R. Young

Expert Advisor Programming by Andrew R. Young

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Working with Time and Date<br />

formats for datetime constants: the MQL Reference topic Basics – Data Types – Datetime constants<br />

has more information. We'll use the format presented above, since it is the only one that can be<br />

easily converted.<br />

To convert a datetime variable to a string constant, use the function TimeToStr(). Here is the<br />

syntax:<br />

string TimeToStr(datetime Time, int Output = TIME_DATE|TIME_MINUTES);<br />

• Time – A datetime variable expressed as the number of seconds elapsed since January 1,<br />

1970.<br />

• Output – An optional parameter that outputs the constant as date only, hour and minute<br />

only; hour, minute and seconds; or any combination of date and time. Valid input values are :<br />

◦ TIME_DATE – Outputs the date, for example, 2009.06.15<br />

◦ TIME_MINUTES – Outputs hour and minute, for example, 05:30<br />

◦ TIME_SECONDS – Outputs hour, minute and seconds, for example, 05:30:45<br />

To output the string constant in the default yyyy.mm.dd hh:mm format, leave Output blank. If you<br />

only want the date, or the hour and minute (or seconds), use the appropriate argument. In this<br />

example, we'll assume that StartTime is equal to 2009.06.15 05:30:45.<br />

TimeToStr(StartTime,TIME_DATE) // Returns "2009.06.15"<br />

TimeToStr(StartTime,TIME_SECONDS) // Returns "05:30:45"<br />

TimeToStr(StartTime,TIME_MINUTES) // Returns "05:30"<br />

TimeToStr(StartTime,TIME_DATE|TIME_SECONDS) // Returns "2009.06.15 05:30:45"<br />

TimeToStr(StartTime) // Returns "2009.06.15 05:30"<br />

We can construct a datetime constant using string concatenation, and convert it to a datetime<br />

variable using the function StrToTime(). The syntax is identical to TimeToStr() above, but without<br />

the Output parameter. The string constant must be in the format yyyy.mm.dd hh:mm to be converted<br />

correctly.<br />

Here's an example of how we can assemble a datetime constant using integers, convert those<br />

integers to string format, and convert the string to a datetime variable. First, we'll declare some<br />

external variables to set a time and date:<br />

extern int UseMonth = 6;<br />

extern int UseDay = 15;<br />

extern int UseHour = 5;<br />

extern int UseMinute = 30;<br />

113

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!