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

traderjs

v2.1.3

Published

Temporal data related to quotes from stock market

Downloads

24

Readme

Traderjs

Travis CI Build Codecov Coverage Version Downloads MIT semantic-release

TraderJS is JavaScript library that retrieves temporal data which is related to quotes from stock market. The library uses the Google Finance API to retrieve the temporal data and to parse it into JSON or text format. The quotes can be fetched in the Candle Stick representation and they can be easily stored into text files.

Installation

This package is distributed via NPM:

$ npm install traderjs

Importing the library

If you are going to use the ES6/ES2015 style, you will need to use Babel to transpile your code into ES5. Please, take a look at: https://babeljs.io/.

ES6/ES2015 import:

import traderjs from 'traderjs';

The simplest approach is to use ES5 style. Just import Traderjs using:

var traderjs = require('traderjs');

Usage

Traderjs uses fluent interfaces to fetch temporal data. To accomplish this goal, the library follows a few basic steps:

  1. Configure the request.
  2. Setup a transformer.
  3. Retrieve the data.
  4. Define a file to write the content. (optional)

The next sections will show you how perform each of these steps.

Configuring your request

Traderjs requires you to specify what you want to retrieve before fetch the data. The configuration is simple and you just need to use the method .config(...) passing a config object as follows:

traderjs
    .config({
        symbol: 'NASD:GOOG', 
        interval: 86400, 
        period: '2d', 
        fields: ['d','o','c','l','h','v']
    })

The configuration above will retrieve the last two trading days (period: '2d') for the stock NASD:GOOG (Google in NASDAQ) using an interval of 86400 seconds between candles (interval: 86400). The fields that are going to be retrieved by default are:

  • 'd' - date
  • 'o' - open
  • 'c' - close
  • 'l' - low
  • 'h' - high
  • 'v' - volume

Setting a transformer

Traderjs will transform the retrieved data so that you can easily use it in other tools. There are two types of transformers: 'json', which will convert your data to a JSON representation; and 'raw', which will convert your data to text with columns and rows.

In the piece of code bellow it's shown how to convert the data into the JSON format.

var traderjs = require('traderjs');

var configuration = {
    symbol: 'NASD:GOOG',
    interval: 86400,
    period: '2d',
    fields: ['d','o','c','l','h','v']
};

traderjs
    .config(configuration)
    .transformer('json') // Converts the data to JSON

    /* the result of the request should be an Array of objects
    like this:
    [
        {
            "date": "1475784000000",
            "close": "776.86",
            "high": "780.48",
            "low": "775.54",
            "open": "779",
            "volume": "1070692"
        },
        {
            "date": "1475870400000",
            "close": "775.08",
            "high": "779.66",
            "low": "770.75",
            "open": "779.66",
            "volume": "933158"
        }
    ]
     */

The data can also be presented in text format. The result of the request will be an Array of strings, in which each element is a quote with comma separated values. By default, the values of each element have the following labels: 'date', 'close', 'high', 'low', 'open', 'volume'. The text representation can be obtained as bellow:

var traderjs = require('traderjs');

traderjs
    .config(configuration)
    .transformer('raw') // Converts the data to text

    /* Each element of the array is quote with comma separated values

    ['1475784000000,776.86,780.48,775.54,779,1070692',
     '1475870400000,775.08,779.66,770.75,779.66,933158']

    */

Retrieving the data

It's important to notice that the data will be retrieved only after you call the method .temporal(...), which accepts a callback that is called only after the data has been retrieved.

The piece of code bellow shows how to get the data:

traderjs
    .config(configuration)
    .transformer('json')
    .temporal(function(data) {
        /* The variable received in the callback (data)
        will contain the output in the required format (JSON).
        [
            {
                "date": "1475784000000",
                "close": "776.86",
                "high": "780.48",
                "low": "775.54",
                "open": "779",
                "volume": "1070692"
            },
            ...
        ]
        */
    });

Defining a file to write the content (optional)

The content will only be written to a file if you specify the filename by using the .writeTo(...) method. Just give the filename as a string, as the piece of code below:

traderjs.
    .config(configuration)
    .transformer('raw')
    .writeTo('/tmp/quotes.dat')
    .temporal(function(data) {
        /* 
        You will receive the content in the variable called 'data'
        and it will also be written in the file /tmp/quotes.dat
        */
    });

The format of the content that is written to the file depends on the transformer that you are using. A 'raw' transformer will imply that each item of the array of data will be a line (with comma separated values) of the file. A file create using the 'raw' transformer will have the following format:

1475784000000,776.86,780.48,775.54,779,1070692
1475870400000,775.08,779.66,770.75,779.66,933158
...

The content of a file created with a 'json' transformer will have the following format:

[{"date": "1475784000000", "close": "776.86", "high": "780.48", "low": "775.54", "open": "779", "volume": "1070692"}, {"date": "1475870400000", "close": "775.08", "high": "779.66", "low": "770.75", "open": "779.66", "volume": "933158"} ]

API

License

MIT License 2016 © Ricardo Erikson and contributors