1 | const LibraryError = require('../globals/LibraryError');
|
2 | const Quote = require('../globals/Quote');
|
3 | const Match = require('../globals/Match');
|
4 | const request = require('request');
|
5 | const _ = require('lodash');
|
6 |
|
7 | /**
|
8 | * Further documentation can be found here: https://www.alphavantage.co/documentation/
|
9 | */
|
10 | class AlphaVantage {
|
11 |
|
12 | /**
|
13 | * Creates a new AlphaVantage instance.
|
14 | * @author Torrey Leonard <https://github.com/Ladinn>
|
15 | * @constructor
|
16 | * @param {String} apiKey - The free API key retrieved from
|
17 | */
|
18 | constructor(apiKey) {
|
19 | this.apiKey = apiKey;
|
20 | this.url = "https://www.alphavantage.co/query"
|
21 | }
|
22 |
|
23 | /**
|
24 | * @author Torrey Leonard <https://github.com/Ladinn>
|
25 | * @param {Object} qs The query string object to pass along to the request module
|
26 | * @param {string} objectKeyOverride Set this to the name of the property in the response object that you wish to return.
|
27 | * Leave empty to let _requester use it's default implementation
|
28 | * @private
|
29 | */
|
30 | _requester(qs, objectKeyOverride = undefined) {
|
31 | const _this = this;
|
32 | return new Promise((resolve, reject) => {
|
33 | qs.apikey = _this.apiKey;
|
34 | qs.datatype = "json";
|
35 | request({
|
36 | uri: _this.url,
|
37 | qs: qs
|
38 | }, (error, response, body ) => {
|
39 | if (error) reject(error);
|
40 | else if (body.indexOf("application-error.html") !== -1) reject(new LibraryError("The Alpha Vantage servers are overloaded. Please try again."));
|
41 | else if (response.statusCode !== 200) reject(body);
|
42 | else {
|
43 | const json = JSON.parse(body);
|
44 | const objectKey = objectKeyOverride ? objectKeyOverride : Object.keys(json)[1];
|
45 | resolve(json[objectKey]);
|
46 | }
|
47 | })
|
48 | });
|
49 | }
|
50 |
|
51 | /**
|
52 | * Returns an array of objects showing historical and real time S&P sector performance.
|
53 | * @author Torrey Leonard <https://github.com/Ladinn>
|
54 | * @returns {Promise}
|
55 | */
|
56 | sectorPerformance() {
|
57 | const _this = this;
|
58 | return new Promise((resolve, reject) => {
|
59 | request({
|
60 | uri: _this.url,
|
61 | qs: {
|
62 | apikey: _this.apiKey,
|
63 | datatype: "json",
|
64 | function: "SECTOR"
|
65 | }
|
66 | }, (error, response, body ) => {
|
67 | if (error) reject(error);
|
68 | else if (body.indexOf("application-error.html") !== -1) reject(new LibraryError("The Alpha Vantage servers are overloaded. Please try again."));
|
69 | else if (response.statusCode !== 200) reject(body);
|
70 | else {
|
71 | resolve(JSON.parse(body));
|
72 | }
|
73 | })
|
74 | });
|
75 | }
|
76 |
|
77 | /**
|
78 | * Returns an array of quotes for the equity specified, updated in real time.
|
79 | * @author Torrey Leonard <https://github.com/Ladinn>
|
80 | * @param {String} symbol
|
81 | * @param {String} interval - How long each quote should represent: 1min, 5min, 15min, 30min, 60min
|
82 | * @returns {Promise.<Array>}
|
83 | */
|
84 | timeSeriesIntraday(symbol, interval) {
|
85 | return this._requester({
|
86 | function: "TIME_SERIES_INTRADAY",
|
87 | symbol: symbol,
|
88 | interval: interval
|
89 | }).then(res => {
|
90 | let array = [];
|
91 | for (const key in res) {
|
92 | if (res.hasOwnProperty(key)) {
|
93 | const o = res[key];
|
94 | array.push(new Quote({
|
95 | symbol: symbol,
|
96 | date: new Date(key),
|
97 | source: "Alpha Vantage",
|
98 | price: {
|
99 | open: Number(o["1. open"]),
|
100 | high: Number(o["2. high"]),
|
101 | low: Number(o["3. low"]),
|
102 | close: Number(o["4. close"]),
|
103 | volume: Number(o["5. volume"])
|
104 | },
|
105 | original: JSON.stringify(o)
|
106 | }))
|
107 | }
|
108 | }
|
109 | return _.sortBy(array, 'date');
|
110 | })
|
111 | }
|
112 |
|
113 | /**
|
114 | * Returns an array of quotes for the equity specified, covering up to 20 years of historical data.
|
115 | * @author Torrey Leonard <https://github.com/Ladinn>
|
116 | * @param {String} symbol
|
117 | * @param {Boolean} compact - If true, this will return the last 100 data points. If false, it will return up to 20 years of historical data.
|
118 | * @param {Boolean} adjusted - If true, prices will be adjusted for split/dividend events.
|
119 | * @returns {Promise<Array>}
|
120 | */
|
121 | timeSeriesDaily(symbol, compact, adjusted) {
|
122 | return this._requester({
|
123 | function: adjusted ? "TIME_SERIES_DAILY_ADJUSTED" : "TIME_SERIES_DAILY",
|
124 | symbol: symbol,
|
125 | outputsize: compact ? "compact" : "full"
|
126 | }).then(res => {
|
127 | let array = [];
|
128 | for (const key in res) {
|
129 | if (res.hasOwnProperty(key)) {
|
130 | const o = res[key];
|
131 | if (adjusted) array.push(new Quote(
|
132 | {
|
133 | symbol: symbol,
|
134 | date: new Date(key),
|
135 | source: "Alpha Vantage",
|
136 | price: {
|
137 | open: Number(o["1. open"]),
|
138 | high: Number(o["2. high"]),
|
139 | low: Number(o["3. low"]),
|
140 | close: Number(o["4. close"]),
|
141 | volume: Number(o["6. volume"]),
|
142 | adjustedClose: Number(o["5. adjusted close"])
|
143 | },
|
144 | meta: {
|
145 | dividendAmount: o["7. dividend amount"],
|
146 | splitCoefficient: o["8. split coefficient"]
|
147 | },
|
148 | original: JSON.stringify(o)
|
149 | }
|
150 | ));
|
151 | else array.push(new Quote(
|
152 | {
|
153 | symbol: symbol,
|
154 | date: new Date(key),
|
155 | source: "Alpha Vantage",
|
156 | price: {
|
157 | open: Number(o["1. open"]),
|
158 | high: Number(o["2. high"]),
|
159 | low: Number(o["3. low"]),
|
160 | close: Number(o["4. close"]),
|
161 | volume: Number(o["5. volume"])
|
162 | },
|
163 | original: JSON.stringify(o)
|
164 | }
|
165 | ))
|
166 | }
|
167 | }
|
168 | return _.sortBy(array, 'date');;
|
169 | })
|
170 | }
|
171 |
|
172 | /**
|
173 | * Returns an array of quotes for the equity specified, covering up to 20 years of historical data.
|
174 | * @author Torrey Leonard <https://github.com/Ladinn>
|
175 | * @param {String} symbol
|
176 | * @param {Boolean} adjusted - If true, prices will be adjusted for split/dividend events.
|
177 | * @returns {Promise<Array>}
|
178 | */
|
179 | timeSeriesWeekly(symbol, adjusted) {
|
180 | return this._requester({
|
181 | function: adjusted ? "TIME_SERIES_WEEKLY_ADJUSTED" : "TIME_SERIES_WEEKLY",
|
182 | symbol: symbol
|
183 | }).then(res => {
|
184 | let array = [];
|
185 | for (const key in res) {
|
186 | if (res.hasOwnProperty(key)) {
|
187 | const o = res[key];
|
188 | if (adjusted) array.push(new Quote(
|
189 | {
|
190 | symbol: symbol,
|
191 | date: new Date(key),
|
192 | source: "Alpha Vantage",
|
193 | price: {
|
194 | open: Number(o["1. open"]),
|
195 | high: Number(o["2. high"]),
|
196 | low: Number(o["3. low"]),
|
197 | close: Number(o["4. close"]),
|
198 | volume: Number(o["6. volume"]),
|
199 | adjustedClose: Number(o["5. adjusted close"])
|
200 | },
|
201 | meta: {
|
202 | dividendAmount: o["7. dividend amount"],
|
203 | splitCoefficient: o["8. split coefficient"]
|
204 | },
|
205 | original: o
|
206 | }
|
207 | ));
|
208 | else array.push(new Quote(
|
209 | {
|
210 | symbol: symbol,
|
211 | date: new Date(key),
|
212 | source: "Alpha Vantage",
|
213 | price: {
|
214 | open: Number(o["1. open"]),
|
215 | high: Number(o["2. high"]),
|
216 | low: Number(o["3. low"]),
|
217 | close: Number(o["4. close"]),
|
218 | volume: Number(o["5. volume"])
|
219 | },
|
220 | original: o
|
221 | }
|
222 | ))
|
223 | }
|
224 | }
|
225 | return _.sortBy(array, 'date');;
|
226 | })
|
227 | }
|
228 |
|
229 | /**
|
230 | * Returns an array of quotes for the equity specified, covering up to 20 years of historical data.
|
231 | * @author Torrey Leonard <https://github.com/Ladinn>
|
232 | * @param {String} symbol
|
233 | * @param {Boolean} adjusted - If true, prices will be adjusted for split/dividend events.
|
234 | * @returns {Promise<Array>}
|
235 | */
|
236 | timeSeriesMonthly(symbol, adjusted) {
|
237 | return this._requester({
|
238 | function: adjusted ? "TIME_SERIES_MONTHLY_ADJUSTED" : "TIME_SERIES_MONTHLY",
|
239 | symbol: symbol
|
240 | }).then(res => {
|
241 | let array = [];
|
242 | for (const key in res) {
|
243 | if (res.hasOwnProperty(key)) {
|
244 | const o = res[key];
|
245 | if (adjusted) array.push(new Quote(
|
246 | {
|
247 | symbol: symbol,
|
248 | date: new Date(key),
|
249 | source: "Alpha Vantage",
|
250 | price: {
|
251 | open: Number(o["1. open"]),
|
252 | high: Number(o["2. high"]),
|
253 | low: Number(o["3. low"]),
|
254 | close: Number(o["4. close"]),
|
255 | volume: Number(o["6. volume"]),
|
256 | adjustedClose: Number(o["5. adjusted close"])
|
257 | },
|
258 | meta: {
|
259 | dividendAmount: o["7. dividend amount"],
|
260 | splitCoefficient: o["8. split coefficient"]
|
261 | },
|
262 | original: o
|
263 | }
|
264 | ));
|
265 | else array.push(new Quote(
|
266 | {
|
267 | symbol: symbol,
|
268 | date: new Date(key),
|
269 | source: "Alpha Vantage",
|
270 | price: {
|
271 | open: Number(o["1. open"]),
|
272 | high: Number(o["2. high"]),
|
273 | low: Number(o["3. low"]),
|
274 | close: Number(o["4. close"]),
|
275 | volume: Number(o["5. volume"])
|
276 | },
|
277 | original: o
|
278 | }
|
279 | ))
|
280 | }
|
281 | }
|
282 | return _.sortBy(array, 'date');
|
283 | })
|
284 | }
|
285 |
|
286 | /**
|
287 | * Returns an array of search results based off your keyword search string
|
288 | * @author Nicklas Laine Overgaard <https://github.com/nover>
|
289 | * @param {String} keyword The search keyword(s), e.g 'Microsoft' or 'Toshiba'
|
290 | * @returns {Promise<Array{Match}>}
|
291 | */
|
292 | search(keyword) {
|
293 | return this._requester({
|
294 | function: 'SYMBOL_SEARCH',
|
295 | keywords: keyword,
|
296 | }, 'bestMatches').then(res => {
|
297 | const array = [];
|
298 | for (const data of res) {
|
299 | array.push(new Match(data));
|
300 | }
|
301 | return array;
|
302 | })
|
303 | }
|
304 |
|
305 | /**
|
306 | * Get a price quote from the market for the given symbol
|
307 | * @author Nicklas Laine Overgaard <https://github.com/nover>
|
308 | * @param {String} symbol The symbol to get a the current quote price for, e.g AAPL
|
309 | * @returns {Promise<Quote>} A quote instance
|
310 | */
|
311 | quote(symbol) {
|
312 | return this._requester({
|
313 | function: 'GLOBAL_QUOTE',
|
314 | symbol
|
315 | }, 'Global Quote').then(res => {
|
316 | return new Quote({
|
317 | symbol: res['01. symbol'],
|
318 | source: 'Alpha Vantage',
|
319 | date: res['07. latest trading day'],
|
320 | price: {
|
321 | open: Number(res['02. open']),
|
322 | high: Number(res['03. high']),
|
323 | low: Number(res['04. low']),
|
324 | last: Number(res['05. price']),
|
325 | volume: Number(res['06. volume']),
|
326 | },
|
327 | original: JSON.stringify(res),
|
328 | meta: {
|
329 | previousClose: Number(res['08. previous close']),
|
330 | change: Number(res['09. change']),
|
331 | changePercent: res['10. change percent'],
|
332 | }
|
333 | });
|
334 | });
|
335 | }
|
336 |
|
337 | // TECHNICALS
|
338 |
|
339 | /**
|
340 | * @author Torrey Leonard <https://github.com/Ladinn>
|
341 | * @private
|
342 | */
|
343 | _technical(type, symbol, interval, timePeriod, seriesType, qs) {
|
344 | let query = {
|
345 | function: type,
|
346 | symbol: symbol,
|
347 | interval: interval,
|
348 | time_period: timePeriod,
|
349 | series_type: seriesType
|
350 | };
|
351 | if (qs) qs.forEach(q => {
|
352 | query[q.key] = q.val;
|
353 | });
|
354 | return this._requester(query).then(res => {
|
355 | let array = [];
|
356 | for (const key in res) {
|
357 | if (res.hasOwnProperty(key)) {
|
358 | const o = res[key];
|
359 | let newObject = { date: new Date(key) };
|
360 | Object.keys(o).forEach(k => {
|
361 | newObject[k] = o[k];
|
362 | });
|
363 | array.push(newObject);
|
364 | }
|
365 | }
|
366 | return array;
|
367 | })
|
368 | }
|
369 |
|
370 | /**
|
371 | * Returns an array of simple moving averages for the equity specified.
|
372 | * https://www.investopedia.com/articles/technical/052201.asp
|
373 | * @author Torrey Leonard <https://github.com/Ladinn>
|
374 | * @param {String} symbol
|
375 | * @param {String} interval - Time between two data points in the series: 1min, 5min, 15min, 30min, 60min, daily, weekly, monthly
|
376 | * @param {Number} timePeriod - Number of data points used to calculate each moving average value. Positive integers are accepted.
|
377 | * @param {String} seriesType - What to base the SMA on: open, high, low, close
|
378 | * @param {String} seriesType - What to base the SMA on: open, high, low, close
|
379 | * @param {String} seriesType - What to base the SMA on: open, high, low, close
|
380 | * @returns {Promise<Array>}
|
381 | */
|
382 | sma(symbol, interval, timePeriod, seriesType) {
|
383 | return this._technical("SMA", symbol, interval, timePeriod, seriesType);
|
384 | }
|
385 |
|
386 | /**
|
387 | * Returns an array of exponential moving averages for the equity specified.
|
388 | * https://www.investopedia.com/terms/e/ema.asp
|
389 | * @author Torrey Leonard <https://github.com/Ladinn>
|
390 | * @param {String} symbol
|
391 | * @param {String} interval - Time between two data points in the series: 1min, 5min, 15min, 30min, 60min, daily, weekly, monthly
|
392 | * @param {Number} timePeriod - Number of data points used to calculate each moving average value. Positive integers are accepted.
|
393 | * @param {String} seriesType - What to base the SMA on: open, high, low, close
|
394 | * @returns {Promise<Array>}
|
395 | */
|
396 | ema(symbol, interval, timePeriod, seriesType) {
|
397 | return this._technical("EMA", symbol, interval, timePeriod, seriesType);
|
398 | }
|
399 |
|
400 | /**
|
401 | * Returns an array of weighted moving averages for the equity specified.
|
402 | * https://www.investopedia.com/articles/technical/060401.asp
|
403 | * @author Torrey Leonard <https://github.com/Ladinn>
|
404 | * @param {String} symbol
|
405 | * @param {String} interval - Time between two data points in the series: 1min, 5min, 15min, 30min, 60min, daily, weekly, monthly
|
406 | * @param {Number} timePeriod - Number of data points used to calculate each moving average value. Positive integers are accepted.
|
407 | * @param {String} seriesType - What to base the SMA on: open, high, low, close
|
408 | * @returns {Promise<Array>}
|
409 | */
|
410 | wma(symbol, interval, timePeriod, seriesType) {
|
411 | return this._technical("WMA", symbol, interval, timePeriod, seriesType);
|
412 | }
|
413 |
|
414 | /**
|
415 | * Returns an array of double exponential moving averages for the equity specified.
|
416 | * http://www.investopedia.com/articles/trading/10/double-exponential-moving-average.asp
|
417 | * @author Torrey Leonard <https://github.com/Ladinn>
|
418 | * @param {String} symbol
|
419 | * @param {String} interval - Time between two data points in the series: 1min, 5min, 15min, 30min, 60min, daily, weekly, monthly
|
420 | * @param {Number} timePeriod - Number of data points used to calculate each moving average value. Positive integers are accepted.
|
421 | * @param {String} seriesType - What to base the SMA on: open, high, low, close
|
422 | * @returns {Promise<Array>}
|
423 | */
|
424 | dema(symbol, interval, timePeriod, seriesType) {
|
425 | return this._technical("DEMA", symbol, interval, timePeriod, seriesType);
|
426 | }
|
427 |
|
428 | /**
|
429 | * Returns an array of double exponential moving averages for the equity specified.
|
430 | * http://www.investopedia.com/articles/trading/10/double-exponential-moving-average.asp
|
431 | * @author Torrey Leonard <https://github.com/Ladinn>
|
432 | * @param {String} symbol
|
433 | * @param {String} interval - Time between two data points in the series: 1min, 5min, 15min, 30min, 60min, daily, weekly, monthly
|
434 | * @param {Number} timePeriod - Number of data points used to calculate each moving average value. Positive integers are accepted.
|
435 | * @param {String} seriesType - What to base the SMA on: open, high, low, close
|
436 | * @returns {Promise<Array>}
|
437 | */
|
438 | dema(symbol, interval, timePeriod, seriesType) {
|
439 | return this._technical("DEMA", symbol, interval, timePeriod, seriesType);
|
440 | }
|
441 |
|
442 | /**
|
443 | * Returns an array of triple exponential moving averages for the equity specified.
|
444 | * https://www.investopedia.com/terms/t/triple-exponential-moving-average.asp
|
445 | * @author Torrey Leonard <https://github.com/Ladinn>
|
446 | * @param {String} symbol
|
447 | * @param {String} interval - Time between two data points in the series: 1min, 5min, 15min, 30min, 60min, daily, weekly, monthly
|
448 | * @param {Number} timePeriod - Number of data points used to calculate each moving average value. Positive integers are accepted.
|
449 | * @param {String} seriesType - What to base the SMA on: open, high, low, close
|
450 | * @returns {Promise<Array>}
|
451 | */
|
452 | tema(symbol, interval, timePeriod, seriesType) {
|
453 | return this._technical("TEMA", symbol, interval, timePeriod, seriesType);
|
454 | }
|
455 |
|
456 | /**
|
457 | * Returns an array of triangular moving averages for the equity specified.
|
458 | * http://www.fmlabs.com/reference/default.htm?url=TriangularMA.htm
|
459 | * @author Torrey Leonard <https://github.com/Ladinn>
|
460 | * @param {String} symbol
|
461 | * @param {String} interval - Time between two data points in the series: 1min, 5min, 15min, 30min, 60min, daily, weekly, monthly
|
462 | * @param {Number} timePeriod - Number of data points used to calculate each moving average value. Positive integers are accepted.
|
463 | * @param {String} seriesType - What to base the SMA on: open, high, low, close
|
464 | * @returns {Promise<Array>}
|
465 | */
|
466 | trima(symbol, interval, timePeriod, seriesType) {
|
467 | return this._technical("TRIMA", symbol, interval, timePeriod, seriesType);
|
468 | }
|
469 |
|
470 | /**
|
471 | * Returns an array of Kaufman adaptive moving averages for the equity specified.
|
472 | * http://stockcharts.com/school/doku.php?id=chart_school:technical_indicators:kaufman_s_adaptive_moving_average
|
473 | * @author Torrey Leonard <https://github.com/Ladinn>
|
474 | * @param {String} symbol
|
475 | * @param {String} interval - Time between two data points in the series: 1min, 5min, 15min, 30min, 60min, daily, weekly, monthly
|
476 | * @param {Number} timePeriod - Number of data points used to calculate each moving average value. Positive integers are accepted.
|
477 | * @param {String} seriesType - What to base the SMA on: open, high, low, close
|
478 | * @returns {Promise<Array>}
|
479 | */
|
480 | kama(symbol, interval, timePeriod, seriesType) {
|
481 | return this._technical("KAMA", symbol, interval, timePeriod, seriesType);
|
482 | }
|
483 |
|
484 | /**
|
485 | * Returns an array of MESA adaptive moving averages for the equity specified.
|
486 | * http://www.binarytribune.com/forex-trading-indicators/ehlers-mesa-adaptive-moving-average
|
487 | * @author Torrey Leonard <https://github.com/Ladinn>
|
488 | * @param {String} symbol
|
489 | * @param {String} interval - Time between two data points in the series: 1min, 5min, 15min, 30min, 60min, daily, weekly, monthly
|
490 | * @param {Number} timePeriod - Number of data points used to calculate each moving average value. Positive integers are accepted.
|
491 | * @param {String} seriesType - What to base the SMA on: open, high, low, close
|
492 | * @param {Number} fastLimit
|
493 | * @param {Number} slowLimit
|
494 | * @returns {Promise<Array>}
|
495 | */
|
496 | mama(symbol, interval, timePeriod, seriesType, fastLimit, slowLimit) {
|
497 | return this._technical("MAMA", symbol, interval, timePeriod, seriesType, [
|
498 | {
|
499 | key: "fastlimit",
|
500 | val: fastLimit
|
501 | },
|
502 | {
|
503 | key: "slowlimit",
|
504 | val: slowLimit
|
505 | }
|
506 | ]);
|
507 | }
|
508 |
|
509 | /**
|
510 | * Returns an array of T3 values for the equity specified.
|
511 | * http://www.fmlabs.com/reference/default.htm?url=T3.htm
|
512 | * @author Torrey Leonard <https://github.com/Ladinn>
|
513 | * @param {String} symbol
|
514 | * @param {String} interval - Time between two data points in the series: 1min, 5min, 15min, 30min, 60min, daily, weekly, monthly
|
515 | * @param {Number} timePeriod - Number of data points used to calculate each moving average value. Positive integers are accepted.
|
516 | * @param {String} seriesType - What to base the SMA on: open, high, low, close
|
517 | * @returns {Promise<Array>}
|
518 | */
|
519 | t3(symbol, interval, timePeriod, seriesType) {
|
520 | return this._technical("T3", symbol, interval, timePeriod, seriesType);
|
521 | }
|
522 |
|
523 | /**
|
524 | * Returns an array of moving average convergence / divergence values for the equity specified.
|
525 | * http://www.investopedia.com/articles/forex/05/macddiverge.asp
|
526 | * @author Torrey Leonard <https://github.com/Ladinn>
|
527 | * @param {String} symbol
|
528 | * @param {String} interval - Time between two data points in the series: 1min, 5min, 15min, 30min, 60min, daily, weekly, monthly
|
529 | * @param {Number} timePeriod - Number of data points used to calculate each moving average value. Positive integers are accepted.
|
530 | * @param {String} seriesType - What to base the SMA on: open, high, low, close
|
531 | * @param {Number|Null} fastPeriod
|
532 | * @param {Number|Null} slowPeriod
|
533 | * @param {Number|Null} signalPeriod
|
534 | * @returns {Promise<Array>}
|
535 | */
|
536 | macd(symbol, interval, timePeriod, seriesType, fastPeriod, slowPeriod, signalPeriod) {
|
537 | return this._technical("MACD", symbol, interval, timePeriod, seriesType, [
|
538 | {
|
539 | key: "fastperiod",
|
540 | val: fastPeriod !== null ? fastPeriod : 12
|
541 | },
|
542 | {
|
543 | key: "slowperiod",
|
544 | val: slowPeriod !== null ? fastPeriod : 26
|
545 | },
|
546 | {
|
547 | key: "signalperiod",
|
548 | val: signalPeriod !== null ? signalPeriod : 9
|
549 | }
|
550 | ]);
|
551 | }
|
552 |
|
553 | /**
|
554 | * Returns an array of moving average convergence / divergence values with controllable moving average type for the equity specified.
|
555 | * http://www.investopedia.com/articles/forex/05/macddiverge.asp
|
556 | * @author Torrey Leonard <https://github.com/Ladinn>
|
557 | * @param {String} symbol
|
558 | * @param {String} interval - Time between two data points in the series: 1min, 5min, 15min, 30min, 60min, daily, weekly, monthly
|
559 | * @param {Number} timePeriod - Number of data points used to calculate each moving average value. Positive integers are accepted.
|
560 | * @param {String} seriesType - What to base the SMA on: open, high, low, close
|
561 | * @param {Number|Null} fastPeriod
|
562 | * @param {Number|Null} slowPeriod
|
563 | * @param {Number|Null} signalPeriod
|
564 | * @param {Number|Null} fastMaType - Integers 0 - 8 are accepted with the following mappings: 0 = Simple Moving Average (SMA), 1 = Exponential Moving Average (EMA), 2 = Weighted Moving Average (WMA), 3 = Double Exponential Moving Average (DEMA), 4 = Triple Exponential Moving Average (TEMA), 5 = Triangular Moving Average (TRIMA), 6 = T3 Moving Average, 7 = Kaufman Adaptive Moving Average (KAMA), 8 = MESA Adaptive Moving Average (MAMA).
|
565 | * @param {Number|Null} slowMaType - Integers 0 - 8 are accepted with the following mappings: 0 = Simple Moving Average (SMA), 1 = Exponential Moving Average (EMA), 2 = Weighted Moving Average (WMA), 3 = Double Exponential Moving Average (DEMA), 4 = Triple Exponential Moving Average (TEMA), 5 = Triangular Moving Average (TRIMA), 6 = T3 Moving Average, 7 = Kaufman Adaptive Moving Average (KAMA), 8 = MESA Adaptive Moving Average (MAMA).
|
566 | * @param {Number|Null} signalMaType - Integers 0 - 8 are accepted with the following mappings: 0 = Simple Moving Average (SMA), 1 = Exponential Moving Average (EMA), 2 = Weighted Moving Average (WMA), 3 = Double Exponential Moving Average (DEMA), 4 = Triple Exponential Moving Average (TEMA), 5 = Triangular Moving Average (TRIMA), 6 = T3 Moving Average, 7 = Kaufman Adaptive Moving Average (KAMA), 8 = MESA Adaptive Moving Average (MAMA).
|
567 | * @returns {Promise<Array>}
|
568 | */
|
569 | macd(symbol, interval, timePeriod, seriesType, fastPeriod, slowPeriod, signalPeriod, fastMaType, slowMaType, signalMaType) {
|
570 | return this._technical("MACD", symbol, interval, timePeriod, seriesType, [
|
571 | {
|
572 | key: "fastperiod",
|
573 | val: fastPeriod !== null ? fastPeriod : 12
|
574 | },
|
575 | {
|
576 | key: "slowperiod",
|
577 | val: slowPeriod !== null ? fastPeriod : 26
|
578 | },
|
579 | {
|
580 | key: "signalperiod",
|
581 | val: signalPeriod !== null ? signalPeriod : 9
|
582 | },
|
583 | {
|
584 | key: "fastmatype",
|
585 | val: fastMaType !== null ? fastMaType : 0
|
586 | },
|
587 | {
|
588 | key: "slowmatype",
|
589 | val: slowMaType !== null ? slowMaType : 0
|
590 | },
|
591 | {
|
592 | key: "signalmatype",
|
593 | val: signalMaType !== null ? signalMaType : 0
|
594 | }
|
595 | ]);
|
596 | }
|
597 |
|
598 | /**
|
599 | * Returns an array of stochastic oscillators for the equity specified.
|
600 | * http://www.investopedia.com/university/indicator_oscillator/ind_osc8.asp
|
601 | * @author Torrey Leonard <https://github.com/Ladinn>
|
602 | * @param {String} symbol
|
603 | * @param {String} interval - Time between two data points in the series: 1min, 5min, 15min, 30min, 60min, daily, weekly, monthly
|
604 | * @param {Number} timePeriod - Number of data points used to calculate each moving average value. Positive integers are accepted.
|
605 | * @param {String} seriesType - What to base the SMA on: open, high, low, close
|
606 | * @param {Number|Null} fastKPeriod
|
607 | * @param {Number|Null} slowKPeriod
|
608 | * @param {Number|Null} slowDPeriod
|
609 | * @param {Number|Null} slowKmaType - Integers 0 - 8 are accepted with the following mappings: 0 = Simple Moving Average (SMA), 1 = Exponential Moving Average (EMA), 2 = Weighted Moving Average (WMA), 3 = Double Exponential Moving Average (DEMA), 4 = Triple Exponential Moving Average (TEMA), 5 = Triangular Moving Average (TRIMA), 6 = T3 Moving Average, 7 = Kaufman Adaptive Moving Average (KAMA), 8 = MESA Adaptive Moving Average (MAMA).
|
610 | * @param {Number|Null} slowDmaType - Integers 0 - 8 are accepted with the following mappings: 0 = Simple Moving Average (SMA), 1 = Exponential Moving Average (EMA), 2 = Weighted Moving Average (WMA), 3 = Double Exponential Moving Average (DEMA), 4 = Triple Exponential Moving Average (TEMA), 5 = Triangular Moving Average (TRIMA), 6 = T3 Moving Average, 7 = Kaufman Adaptive Moving Average (KAMA), 8 = MESA Adaptive Moving Average (MAMA).
|
611 | * @returns {Promise<Array>}
|
612 | */
|
613 | stoch(symbol, interval, timePeriod, seriesType, fastKPeriod, slowKPeriod, slowDPeriod, slowKmaType, slowDmaType) {
|
614 | return this._technical("STOCH", symbol, interval, timePeriod, seriesType, [
|
615 | {
|
616 | key: "fastkperiod",
|
617 | val: fastKPeriod !== null ? fastKPeriod : 12
|
618 | },
|
619 | {
|
620 | key: "slowkperiod",
|
621 | val: slowKPeriod !== null ? slowKPeriod : 26
|
622 | },
|
623 | {
|
624 | key: "slowdperiod",
|
625 | val: slowDPeriod !== null ? slowDPeriod : 9
|
626 | },
|
627 | {
|
628 | key: "slowkmatype",
|
629 | val: slowKmaType !== null ? slowKmaType : 0
|
630 | },
|
631 | {
|
632 | key: "slowdmatype",
|
633 | val: slowDmaType !== null ? slowDmaType : 0
|
634 | }
|
635 | ]);
|
636 | }
|
637 |
|
638 | /**
|
639 | * Returns an array of stochastic fast oscillators for the equity specified.
|
640 | * http://www.investopedia.com/university/indicator_oscillator/ind_osc8.asp
|
641 | * @author Torrey Leonard <https://github.com/Ladinn>
|
642 | * @param {String} symbol
|
643 | * @param {String} interval - Time between two data points in the series: 1min, 5min, 15min, 30min, 60min, daily, weekly, monthly
|
644 | * @param {Number} timePeriod - Number of data points used to calculate each moving average value. Positive integers are accepted.
|
645 | * @param {String} seriesType - What to base the SMA on: open, high, low, close
|
646 | * @param {Number|Null} fastKPeriod
|
647 | * @param {Number|Null} fastDPeriod
|
648 | * @param {Number|Null} fastDmaType - Integers 0 - 8 are accepted with the following mappings: 0 = Simple Moving Average (SMA), 1 = Exponential Moving Average (EMA), 2 = Weighted Moving Average (WMA), 3 = Double Exponential Moving Average (DEMA), 4 = Triple Exponential Moving Average (TEMA), 5 = Triangular Moving Average (TRIMA), 6 = T3 Moving Average, 7 = Kaufman Adaptive Moving Average (KAMA), 8 = MESA Adaptive Moving Average (MAMA).
|
649 | * @returns {Promise<Array>}
|
650 | */
|
651 | stochf(symbol, interval, timePeriod, seriesType, fastKPeriod, fastDPeriod, fastDmaType) {
|
652 | return this._technical("STOCHF", symbol, interval, timePeriod, seriesType, [
|
653 | {
|
654 | key: "fastkperiod",
|
655 | val: fastKPeriod !== null ? fastKPeriod : 12
|
656 | },
|
657 | {
|
658 | key: "fastdperiod",
|
659 | val: fastDPeriod !== null ? fastDPeriod : 26
|
660 | },
|
661 | {
|
662 | key: "fastdmatype",
|
663 | val: fastDmaType !== null ? fastDmaType : 9
|
664 | }
|
665 | ]);
|
666 | }
|
667 |
|
668 | /**
|
669 | * Returns an array of relative strength index values for the equity specified.
|
670 | * http://www.investopedia.com/articles/technical/071601.asp
|
671 | * @author Torrey Leonard <https://github.com/Ladinn>
|
672 | * @param {String} symbol
|
673 | * @param {String} interval - Time between two data points in the series: 1min, 5min, 15min, 30min, 60min, daily, weekly, monthly
|
674 | * @param {Number} timePeriod - Number of data points used to calculate each moving average value. Positive integers are accepted.
|
675 | * @param {String} seriesType - What to base the SMA on: open, high, low, close
|
676 | * @returns {Promise<Array>}
|
677 | */
|
678 | rsi(symbol, interval, timePeriod, seriesType) {
|
679 | return this._technical("RSI", symbol, interval, timePeriod, seriesType);
|
680 | }
|
681 |
|
682 | /**
|
683 | * Returns an array of stochastic relative strength index values for the equity specified.
|
684 | * http://www.fmlabs.com/reference/default.htm?url=StochRSI.htm
|
685 | * @author Torrey Leonard <https://github.com/Ladinn>
|
686 | * @param {String} symbol
|
687 | * @param {String} interval - Time between two data points in the series: 1min, 5min, 15min, 30min, 60min, daily, weekly, monthly
|
688 | * @param {Number} timePeriod - Number of data points used to calculate each moving average value. Positive integers are accepted.
|
689 | * @param {String} seriesType - What to base the SMA on: open, high, low, close
|
690 | * @param {Number|Null} fastKPeriod
|
691 | * @param {Number|Null} fastDPeriod
|
692 | * @param {Number|Null} fastDmaType - Integers 0 - 8 are accepted with the following mappings: 0 = Simple Moving Average (SMA), 1 = Exponential Moving Average (EMA), 2 = Weighted Moving Average (WMA), 3 = Double Exponential Moving Average (DEMA), 4 = Triple Exponential Moving Average (TEMA), 5 = Triangular Moving Average (TRIMA), 6 = T3 Moving Average, 7 = Kaufman Adaptive Moving Average (KAMA), 8 = MESA Adaptive Moving Average (MAMA).
|
693 | * @returns {Promise<Array>}
|
694 | */
|
695 | stochRSI(symbol, interval, timePeriod, seriesType, fastKPeriod, fastDPeriod, fastDmaType) {
|
696 | return this._technical("STOCHRSI", symbol, interval, timePeriod, seriesType, [
|
697 | {
|
698 | key: "fastkperiod",
|
699 | val: fastKPeriod !== null ? fastKPeriod : 12
|
700 | },
|
701 | {
|
702 | key: "fastdperiod",
|
703 | val: fastDPeriod !== null ? fastDPeriod : 26
|
704 | },
|
705 | {
|
706 | key: "fastdmatype",
|
707 | val: fastDmaType !== null ? fastDmaType : 9
|
708 | }
|
709 | ]);
|
710 | }
|
711 |
|
712 | /**
|
713 | * Returns an array of bollinger bands for the equity specified.
|
714 | * https://www.investopedia.com/articles/technical/04/030304.asp
|
715 | * @author Torrey Leonard <https://github.com/Ladinn>
|
716 | * @param {String} symbol
|
717 | * @param {String} interval - Time interval between two consecutive data points in the time series. The following values are supported: 1min, 5min, 15min, 30min, 60min, daily, weekly, monthly
|
718 | * @param {Number} timePeriod - Number of data points used to calculate each BBANDS value. Positive integers are accepted (e.g., time_period=60, time_period=200)
|
719 | * @param {String} seriesType - The desired price type in the time series. Four types are supported: close, open, high, low
|
720 | * @param {Number|Null} nbdevup - The standard deviation multiplier of the upper band. Positive integers are accepted. By default, nbdevup=2.
|
721 | * @param {Number|Null} nbdevdn - The standard deviation multiplier of the lower band. Positive integers are accepted. By default, nbdevdn=2.
|
722 | * @param {Number|Null} matype - Moving average type of the time series. By default, matype=0. Integers 0 - 8 are accepted with the following mappings. 0 = Simple Moving Average (SMA), 1 = Exponential Moving Average (EMA), 2 = Weighted Moving Average (WMA), 3 = Double Exponential Moving Average (DEMA), 4 = Triple Exponential Moving Average (TEMA), 5 = Triangular Moving Average (TRIMA), 6 = T3 Moving Average, 7 = Kaufman Adaptive Moving Average (KAMA), 8 = MESA Adaptive Moving Average (MAMA).
|
723 | * @returns {Promise<Array>}
|
724 | */
|
725 | bbands(symbol, interval, timePeriod, seriesType, nbdevup, nbdevdn, matype) {
|
726 | return this._technical("BBANDS", symbol, interval, timePeriod, seriesType, [
|
727 | {
|
728 | key: "nbdevup",
|
729 | val: nbdevup !== null ? nbdevup : 2
|
730 | },
|
731 | {
|
732 | key: "nbdevdn",
|
733 | val: nbdevdn !== null ? nbdevdn : 2
|
734 | },
|
735 | {
|
736 | key: "matype",
|
737 | val: matype !== null ? matype : 0
|
738 | }
|
739 | ]);
|
740 | }
|
741 |
|
742 | /**
|
743 | * This API returns the minus directional indicator (MINUS_DI) values
|
744 | * http://www.investopedia.com/articles/technical/02/050602.asp
|
745 | * @author Colin Gillingham <https://github.com/Gillinghammer>
|
746 | * @param {String} symbol
|
747 | * @param {String} interval - Time between two data points in the series: 1min, 5min, 15min, 30min, 60min, daily, weekly, monthly
|
748 | * @param {Number} timePeriod - Number of data points used to calculate each moving average value. Positive integers are accepted.
|
749 | * @returns {Promise<Array>}
|
750 | */
|
751 | minus_di(symbol, interval, timePeriod) {
|
752 | return this._technical('MINUS_DI', symbol, interval, timePeriod);
|
753 | }
|
754 |
|
755 | /**
|
756 | * This API returns the plus directional indicator (PLUS_DI) values
|
757 | * http://www.investopedia.com/articles/technical/02/050602.asp
|
758 | * @author Colin Gillingham <https://github.com/Gillinghammer>
|
759 | * @param {String} symbol
|
760 | * @param {String} interval - Time between two data points in the series: 1min, 5min, 15min, 30min, 60min, daily, weekly, monthly
|
761 | * @param {Number} timePeriod - Number of data points used to calculate each moving average value. Positive integers are accepted.
|
762 | * @returns {Promise<Array>}
|
763 | */
|
764 | plus_di(symbol, interval, timePeriod) {
|
765 | return this._technical('PLUS_DI', symbol, interval, timePeriod);
|
766 | }
|
767 |
|
768 | /**
|
769 | * This API returns the average directional movement index (ADX) values
|
770 | * http://www.investopedia.com/articles/trading/07/adx-trend-indicator.as
|
771 | * @author Colin Gillingham <https://github.com/Gillinghammer>
|
772 | * @param {String} symbol
|
773 | * @param {String} interval - Time between two data points in the series: 1min, 5min, 15min, 30min, 60min, daily, weekly, monthly
|
774 | * @param {Number} timePeriod - Number of data points used to calculate each ADX value. Positive integers are accepted
|
775 | * @returns {Promise<Array>}
|
776 | */
|
777 | adx(symbol, interval, timePeriod) {
|
778 | return this._technical('ADX', symbol, interval, timePeriod);
|
779 |
|
780 | }
|
781 |
|
782 | }
|
783 |
|
784 | module.exports = AlphaVantage;
|