Updating Time Series Data Array with Historical Values - JavaScript

My dataset consists of time series data for stock prices. However, the data is not continuously distributed, with some days missing. I am looking for an efficient way to fill in these missing days with the previous values.

In total, there are more than a thousand markets with over 1000 days of data. Each day contains timestamps and pricing information.

One of the tasks I want to perform is to retrieve the prices for every market on a specific day and then sort them.

I am uncertain whether it would be more beneficial to populate all the missing days by inserting timestamps and prices where necessary. This method may increase memory usage, but it could improve data access speed. I am not sure about the exact memory requirements, but with 1000 markets, 1000 days, and 6 object keys, if a number occupies 8 bytes, the total memory needed would be around 43MB plus any additional overheads from objects.

Alternatively, another approach could be to find the previous match, which would involve searching through each array for the matching timestamp. This method might be computationally expensive if carried out for every day of the dataset.

Explore a sample dataset on JSBin

let data = {
  "name": "MARKET",
  "values": [{
    "time": 1440338400000,
    "close": 0.142163,
    "high": 0.152869,
    "low": 0.142163,
    "open": 0.152221,
    "volume": 14.2163,
    "marketCap": 0
  }, 
  // Remaining dataset entries...
  ]
};

data.values.forEach(value => {
  let date = new Date(value.time)
  console.log(date.toString())
})

Answer №1

If you're searching for a different approach, consider re-sampling the data. Check out this resource for more information:

Answer №2

One approach I'll take is to iterate backwards through the array and insert the missing timeframe data using splice. By referencing the last value, the timestamps may be incorrect, but given the uniform intervals (daily), I can easily obtain the desired data using the array index. This method should also help minimize unnecessary duplication of memory usage.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Use .empty() method to remove all contents from the tbody element after creating a table

Currently, I am working on a project where I am creating a drop-down list to assist users in selecting media and ink for their printers. The goal is to then generate a table displaying the selected results. While I have managed to successfully generate the ...

Manipulate elements by adding and removing classes sequentially based on an array

Previously, some had difficulty understanding my question. I have an array of variables representing the ids of 5 divs. My goal is to change the color of each div sequentially for a brief moment before reverting back, mimicking the behavior of traffic ligh ...

Error: In Nodejs Promises, you cannot invoke the "then" method on an undefined value

Could you clarify what's incorrect about the following code snippet? var promise = fs.readFile(file); var promise2 = promise.then(function(data){ var base64 = new Buffer(data, 'binary').toString('base64'); res.e ...

How to extract information from JSON arrays in C# without using object property names

When working with an API that provides order data as an array of strings, the goal is to parse it using Json.Net into a specific object format. The sample data looks like this: "orders": [ //[ price, size, order_id ] [ "295.96","0.05088265","3b0f1 ...

PHP form submission upon conditions being satisfied

I am not utilizing Javascript as it is disabled in my current environment. Here is the code that I would like you to review. I am attempting to use PHP to replicate the functionality of Javascript or jQuery's simple : document.form2.submit() <div ...

Steps for replacing the firestore document ID with user UID in a document:

I've been attempting to retrieve the user UID instead of using the automatically generated document ID in Firebase/Firestore, but I'm encountering this error: TypeError: firebase.auth(...).currentUser is null This is the content of my index.js ...

Attempting to incorporate country flags into the Currency Converter feature

www.womenpalace.com hello :) i'm looking to customize my Currency Converter with Flags images. this is the code for the converter: <select id ="currencies" class="currencies" name="currencies" data-default-shop-currency="{{ shop.currency }}" ...

Showcasing unique <div> content after a delay with setTimeout()

Within this code snippet, I am utilizing a setTimeout() function to make an API call to my backend node.js application every 5 seconds. The AJAX success section determines when to display divContent1 and divContent2 based on specific conditions that must b ...

Unable to perform updates for personalized fonts

I've been trying to incorporate custom fonts into my website, but I seem to be facing difficulties in actually implementing them. Could someone please let me know if I am doing it correctly? .pattern{ background:rgba(0,0,0,.5) url(../images/pattern ...

Having trouble with Mongoose's findOne method not functioning as expected

I am developing an application where users can input data such as the number of hours they slept and their eating habits. This information is then stored in a collection known as the Journal. When a user clicks on "stats", it activates a function called f ...

Exploring the power of the JavaScript this keyword

Can you explain the purpose of the line this.tasks = tasks; in relation to the constructor method? class TaskCollection { constructor(tasks =[]) { this.tasks = tasks; } } ...

Attempt to insert an HTML page containing a script into a different webpage

Greetings and thank you for taking the time to read my first post here :) I have a script that is functioning properly, here is a simplified version of my page1.html: <html> <head> <script type="text/javascript" src="js/jquery-1.6.4.mi ...

The script file (.js) isn't showing up on the PHP or HTML webpage

Experiencing a peculiar issue and seeking advice on alternative solutions due to my limited experience in this matter. The Issue: I currently have the following script running smoothly: <script type="text/javascript" id="myscript" src="http://piclau ...

Is there a way to verify if a time interval has already been established using Javascript?

I've encountered an issue with a bouncing div that activates every 5 seconds through an interval. As I scroll to the bottom of the page, the div fades out and the interval is supposed to be cleared. However, it seems like there's a problem with ...

Hide panel when button is clicked - bootstrap

Is it possible to make a panel collapse by clicking a regular bootstrap button? Below is the code snippet: <div class="panel panel-primary" style="border-color: #464646;"> <div class="panel-heading" style="border-color: #BBBBBB; height: 35px; ...

What's the best way to retrieve the id or index of a card within a list?

Struggling to fetch the id's of documents retrieved from a MongoDB database and displayed on React and Material-Ui cards. Tried logging id in functions and APIs, but receiving 'undefined' or metadata from the delete function. Delete functi ...

Issues with implementing AddEventListener in InAppBrowser on IONIC 2

I am currently working on implementing AddeventListener to listen for 'Exit' and 'LoadStart' events in InAppBrowser within IONIC2. Here is my HTML: <button (click)="browsersystem('https://www.google.com')" > Visit URL& ...

Unexpected behavior: custom event firing multiple times despite being emitted only once

I am utilizing the ws module for incorporating web sockets functionality. An event named newmessage seems to be triggering multiple times in correlation with the number of active sockets connected to the web-socket-server. The scenario puzzled me initiall ...

Stopping setinterval on click can be achieved by using the clearInterval function in

I've encountered an issue while using a slideshow on my website. The plugin I'm using does not have an autoplay feature, so I had to implement it using setinterval in my code. Below is the code I used: var myInterval, startInt = function(){ ...

Incorporate AJAX into your Javascript code for ordering

My issue arises when pointOnMap() is executed before xmlPreparer(), even though they are called in the correct order. I suspect this has something to do with the use of AJAX, as parseXML creates the object that pointOnMap() requires to be initialized befor ...