forked from pilwon/node-yahoo-finance
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
350 lines (315 loc) · 11.3 KB
/
index.js
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
var os = require('os');
var util = require('util');
var _ = require('lodash');
var S = require('string');
var moment = require('moment');
var Promise = require('bluebird');
var _constants = require('./constants');
var _fields = require('./fields');
var _utils = require('./utils');
var _cookies = require('./financeCookies');
function _sanitizeHistoricalOptions(options) {
if (!_.isPlainObject(options)) {
throw new Error('"options" must be a plain object.');
}
if (_.isUndefined(options.symbol) && _.isUndefined(options.symbols)) {
throw new Error('Either "options.symbol" or "options.symbols" must be defined.');
}
if (!_.isUndefined(options.symbol) && !_.isUndefined(options.symbols)) {
throw new Error('Either "options.symbol" or "options.symbols" must be undefined.');
}
if (!_.isUndefined(options.error) && !_.isBoolean(options.error)) {
throw new Error('"options.error" must be a boolean value');
}
if (!_.isUndefined(options.symbol)) {
if (!_.isString(options.symbol) || _.isEmpty(options.symbol)) {
throw new Error('"options.symbol" must be a non-empty string.');
}
} else {
if (!_.isArray(options.symbols) || _.isEmpty(options.symbols)) {
throw new Error('"options.symbols" must be a non-empty string array.');
}
}
if (_.isString(options.from) && !_.isEmpty(options.from)) {
options.from = moment(options.from);
if (!options.from.isValid()) {
throw new Error('"options.from" must be a valid date string.');
}
} else {
if (!_.isDate(options.from) && !_.isUndefined(options.from) && !_.isNull(options.from)) {
throw new Error('"options.from" must be a date or undefined/null.');
}
if (_.isDate(options.from)) {
options.from = moment(options.from);
}
}
if (_.isString(options.to) && !_.isEmpty(options.to)) {
options.to = moment(options.to);
if (!options.to.isValid()) {
throw new Error('"options.to" must be a valid date string.');
}
} else {
if (!_.isDate(options.to) && !_.isUndefined(options.to) && !_.isNull(options.to)) {
throw new Error('"options.to" must be a date or undefined/null.');
}
if (_.isDate(options.to)) {
options.to = moment(options.to);
}
}
if (_.isString(options.period)) {
if (!_.includes(['d', 'w', 'm', 'v'], options.period)) {
throw new Error('"options.period" must be "d", "w", "m", or "v".');
}
} else {
if (!_.isUndefined(options.period) && !_.isNull(options.period)) {
throw new Error('"options.period" must be a string or undefined/null.');
}
}
if (!options.from) {
options.from = moment('1900-01-01');
}
if (!options.to) {
options.to = moment({ hour: 0 });
}
if (!options.period) {
options.period = 'd';
}
options.events = 'history';
// Convert to yahoo v7 API
switch (options.period) {
case 'd': options.period = '1d'; break;
case 'w': options.period = '1wk'; break;
case 'm': options.period = '1mo'; break;
case 'v': options.period = '1d'; options.events = 'div'; break;
// TODO
}
if ((options.from || options.to) && options.from.isAfter(options.to)) {
throw new Error('"options.to" must be be greater than or equal to "options.from".');
}
}
function _sanitizeSnapshotOptions(options) {
if (!_.isPlainObject(options)) {
throw new Error('"options" must be a plain object.');
}
if (_.isUndefined(options.symbol) && _.isUndefined(options.symbols)) {
throw new Error('Either "options.symbol" or "options.symbols" must be defined.');
}
if (!_.isUndefined(options.symbol) && !_.isUndefined(options.symbols)) {
throw new Error('Either "options.symbol" or "options.symbols" must be undefined.');
}
if (!_.isUndefined(options.symbol)) {
if (!_.isString(options.symbol) || _.isEmpty(options.symbol)) {
throw new Error('"options.symbol" must be a non-empty string.');
}
} else {
if (!_.isArray(options.symbols) || _.isEmpty(options.symbols)) {
throw new Error('"options.symbols" must be a non-empty string array.');
}
}
if ((!_.isArray(options.fields) || _.isEmpty(options.fields)) && !_.isUndefined(options.fields)) {
throw new Error('"options.fields" must be a non-empty string array or undefined.');
}
if (!options.fields) {
// fetch all fields if undefined
options.fields = _.keys(_fields).sort();
}
// Avoid CSV column result mis-alignment (000,000,000).
options.fields = _.without(options.fields, 't6', 'f6', 'j2', 'a5', 'b6', 'k3', 's6');
// Ensure error field exists.
options.fields = _.union(options.fields, ['e1']);
}
function _transformHistorical(symbol, data) {
var headings = data.shift();
return _(data)
.reverse()
.map(function (line) {
var result = {};
headings.forEach(function (heading, i) {
var value = line[i];
if (_.includes(['Volume'], heading)) {
value = _utils.toInt(value, null);
} else if (_.includes(['Open', 'High', 'Low', 'Close', 'Adj Close', 'Dividends'], heading)) {
value = _utils.toFloat(value, null);
} else if (_.includes(['Date'], heading)) {
value = _utils.toDate(value, null);
if (value && !moment(value).isValid()) {
value = null;
}
}
result[_utils.camelize(heading)] = value;
});
result.symbol = symbol;
return result;
})
.value();
}
function _transformSnapshot(fields, symbols, data) {
return _(data)
.map(function (line, i) {
var result = {
symbol: symbols[i]
};
var lineIdx = 0;
var field;
var fieldIdx;
var value;
for (fieldIdx = 0; fieldIdx < fields.length; ++fieldIdx) {
field = fields[fieldIdx];
value = line[lineIdx++];
value = S(value).chompLeft('N/A - ').s;
if (_.includes(['-', '- - -', 'N/A'], value)) {
value = null;
}
if (field === 'e1') {
if (value) {
return value;
} else {
continue;
}
}
if (value && (S(value).startsWith('+') || S(value).startsWith('-')) && S(value).endsWith('%')) {
value = _utils.toFloat(S(value).chompRight('%').s, null);
if (value) {
value /= 100;
}
}
if (_.includes(['a2', 'v'], field)) {
value = _utils.toInt(value, null);
} else if (_.includes([
'a', 'b', 'b2', 'b3', 'b4', 'c1', 'c6', 'd', 'e', 'e7', 'e8',
'e9','g', 'h', 'j', 'j5', 'k', 'k4', 'l1', 'm3', 'm4', 'm5',
'm7', 'o', 'p', 'p5', 'p6', 'r', 'r5', 'r6', 'r7', 't8', 'y'
], field)) {
value = _utils.toFloat(value, null);
} else if (_.includes(['d1'], field)) {
value = _utils.toDate(value, null);
if (value && !moment(value).isValid()) {
value = null;
}
}
result[_utils.camelize(_fields[field])] = value;
}
if (line.length !== lineIdx) {
throw new Error('CSV column mis-alignment error');
}
return result;
})
.value();
}
function augmentHttpRequestOptions(optionalOptions) {
const finalOptions = optionalOptions ? _.clone(optionalOptions) : {};
if (finalOptions.jar)
throw new Error("node-yahoo-finance does not support 'jar' key in " +
"optionalHttpRequestOptions, since we need to use our own cookiejar.");
finalOptions.jar = _cookies.jar;
return finalOptions;
}
function historical(options, optionalHttpRequestOptions, cb) {
options = _.clone(options);
_sanitizeHistoricalOptions(options);
var symbols = options.symbols || _.flatten([options.symbol]);
if(optionalHttpRequestOptions && typeof optionalHttpRequestOptions === 'function') {
cb = optionalHttpRequestOptions;
optionalHttpRequestOptions = undefined;
}
var finalHttpRequestOptions = augmentHttpRequestOptions(optionalHttpRequestOptions);
return _cookies.getCrumb(symbols[0])
.then(function(crumb) {
return Promise.map(symbols, function (symbol) {
var url = _constants.HISTORICAL_DOWNLOAD_URL.replace(/\$SYMBOL/, symbol);
return _utils.download(url, {
period1: options.from.format('X'),
period2: options.to.format('X'),
interval: options.period,
events: options.events,
crumb: crumb
}, finalHttpRequestOptions)
.then(_utils.parseCSV)
.then(function (data) {
return _transformHistorical(symbol, data);
})
.catch(function (err) {
if (options.error) {
throw err;
} else {
return [];
}
});
}, {concurrency: options.maxConcurrentSymbols || os.cpus().length})
.then(function (result) {
if (options.symbols) {
return _.zipObject(symbols, result);
} else {
return result[0];
}
})
.catch(function (err) {
throw new Error(util.format('Failed to download data (%s)', err.message));
})
.nodeify(cb);
});
}
function snapshot(options, optionalHttpRequestOptions, cb) {
var symbols = options.symbols || _.flatten([options.symbol]);
options = _.clone(options);
_sanitizeSnapshotOptions(options);
if(optionalHttpRequestOptions && typeof optionalHttpRequestOptions == 'function') {
cb = optionalHttpRequestOptions;
optionalHttpRequestOptions = undefined;
}
var finalHttpRequestOptions = augmentHttpRequestOptions(optionalHttpRequestOptions);
finalHttpRequestOptions.json = true;
if (symbols.length > 1)
throw new Error("TODO multi symbol support, requires multiple requests in new API");
return _cookies.getCrumb(symbols[0])
.then(function(crumb) {
var url = _constants.SNAPSHOT_URL.replace(/\$SYMBOL/, symbols[0]);
return _utils.download(url, {
// f: options.fields.join('') TODO
formatted: 'false',
crumb: crumb,
// summaryProfile,m financialData, recommendationTrend, upgradeDowngradeHistory
// earnings, price, summaryDetail, defaultKeyStatistics, calendarEvents
modules: 'price,summaryDetail',
corsDomain: 'finance.yahoo.com'
}, finalHttpRequestOptions)
.then(function(result) {
var quoteSummary = result.quoteSummary;
if (!quoteSummary || quoteSummary.error)
throw new Error(quoteSummary.error);
var result = quoteSummary.result;
if (!_.isArray(result) || result.length > 1)
throw new Error("quoteSummary format has changed, please report "
+ "this.");
return result[0];
})
/*
.then(_utils.parseCSV)
.then(function (data) {
return _transformSnapshot(options.fields, symbols, data);
})
*/
.then(function (results) {
if (options.symbols) {
// TODO
return _(symbols)
.zipObject(results)
.filter(function (result) {
return _.isPlainObject(result);
})
.value();
} else if (_.isPlainObject(results)) {
// TODO, return old format?
return results;
} else {
throw new Error(results);
}
})
.catch(function (err) {
throw new Error(util.format('Failed to download data (%s)', err.message));
})
.nodeify(cb);
});
}
exports.historical = historical;
exports.snapshot = snapshot;