Trade Smarter with Automation

Date utilities

APIs and shortcuts for working with dates.

Time zone

All dates and times in Alta5 are set to the Eastern Standard Time (EST) zone to predictably match up with market times.


$now

Use the global variable, $now, to access to the current date and time as a Date object.

if($now.isThursday()){
    // it's thursday
}
if(!$now.isMarketDay(-1)){
    // the market was closed yesterday
}

Formats

Alta5 APIs accept date arguments in one of the the following formats:

  • JavaScript Date object
  • String in the format YYYY-MM-DD
  • Relative dates in english, e.g. 'yesterday', '3 days ago', 'last friday'.
var query = {
    symbol: 'JPM',
    interval: '1d',
    start: new Date(2017, 1, 16),   // <-- Date object
    end: 'yesterday'   // <-- relative date
};

Date API

Alta5 extends the native JavaScript Date object to make it easier to work with market related dates. The additional properties and functions defined by Alta5 work the same as JavaScript's built-in Date API.

For example:

// relative days
if($now.isMarketDay(-1, 0, 1)){
    // the market was open yesterday (-1),
    // is open today (0),
    // and tomorrow (1)
}

All dates in Alta5 have the following additional properties and functions:

Properties

  • Number

    The date as a number in the format YYYYMMDD, e.g. 20170415

  • Date

    The closest monthly expiration date (3rd Friday) after this date.

  • Number

    The time (EST) as a number in the format HHMM, e.g. 930 or 1545

  • Date

    The last date/time the market closed before this date.

  • Date

    The date 1 day before this date.

  • Date

    The date 1 week before this date.

  • Date

    The Weekday before this date, e.g. $now.prevFriday for the previous friday.

  • Date

    The date 1 month before this date.

  • Date

    The closest monthly expiration date (3rd Friday) before this date.

  • Date

    The last date/time the market opened before this date.

  • Date

    The date 1 year before this date.

  • Date

    The date 1 day after this date.

  • Date

    The next date/time the market opens after this date.

  • Date

    The date 1 week after this date.

  • Date

    The next Weekday after this date, e.g. $now.nextMonday for the next Monday.

  • Date

    The date 1 month after this date.

  • Date

    The date 1 year after this date.

Functions

add(period) Date

Returns a new date object with date adjusted the specified period from this date.

  • Number/String

    An integer representing the number of days or a string representing a period of time. Can be negative to adjust into the past.

// 3 days ago
var date = $now.add(-3);
// String periods:
$log(
    $now.add('15m'),  // minutes
    $now.add('2h'),   // hours
    $now.add('-30d'), // days
    $now.add('-2w'),  // weeks
    $now.add('3mo'),  // months
    $now.add('-1y')   // years
);

addMarketDays(offset) Date

Returns a new date object offset open market days from this date.

  • Number

    Positive or negative integer representing the number of open market days to offset from this date.

Only counts days the market is actually open; e.g. the example below returns the previous friday, not sunday, because it was the previous day the market was open.

var friday = $now.prevMonday.addMarketDays(-1);

applyMarketClose(clone) Date

Adjusts the time to the market close time on this date (or midnight if the market was closed).

  • Boolean

    true to return a clone of this date object with the time adjusted.

var close = $now.applyMarketClose(true);

applyMarketOpen(clone) Date

Adjusts the time to the market open time on this date (or midnight if the market was closed).

  • Boolean

    true to return a clone of this date object with the time adjusted.

var open = $now.applyMarketOpen(true);

clearTime(clone) Date

Returns this date object (or a clone) with the time reset to midnight.

  • Boolean

    true to return a clone of this date object.

var midnight = $now.clearTime(true);

daysFrom(date) Number

Returns the number of 24 hour periods between 2 dates.

  • Date/String

    The other date or a string with a relative date.

var days = $now.daysFrom('next friday');

getMarketClose() Number

Returns the time the market closes on this date as a numeric time, e.g. 1600. Returns 0 if the market is closed.

if($now.getMarketClose() === 1300){
    // market closes early at 1pm
}

is3rdFriday() Boolean

Returns true if this date is the 3rd friday of the month (when standard monthly option expire).

if($now.is3rdFriday()){
    // expiration friday
}

isMarketDay(...offset) Boolean

Returns true if the market is open at the date and time of this Date object.

  • Number

    Integer(s) representing days relative to this date. See below for example.

if($now.isMarketDay()){
    // the market is open today
}
// relative days
if($now.isMarketDay(-1, 0, 1)){
    // the market was open yesterday (-1),
    // is open today (0),
    // and tomorrow (1)
}

isMarketOpen() Number

Returns true if the market is open at the date and time of this Date object.

if($now.isMarketOpen()){
    // the market is open
}

marketDaysFrom(date) Number

Returns the number of days the market is open between 2 dates.

  • Date/String

    The other date or a string with a relative date.

var days = $now.marketDaysFrom('next friday');

text(format) String

Returns the date formatted as specified by format or in the default short display format (e.g. Apr 15 2018).

  • String

    A date format string. (optional)

Token Output
Month M 1 2 ... 11 12
Mo 1st 2nd ... 11th 12th
MM 01 02 ... 11 12
MMM Jan Feb ... Nov Dec
MMMM January February ... November December
Quarter Q 1 2 3 4
Qo 1st 2nd 3rd 4th
Day of Month D 1 2 ... 30 31
Do 1st 2nd ... 30th 31st
DD 01 02 ... 30 31
Day of Year DDD 1 2 ... 364 365
DDDo 1st 2nd ... 364th 365th
DDDD 001 002 ... 364 365
Day of Week d 0 1 ... 5 6
do 0th 1st ... 5th 6th
dd Su Mo ... Fr Sa
ddd Sun Mon ... Fri Sat
dddd Sunday Monday ... Friday Saturday
Day of Week (Locale) e 0 1 ... 5 6
Day of Week (ISO) E 1 2 ... 6 7
Week of Year w 1 2 ... 52 53
wo 1st 2nd ... 52nd 53rd
ww 01 02 ... 52 53
Week of Year (ISO) W 1 2 ... 52 53
Wo 1st 2nd ... 52nd 53rd
WW 01 02 ... 52 53
Year YY 70 71 ... 29 30
YYYY 1970 1971 ... 2029 2030
Y 1970 1971 ... 9999 +10000 +10001
Note: This complies with the ISO 8601 standard for dates past the year 9999
Week Year gg 70 71 ... 29 30
gggg 1970 1971 ... 2029 2030
Week Year (ISO) GG 70 71 ... 29 30
GGGG 1970 1971 ... 2029 2030
AM/PM A AM PM
a am pm
Hour H 0 1 ... 22 23
HH 00 01 ... 22 23
h 1 2 ... 11 12
hh 01 02 ... 11 12
k 1 2 ... 23 24
kk 01 02 ... 23 24
Minute m 0 1 ... 58 59
mm 00 01 ... 58 59
Second s 0 1 ... 58 59
ss 00 01 ... 58 59
Fractional Second S 0 1 ... 8 9
SS 00 01 ... 98 99
SSS 000 001 ... 998 999
SSSS ... SSSSSSSSS 000[0..] 001[0..] ... 998[0..] 999[0..]
Time Zone z or zz EST CST ... MST PST
Z -07:00 -06:00 ... +06:00 +07:00
ZZ -0700 -0600 ... +0600 +0700
Unix Timestamp X 1360013296
Unix Millisecond Timestamp x 1360013296123