Trade Smarter with Automation

Date Utilities

$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 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 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);

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');