Share price data is freely available on the Internet. Good news if you are a stock market investor that wants to automatically update their
Excel spreadsheet with stock prices, or a website developer that has been asked to
put a live share price on the home page of a company website, or an
applications developer that wants to create a stock analysis application.
Although freely available, share price data comes in antiquated formats.
For example Yahoo serves share price data in the form of comma-separated ascii
files. These unstructured data formats require extra processing
that can trip up your .Net or JavaScript parsing functions.
So the mission is to obtain
current share price information in xml format, for one or more tick codes,
and that share price information should include min, max,
opening and closing values.
A tick code
is an abbreviated code used in stock exchanges to identify a share, for
example, Microsoft's tick is MSFT, Google's is GOOG, and Arm Holdings Plc is
ARM.L, where the ".L" identify this share as a London based share.
Yahoo.Finance
Yahoo has an online service that will return a CSV file of share price data, if requested in the following format:
FIELDS is a list of codes describing the fields that should be included in
the returned share price data. More details on that later.
TICK is the tick code of the share.
The TICK can include multiple codes delimited by a plus sign, for example MSFT+GOOG.
Yahoo Services and XMLHttpRequest
Yahoo services, maybe for reasons of security or
licensing, do not accept requests for data from a browser. So we
cannot use XMLHttpRequest, which is the standard way of asynchronously
updating page content. This would have been a very simple solution.
Instead, we are going to have to use a "proxy" page stored on an
internet site. This proxy page will load the share price data from the
Yahoo service. We can then call this proxy page from a
browser using XMLHttpRequest.
Since this proxy page is going to have to get the
Yahoo share price data and pass it over to our browser, it is the best place
to convert the clunky CSV into XML data at the same time.
PHP proxy file
The proxy file has been written in PHP, because I happen to be on an
Apache server. You can download the proxy here:
The proxy file contains no HTML, it simply uses PHP XML functions to
create an XML document, and return the document as "text/xml" content to whoever
requested it.
The Yahoo url consists of the Yahoo web address for
the quotes service, a code sequence for the share price fields we want, and
the tick code provided by the request.
The code sequence "sabvnpgho" equates to the following fields:
s
The share price symbol, or tick code.
MSFT
a
The ask or sell price for the share.
12.34
b
The buy price for the share.
12.34
v
The volume traded since the market opened.
123456
n
A descriptive name for the company, truncated.
Microsoft Corpora
p
The previous close price.
12.34
g
The lowest price since the market opened.
12.34
h
The highest price since the market opened.
12.34
o
The opening price, the price when the market opened.
12.34
There is a function in the proxy file, preserveExplode that
chops up the comma-delimited data. Sometime the descriptive name for the company includes a
comma. The preserveExplode function chops up the share price data,
but preserves comma delimited data if it is sandwiched between quotation
marks. The function also chops off bookend quotation marks. (This function could
probably be replaced with a regular
expression function.)
If an error occurs during the parsing of the
share price data, or if Yahoo returns an error code, this error message is
included in the XML document as an errorMessage element.
So now we have a way of retrieving live share price information in a structured format.
Automatically updated share price on a webpage
We can use this XML service to put an automatically updating share price on our page. And here it is:
[Updated every 30 seconds]
The JavaScript is embedded in this page, so open the source to see the
functions. This code has been tested on Internet Explorer 9.
I have picked a US share and a UK share. If the shares exchange is closed, no price may be displayed.
Note the use of the "cache buster", a random number
appended to the url for the XMLHttpRequest url to prevent the same data
being returned with each call.
Contact form
Use the contact form to send comments and requests for information to Broccoli Products.