stock analysis

by frogggeee McFrogggeee

HTML

<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: [],
   ...