Alpha Vantage describe themselves as a "... tight-knit community of researchers, engineers, and business professionals" that are a "a leading provider of free APIs for realtime and historical stock market data." They claim that their success is "driven by rigorous research, cutting edge technology, and a disciplined focus on democratizing access to data." It's rare to find a supplier of freely available source of stock data and, despite making attempts to find 'the catch', thus far I'm suitably impressed with their tools. Alpha offer free API access with an extremely permissive quote of 100 queries per minute.
Note: The Google Graphs will soon be replaced with more reliable representations. Until then, don't be surprised if the graphs fail to render.
This is the first of a number of articles we'll write that deal with displaying stock market data, and it's the latest in an ongoing series that seeks to utilize Google Charts to render interesting data (in particularly financial data for our partners in relevant industries).
Given the broad scope of Alpha's data, it's difficult to provide the array of examples necessary to illustrate the multiple means in which to represent the information they provide. For that reason we'll provide a single WordPress shortcode example that'll only render the close price of FORD (F) stock.
Graphing the closing price of FORD stock (
TIME_SERIES_DAILY, default) is returned with the following shortcode:
Note: If you're seeing an empty graph it's because the attempt to retrieve data was unsuccessful (the API isn't overly reliable). If this behaviour continues, consider using Simple Cache (which returns old data if the API request was unsuccessful) or storing the result as a post option (and using the transient as a trigger).
Copy and paste the WordPress function into your theme's
functions.php file or, if you sensibly have one installed, your custom functions plugin.
Given the scope of data returned by Alpha Vantage is rather broad, the shortcode needs to built upon to return more robust graphs. The basic attributes for single-line line-graphs are detailed below.
number="90"(where 90 is the number of data points).
full. The latter returns far more historical data (perhaps reserved for CSV downloads).
TIME_SERIES_INTRADAYendpoint), you can choose to return intervals of
60min. Include only the integer.
- Register for an API key here .
- Details on Google's Line Charts here .
- The data is returned with a header that includes the symbol and refresh time. It's the second array that includes the stock data. Note that it includes open, high, low, close, and volume data. We're using the close price the purpose of the example. Example data below:
- Google Graphs can be quite slow for large amounts of data. Consider swapping for another open source and locally hosted graphing library. If you're a client, we'll do this for you.
- If you're after some active quotes to play with, use this list from the WSJ.
- The time returned is USA Eastern. To convert to local, use
strtotime()to convert the time to a timestamp, subtract or add the UTC offset, then convert back to the correct data format with
date(). If you're taking this path you won't need the
str_replace()array as provided; simply return the date chunks formatted with a comma.
- Google's Candlestick Chart is ideal because it renders the high and low range of trade along with the open and close price (in the block). An example for General Motors (GM) graph is as follows: