Skip to content

Latest commit

 

History

History
76 lines (53 loc) · 2.59 KB

README.md

File metadata and controls

76 lines (53 loc) · 2.59 KB

StockQuote plugin for Craft CMS 3.x

Simple real-time stock quotes from the Alpha Vantage API.

The Alpha Vantage API is free, but you must register to receive and API Key.

This is a 3.x port of Surprise Highway 2.0 version of the same plugin.

Installation

To install Stock Quote, follow these steps:

  1. Download & unzip the file and place the stockquote directory into your craft/plugins directory
  2. -OR- do a git clone https://github.com/surprisehighway/craft-stockquote.git directly into your craft/plugins folder. You can then update it with git pull
  3. -OR- install with Composer via composer require surprisehighway/craft-stockquote
  4. Install plugin in the Craft Control Panel under Settings > Plugins
  5. The plugin folder should be named stockquote for Craft to see it. GitHub recently started appending -master (the branch name) to the name of the folder for zip file downloads.

Stock Quote works on Craft 2.6.x.

Settings

  1. Navigate to Settings > Stock Quote.
  2. Enter your Alpha Vantage API Key.
  3. Save the plugin settings.

Usage

{% set quote = craft.stockQuote.getQuote('MSFT') %}

{% if quote|length %}
	{{ quote.symbol }}
	{{ quote.last }}
	{{ quote.date|date('F j, Y g:i a') }}
	{{ quote.change }}
	{{ quote.open }}
	{{ quote.high }}
	{{ quote.low }}
	{{ quote.volume }}
	{{ quote.previous }}
	{{ quote.percent }}
	{{ quote.price }}
{% endif %}

If you need a json string for a chart, you can also use this...

{% set historyData = craft.stockQuote.getHistoryJson('MSFT') %}
{{historyData}}

Parameters

  • Symbol: Required.
  • Expire: Optional. Cache duration in seconds. Default is 1200 (20 minutes).

Refresh interval and caching

Stock Quote uses the Alpha Vantage Time Series Daily API.

The results from the API are close to real-time, but the plugin caches results. You can set a cache expiration duration (in seconds) to control the rate of refresh. Default is 1200 (20 minutes).

Note that if the API is unavailable or returns an error or incomplete data the plugin will attempt to fallback to the last valid cached data.

Wrapping the plugin output in a conditional using the Twig’s length filter is recommended.

{% set quote = craft.stockQuote.getQuote('MSFT', 300) %}

{% if quote|length %}
	{{ quote.price }}
{% endif %}

Brought to you by Jesse Knowles