-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetch-market-data.js
More file actions
39 lines (38 loc) · 1.25 KB
/
Copy pathfetch-market-data.js
File metadata and controls
39 lines (38 loc) · 1.25 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
const fetch = require("node-fetch");
const fs = require("fs");
require("dotenv").config();
fetch(
`https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=SP&apikey=${process.env.API_KEY}`
)
.then(res => res.json())
.then(data => {
const timeseries = data["Time Series (Daily)"];
const json = Object.keys(data["Time Series (Daily)"]).map(date => {
const open = (timeseries[date]["1. open"] * 100 - 1000)
.toFixed(2)
.toString();
const high = (timeseries[date]["2. high"] * 100 - 1000)
.toFixed(2)
.toString();
const low = (timeseries[date]["3. low"] * 100 - 1000)
.toFixed(2)
.toString();
const close = (timeseries[date]["4. close"] * 100 - 1000)
.toFixed(2)
.toString();
return {
x: date,
y: [
open > 2800 ? (open - 400).toString() : open,
high > 2800 ? (high - 400).toString() : high,
low > 2800 ? (low - 400).toString() : low,
close > 2800 ? (close - 400).toString() : close
]
};
});
fs.writeFile("data.json", JSON.stringify(json.reverse()), err => {
if (err) throw err;
console.log("Written to data.json");
});
})
.catch(err => console.log(err.message));