Jarloo

Menu

Google Stock API

It’s not very well known and Google has no support or docs for it, but there is an elusive Google Stock API. Like most of Google’s API’s it’s REST based. (I wish all API’s were, as I hate SOAP)

Please note this API no longer appears to work. I’m leaving this information here for historical purposes. Here is the new Google Stock API you can use:

Real-Time Google Stock API

The API will give you the following:

  • Stock Symbol
  • Company Name
  • Exchange
  • Exchange Timezone
  • Exchange UTC Offset
  • Exchange Closing
  • Divisor? (I assume this is the number of decimals)
  • Currency
  • Last
  • High
  • Low
  • Volume
  • AVG Volume
  • Market Cap
  • Open
  • Yesterdays Close
  • Change
  • Percent Change
  • Delay
  • Trade Timestamp
  • Trade Date (in UTC)
  • Trade Time (in UTC)
  • Current Date (in UTC)
  • Current Time (in UTC)
  • Link to Google Finance for this stock
  • Link to Google Finance for this stocks chart
  • Daylight Savings Flag

 

API Url Formatting

The API is very simple. It only has a single parameter that accepts a stock ticker.
http://www.google.com/ig/api?stock=MY_STOCK_TICKER_GOES_HERE

Here’s an example:
http://www.google.com/ig/api?stock=AAPL

It should return:

<!--?xml version="1.0"?-->
<xml_api_reply version="1">

		<pretty_symbol data="AAPL"/>
		<symbol_lookup_url data="/finance?client=ig&q=AAPL"/>

		<exchange_timezone data="ET"/>
		<exchange_utc_offset data="+05:00"/>
		<exchange_closing data="960"/>

		<avg_volume data="12682"/>
		<market_cap data="295281.14"/>

		<y_close data="323.66"/>

		<perc_change data="-0.54"/>
		<trade_timestamp data="54 seconds ago"/>
		<trade_date_utc data="20101231"/>
		<trade_time_utc data="184724"/>
		<current_date_utc data="20101231"/>
		<current_time_utc data="184818"/>
		<symbol_url data="/finance?client=ig&q=AAPL"/>
		<chart_url data="/finance/chart?q=NASDAQ:AAPL&tlf=12"/>
		<disclaimer_url data="/help/stock_disclaimer.html"/>
		<ecn_url data=""/>
		<isld_last data="323.00"/>
		<isld_trade_date_utc data="20101231"/>
		<isld_trade_time_utc data="143001"/>
		<brut_last data=""/>
		<brut_trade_date_utc data=""/>
		<brut_trade_time_utc data=""/>
		<daylight_savings data="false"/>

As you can see it’s nice simple XML, very well formated, so it’s makes getting the data out a snap.

 

Getting Multiple Stock Symbols

You can get multiple stock symbols at once by repeating the stock parameter in the url. (thanks to Patrick Fisher (see comments))
Here is an example of the response Google finance returns:

<!--?xml version="1.0"?-->
<xml_api_reply version="1">

		<pretty_symbol data="AAPL"/>
		<symbol_lookup_url data="/finance?client=ig&q=AAPL"/>

		<exchange_timezone data="ET"/>
		<exchange_utc_offset data="+05:00"/>
		<exchange_closing data="960"/>

		<avg_volume data="25140"/>
		<market_cap data="535126.61"/>

		<y_close data="568.18"/>

		<perc_change data="0.72"/>
		<trade_timestamp data="40 seconds ago"/>
		<trade_date_utc data="20120509"/>
		<trade_time_utc data="161528"/>
		<current_date_utc data="20120509"/>
		<current_time_utc data="161608"/>
		<symbol_url data="/finance?client=ig&q=AAPL"/>
		<chart_url data="/finance/chart?q=NASDAQ:AAPL&tlf=12"/>
		<disclaimer_url data="/help/stock_disclaimer.html"/>
		<ecn_url data=""/>
		<isld_last data=""/>
		<isld_trade_date_utc data=""/>
		<isld_trade_time_utc data=""/>
		<brut_last data=""/>
		<brut_trade_date_utc data=""/>
		<brut_trade_time_utc data=""/>
		<daylight_savings data="true"/>

		<pretty_symbol data="GOOG"/>
		<symbol_lookup_url data="/finance?client=ig&q=GOOG"/>

		<exchange_timezone data="ET"/>
		<exchange_utc_offset data="+05:00"/>
		<exchange_closing data="960"/>

		<avg_volume data="2668"/>
		<market_cap data="199486.59"/>

		<y_close data="612.79"/>

		<perc_change data="-0.14"/>
		<trade_timestamp data="1 minute ago"/>
		<trade_date_utc data="20120509"/>
		<trade_time_utc data="161455"/>
		<current_date_utc data="20120509"/>
		<current_time_utc data="161608"/>
		<symbol_url data="/finance?client=ig&q=GOOG"/>
		<chart_url data="/finance/chart?q=NASDAQ:GOOG&tlf=12"/>
		<disclaimer_url data="/help/stock_disclaimer.html"/>
		<ecn_url data=""/>
		<isld_last data=""/>
		<isld_trade_date_utc data=""/>
		<isld_trade_time_utc data=""/>
		<brut_last data=""/>
		<brut_trade_date_utc data=""/>
		<brut_trade_time_utc data=""/>
		<daylight_savings data="true"/>

http://www.google.com/ig/api?stock=MY_FIRST_SYMBOL&stock=MY_SECOND_SYMBOL
Example: http://www.google.com/ig/api?stock=AAPL&stock=GOOG

 

ASP.NET Example

Here is a quick example of using the Google Stock API with your ASP.NET C# apps. This example has only a single page and a quick form. You enter the stock symbol into the box and click the button and it will return the details via the Google Stock API.

Default.aspx ASP.NET Code

<%@ Page Title="Home Page" Language="C#"  AutoEventWireup="true"     CodeBehind="Default.aspx.cs" Inherits="GoogleStockApi.Default" %></pre>
<form id="getQuote">
<h1>Stock Quote Service</h1>

<div>Company: <%=Company %></div>
<div>Company: <%=Exchange %></div>
<div>High: <%=High %></div>
<div>Low: <%=Low %></div>
<div>Last: <%=Last %></div>
</form>
<pre>

Default.aspx.cs C# Code

using System;
using System.Web.UI;
using System.Xml.Linq;

namespace GoogleStockApi
{
    public partial class Default : Page
    {
        public string Company { get; set; }
        public string Exchange { get; set; }
        public double Last { get; set; }
        public double High { get; set; }
        public double Low { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.Form["symbol"] != null)
            {
                string symbol = Request.Form["symbol"];
                FetchQuote(symbol);
            }
        }

        private void FetchQuote(string symbol)
        {
            string url = "http://www.google.com/ig/api?stock=" + Server.UrlEncode(symbol);
            XDocument doc = XDocument.Load(url);
            Company = GetData(doc, "company");
            Exchange = GetData(doc, "exchange");
            Last = Convert.ToDouble(GetData(doc, "last"));
            High = Convert.ToDouble(GetData(doc, "high"));
            Low = Convert.ToDouble(GetData(doc, "low"));
        }

        private string GetData(XDocument doc, string name)
        {
            return doc.Root.Element("finance").Element(name).Attribute("data").Value;
        }
    }
}

 

Categories:   Finance

Comments

Sorry, comments are closed for this item.