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.

EXPERT ADVISOR PROGRAMMING<br />

Chapter 7<br />

Working with Time and Date<br />

Datetime Variables<br />

Internally, the datetime variable is represented as the number of seconds elapsed since January 1,<br />

1970. For example, June 15, 2009 at 0:00 (midnight) would be 1245024000. The advantage of<br />

datetime format is that it makes past and future time comparisons and mathematical manipulations<br />

very easy.<br />

For example, if you wanted to check whether one date comes before or after another date, you<br />

would do a simple relational operation. Let's say that StartDate is June 15, 2009 at 14:00, and<br />

EndDate is June 16, 2009 at 5:00.<br />

if(StartDate < EndDate) // Result is true<br />

if(StartDate > EndDate) // Result is false<br />

Another advantage is that you can add or subtract time from a particular date, simply <strong>by</strong> adding or<br />

subtracting the appropriate number of seconds. If you want to add 24 hours to StartDate, simply<br />

add the number of seconds in a day:<br />

datetime AddDay = StartDate + 86400;<br />

If you're planning to do a lot of mathematical manipulation with datetime variables, it might be a<br />

good idea to declare some integer constants to represent certain units of time:<br />

#define SEC_H1 3600<br />

#define SEC_D1 86400<br />

// Seconds in an hour<br />

// Seconds in a day<br />

The disadvantage of datetime format is that it is not very readable. You can't look at a value such as<br />

1245024000 and automatically tell that it represents June 15, 2009 at 0:00. For this, we use<br />

conversion functions to convert datetime to and from a more readable form.<br />

Datetime Constants<br />

A datetime constant is a date and time presented in the following string format: yyyy.mm.dd hh:mm.<br />

For example, June 15, 2009 at 0:00 would be 2009.06.15 00:00. There are other acceptable<br />

112

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

Saved successfully!

Ooh no, something went wrong!