nse-business-time
Version:
Calculate Business Time for NSE,BSE,MCX and NFO
165 lines (128 loc) • 5.91 kB
JavaScript
const moment = require('moment');
const momentz = require('moment-timezone');
const _ = require('lodash');
const _cnst = require('../utils/constants');
const _holidayList = require('../utils/holiday-list');
const _date_utils = require('./date-utils');
function covnertToString(milliseconds){
return momentz(milliseconds).tz(_cnst.TIME_ZONE).format(_cnst.DATE_FORMAT);
}
//This method is tested & results are OK.
function marketOpenTime(time){
console.log("-----------------------------------------");
_.forEach(_cnst.ARR_EXCHANGES, (exchange) => {
let tmpTime = time;
while(_holidayList.isHoliday(exchange, tmpTime)){
tmpTime = tmpTime + (24 * 60 * 60 * 1000);
}
console.log("OPEN " + exchange + " = " + momentz.tz(covnertToString(tmpTime) + " " + _cnst.O_MKT_START[exchange], _cnst.DATE_TIME_FORMAT, _cnst.TIME_ZONE).toString());
let startDate= momentz.tz(covnertToString(tmpTime) + " " + _cnst.O_MKT_START[exchange], _cnst.DATE_TIME_FORMAT, _cnst.TIME_ZONE).valueOf();
_cnst.O_MKT_TIMING.OPEN[exchange] = startDate;
});
_cnst.O_MKT_TIMING.OPEN['MCX'] = Math.min(_cnst.O_MKT_TIMING.OPEN.MCX_MORNING, _cnst.O_MKT_TIMING.OPEN.MCX_EVENING);
console.log("OPEN MCX" + " = " + momentz(_cnst.O_MKT_TIMING.OPEN['MCX']).tz(_cnst.TIME_ZONE).format(_cnst.DATE_TIME_FORMAT));
return _cnst.O_MKT_TIMING.OPEN;
}
//This method is tested & results are OK.
function marketCloseTime(time){
console.log("-----------------------------------------");
_.forEach(_cnst.ARR_EXCHANGES, (exchange) => {
let tmpTime = time;
while(_holidayList.isHoliday(exchange, tmpTime)){
tmpTime = tmpTime + (24 * 60 * 60 * 1000);
}
console.log("CLOSE " + exchange + " = " +momentz.tz(covnertToString(tmpTime) + " " + _cnst.O_MKT_END[exchange], _cnst.DATE_TIME_FORMAT, _cnst.TIME_ZONE).toString());
let endDate= momentz.tz(covnertToString(tmpTime) + " " + _cnst.O_MKT_END[exchange], _cnst.DATE_TIME_FORMAT, _cnst.TIME_ZONE).valueOf();
_cnst.O_MKT_TIMING.CLOSE[exchange] = endDate;
});
if(_cnst.O_MKT_TIMING.CLOSE.MCX_EVENING - _cnst.O_MKT_TIMING.CLOSE.MCX_MORNING > (24 * 60 * 60 * 1000)){
_cnst.O_MKT_TIMING.CLOSE['MCX'] = Math.max(_cnst.O_MKT_TIMING.CLOSE.MCX_MORNING, 0);
}else{
_cnst.O_MKT_TIMING.CLOSE['MCX'] = Math.max(_cnst.O_MKT_TIMING.CLOSE.MCX_MORNING, _cnst.O_MKT_TIMING.CLOSE.MCX_EVENING);
}
console.log("CLOSE MCX" + " = " + momentz(_cnst.O_MKT_TIMING.CLOSE['MCX']).tz(_cnst.TIME_ZONE).format(_cnst.DATE_TIME_FORMAT));
return _cnst.O_MKT_TIMING.CLOSE;
}
//This method is tested & results are OK.
function isOpen(exchange, currentTime=0){
if(currentTime === 0){
currentTime = momentz().tz(_cnst.TIME_ZONE).valueOf();
}
if(_cnst.O_MKT_TIMING.OPEN[exchange] === 0 || !_cnst.O_MKT_TIMING.OPEN[exchange]){
marketOpenTime (currentTime);
marketCloseTime(currentTime);
}
let start = _cnst.O_MKT_TIMING.OPEN[exchange];
let end = _cnst.O_MKT_TIMING.CLOSE[exchange];
if(currentTime >= start && currentTime <= end){
return true;
}else{
if(currentTime >= end + 28800000){ //8 hours after market close
console.log("It's been long, refreshing market open & close timing");
marketOpenTime (currentTime);
marketCloseTime(currentTime);
}
return false;
}
}
//This method is tested & results are OK.
function getNextSchedulingTime(exchange, currentTime=0){
if(currentTime === 0){
currentTime = momentz().tz(_cnst.TIME_ZONE).valueOf();
}
let open = isOpen(exchange, currentTime);
if(open){
return 60;
}else{
let nextWorkingDay = _date_utils.getNextOpenTime(exchange, currentTime);
let nextTime = nextWorkingDay - currentTime;
return nextTime > 60 ? Math.round(nextTime/1000) + getRandomTime(exchange) : 60;
}
function getRandomTime(exchange){
//if MCX
if(exchange === 'MCX'){
return (Math.floor(Math.random() * 60));
}else{
return (Math.floor(Math.random() * 60));
}
}
}
//This method is tested & results are OK.
function squareOffTime(exchange, currentTime=0){
if(currentTime === 0){
currentTime = momentz().tz(_cnst.TIME_ZONE).valueOf();
}
let open = isOpen(exchange, currentTime);
if(open){
let end = _cnst.O_MKT_TIMING.CLOSE[exchange];
let sqroffTime = end - 10 * 60 * 60 - (getRandomTime(exchange)*(Math.floor(Math.random() * 5))); //~~ closing time - 15 mins interval ~~
let pendingTime = Math.round((sqroffTime - currentTime)/1000);
return pendingTime < 0 ? 0 : pendingTime;
}else{
let nextWorkingDay = _date_utils.getNextOpenTime(exchange, currentTime);
let nextTime = nextWorkingDay - currentTime;
return nextTime > 60 ? Math.round(nextTime/1000) + getRandomTime(exchange) : 60;
}
function getRandomTime(exchange){
//if MCX
if(exchange === 'MCX'){
return (Math.floor(Math.random() * 60));
}else{
return (Math.floor(Math.random() * 60));
}
}
}
//This method is tested & results are OK.
function getTodaysOpenTime(exchange, currentTime=0){
if(currentTime === 0){
currentTime = momentz().tz(_cnst.TIME_ZONE).valueOf();
}
let open = isOpen(exchange, currentTime);
return _date_utils.getCurrentOpenTime(exchange, currentTime);
}
exports.getNextSchedulingTime = getNextSchedulingTime;
exports.isOpen = isOpen;
exports.marketOpenTime = marketOpenTime;
exports.marketCloseTime = marketCloseTime;
exports.squareOffTime = squareOffTime;
exports.getTodayOpenTime = getTodaysOpenTime