1 | const Quote = require('../globals/Quote');
|
2 | const LibraryError = require('../globals/LibraryError');
|
3 | const News = require('./News');
|
4 | const request = require('request');
|
5 |
|
6 | const url = "https://api.iextrading.com/1.0/";
|
7 |
|
8 | /**
|
9 | * Used to interact with the IEX api. See the official documentation for more: https://iextrading.com/developer/docs/#last
|
10 | */
|
11 | class IEX {
|
12 |
|
13 | /**
|
14 | * @author Torrey Leonard <https://github.com/Ladinn>
|
15 | * @private
|
16 | */
|
17 | static _request(endpoint, qs) {
|
18 | return new Promise((resolve, reject) => {
|
19 | request({
|
20 | uri: url + endpoint,
|
21 | qs: qs
|
22 | }, (error, response, body) => {
|
23 | if (error) reject(error);
|
24 | else if (response.statusCode !== 200) reject(new LibraryError(body));
|
25 | else resolve(JSON.parse(body), body);
|
26 | })
|
27 | });
|
28 | }
|
29 |
|
30 | /**
|
31 | * Returns a market object.
|
32 | * @author Colin Gillingham <https://github.com/Gillinghammer>
|
33 | * @returns {Promise<Market>}
|
34 | */
|
35 | static getMarket() {
|
36 | return IEX._request('market');
|
37 | }
|
38 |
|
39 | /**
|
40 | * Returns a quote object for the given symbol.
|
41 | * @author Torrey Leonard <https://github.com/Ladinn>
|
42 | * @param {String} symbol
|
43 | * @returns {Promise<Quote>}
|
44 | */
|
45 | static getQuote(symbol) {
|
46 | return IEX._request(`stock/${symbol}/book`).then((json, body) => {
|
47 | return new Quote({
|
48 | symbol: symbol,
|
49 | date: new Date(json.quote.latestUpdate),
|
50 | source: "IEX",
|
51 | price: {
|
52 | last: json.quote.latestPrice,
|
53 | open: json.quote.open,
|
54 | high: json.quote.high,
|
55 | low: json.quote.low,
|
56 | close: json.quote.close,
|
57 | volume: json.quote.latestVolume
|
58 | },
|
59 | dom: {
|
60 | bids: json.bids,
|
61 | asks: json.asks
|
62 | },
|
63 | original: body
|
64 | });
|
65 | })
|
66 | }
|
67 |
|
68 | /**
|
69 | * Returns an array of quote objects for the given symbols.
|
70 | * @author Torrey Leonard <https://github.com/Ladinn>
|
71 | * @param {String[]} symbolArray - An array of symbols to query
|
72 | * @returns {Promise<Quote[]>}
|
73 | */
|
74 | static getBatchQuotes(symbolArray) {
|
75 | return IEX._request("/stock/market/batch", {
|
76 | symbols: symbolArray.join(','),
|
77 | types: "quote"
|
78 | }).then((json, body) => {
|
79 | let array = [];
|
80 | Object.keys(json).forEach(key => {
|
81 | let self = json[key].quote;
|
82 | array.push(new Quote({
|
83 | symbol: self.symbol,
|
84 | date: new Date(self.latestUpdate),
|
85 | source: "IEX",
|
86 | price: {
|
87 | last: self.latestPrice,
|
88 | open: self.open,
|
89 | high: self.high,
|
90 | low: self.low,
|
91 | close: self.close,
|
92 | volume: self.latestVolume
|
93 | },
|
94 | original: body
|
95 | }));
|
96 | });
|
97 | return array;
|
98 | });
|
99 | }
|
100 |
|
101 | // /**
|
102 | // * Returns an array of quotes ordered chronologically for the given symbol (aka 'chart)
|
103 | // * @param {String} symbol
|
104 | // * @returns {Promise<Quote[]>}
|
105 | // */
|
106 | // static getQuotes(symbol) {
|
107 | //
|
108 | // }
|
109 |
|
110 | /**
|
111 | * Returns an object containing data on the given company.
|
112 | * https://iextrading.com/developer/docs/#company
|
113 | * @author Torrey Leonard <https://github.com/Ladinn>
|
114 | * @param {String} symbol
|
115 | * @returns {Promise<Object>}
|
116 | */
|
117 | static getCompanyDetails(symbol) {
|
118 | return IEX._request(`/stock/${symbol}/company`).then(res => {
|
119 | return res;
|
120 | })
|
121 | }
|
122 |
|
123 | /**
|
124 | * Returns an object containing data on dividends issued by the given company.
|
125 | * https://iextrading.com/developer/docs/#dividends
|
126 | * @author Torrey Leonard <https://github.com/Ladinn>
|
127 | * @param {String} symbol
|
128 | * @returns {Promise<Object>}
|
129 | */
|
130 | static getDividends(symbol) {
|
131 | return IEX._request(`/stock/${symbol}/dividends`, {
|
132 | range: "5y"
|
133 | }).then(res => {
|
134 | return res;
|
135 | })
|
136 | }
|
137 |
|
138 | /**
|
139 | * Returns an object containing data on the four most recent earnings reports by the given company.
|
140 | * https://iextrading.com/developer/docs/#earnings
|
141 | * @author Torrey Leonard <https://github.com/Ladinn>
|
142 | * @param {String} symbol
|
143 | * @returns {Promise<Object>}
|
144 | */
|
145 | static getEarnings(symbol) {
|
146 | return IEX._request(`/stock/${symbol}/earnings`).then(res => {
|
147 | return res;
|
148 | })
|
149 | }
|
150 |
|
151 | /**
|
152 | * Returns an object containing data on the given company's income statement, balance sheet, and cash flow from the four most recent reported quarters.
|
153 | * https://iextrading.com/developer/docs/#financials
|
154 | * @author Torrey Leonard <https://github.com/Ladinn>
|
155 | * @param {String} symbol
|
156 | * @returns {Promise<Object>}
|
157 | */
|
158 | static getFinancials(symbol) {
|
159 | return IEX._request(`/stock/${symbol}/financials`).then(res => {
|
160 | return res;
|
161 | })
|
162 | }
|
163 |
|
164 | /**
|
165 | * Returns an object containing data on the given company's market cap, beta, 52-week high & low, change, short intereste, dividend rate, float, EBITDA, cash, and more.
|
166 | * https://iextrading.com/developer/docs/#key-stats
|
167 | * @author Torrey Leonard <https://github.com/Ladinn>
|
168 | * @param {String} symbol
|
169 | * @returns {Promise<Object>}
|
170 | */
|
171 | static getStats(symbol) {
|
172 | return IEX._request(`/stock/${symbol}/stats`).then(res => {
|
173 | return res;
|
174 | })
|
175 | }
|
176 |
|
177 | /**
|
178 | * Returns a string containing a URL endpoint with the given company's logo.
|
179 | * https://iextrading.com/developer/docs/#logo
|
180 | * @author Torrey Leonard <https://github.com/Ladinn>
|
181 | * @param {String} symbol
|
182 | * @returns {Promise<String>}
|
183 | */
|
184 | static getLogo(symbol) {
|
185 | return IEX._request(`/stock/${symbol}/logo`).then(res => {
|
186 | return res.url;
|
187 | })
|
188 | }
|
189 |
|
190 | /**
|
191 | * Returns an array of news objects for the given symbol.
|
192 | * https://iextrading.com/developer/docs/#logo
|
193 | * @author Torrey Leonard <https://github.com/Ladinn>
|
194 | * @param {String} symbol
|
195 | * @returns {Promise<News[]>}
|
196 | */
|
197 | static getNews(symbol) {
|
198 | return IEX._request(`/stock/${symbol}/news`).then(res => {
|
199 | let array = [];
|
200 | res.forEach(o => {
|
201 | array.push(new News({
|
202 | title: o.headline,
|
203 | description: o.summary,
|
204 | date: new Date(o.datetime),
|
205 | source: o.source,
|
206 | url: o.url
|
207 | }))
|
208 | });
|
209 | return array;
|
210 | })
|
211 | }
|
212 |
|
213 | /**
|
214 | * Returns an array of peer tickers as defined by IEX.
|
215 | * https://iextrading.com/developer/docs/#peers
|
216 | * @author Torrey Leonard <https://github.com/Ladinn>
|
217 | * @param {String} symbol
|
218 | * @returns {Promise<String[]>}
|
219 | */
|
220 | static getPeers(symbol) {
|
221 | return IEX._request(`/stock/${symbol}/peers`).then(res => {
|
222 | return res;
|
223 | })
|
224 | }
|
225 |
|
226 | /**
|
227 | * Returns an object containing data on stock splits issued by the given company.
|
228 | * https://iextrading.com/developer/docs/#dividends
|
229 | * @author Torrey Leonard <https://github.com/Ladinn>
|
230 | * @param {String} symbol
|
231 | * @returns {Promise<Object>}
|
232 | */
|
233 | static getSplits(symbol) {
|
234 | return IEX._request(`/stock/${symbol}/splits`, {
|
235 | range: "5y"
|
236 | }).then(res => {
|
237 | return res;
|
238 | })
|
239 | }
|
240 |
|
241 | /**
|
242 | * This returns 15 minute delayed and 30 day average consolidated volume percentage of a stock, by market.
|
243 | * This call will always return 13 values, and will be sorted in ascending order by current day trading volume percentage.
|
244 | * https://iextrading.com/developer/docs/#volume-by-venue
|
245 | * @author Torrey Leonard <https://github.com/Ladinn>
|
246 | * @param {String} symbol
|
247 | * @returns {Promise<String[]>}
|
248 | */
|
249 | static getVolumeByVenue(symbol) {
|
250 | return IEX._request(`/stock/${symbol}/volume-by-venue`).then(res => {
|
251 | return res;
|
252 | })
|
253 | }
|
254 |
|
255 | /**
|
256 | * Returns an array of symbols IEX supports for trading.
|
257 | * https://iextrading.com/developer/docs/#symbols
|
258 | * @author Torrey Leonard <https://github.com/Ladinn>
|
259 | * @returns {Promise<Object[]>}
|
260 | */
|
261 | static getAllSymbols() {
|
262 | return IEX._request(`/ref-data/symbols`).then(res => {
|
263 | return res;
|
264 | })
|
265 | }
|
266 |
|
267 | }
|
268 |
|
269 | module.exports = IEX;
|