trading-calendar
Version:
a Node.js library that allows you to check if an exchange is open or closed at a specific date (or in real-time).
89 lines (88 loc) • 4.1 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.supportedExchanges = exports.exchange = void 0;
var luxon_1 = require("luxon");
var exchanges_json_1 = __importDefault(require("./exchanges.json"));
var Exchange = /** @class */ (function () {
function Exchange(name) {
this.name = name;
var exchangeInfo = exchanges_json_1.default[name];
if (!exchangeInfo)
throw new Error('Incorrect exchange name or exchange not supported. Check the docs for supported exchanges. If you would like to add an exchange, create an issue on the github page');
this.timezone = exchangeInfo.timezone;
this.openTime = exchangeInfo.openTime;
this.closeTime = exchangeInfo.closeTime;
this.calendar = exchangeInfo.calendar;
}
Exchange.prototype.isTradingToday = function () {
return this.checkIfTradingDay(luxon_1.DateTime.local().setZone(this.timezone));
};
Exchange.prototype.isTradingDay = function (date) {
if (typeof date === 'string') {
var reg = /^(19|20)\d\d[- \/.](0[1-9]|1[012])[- \/.](0[1-9]|[12][0-9]|3[01])$/;
if (reg.test(date)) {
return this.checkIfTradingDay(luxon_1.DateTime.fromFormat(date, 'yyyy-mm-dd'));
}
else {
throw new Error("Incorrect format on " + date + ". The correct format is yyyy-mm-dd, for example 2021-08-11");
}
}
if (date instanceof Date) {
return this.checkIfTradingDay(luxon_1.DateTime.fromJSDate(date));
}
if (date instanceof luxon_1.DateTime) {
return this.checkIfTradingDay(date);
}
throw new Error("Date:" + date + " must be of type string, Date or DateTime.");
};
Exchange.prototype.checkIfTradingDay = function (localTime) {
if (localTime.weekday > 5)
return false;
var months = this.calendar[localTime.year];
if (!months)
throw new Error(localTime.year + " is unsupported. Feel free to add more support with creating an issue on the github page.");
if (months.hasOwnProperty(localTime.monthLong)) {
return !months[localTime.monthLong].includes(localTime.day);
}
return true;
};
Exchange.prototype.isTradingNow = function () {
return this.checkIfTradingTime(luxon_1.DateTime.local().setZone(this.timezone));
};
Exchange.prototype.isTradingTime = function (date) {
if (typeof date === 'string') {
var reg = /^(19|20)\d\d[-](0[1-9]|1[012])[-](0[1-9]|[12][0-9]|3[01])[-]([01][0-9]|2[0123])[:]([0-5][0-9])$/;
if (reg.test(date)) {
return this.checkIfTradingTime(luxon_1.DateTime.fromFormat(date, 'yyyy-MM-dd-HH:mm'));
}
else {
throw new Error("Incorrect format on " + date + ". The corrrect format is yyyy-mm-dd-HH24-MI, for example 2021-08-11-20:30");
}
}
if (date instanceof Date) {
return this.checkIfTradingTime(luxon_1.DateTime.fromJSDate(date));
}
if (date instanceof luxon_1.DateTime) {
return this.checkIfTradingTime(date);
}
throw new Error("Date:" + date + " must be of type string, Date or DateTime.");
};
Exchange.prototype.checkIfTradingTime = function (localTime) {
if (!this.checkIfTradingDay(localTime))
return false;
var hour = localTime.hour;
var minute = localTime.minute;
return (hour > this.openTime.hour && hour < this.closeTime.hour)
|| (hour === this.openTime.hour && minute >= this.openTime.minute)
|| (hour === this.closeTime.hour && minute < this.closeTime.minute);
};
return Exchange;
}());
function exchange(name) {
return new Exchange(name);
}
exports.exchange = exchange;
exports.supportedExchanges = Object.keys(exchanges_json_1.default);