algotrader
Version:
Algorithmically trade stocks and options using Robinhood, Yahoo Finance, and more.
90 lines (80 loc) • 2.19 kB
JavaScript
const ftp = require('ftp');
const csv = require('fast-csv');
const _ = require('lodash');
/**
* For use with the Nasdaq's public data repository.
*/
class Nasdaq {
/**
* @private
*/
static _request(directory) {
return new Promise((resolve, reject) => {
const client = new ftp();
client.on('ready', () => {
client.get(directory, (error, stream) => {
if (error) reject(error);
stream.once('close', () => { client.end(); });
let array = [];
csv.fromStream(stream, { headers: true, delimiter: "|" })
.on('data', data => {
array.push(data);
})
.on('end', () => {
resolve(array);
})
})
});
client.connect({ host: "ftp.nasdaqtrader.com" });
});
}
/**
* Returns an array of objects with details on Nasdaq listed equities.
* @returns {Promise<Object[]>}
*/
static getListings() {
return Nasdaq._request("SymbolDirectory/nasdaqlisted.txt");
}
/**
* Returns an array of objects with details on Nasdaq traded equities.
* @returns {Promise<Object[]>}
*/
static getTraded() {
return Nasdaq._request("SymbolDirectory/nasdaqtraded.txt");
}
/**
* Returns an array of objects with details on other equities.
* @returns {Promise<Object[]>}
*/
static getOtherListings() {
return Nasdaq._request("SymbolDirectory/otherlisted.txt");
}
/**
* Returns an array of objects with details on OTC traded equities.
* @returns {Promise<Object[]>}
*/
static getOTCListings() {
return Nasdaq._request("SymbolDirectory/otclist.txt");
}
/**
* Returns an array of objects with details on equities whose name matchs the given filter.
* @param {String} string
* @returns {Promise<Object[]>}
*/
static getByName(string) {
return Nasdaq.getTraded().then(array => {
return (_.filter(array, o => { return o["Security Name"].indexOf(string) !== -1; }))
})
}
/**
* Returns an array of symbols that represent exchange traded funds.
* @returns {Promise<String[]>}
*/
static getETFs() {
return Nasdaq.getTraded().then(array => {
array = _.filter(array, o => { return o["ETF"] === "Y"; });
return (_.map(array, 'Symbol'));
})
}
}
module.exports = Nasdaq;