npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ystocks

v0.1.1

Published

A wrapper for the Yahoo Finance API

Downloads

14

Readme

ystocks

A Node.js wrapper for the Yahoo! Finance API.

Usage

const ystocks = require("ystocks"),
      Api = ystocks();

Api.quote(["GOOG", "AAPL"], function(err, data, meta) {
    if (err) {
        console.log(err);
    } else {
        // do stuff with data
        console.log(data[0].AverageDailyVolume);     // "1394830"
        console.log(meta.count);                     // 2
    }
});

API

Import/Authentication

After importing the module, you must instatiate an api object:

const ystocks = require("ystocks"),
      Api = ystocks();

Passing no arguments to the constructor as above will create an object that uses the public version of the Yahoo! API. Users on the public API are subject to smaller request caps than authorized users, as detailed in Yahoo's YQL Guide. To authenticate an API object, pass your Consumer Key and Consumer Secret as an object when you instantiate:

const ystocks = require("ystocks");

const params = {
        key: <your_consumer_key>,
        secret: <your_consumer_secret
};

const Api = ystocks(params);

Any errors in authentication will be passed upon calling the object's methods.

Methods

ystocks supports requests on two Yahoo! Finance tables: quote and history. All methods pass three objects to a callback: an error (null on no error), the stock data, and metadata about the response. All responses are JS objects - i.e., they are not JSON and should not be parsed.

Api.quote(symbols, callback)

Returns current value information for a given stock or stocks.

symbols can either be an array of strings (["GOOG", "YHOO"]) or a single string ("MSFT").

callback is passed three parameters: err, data, and meta.

  • err is null on no error.
  • data is an array of result objects. Note that even if only one symbol is given in the query, the result will still be at data[0]. If the stock does not exist, the object will still exist; however, all properties will be null except for data.Symbol.
  • meta is an object containing Yahoo's info on the request.
Api.quote(["F", "YHOO"], function(err, data, meta) {
        if (err) {
                console.log(err);
        } else {
                console.log(data[0].Symbol);          // "F"
                console.log(data[0].Name);            // "Ford Motor Company Common Stock"
                console.log(data[1].Ask);             // "40.05"
                console.log(data[1].PercentChangeFromYearLow); // "+53.58%"
        }
});

Api.quote(["FOOBAR"], function(err, data, meta) {
        if (err) {
                console.log(err);                     // null
        } else {
                console.log(data[0].Name);            // null
                console.log(meta.count);              // 1
                console.log(meta.created);            // "2016-11-11T02:04:49Z"
        }
});

Api.history(params, callback)

Returns historical stock data for a given stock over a given date range.

params is an object with the following parameters:

  • symbol: a string representing a single stock symbol
  • start: the start date of the query. This can be any value recognized by javascript's Date.parse() method.
  • end: the end date, as above.

callback is passed three parameters: err, data, and meta.

  • err is null on no error.
  • data is an array of result objects, or null on no response. The array is sorted with the newest result first.
  • meta is an object containing Yahoo's info on the request. If err and data are both null, meta will be non-null.
let params = {
    symbol: "GOOG",
    start: "2009-09-11",
    end: "2010-03-10"
};

Api.history(params, function(err, data, meta) {
    if (err) {
        console.log(err);
    } else if (data) {
        console.log(data[0].Date);                // "2010-03-10"
                console.log(data[0].Adj_Close);   // "287.937542"
        } else {
                console.log("No results!");
        }
});

params = {
        symbol: "FOOBAR",
        start: "2012-08-19",
        end: "2013-09-25"
};

Api.history(params, function(err, data, meta) {
        if (err) {
                console.log(err);
        } else if (data) {
                console.log(data[0].Open);
        } else {
                console.log("No results!");       // "No results!"
        }
});

Installation

npm install ystocks --save

Attribution

Be sure to check Yahoo's Attribution Guidelines.

License

The MIT License (MIT)

Copyright (c) 2016 Shane Hughes

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.