Quantcast
Channel: Market Calls
Viewing all articles
Browse latest Browse all 2070

How to Get Realtime Stock Data From Google

$
0
0

Sometime back we discussed about how to get Intraday backfill data from Google and this short article focus on how to get Realtime stock data from Google. Google provides realtime cash market/index/currency data for most of the markets in JSON format.

google realtime

Url format to retrive Realtime Quotes

http://www.google.com/finance/info?q=AAPL

http://www.google.com/finance/info?q=NSE:NIFTY

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

http://finance.google.com/finance/info?client=ig&q=NSE:NIFTY,NSE:RELIANCE

sample JSON output format for Nifty Spot Index(India)

Google finance can return stock quotes using the JSON format, with can be read programming langugages like PHP,Python,Java..etc

// [ { "id": "207437" ,"t" : "NIFTY" ,"e" : "NSE" ,"l" : "7,329.65" ,"l_fix" : "7329.65" ,"l_cur" : "Rs.7,329.65" ,"s": "0" ,"ltt":"3:31PM GMT+5:30" ,"lt" : "May 28, 3:31PM GMT+5:30" ,"lt_dts" : "2014-05-28T15:31:28Z" ,"c" : "+11.65" ,"c_fix" : "11.65" ,"cp" : "0.16" ,"cp_fix" : "0.16" ,"ccol" : "chg" ,"pcls_fix" : "7318" } ]

sample python code to fetch realtime stock data

from urllib import urlopen
import json
def googleQuote(ticker):
    url = '%s%s' % ('http://www.google.com/finance/info?q=', ticker)
    doc = urlopen(url)
    content = doc.read()
    quote = json.loads(content[3:])
    quote = float(quote[0][u'l'])
    return quote
if __name__ == "__main__":
    ticker = 'GOOG'
    print googleQuote(ticker)

To fetch the data every 30th second here is the python code. The below code prints the Quote for Reliance stock on NSE every 30 seconds.

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(“RELIANCE”,”NSE”)
print quote
time.sleep(30)

Related Readings and Observations

The post How to Get Realtime Stock Data From Google appeared first on Marketcalls.


Viewing all articles
Browse latest Browse all 2070