-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStock Monitor.js
More file actions
76 lines (64 loc) · 2.78 KB
/
Stock Monitor.js
File metadata and controls
76 lines (64 loc) · 2.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
document.addEventListener('DOMContentLoaded', function () {
const apiKey = 'API_KEY';
const ticker = 'NVDA';
const apiUrl = `https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol=${ticker}&interval=1min&apikey=${apiKey}`;
// Fetch price
const fetchAndRenderStockData = async () => {
try {
const response = await fetch(apiUrl);
const data = await response.json();
const timeSeries = data['Time Series (1min)'];
const labels = Object.keys(timeSeries).reverse(); // Reverse to display in chronological order
const prices = labels.map(label => parseFloat(timeSeries[label]['4. close']));
const highs = labels.map(label => parseFloat(timeSeries[label]['2. high']));
const lows = labels.map(label => parseFloat(timeSeries[label]['3. low']));
document.getElementById('ticker').textContent = `${ticker}`;
document.getElementById('latest').textContent = `$${prices[0]}`;
document.getElementById(`high`).textContent = `$${Math.max(...highs).toFixed(3)}`;
document.getElementById(`low`).textContent = `$${Math.min(...lows).toFixed(3)}`;
const chart = window.myLine;
chart.data.labels = labels;
chart.data.datasets[0].data = prices;
chart.update();
} catch (error) {
console.error('Error fetching stock data:', error);
}
};
fetchAndRenderStockData();
setInterval(fetchAndRenderStockData, 3600000);
// Creating chart using Chart.js
const ctx = document.getElementById('stockChart').getContext('2d');
window.myLine = new Chart(ctx, {
type: 'line',
data: {
labels: [],
datasets: [{
label: 'Stock Prices',
borderColor: '#eb052b',
data: [],
}],
},
});
// Clock
const updateClock = () => {
const now = new Date();
const hours = now.getHours().toString().padStart(2, '0');
const minutes = now.getMinutes().toString().padStart(2, '0');
const seconds = now.getSeconds().toString().padStart(2, '0');
const formattedTime = `${hours}:${minutes}:${seconds}`;
document.getElementById('clock').textContent = formattedTime;
};
// Synch with Google's time server
const syncWithGoogleTime = async () => {
try {
const response = await fetch('https://www.googleapis.com');
const serverTime = new Date(response.headers.get('date'));
updateClock(serverTime);
} catch (error) {
console.error('Error syncing with Google Time Server:', error);
}
};
updateClock();
setInterval(updateClock, 1000);
setInterval(syncWithGoogleTime, 3600000);
});