Breaking News
Loading...
July 18, 2015

How to Access Stock Quotes Realtime through Google Finance

9:55 AM
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
Source URL: http://digitalpbk.com/stock/google-finance-get-stock-quote-realtime

0 comments:

Post a Comment

 
Toggle Footer