<head>
<!-- Load plotly.js into the DOM -->
<script src='https://cdn.plot.ly/plotly-2.35.0.min.js'></script>
</head>
<body>
<div id='graph'><!-- Plotly chart will be drawn inside this DIV --></div>
</body>
<button onclick = "scale();">
Auto scale
</button>
JavaScript
const startDate = new Date(Date.UTC(2023, 9, 1, 13, 30)); // 9:30 AM ET is 1:30 PM UTC
// Get the millisecond timestamp
let timestamp = startDate.getTime();
const endDate = new Date(Date.UTC(2023, 9, 1, 17, 30));
// Get the millisecond timestamp
let timestamp2 = endDate.getTime();
timestamp = "2023-01-09"
timestamp2 = "2023-01-09"
const dateEST = new Date(timestamp + "T09:30:00-05:00"); // ISO format with EST offset (-05:00)
const uTimestampOpen = Math.floor(dateEST.getTime());
const dateEST2 = new Date(timestamp + "T16:00:00-05:00"); // ISO format with EST offset (-05:00)
const uTimestampClose = Math.floor(dateEST2.getTime());
console.log(timestamp)
//console.log(timestamp2)
let layout;
let graph;
let traces = [];
// Function to fetch data from the API
async function fetchStockData(ticker) {
console.log("fetching data...")
const url = "https://api.polygon.io/v2/aggs/ticker/" + ticker + "/range/1/hour/" + timestamp + "/" + timestamp2 + "?apiKey=NLuCX1X16ryXihd1oQCMIOe2D_1Q6mAp";
try {
// Make the request using fetch
const response = await fetch(url);
// Check if the response is ok (status code 200)
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
// Parse the JSON data
let data = await response.json();
console.log("successfully fetched, processing")
process(data);
} catch (error) {
// Handle any errors that occur during the fetch
console.error("Error fetching data:", error);
}
}
function process(data) {
// data has been fetched, analyze it
addChart(data)
Plotly.newPlot('graph', traces, layout);
console.log("Graphing...")
}
function timestampToDate(timestampLocal) {
let date = new Date(timestampLocal).toLocaleDateString().split("/");
return (date[2] + "-" + date[0] + "-" + date[1]) + " " + new Date(timestampLocal).toTimeString().split(" ")[0]
}
function addChart(data1) {
// Candlestick chart
graph = {
x: [],
close: [],
...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.