31 Mar, 2011

How to Access Stock Quotes Realtime through Google Finance

Google Finance is a product of Google, that tracks everything related to the Stock market and manage your Portfolio etc. It has access to realtime data of various stock exchanges around the world like NASDAQ, NSE of India etc. We can use this to get realtime data of stocks for programatically accessing the value of a stock.

Tell me already, Where are the quotes?

http://finance.google.com/finance/info?client=ig&q=NASDAQ:GOOG
The above URL gives us the latest quote for Google Shares. The Google Finance URL structure is pretty simple. The Query string q= requires the symbol of the stock you are interested in. It is of the format Exchange:Symbol. Some examples of these query strings are
  • Microsoft : NASDAQ:MSFT
  • Apple : NASDAQ:AAPL
  • Yahoo : NASDAQ:YHOO
  • Reliance (NSE of India) : NSE:RELIANCE
  • Pearson (London Stock Exchange) : LON:PSE
The response for the above URL is as follows
// [
{
"id": "694653"
,"t" : "GOOG"
,"e" : "NASDAQ"
,"l" : "581.84"
,"l_cur" : "581.84"
,"s": "0"
,"ltt":"4:00PM EDT"
,"lt" : "Mar 30, 4:00PM EDT"
,"c" : "+0.11"
,"cp" : "0.02"
,"ccol" : "chg"
}
]
Getting the Quotes from the above response is much simpler than scrapping a page. UPDATE:
http://finance.google.com/finance/info?client=ig&q=NASDAQ:GOOG,NASDAQ:YHOO

You can get multiple quotes in a single request by comma separating the symbols on q parameter.

Will come back soon with a script that does something based on this realtime data from Google Finance.

UPDATE:

The Code in Python

import urllib2
import json
import time

class GoogleFinanceAPI:
    def __init__(self):
        self.prefix = "http://finance.google.com/finance/info?client=ig&q="
    
    def get(self,symbol,exchange):
        url = self.prefix+"%s:%s"%(exchange,symbol)
        u = urllib2.urlopen(url)
        content = u.read()
        
        obj = json.loads(content[3:])
        return obj[0]
        
        
if __name__ == "__main__":
    c = GoogleFinanceAPI()
    
    while 1:
        quote = c.get("MSFT","NASDAQ")
        print quote
        time.sleep(30)

The above code prints the Quote for Microsoft stock on NASDAQ every 30 seconds.

Meanwhile all ye mates try something interesting with the stock quotes
(Than making profits and losses :) )

Dont forget to ping me on your updates. Post a comment.
Thank you Google.


Comments (15) Post a comment
  • Watch out!

    This service is going down in October, 2012.

    http://googlecode.blogspot.com/2011/05/spring-cleaning-for-some-of-our-apis.html

    By Good Samaritan on 23 Apr, 2012 Reply
    • This is not exactly API,

      This is not exactly a documented API, but something the Google finance website uses. So I suppose we should be safe.

      By digitalpbk on 24 Apr, 2012 Reply
  • HI Arun

    How can i fetch the GOLD price from google finance.
    when i go for
    "http://finance.google.co.uk/finance/info?client=ig&q=NASDAQ:GOLD" it will give me price of company name with GOLD.

    I want price of Gold metal.
    please help.
    Thanks in advance.

    By Ankur on 05 Apr, 2012 Reply
  • Sir,
    A lot of thanks for this post.It is really useful.I have a doubt.How can we get days open,high,low,and last price and volume in between trading hours?
    may you help me
    With thanks
    Gilari Decosta

    By gilari decosta on 05 Mar, 2012 Reply
  • Dear arun, how to redirect that data to mysql... in a realtime basis..

    By kannan on 13 Feb, 2012 Reply
  • This is awesome, it is exactly what I have been looking for. But I want to modify it to show stock options quotes, not just the primary equity quote. (I know C, and a bit of Ruby, so I should be able to modify the code.) But I need to know what the URL looks like to get an options quote, do you know that? Please share! Thanks!

    By Anonymous Coward on 27 Dec, 2011 Reply
  • Gold ounce price

    How can i get the current gold ounce price like goldprice.org/live-gold-price.html ?

    By Anonymous on 11 Sep, 2011 Reply
    • look up an equivalent Gold ETF

      Look for the price of an equivalent Gold ETF fund price on google for your country.
      By digitalpbk on 12 Sep, 2011 Reply
  • PHP Version

    Here's how to collect stock prices using PHP :
    <?php
    
      //Obtain Quote Info - This collects the Microsoft Stock Info
      $quote = file_get_contents('http://finance.google.co.uk/finance/info?client=ig&q=NASDAQ:MSFT');
      
      //Remove CR's from ouput - make it one line
      $json = str_replace("\n", "", $quote);
      
      //Remove //, [ and ] to build qualified string  
      $data = substr($json, 4, strlen($json) -5);
        
      //decode JSON data
      $json_output = json_decode($data, true);
      
      //Un-remark print_r to see all array keys
      //print_r($json_output);
      
      //Output Stock price array key.
      echo "\n".$json_output['l']; 
    
    ?>
    
    By WH101 on 24 Aug, 2011 Reply
  • PHP

    Can this script be run in PHP using the Python in PHP (PiP)?

    Thanks,
    TRH

    By TRH on 09 Aug, 2011 Reply
    • Can easily port the code

      You can easily port the code to PHP, not sure why you want to run python inside php.
      By digitalpbk on 10 Aug, 2011 Reply
  • Is there some way to get

    Is there some way to get Volume also?

    By Piyush Daga on 13 Jul, 2011 Reply
    • Were you able to get the Vol as well ?

      Thanks

      By Chen on 16 Feb, 2012 Reply
  • Programming Language

    Thanks. Your URL to Google is exactly what I've been looking for. What language is "The Code" written in?

    By Anonymous on 20 Jun, 2011 Reply
    • Python

      Ooops totally missed out that I guess. The code for getting realtime stocks from google was written using Python.
      By digitalpbk on 20 Jun, 2011 Reply
You may also like



Email Newsletter
Email:
Popular Posts
Recent Posts
Tags
Random photo
Piercing Light Radha Nagar Beach 1 Havelock Piercing Light Radha Nagar Beach 1 Havelock in Havelock Island Andaman
On Facebook
Recent Comments


digitalpbk