Time and Calendar Functions

year (int offset): int

Year number of the given bar, f.i. 2011.

month (int offset): int

Month number of the given bar, 1..12.

week (int offset): int

Week number of the given bar, 0..53, with Monday as first day of week.

day (int offset): int

Day of month of the given bar, 1..31.

dom (int offset): int

Number of days of the month of the given bar, 28..31.

tdm (int offset): int

Trading day of the month of the given bar, 1..23. Uses BarZone for determining Weekends and Holidays (see BarMode) when counting trading days.

tom (int offset): int

Total number of trading days of the month of the given bar, 20..23. (tom(offset)-tdm(offset)) gives the days until the last trading day of the month. 

dow (int offset): int

Day of week of the given bar, MONDAY (1), TUESDAY (2) ... SUNDAY (7).

ndow (int offset): int

Number of the current day of week in the current month; to be used f.i. for finding the first Monday (1), the third Friday (3), etc.

ldow (int zone,int offset): int

Local day of week of the given bar in the given time zone (see timeOffset below), considering daylight saving time (see dst below). 

hour (int offset): int

Closing hour of the given bar, 0..23, with 1 second precision. hour(0) gives the current hour and can be used to apply different trade tactics in the US, European, or Asia-Pacific session.

lhour (int zone, int offset): int

Closing local hour of the given bar in the given time zone (see timeOffset below), considering daylight saving time (see dst below). F.i. lhour(EST) returns the current hour at the New York Stock Exchange. 

minute (int offset): int

Closing minute of the given bar, 0..59, with 1 ms precision. minute(0) gives the current minute.

tod (int offset): int

Time of day of the given bar in the form hhmm, f.i. 2107 for 9:07 pm. A time in hhmm can be converted to minutes with int MinuteOfDay = ((tod(0)/100)*60 + (tod(0)%100);.

ltod (int zone, int offset): int

Local time of day of the given bar in the form hhmm.

tow (int offset): int

Time of week of the given bar in the form dhhmm. d is the weekday number starting with Monday(1), f.i. 52107 = Friday 9:07 pm. See also StartWeek, EndWeek.

ltow (int zone, int offset): int

Local time of week of the given bar in the form dhhmm.

second (): var

Closing second of the current bar. The decimals give the fraction of the second with 1 ms precision. The current second number of the day can be calculated with var SecondOfDay = modf(wdate(0),0)*24*60*60;.

second (var Time): var

Second of the given Time value in Windows DATE format. The decimals give the fraction of the second with 1 ms precision. The returned value can be different to the seconds printed by strdate, which are rounded to the next full second. 
 

minutesAgo (int bar): int

Total number of minutes between the end of the bar with the given offset and the end of the current bar.

minutesWithin (int bar): var

Total number of minutes from start to end of the bar with the given offset. Normally identical to BarPeriod, but can be different due to weekends, holidays, daylight saving time, gaps due to lack of price quotes, or when called from an intrabar function.
 

workday (int offset): bool

Returns true when the given date or the end of the given bar is not a holiday or a weekend determined by Holidays, StartWeek, and EndWeek.

market (int zone, int offset): bool

Returns true when the end of the given bar is within market hours given by StartMarket and EndMarket (see also suspended()).

at (int hhmm): bool

Returns true when the given time in hhmm format is at the end or inside the current bar in the BarZone.

dst (int zone, int offset): int

Returns 1 when daylight saving time is active at the end of the given bar, otherwise 0. A simplified daylight saving schedule is used, based on US east coast daylight saving periods for all time zones in America, Sydney daylight saving periods for Australian and Pacific time zones, and European daylight saving periods for all other time zones. UTC and JST time zones have no daylight saving time. For years between 2000 and 2024 the real historical time switching dates are used, for all other years estimated dates from an average of the historical dates.
 

Parameters:

bar Offset of the bar for which the date or time is returned; 0 for the current bar, 1 for the previous bar and so on. When multiple time frames are used, bar offsets are multiplied with TimeFrame.
offset Offset of the bar for which the date or time is returned; or NOW for the current PC time; or a day number in DATE format with no decimals.
zone UTC for UTC/GMT time, WET for London, CET for Frankfurt, EST for New York, CST for Chicago, JST for Tokyo, AEST for Sydney, or the zone offset in hours, from -12...+12 (with daylight saving) or from UTC-12..UTC+12 (without daylight saving). For the daylight saving period, US periods are used for time zones -4 or below, Australian periods are used for time zones 10, 11, 12, and European periods for all other time zones. Note that JST is defined as UTC+9 since Japan uses no daylight saving.

Usage:

hour(1) returns the hour of the previous bar.

 

wdate (int offset): var

Returns UTC date and time of the given bar offset in Windows DATE format. 

ldate (int zone, int offset): var

Returns date and time in the given time zone of the given bar offset in DATE format. 

wdateBar (int n): var

Returns UTC date and time of the bar with the given bar number (0...NumBars-1) in DATE format.

wdatef (string Format, string DateTime): var

Returns date and time in DATE format of the given DateTime string. Can be used to parse date/time fields in CSV files in various formats. Seconds are read with decimals.

strdate (string Format, int offset): string

strdate (string Format, int zone, int offset): string

strdate (string Format, var Date): string

Returns a temporary string in the given Format with the date and time of the given bar offset or DATE variable; can be used in printf statements. Predefined formats are YMD, HM, HMS, YMDHMS. If the Format string ends with "%S.", milliseconds are added; otherwise seconds are rounded to the next full second. Examples: strdate(YMD,0) returns a string with the date of the current bar, and strdate("%y-%m-%d %H:%M:%S.",NOW) returns the current date and time in millisecond precision. 

nthDay(int Year, int Month, int Dow, int N): var

Returns the Windows DATE format of the Nth given weekday of the given month. For instance, nthDay(2016,11,FRIDAY,3) returns the date of the third friday in November 2016. Source code in contract.c, which must be included for this function.

dmy (int YYYYMMDD): var

ymd (var Date): int

Convert a date in the YYYYMMDD format to the Windows DATE format and vice versa. For instance, ymd(wdate(NOW) - 10) returns the date from 10 days ago in the YYYYMMDD format.

utm (var Date): int

mtu (int UnixTime): var

Convert a date in Windows DATE format to Unix time in seconds since 1-1-1970, resp. from Unix time to DATE. Unix time in milliseconds can be converted to seconds by dividing by 1000.

Parameters:

Format

Format string, see format codes. Some often used format strings are predefined in variables.h: YMD = "%Y%m%d";   HMS = "%H:%M:%S";    YMDHMS = "%y%m%d %H:%M:%S".

offset Bar offset, 0 for the current bar, 1 for the previous bar and so on, or NOW for the current time (see remarks).
YYYYMMDD Date as integer, f.i. 20180403 for April 3, 2018.
Date Date/time in Windows DATE format, as returned from wdate(). Days are represented by whole number increments starting on Saturday, 30 December 1899, midnight UTC. The time of the day is represented in the fractional part of the number. This format allows easy and precise date comparison down to a microsecond (= 0.000001/(24*60*60)).
DateTime Date/time string in various formats, see format codes.

Usage:

printf("\nDate: %s",strdate(YMD,0)); prints the current bar date in YYYYMMDD format. wdatef("%d.%m.%y","15.9.16") returns the DATE value of September 15, 2016.

 

timeOffset (int zone, int days, int hour, int minute): int

Returns the offset (in bars) of the last bar closing at the given time, or a negative offset for a bar in the future.

Parameters:

zone UTC for UTC time, ET for New York, WET for London, CET for Frankfurt, AEST for Sydney, JST for Tokyo, or a number giving the zone offset in hours to UTC. Daylight saving time is considered in non-UTC zones from 2000 to 2024.
days Number of days in the past, or 0 for the day of the current bar, or a negative number for days in the future.
hour Hour of the day, 0..23. If above 23, days is adjusted accordingly.
minute Minute of the hour, 0..59. If above 59, hour is adjusted accordingly.

Usage:

timeOffset(ET,1,16,0) returns the bar offset of yesterday's NYSE closing time. timeOffset(ET,0,9,30) returns 0 when the NYSE is just opening. if(0 == timeOffset(0,0,TIME/100,TIME%100)) is fulfilled when the current time is at the HHMM value given by TIME

 

Remarks:

Examples:

// buy at UTC midday on the last day of the month, 
// sell 2 trading days later
if(NumOpenLong == 0) { // nothing yet bought this month
  if((tom(0) == tdm(0)) && hour() >= 12)
    enterLong();
} else if(tdm(0) >= 2)
    exitLong();

// print New York local time to the log
printf(strdate("# NY: %H:%M",EST,0)); // close all position at 16:15 local time if(at(1615)) { printf("\nClosing all positions"); exitLong("**"); exitShort("**"); } // convert a time stamp from EST to UTC TimeStamp -= (EST+dst(EST,TimeStamp))/24.;

See also:

bar, dayHigh, timer, BarZone, BarMode, format codes

► latest version online