Friday 20 April 2012

Getting Google Live Stock Data in Silverlight

                We can use URL Query string for getting the Details from Google API.

Url Format for Google: http://www.google.com/ig/api?stock=symbol name
for multiple stocks : http://www.google.com/ig/api?stock=symbolname&stock=symbolname

e.g
http://www.google.com/ig/api?stock=BSE

Output of the Url :

<xml_api_reply version="1">
<finance module_id="0" tab_id="0" mobile_row="0" mobile_zipped="1" row="0" section="0">
<symbol data="BSE"/>
<pretty_symbol data="BSE"/>
<symbol_lookup_url data="/finance?client=ig&q=BSE"/>
<company data="BlackRock New York Insured Municipal Inc"/>
<exchange data="NYSE"/>
<exchange_timezone data="ET"/>
<exchange_utc_offset data="+05:00"/>
<exchange_closing data="960"/>
<divisor data="2"/>
<currency data="USD"/>
<last data="15.40"/>
<high data="15.57"/>
<low data="15.33"/>
<volume data="12678"/>
<avg_volume data="12"/>
<market_cap data="99.87"/>
<open data="15.51"/>
<y_close data="15.43"/>
<change data="-0.03"/>
<perc_change data="-0.19"/>
<delay data="0"/>
<trade_timestamp data="19 minutes ago"/>
<trade_date_utc data="20120419"/>
<trade_time_utc data="184149"/>
<current_date_utc data="20120419"/>
<current_time_utc data="190143"/>
<symbol_url data="/finance?client=ig&q=BSE"/>
<chart_url data="/finance/chart?q=NYSE:BSE&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"/>
</finance>
</xml_api_reply>

c# Code :



string url = "http://www.google.com/ig/api?stock=bse"
            Uri GoogleUri = new Uri(url, UriKind.Absolute);
            if (WebRequest.RegisterPrefix("http://", System.Net.Browser.WebRequestCreator.ClientHttp))
            {
                 WebClient queryWebClient = new WebClient();
                 queryWebClient.OpenReadCompleted += new  OpenReadCompletedEventHandler(queryWebClient_OpenReadCompleted);
                 queryWebClient.OpenReadAsync(GoogleUri);
            }




 static void queryWebClient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
        {
            if (e.Error != null)
                return;
            var webClient = sender as WebClient;
            if (webClient == null)
                return;
            XDocument doc = XDocument.Load(new StreamReader(e.Result));
            Parse(_Quotes, doc);
            if (CompletedEvent != null)
                CompletedEvent(_Quotes, e);
        }
        private static void Parse(ObservableCollection<GoogleQuote> _Quotes, XDocument doc)
        {
            XElement results = doc.Root.Element("finance");
            foreach (GoogleQuote quote in _Quotes)
            {
                XElement q = doc.Root.Elements("finance").First(w => w.Element("symbol").Attribute("data").Value == quote.Symbol);
                if (q != null)
                {
                    quote.Symbol = q.Element("symbol").Attribute("data").Value;
                    quote.Prettysymbol = q.Element("pretty_symbol").Attribute("data").Value;
                    quote.Company = q.Element("company").Attribute("data").Value;
                    quote.Exchange_timezone = q.Element("exchange_timezone").Attribute("data").Value;
                    quote.Exchange_utc_offset = q.Element("exchange_utc_offset").Attribute("data").Value;
                    quote.Exchange_closing = GetDecimal(q.Element("exchange_closing").Attribute("data").Value);
                    quote.Divisor = GetDecimal(q.Element("divisor").Attribute("data").Value);
                    quote.Currency = q.Element("currency").Attribute("data").Value;
                    quote.Avg_volume = GetDecimal(q.Element("avg_volume").Attribute("data").Value);
                    quote.Market_cap = q.Element("market_cap").Attribute("data").Value;
                    quote.Delay = q.Element("delay").Attribute("data").Value;
                    quote.LastTradeDateTime = GetDateTime(q.Element("trade_date_utc").Attribute("data").Value, q.Element("trade_time_utc").Value);
                    quote.CurrentDateTime = GetDateTime(q.Element("current_date_utc").Attribute("data").Value,q.Element("current_time_utc").Value);
                    quote.Perc_change = GetDecimal(q.Element("perc_change").Attribute("data").Value);
                    quote.Change = GetDecimal(q.Element("change").Attribute("data").Value);
                    quote.Last = GetDecimal(q.Element("last").Attribute("data").Value);
                    quote.StockExchange = q.Element("exchange").Attribute("data").Value;
                    quote.Low = GetDecimal(q.Element("low").Attribute("data").Value);
                    quote.High = GetDecimal(q.Element("high").Attribute("data").Value);
                    quote.Volume = GetDecimal(q.Element("volume").Attribute("data").Value);
                    quote.Open = GetDecimal(q.Element("open").Attribute("data").Value);
                    quote.Close = GetDecimal(q.Element("y_close").Attribute("data").Value);
                }
            }
        }