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 :
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);
}
}
}
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">
</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);
}
}
}