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.
The Result
Graphing the closing price of FORD stock (TIME_SERIES_DAILY
, default) is returned with the following shortcode: [stock symbol="F"]
.
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).
WordPress Shortcode
Copy and paste the WordPress function into your theme's functions.php
file or, if you sensibly have one installed, your custom functions plugin.
If you require shortcode to work in a sidebar widget, you'll have to enable the functionality with a filter. If you're using our custom functions plugin, you'll have that feature enabled by default.
Shortcode Attributes
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.
symbol
width
height
time
number
number="90"
(where 90 is the number of data points).size
compac
(default) or full
. The latter returns far more historical data (perhaps reserved for CSV downloads).interval
TIME_SERIES_INTRADAY
endpoint), you can choose to return intervals of 1
min, 5
min, 15
min, 30
min, or 60
min. Include only the integer.apikey
cache
PHP Function
A PHP function is available here for use outside of WordPress. Usage requires Simple Cache.
Considerations
- 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:
1Array2(3[Meta Data] => Array4(5[1. Information] => Intraday (1min) prices and volumes6[2. Symbol] => KPG7[3. Last Refreshed] => 2017-10-02 20:04:008[4. Interval] => 1min9[5. Output Size] => Compact10[6. Time Zone] => US/Eastern11)1213[Time Series (1min)] => Array14(15[2017-10-02 20:04:00] => Array16(17[1. open] => 1.570018[2. high] => 1.580019[3. low] => 1.570020[4. close] => 1.580021[5. volume] => 150022)2324[2017-10-01 19:08:00] => Array25(26[1. open] => 1.570027[2. high] => 1.570028[3. low] => 1.570029[4. close] => 1.570030[5. volume] => 62731)3233...
- 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 withdate()
. If you're taking this path you won't need thestr_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:
Download
Title: Add Stock Quote Graphs With WordPress Shortcode
Description: Add a stock quote graph with a PHP function. Utilizes the Alpha Vantage API. WordPress shortcode version is also available.
Download • Version 0.1, 1.6K, zip, Category: WordPress Shortcodes
PHP Code & Snippets, (1.6K)