I am dealing with a lengthy array of sorted timestamps representing stock price quotes. These timestamps have minimal resolution, meaning that each timestamp is at least 1 minute bigger than the previous one. However, there can be gaps during the day, especially in pre/after-hours sessions, and no data exists for weekends and holidays. My goal is to efficiently find the timestamps that mark the start of new intervals (e.g., daily, weekly, or monthly) for charting purposes.
Currently, my approach involves looping through the array, creating a Date object from each timestamp, and comparing the .getDay() values between the previous and current array elements.
let rangeSecs = data[data.length-1] - data[0];
let rangeDays = rangeSecs / 86400;
let spt=[];
const minInterval = data.slice(0,9).reduce((ac,x,i,ar)=>Math.min(ac,x-ar[i-1]))
let prevdate = new Date(data[0]*1000)
for(let i=1; i < data.length; i++){
const curDate = new Date(data[i]*1000)
if(rangeDays > 70 && prevdate.getMonth() != curDate.getMonth()){
spt.push(i)
} else if(rangeDays <= 70 && rangeDays > 14 && prevdate.getDay() > curDate.getDay()){
spt.push(i)
} else if(rangeDays <= 14 && prevdate.getDay() != curDate.getDay()){
spt.push(i)
}
prevdate = curDate;
}
The current implementation works but is slow. I am looking for suggestions on how to optimize its performance.
I have tried skipping a certain number of "safe" timestamps based on assumptions about daily trading hours. However, this optimization is not efficient as I cannot accurately predict the number of data points per day beyond regular trading hours.
SAMPLE DATA:
[1625572800,1625573700, ... , 1627676100]