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>
  <script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"></script>

</head>

<body>
<style>
  .inline-elements {
    display: flex;
    align-items: center;
  }

  .inline-elements p {
    margin: 0;
    border: 0px solid;
  }
</style>

<div class="inline-elements">
  <button onclick="change(-1);">
    -1
  </button>
  <p id="info">
    info
  </p>
  <button onclick="change(1);">
    +1
  </button>
</div>

	<div id='graph'><!-- Plotly chart will be drawn inside this DIV --></div>

</body>
<button onclick = "button();">
Button
</button>

JavaScript

let ticker = "C:EURUSD";
/*
Tickers:
C:EURUSD
X:BTCUSD
*/

let startDate = new Date(Date.UTC(2023, 4, 2, 13, 30))
// Get the millisecond timestamp
let timestamp = formatDate(startDate.getTime());
let endDate = startDate;
// Get the millisecond timestamp
let timestamp2 = formatDate(endDate.getTime());
let dateEST = new Date(timestamp + "T09:30:00-05:00"); // ISO format with EST offset (-05:00)
let uTimestampOpen = Math.floor(dateEST.getTime());
let dateEST2 = new Date(timestamp2 + "T16:00:00-05:00"); // ISO format with EST offset (-05:00)
let uTimestampClose = Math.floor(dateEST2.getTime());
document.getElementById("info").innerHTML = timestamp;

let aiData = {
	x: [],
	open: [],
	close: [],
	high: [],
	low: [],
	ma: [],
	volume: [],
	rsi: [],
  labels: []
};
let layout;
let graph;
let traces = [];
let labels; // buy/sell labels for the machine learning program

function findPeaksAndTroughs(graphY, graphX, threshold) {
  let peaks = [];
  let troughs = [];
  let xValues = [];

  let trackingHigh = true; // Start by tracking a high (peak)
  let lastHigh = graphY[0];
  let lastHighX = graphX[0];
  let lastLow = graphY[0];
  let lastLowX = graphX[0];
  let results = {
    x: [...graphX],
    y: [0]
  };

  for (let i = 1; i < graphY.length; i++) {
    results.y.push(0);
    let current = graphY[i];

    if (trackingHigh) {
      // Check if we have a new high
      if (current > lastHigh) {
        lastHigh = current;
        lastHighX = graphX[i];
      } else if (lastHigh - current > threshold) {
        // A significant drop has occurred, so the previous high is a peak
        peaks.push(lastHigh);
        troughs.push(0);
        xValues.push(lastHighX);
        results.y[results.x.indexOf(lastHighX)] = (-1);
        addAnn(lastHigh, lastHighX, "sell", "red");


        // Switch to tracking a low (trough)
        trackingHigh = false;
        lastLow = current;
        lastLowX = graphX[i];
      }
    } else {
      // Check if we have a new low
      if...