Click here to Skip to main content
13,556,387 members
Click here to Skip to main content
Add your own
alternative version

Stats

5.8K views
2 bookmarked
Posted 7 Jan 2018
Licenced CPOL

Acquiring stock market data from Alpha Vantage

, 7 Jan 2018
Rate this:
Please Sign up or sign in to vote.
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 data API from Alpha Vantage has arisen. It's also free and very easy to use. Alpha Vantage offer both daily and intraday data and I have created an open-source command line app for downloading it.

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 OldView
Australia Australia
My book Data Wrangling with JavaScript is now live:

https://www.manning.com/books/data-wrangling-with-javascript

I have a long history in software development with many years in apps, web apps, backends, serious games, simulations 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 development 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:

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

Web

www.codecapers.com.au

Meetups

www.meetup.com/Game-Technology-Brisbane
www.meetup.com/Game-development-Brisbane
www.meetup.com/brisbane-unity-developers

Open source

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

Blogs

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

Skills

Management of development teams & projects
Coaching developers
Making sense of technology for senior management
Developing technical strategies
Data wrangling & visualization.
Desktop, cloud/web & mobile apps.
Game dev, serious games, simulations & VR experiences.

See Linked-in for full history:

https://www.linkedin.com/in/ashleydavis75/

You may also be interested in...

Pro
Pro

Comments and Discussions

 
QuestionGood Read and CLI Pin
Jerry72212-Jan-18 10:58
professionalJerry72212-Jan-18 10:58 
I enjoyed the article and played with the CLI. I found one thing about the CLI, when i was trying to use the short version of the options i wanted to use the output-data-size didn't seem to have an option for a dash like -ods= or -ds=. when i added the --output-data-size to the middle of my dashes it didn't work. I then move it to the very end of the command it seemed to work. Over all a very good start. I'm wondering if you have plans to wrap the whole Alpha Vantage API or not. I checked out the whole website and was wondering what the future plan is going to be for this CLI. I tried to use a .js file with the code you provide but it didn't seem to understand how to add alpha-vantage-cli/node modules to JS. I'm sure his is my issue that i don't fully understand how to add the node module. I have played around a lot with node type projects and the VS Code editor and it all works well using the CLI in VS Code. More and better samples of javascript projects would be great!Thumbs Up | :thumbsup:
Jerry

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02-2016 | 2.8.180515.1 | Last Updated 8 Jan 2018
Article Copyright 2018 by Ashley Davis
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid