Click here to Skip to main content
14,937,163 members
Articles / Web Development / Node.js
Article
Posted 7 Jan 2018

Stats

21.6K views
4 bookmarked

Acquiring stock market data from Alpha Vantage

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
7 Jan 2018CPOL4 min read
How to download daily and intraday stock market data from Alpha Vantage from the command line and with Node.js and JavaScript.

First published on The Data Wrangler.

As a stock trader I need a ready of supply stock market data for analysis and visualisation. That data is needed for decision making and I often render it to a chart to better understand it.

The free Yahoo financial API was the place to go for stock market data. However since it's demise a new stock API from Alpha Vantage has arisen. It's also free and very easy to use. Alpha Vantage offers both daily and intraday data and I have created an open-source command line app for downloading it. You may also refer to this article that describes the stock API standard and best practices - e.g., adjusting historical stock prices based on splits and dividends.

This posts introduces alpha-vantage-cli. I'll show you how to use this tool to download stock market data. I'll give examples of use both from the command line and also using it as an API from your Node.js JavaScript code.

Here's an example of what the downloaded CSV file looks like:

Microsoft stock price history

Contents

generated with DocToc

Getting an Alpha Vantage API key

Alpha Vantage is free, but to use it you must sign up for an API key. Please follow this link to sign up:

https://www.alphavantage.co/support/#api-key

The examples that follow use the 'demo' API key, please be aware that this has very limited usage.

Using the command line app

You can use alpha-vantage-cli as a command line application to download stock market data to a CSV file.

Node.js installation

First you need to have Node.js installed. This is quite straight forward, please see the Node.js web site for more details:

http://nodejs.org/

Installing the command line app

Once you have Node.js installed you can install the tool via npm by running the following command:

> npm install -g alpha-vantage-cli

This installs the tool globally so that you can run it from any directory.

You can check that it installed correctly using the --version argument:

> alpha-vantage-cli --version

That should display the most recent version of the tool. There is also a --help argument that will help you understand the tool's options:

> alpha-vantage-cli --help

Acquiring stock market data from the command line

Minimal usage looks like this:

> alpha-vantage-cli --type=<data-type> --symbol=<code-for-the-instrument> --api-key=<your-api-key> 

Here's an example that downloads download daily data for Microsoft:

> alpha-vantage-cli --type=daily --symbol=MSFT --api-key=demo --out=MSFT-daily.csv

This downloads data to a file named MSFT-daily.csv. See the screenshot at the start of this post to see what daily data looks like.

You can also download intraday data like so:

> alpha-vantage-cli --type=intraday --symbol=MSFT --api-key=demo --out=MSFT-intraday.csv

This downloads to a file named MSFT-intraday.csv.

Intraday data looks similar to daily data:

Microsoft intraday stock history

Notice the difference in the timestamp between daily and intraday data.

There are various other options including setting the interval for the intraday data. Please use the tool's --help argument for details.

Using the code module

alpha-vantage-cli can also be imported into a Node.js script to be used from code.

Installing the code module

To use please install locally in your Node.js project using npm as follows:

npm install --save alpha-vantage-cli

Acquiring stock market data from code

Here's an example of use from a JavaScript code file. Don't forget to replace the API key with your own!

var AlphaVantageAPI = require('alpha-vantage-cli').AlphaVantageAPI;

var yourApiKey = 'demo';
var alphaVantageAPI = new AlphaVantageAPI(yourApiKey, 'compact', true);

alphaVantageAPI.getDailyData('MSFT')
    .then(dailyData => {
        console.log("Daily data:");
        console.log(dailyData);
    })
    .catch(err => {
        console.error(err);
    });

The example code above gets daily data. Getting intraday data is almost the same, just use the getIntradayData function instead. alpha-vantage-cli can also be used from TypeScript code, please see the Github page for more examples.

Getting the source code

Source code for alpha-vantage-cli is available on Github:

https://github.com/codecapers/alpha-vantage-cli

Need support?

Would you like help getting started with alpha-vantage-cli? Would you prefer to use a desktop app instead of a command line tool? Please become a patron of The Data Wrangler and I will be happy to help you.

How does it work?

alpha-vantage-cli uses request-promise to download data from the Alpha Vantage stock history API.

It's fairly easy to download data manually. You can try out yourself by opening the following link in your browser:

https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=MSFT&apikey=demo&datatype=csv

That link downloads the recent price history for Microsoft as a CSV file. It is using Alpha Vantage's demo API key. To use it for any other company you'll need to sign up for your own API key.

alpha-vantage-cli provides a simple command line tool and API as an interface to the REST API. This allows you to script batch files to download your stock data or if you need something more sophisticated you can call it directly from your Node.js scripts.

Conclusion

In this post I've introduced alpha-vantage-cli, my open-source command line tool and API for downloading stock data from Alpha Vantage.

You have learned how to pull down stock data for use in your own analysis and visualisation and to support your stock trading decisions.

Resources

Alpha Vantage: https://www.alphavantage.co/

alpha-vantage-cli
npm: https://www.npmjs.com/package/alpha-vantage-cli
github: https://github.com/codecapers/alpha-vantage-cli

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Ashley Davis
Chief Technology Officer Sortal
Australia Australia
My new book, Bootstrapping Microservices, is a practical and project-based guide to building distributed applications with microservices: http://bit.ly/2o0aDsP

My first book, Data Wrangling with JavaScript, is a comprehensive overview of working with data in JavaScript: http://bit.ly/2t2cJu2

Data-Forge Notebook is my notebook-style application for data transformation, analysis and visualizatiion in JavaScript:
http://www.data-forge-notebook.com/

I have a long history in software development with many years in apps, web apps, serious games, sims and VR. Making technology work for business is what I do: building bespoke software solutions that span multiple platforms.

I have years of experience managing teams, preparing technical strategies and creation of software products. I can explain complicated technology to senior management. I have delivered cutting-edge products in fast-paced and high-pressure environments. I know how to focus and prioritize to get the important things done.

I am a passionate technologist and agile practitioner. I have had great success with TDD and am convinced of its ability to produce better code that stabilizes more quickly while being evolved rapidly. I'm a fan of functional programming and its potential for safety, predictability & concurrency.

I contribute to open source and have founded multiple industry groups.

I'm also a keen systems trader, currently trading stocks on the ASX.

Creator of Market Wizard:

https://www.market-wizard.com.au/

Creator of Data-Forge and Data-Forge Notebook:

http://www.data-forge-js.com/
http://www.data-forge-notebook.com/

Web

www.codecapers.com.au

Open source

https://github.com/ashleydavis
https://github.com/data-forge
https://github.com/Real-Serious-Games

Blogs

www.the-data-wrangler.com
www.what-could-possibly-go-wrong.com

Skills

Management of teams & projects
Making sense of technology for senior management
Developing technical strategies
Data wrangling & visualiz

Comments and Discussions

 
QuestionGood Read and CLI Pin
Jerry72212-Jan-18 10:58
professionalJerry72212-Jan-18 10:58 
AnswerRe: Good Read and CLI Pin
Jerry72216-Jan-18 6:20
professionalJerry72216-Jan-18 6:20 
QuestionAlpha Vantage Pin
Dave Burns10-Jan-18 1:17
MemberDave Burns10-Jan-18 1:17 
BugSome minor bugs Pin
Dirk_B7-Jan-18 22:21
MemberDirk_B7-Jan-18 22:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.