Google Stock API

API Url Formatting

The API from what I can tell is very simple. It only has a single parameter that accepts a stock ticker.

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

It should return:

<?xml version="1.0"?>
<xml_api_reply version="1">
    <finance module_id="0" tab_id="0" mobile_row="0" mobile_zipped="1" row="0" section="0" >
        <symbol data="AAPL"/>
        <pretty_symbol data="AAPL"/>
        <symbol_lookup_url data="/finance?client=ig&amp;q=AAPL"/>
        <company data="Apple Inc."/>
        <exchange data="Nasdaq"/>
        <exchange_timezone data="ET"/>
        <exchange_utc_offset data="+05:00"/>
        <exchange_closing data="960"/>
        <divisor data="2"/>
        <currency data="USD"/>
        <last data="321.90"/>
        <high data="323.48"/>
        <low data="321.33"/>
        <volume data="3753391"/>
        <avg_volume data="12682"/>
        <market_cap data="295281.14"/>
        <open data="323.00"/>
        <y_close data="323.66"/>
        <change data="-1.76"/>
        <perc_change data="-0.54"/>
        <delay data="0"/>
        <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&amp;q=AAPL"/>
        <chart_url data="/finance/chart?q=NASDAQ:AAPL&amp;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"/>
    </finance>
</xml_api_reply>

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

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" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
</head>
<body>
    <form id="getQuote" runat="server">
        <h1>Stock Quote Service</h1>
        <asp:Label Text="Enter a symbol to lookup:" />
        <asp:TextBox id="symbol" runat="server" />
        <asp:Button Text="Get Quote" runat="server" />
        <div>Company: <%=Company %></div>
        <div>Company: <%=Exchange %></div>
        <div>High: <%=High %></div>
        <div>Low: <%=Low %></div>
        <div>Last: <%=Last %></div>
    </form>
</body>
</html>

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;
        }
    }
}
Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s