-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
135 lines (118 loc) · 4.09 KB
/
index.js
File metadata and controls
135 lines (118 loc) · 4.09 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
const axios = require('axios')
const { fstat } = require('fs')
const fs = require("fs")
const sleep = require('sleep-promise')
let list = new Array()
list.push(["GDP", "GDP"])
list.push(["PCE", "conGDP"])
list.push(["GCE", "govGDP"])
list.push(["GPDI", "invGDP"])
list.push(["EXPGS", "expGDP"])
list.push(["IMPGS", "impGDP"])
list.push(["NETEXP", "netexGDP"])
list.push(["USAGDPDEFQISMEI", "deflator"])
list.push(["POPTHM", "pop"])
list.push(["PSAVE", "persSave"])
list.push(["PSAVERT", "APS"])
list.push(["DSPI", "dispPersInc"])
list.push(["PI", "GY"])
list.push(["RPI", "Y"])
list.push(["DSPI", "GYd"])
list.push(["DPIC96", "Yd"])
list.push(["QTAXTOTALQTAXCAT1USYES", "tax"])
list.push(["GFDEBTN", "debt"])
list.push(["FEDFUNDS", "fedRate"])
list.push(["USSTHPI", "housePrice"])
list.push(["GDPC1", "realGDP"])
list.push(["GDPPOT", "realPotGDP"])
list.push(["PCEC96", "realPersCons"])
list.push(["GPDIC1", "realInv"])
list.push(["GCEC1", "realGov"])
list.push(["IMPGSC1", "realImp"])
list.push(["EXPGSC1", "realExp"])
list.push(["SP500", "sp500"])
list.push(["MORTGAGE30US", "mort"])
list.push(["NETEXC", "realNetEx"])
list.push(["W068RCQ027SBEA", "a1"])
list.push(["FGEXPND", "a2"])
list.push(["ND000342Q", "a3"])
list.push(["M2SL", "M2"])
list.push(["WM1NS", "M1"])
retrieveFred()
async function retrieveFred(){
let output = {}
for(let i = 0; i < list.length; i++){
console.log(i)
console.log(list[i])
let series = await getSeries(list[i][0])
let observations = await getObservations(list[i][0], series.frequency)
series.lengthD = observations.data.length
series.lengthL = observations.labels.length
series.data = observations.data
series.labels = observations.labels
if(series.frequency.indexOf("Quarterly") > -1){
series.frequency = series.frequency + " Changed to Monthly"
}
output[list[i][1]] = series
}
let toWrite = JSON.stringify(output, null, 4)
fs.writeFile("data/FREDdata.json", toWrite, function(err) {
if(err){ return console.log(err)}
console.log("The file was saved!")
})
}
function getSeries(id){
return new Promise((resolve, reject) => {
axios.get('https://api.stlouisfed.org/fred/series?series_id='+id+'&api_key=5f11050cc1fbd2254dcee4489d8ae9fa&file_type=json')
.catch(function (error){ if(error) {console.log("error")}})
.then(resp => {
resolve(resp.data.seriess[0])
})
})
}
function getObservations(id, frequency){
return new Promise((resolve, reject) => {
axios.get('https://api.stlouisfed.org/fred/series/observations?series_id='+id+'&api_key=5f11050cc1fbd2254dcee4489d8ae9fa&file_type=json')
.catch(function (error){ if(error) {console.log("error")}})
.then(resp => {
let obs = resp.data.observations
let dataArray = new Array()
let dateArray = new Array()
if(frequency.indexOf("Quarterly") > -1){
let j = 0
for(let i = 0; i < obs.length; i++){
let year = parseInt(obs[i].date.split('-')[0])
let month = parseInt(obs[i].date.split('-')[1]-1)
dataArray[j] = parseFloat(obs[i].value)
dateArray[j] = obs[i].date
let date = new Date(year, month+1, 1)
dateArray[j+1] = date.getFullYear() + "-" + '0'.concat(parseInt(date.getMonth()+1).toString()).slice(-2) + "-" + 01
date = new Date(year, month+2, 1)
dateArray[j+2] = date.getFullYear() + "-" + '0'.concat(parseInt(date.getMonth()+1).toString()).slice(-2) + "-" + 01
if(i < obs.length-1){
dataArray[j+1] = parseFloat((parseFloat(obs[i].value) + ((parseFloat(obs[i+1].value) - parseFloat(obs[i].value))*(1/3))))
dataArray[j+2] = parseFloat((parseFloat(obs[i].value) + ((parseFloat(obs[i+1].value) - parseFloat(obs[i].value))*(2/3))))
}else{
dataArray[j+1] = parseFloat(obs[i].value)
dataArray[j+2] = parseFloat(obs[i].value)
}
j+=3
}
}
else if(frequency.indexOf("Monthly") > -1){
for(let i = 0; i < obs.length; i++){
dataArray[i] = parseFloat(obs[i].value)
dateArray[i] = obs[i].date
}
}
else{
for(let i = 0; i < obs.length; i++){
dataArray[i] = parseFloat(obs[i].value)
dateArray[i] = obs[i].date
}
}
let all = {"data":dataArray, "labels":dateArray}
resolve(all)
})
})
}