-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapiInterface.js
More file actions
288 lines (245 loc) · 10.1 KB
/
Copy pathapiInterface.js
File metadata and controls
288 lines (245 loc) · 10.1 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
var MSF = require("./MSF");
var fs = require('fs');
//API Credentials
var username = "eikonstudent33@thomsonreuters.com";
var password = "Secret123";
var msf = new MSF(true);
//Variables representing start & end dates for the financial mention query
var start = "2014-11-19T00:00:00";
var end = "2014-11-19T23:59:59";
var mentionVar = 3;
//Variable to aggregate headline queries into
var newsArr = [];
//Global variables to capture information to be returned
var mentionTop = [];
var priceHistory = [];
var outstandingShs = [];
//Variables representing callback & response object (can make calls to this to display data)
var callbackFunc;
//var responseObject = {};
//Variables to track when price history & outstanding shares queries have completed
var priceHistoryCount = 0;
var outstandingShsCount = 0;
var mentionsComplete = false;
/*
* Variable to keep track of the last story date
* (to work out when we have successfully downloaded all stories for a given period)
*/
var prevlastArticleTimestamp = "";
/*
* Variable to keep track of the current price history slice
* (to work out when we have successfully downloaded all price histories for a given period)
*/
var tickerList = [];
var origTickerList = [];
/* Recursively called function to aggregate blocks of 20 news articles
* into a list representing all for the period (initially called by getHeadlineData() )
*/
function makeNewsRequest() {
msf.getData(
{
"Entity": {
"E": "NewsArticles",
"W": {
"Codes": "A:1",
"HeadlineLang": "L:en",
"LastStoryDate": "0001-01-01T01:00:00",
"Order": "ToEnd",
"Repository": "reuters",
"HeadlinesOnly": false,
"StartTime": start,
"EndTime": end,
"BrokerResearch": false
}
}
}, function (error, response) {
if (!error) {
//Find the timestamp of the last article in the group of 20
var lastArticleTimestamp = response.Headlines[response.Headlines.length - 1].LastUpdateDateTime;
//Give some indication of progress on console
console.log(lastArticleTimestamp);
//Check whether we have re-downloaded the same articles as before
if (lastArticleTimestamp !== prevlastArticleTimestamp) {
//Push each of the articles up into the newsArr array
for (var i = 0; i < response.Headlines.length; i++) {
newsArr.push(response.Headlines[i]);
}
end = lastArticleTimestamp;
prevlastArticleTimestamp = lastArticleTimestamp
makeNewsRequest();
} else {
//We have finished aggregating the news articles
//Import the mentionCounter module
var mentionCounter = require('./mentionCount.js');
/*Create an array of
* [{ "ticker":"GPRO.O", "mentions":2 },......]
* representing the number of times each ticker is found in the news article list
*/
var mentionCount = mentionCounter.count(newsArr);
//Filter out the noise of companies mentioned only once or twice
mentionTop = (mentionCount.filter(function (el) {
return el.mentions >= mentionVar;
}));
// Extract the list of tickers from the more-often mentioned companies (>=3)
tickerList = mentionTop.map(function (node) { return node.ticker; });
for (var j = 0; j < tickerList.length; j++) {
origTickerList.push(tickerList[j]);
mentionsComplete = true;
//makePriceRequest(tickerList);
makeSplicedPriceRequest();
}
}
}
});
}
function makeSplicedPriceRequest() {
var thisSplice = tickerList.splice(0, 10);
priceHistoryCount++;
console.log(priceHistoryCount);
msf.getData({
"Entity": {
"E": "TATimeSeries",
"W": {
"Tickers": thisSplice,
"NoInfo": false,
"Interval": "Daily",
"IntervalMultiplier": 1,
"DateRangeMultiplier": 1,
"StartDate": start,
"EndDate": start,
"Analysis": [
"OHLCV"
]
}
}
}, function (error, response) {
if (!error) {
var hist = response.R.filter(function (filterNode) {
return (filterNode.Data.length != 0);
}).map(function (node) {
var obj = {};
obj.ticker = node.Ticker;
var open = parseFloat(node.Data[0].Open);
obj.close = parseFloat(node.Data[0].Close);
obj.performance = (obj.close - open) / open;
obj.date = node.Data[0].Date;
return obj;
});
for (var i = 0; i < hist.length; i++) {
priceHistory.push(hist[i]);
}
makeOutstandingSharesRequest(thisSplice);
}
});
}
function makeOutstandingSharesRequest(tickers) {
outstandingShsCount++;
console.log(outstandingShsCount);
msf.getData({
"Entity": {
"E": "Financials",
"W": {
"Symbols": tickers,
"COACodes": [
"QTCO"
],
"Type": "ABS",
"NumberOfPeriods": 1,
"ReportBasis": "A",
"SymbolType": "RIC"
}
}
}, function (error, response) {
if (!error) {
console.log("Processing outstanding request");
outstandingShs = response[""].filter(function (em) {
return (em.Error == undefined);
}).map(function (node) {
return shareInfo = { "ticker": node.MsfId , "outstandingShares" : getOutstandingShares(node) };
});
tickers.forEach(function (ticker) {
if (outstandingShs.map(function (item) { return item.ticker; }).indexOf(ticker) == -1) {
outstandingShs.push(shareInfo = { "ticker": ticker, "outstandingShares" : 0 });
}
});
if (tickerList.length != 0) {
makeSplicedPriceRequest();
} else {
outstandingShs = outstandingShs.filter(function (node) {
return (node.outstandingShares !== 0);
});
zipArrays();
}
}
});
}
function getOutstandingShares(financialData) {
var outstandingShares = 0;
var units = "O";
try {
console.log(financialData.MsfId);
} catch (err) { }
var shares = 0;
try {
if (financialData.StandardizedFinancials.FinancialInformation.FinancialStatements.Period != undefined) {
if (financialData.StandardizedFinancials.FinancialInformation.FinancialStatements.Period[0].PeriodFilings.PeriodFiling != undefined) {
units = financialData.StandardizedFinancials.FinancialInformation
.FinancialStatements.Period[0].PeriodFilings.PeriodFiling[0].PeriodFilingHeader
.Units.ConvertedTo;
}
}
if (financialData.StandardizedFinancials.FinancialInformation.FinancialStatements.Period[0].PeriodFilings.PeriodFiling[0].Statements.Statement[0].FinancialValues.FV != undefined) {
shares = financialData.StandardizedFinancials.FinancialInformation
.FinancialStatements.Period[0].PeriodFilings.PeriodFiling[0].Statements
.Statement[0].FinancialValues.FV[0].___MSFVALUE;
}
} catch (err) {
}
if (units == "M") {
outstandingShares = shares * 1000000;
} else if (units == "T") {
outstandingShares = shares * 1000;
} else if (units == "O") {
outstandingShares = shares;
}
return outstandingShares;
}
function zipArrays() {
var respJson = { "mentions": mentionTop, "performance": priceHistory, "outstandingShares": outstandingShs };
var aggregate = [];
respJson.mentions.forEach(function (node) {
aggregate.push(node);
});
respJson.performance.forEach(function (node) {
var tickerPos = aggregate.map(function (aggregateNode) {
return aggregateNode.ticker;
}).indexOf(node.ticker);
if (tickerPos != -1) {
aggregate[tickerPos].performance = node.performance;
aggregate[tickerPos].date = node.date;
aggregate[tickerPos].close = node.close;
}
});
respJson.outstandingShares.forEach(function (node) {
var tickerPos = aggregate.map(function (aggregateNode) {
return aggregateNode.ticker;
}).indexOf(node.ticker);
if (tickerPos != -1) {
aggregate[tickerPos].marketCap = aggregate[tickerPos].close * node.outstandingShares;
}
});
aggregate = aggregate.filter(function (node) {
return (node.marketCap != undefined && node.marketCap != null);
});
fs.writeFile("aggregated-file-data-19-11.json", JSON.stringify(aggregate, false, 2));
callbackFunc(aggregate);
}
//Externally exposed function called by server.js to initiate download procedures
exports.getHeadlineData = function (callback) {
msf.login(username, password, function (error) {
if (!error) {
callbackFunc = callback;
makeNewsRequest();
}
});
}