UNPKG

2.57 kBJavaScriptView Raw
1const ftp = require('ftp');
2const csv = require('fast-csv');
3const _ = require('lodash');
4
5/**
6 * For use with the Nasdaq's public data repository.
7 */
8class Nasdaq {
9
10 /**
11 * @author Torrey Leonard <https://github.com/Ladinn>
12 * @private
13 */
14 static _request(directory) {
15 return new Promise((resolve, reject) => {
16 const client = new ftp();
17 client.on('ready', () => {
18 client.get(directory, (error, stream) => {
19 if (error) reject(error);
20 stream.once('close', () => { client.end(); });
21 let array = [];
22 csv.fromStream(stream, { headers: true, delimiter: "|" })
23 .on('data', data => {
24 array.push(data);
25 })
26 .on('end', () => {
27 resolve(array);
28 })
29 })
30 });
31 client.connect({ host: "ftp.nasdaqtrader.com" });
32 });
33 }
34
35 /**
36 * Returns an array of objects with details on Nasdaq listed equities.
37 * @author Torrey Leonard <https://github.com/Ladinn>
38 * @returns {Promise<Object[]>}
39 */
40 static getListings() {
41 return Nasdaq._request("SymbolDirectory/nasdaqlisted.txt");
42 }
43
44 /**
45 * Returns an array of objects with details on Nasdaq traded equities.
46 * @author Torrey Leonard <https://github.com/Ladinn>
47 * @returns {Promise<Object[]>}
48 */
49 static getTraded() {
50 return Nasdaq._request("SymbolDirectory/nasdaqtraded.txt");
51 }
52
53 /**
54 * Returns an array of objects with details on other equities.
55 * @author Torrey Leonard <https://github.com/Ladinn>
56 * @returns {Promise<Object[]>}
57 */
58 static getOtherListings() {
59 return Nasdaq._request("SymbolDirectory/otherlisted.txt");
60 }
61
62 /**
63 * Returns an array of objects with details on OTC traded equities.
64 * @author Torrey Leonard <https://github.com/Ladinn>
65 * @returns {Promise<Object[]>}
66 */
67 static getOTCListings() {
68 return Nasdaq._request("SymbolDirectory/otclist.txt");
69 }
70
71 /**
72 * Returns an array of objects with details on equities whose name matchs the given filter.
73 * @author Torrey Leonard <https://github.com/Ladinn>
74 * @param {String} string
75 * @returns {Promise<Object[]>}
76 */
77 static getByName(string) {
78 return Nasdaq.getTraded().then(array => {
79 return (_.filter(array, o => { return o["Security Name"].indexOf(string) !== -1; }))
80 })
81 }
82
83 /**
84 * Returns an array of symbols that represent exchange traded funds.
85 * @author Torrey Leonard <https://github.com/Ladinn>
86 * @returns {Promise<String[]>}
87 */
88 static getETFs() {
89 return Nasdaq.getTraded().then(array => {
90 array = _.filter(array, o => { return o["ETF"] === "Y"; });
91 return (_.map(array, 'Symbol'));
92 })
93 }
94
95}
96
97module.exports = Nasdaq;
\No newline at end of file