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 />

Next, we create the string constant using the StringConcatenate() function, and finally convert the<br />

string to datetime format using StrToTime().<br />

string DateConstant = StringConcatenate(Year(),".",UseMonth,".",UseDay," ",<br />

UseHour,":",UseMinute); // DateConstant is "2009.6.15 05:30"<br />

datetime StartTime = StrToTime(DateConstant); // StartTime is "1245043800"<br />

Note that in the StringConcatenate() function, we use Year() to return the current year instead of<br />

using an external variable. You can use functions like Month(), Day() and so on to insert current<br />

time values. We'll cover these in the next section.<br />

Date and Time Functions<br />

There are two functions that return the current time: TimeCurrent() returns the current server time,<br />

while TimeLocal() returns your local computer time. You can use whichever you prefer. You may<br />

want to create a boolean external variable to choose between the two:<br />

extern bool UseLocalTime = true;<br />

Here is the code to assign either the current local time or the current server time to a variable named<br />

CurrentTime.<br />

if(UseLocalTime == true) datetime CurrentTime = TimeLocal();<br />

else CurrentTime = TimeCurrent();<br />

// Local time<br />

// Server time<br />

Sometimes you may just need to retrieve a part of the current time, such as the hour or day. Here is<br />

the list of the most useful functions you can use to return current time values. All of these functions<br />

use the server time – not your local computer time. The return value is of type integer:<br />

• Year() – The current four-digit year, for example, 2009.<br />

• Month() – The current month of the year from 1 to 12.<br />

• Day() – The current day of the month from 1 to 31.<br />

• DayOfWeek() – An integer representing the current day of the week. Sunday is 0, Monday is<br />

1, Friday is 5 and so on.<br />

• Hour() – The current hour in 24 hour time, from 0 to 23. For example, 3am is 3, and 3pm is<br />

15.<br />

• Minute() – The current minute from 0 to 59.<br />

114

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

Saved successfully!

Ooh no, something went wrong!