UNPKG

@stoqey/finnhub

Version:
306 lines 13.4 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (_) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.FinnhubAPI = void 0; var axios_1 = __importDefault(require("axios")); var lodash_1 = require("lodash"); var fundamentals_1 = __importDefault(require("./fundamentals/fundamentals")); var quote_1 = require("./quote"); var stockEstimates_1 = require("./stockEstimates"); var tick_1 = require("./tick"); var round = function (num) { return Math.round(num); }; /** * FinnhubAPI * @StockCandles Get candlestick data for stocks. * https://finnhub.io/docs/api#stock-candles * * @TickData Get historical tick data for US stocks from all 13 exchanges * https://finnhub.io/docs/api#stock-tick * * @Quote Get stocks quote price * https://finnhub.io/docs/api#quote * * @CompanyProfile2 Get symbol Company info * https://finnhub.io/docs/api/company-profile2 * * @RecommendationTrends Get Recommendation Trends * https://finnhub.io/docs/api/recommendation-trends * @Peers Get peers for company * https://finnhub.io/docs/api/company-peers */ var FinnhubAPI = /** @class */ (function () { function FinnhubAPI(token) { this.api = axios_1.default.create({ baseURL: "https://finnhub.io/api/v1", }); this.token = token ? token : (process && process.env && process.env.FINNHUB_KEY) || ""; this.fundamentalsApi = new fundamentals_1.default(this); } /** * Get candlestick data for stocks. * @param symbol * @param start * @param end * @param resolution * https://finnhub.io/docs/api#stock-candles */ FinnhubAPI.prototype.getCandles = function (symbol, start, end, resolution) { return __awaiter(this, void 0, void 0, function () { var token, to, from, params, candles, data_1, closes, marketData, error_1; return __generator(this, function (_a) { switch (_a.label) { case 0: token = this.token; to = end.getTime() / 1000; from = start.getTime() / 1000; params = { to: round(to), from: round(from), symbol: symbol, token: token, resolution: resolution, }; if (isNaN(from) || isNaN(to)) { console.log("error with parameters", { from: from, to: to }); return [2 /*return*/, []]; } _a.label = 1; case 1: _a.trys.push([1, 3, , 4]); return [4 /*yield*/, this.api.get("stock/candle", { method: "GET", params: params, })]; case 2: candles = _a.sent(); data_1 = candles.data; closes = data_1.c; marketData = lodash_1.isEmpty(closes) ? [] : data_1.c.map(function (cc, index) { var close = cc; // a.k. var open = data_1.o[index]; var time = data_1.t[index]; var volume = data_1.v[index]; var high = data_1.h[index]; var low = data_1.l[index]; return { close: close, date: new Date(+time * 1000), volume: volume, open: open, high: high, low: low, }; }); return [2 /*return*/, marketData]; case 3: error_1 = _a.sent(); console.log("error getting candles", error_1 && error_1.message); return [2 /*return*/, []]; case 4: return [2 /*return*/]; } }); }); }; /** * TickData Get historical tick data for US stocks from all 13 exchanges * https://finnhub.io/docs/api#stock-tick * @param symbol * @param date */ FinnhubAPI.prototype.getTick = function (symbol, date) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/, tick_1.getTickData({ symbol: symbol, date: date, context: this })]; }); }); }; /** * GetQuote * Get real-time quote data for US stocks. Constant polling is not recommended. Use websocket if you need real-time update. * @param symbol */ FinnhubAPI.prototype.getQuote = function (symbol) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/, quote_1.getQuoteData({ symbol: symbol, context: this })]; }); }); }; /** * Symbol Lookup * Search for best-matching symbols based on your query. You can input anything from symbol, security's name to ISIN and Cusip. * @param query Query text can be symbol, name, isin, or cusip * @returns {SymbolLookup | null} */ FinnhubAPI.prototype.symbolLookup = function (query) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/, this.fundamentalsApi.symbolLookup(query)]; }); }); }; /** * companyProfile2 * Get general information of a company * https://finnhub.io/docs/api/company-profile2 * @param args @type {CompanyProfile2Request} * @return {CompanyProfile2 | null} */ FinnhubAPI.prototype.companyProfile2 = function (args) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/, this.fundamentalsApi.companyProfile2(args)]; }); }); }; /** * Market News - https://finnhub.io/docs/api/market-news * Get latest market news. * @param args @type {MarketNewsRequest} * @returns {MarketNews | null} */ FinnhubAPI.prototype.marketNews = function (args) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/, this.fundamentalsApi.marketNews(args)]; }); }); }; /** * Company News - https://finnhub.io/docs/api/company-news * List latest company news by symbol. This endpoint is only available for North American companies. * @param args @type {CompanyNewsRequest} * @returns {CompanyNews | null} */ FinnhubAPI.prototype.companyNews = function (args) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/, this.fundamentalsApi.companyNews(args)]; }); }); }; /** * News Sentiment - https://finnhub.io/docs/api/news-sentiment * Get company's news sentiment and statistics. This endpoint is only available for US companies. * @param symbol * @returns {NewsSentiment | null} */ FinnhubAPI.prototype.newsSentiment = function (symbol) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/, this.fundamentalsApi.newsSentiment(symbol)]; }); }); }; /** * GetRecommendationTrends * Get general information of a company * https://finnhub.io/docs/api/company-profile2 */ FinnhubAPI.prototype.GetRecommendationTrends = function (symbol) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/, stockEstimates_1.GetRecommendationTrends({ symbol: symbol, context: this })]; }); }); }; /** * @deprecated - please use peers API * GetPeers * Get company peers. Return a list of peers in the same country and GICS sub-industry * @param symbol */ FinnhubAPI.prototype.getPeers = function (symbol) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/, this.peers(symbol)]; }); }); }; /** * Peers - https://finnhub.io/docs/api/company-peers * Get company peers. Return a list of peers in the same country and GICS sub-industry * @param symbol Symbol of the company * @returns Array of peers' symbol. */ FinnhubAPI.prototype.peers = function (symbol) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/, this.fundamentalsApi.peers(symbol)]; }); }); }; /** * Basic Financials - https://finnhub.io/docs/api/company-basic-financials * Get company basic financials such as margin, P/E ratio, 52-week high/low etc. * @param args @type {BasicFinancialsRequest} * @returns {BasicFinancials} */ FinnhubAPI.prototype.basicFinancials = function (args) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/, this.fundamentalsApi.basicFinancials(args)]; }); }); }; /** * Insider Transactions - https://finnhub.io/docs/api/insider-transactions * Company insider transactions data sourced from Form 3,4,5. This endpoint only covers US companies at the moment. * Limit to 100 transactions per API call. * @param args @type {InsiderTransactionRequest} * @returns {InsiderTransaction} */ FinnhubAPI.prototype.insiderTransactions = function (args) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/, this.fundamentalsApi.insiderTransactions(args)]; }); }); }; return FinnhubAPI; }()); exports.FinnhubAPI = FinnhubAPI; exports.default = FinnhubAPI; //# sourceMappingURL=index.js.map