and like us on FB

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.

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"].

Add Stock Quote Graphs to WordPress With Shortcode (Alpha Vantage API)

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

The appropriate stock symbol. Some symbols return limited data.

width

The width of your graph DIV container.

height

The height of your graph DIV container.

time

The time determines what range of data is returned. The most commonly used (and those that are supported in the shortcode) include 1 (TIME_SERIES_INTRADAY), 2 (TIME_SERIES_DAILY), 3 (TIME_SERIES_DAILY_ADJUSTED), 4 (TIME_SERIES_WEEKLY) and 5 (TIME_SERIES_MONTHLY).

number

If truncting the size of the returned array, use number="90" (where 90 is the number of data points).

size

The size may be compac (default) or full. The latter returns far more historical data (perhaps reserved for CSV downloads).

interval

If you’re rendering regular quotes (up to every single minute, using TIME_SERIES_INTRADAY endpoint), you can choose to return intervals of 1min, 5min, 15min, 30min, or 60min. Include only the integer.

apikey

The API key is free and can be retrieved via Alpha Vantage website . Queries are limited to an extremely permissive 100 requests per minute.

cache

The time interval to cache the entire graph and data. Defaults to one hour. Keep in mind it takes time to retrieve and process the data.

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:

  • 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:

    Add Stock Quote Graphs to WordPress With Shortcode (Alpha Vantage API)

Download

Title: Add Stock Quote Graphs With PHP
Description: Add a stock quote graph with a PHP function. Utilizes the Alpha Vantage API. WordPress shortcode version is also available.
Download: Shortcode (V0.1) | PHP Code (V0.1) | Plugin Page

Shortt URL for this post: http://shor.tt/2k2v