Home > Coding, Day Trading, Python 3.3 > Python3.3 | Access To Google Finance Intraday Data

Python3.3 | Access To Google Finance Intraday Data

This is my first official project in Python 3.3.2.

First of all, I would like to thank Mr. Mark Chenoweth, who originally wrote the entire script in Python 2.7, without his encouragement I would not have ventured into converting the script to Python 3.3.2. I also wish to thank Python-forum, whose guidance was crucial in completing my task with ease.

Version Details:

Python 3.3.2

Few words about the script: With this script you can both view the downloaded data as well as store the data in csv format. The data will be stored in the directory from where you run the script. I have added user input feature so that you can enter the symbol of your choice. In the second user input, you have to enter “symbol”.csv. Since I am a total beginner to programming as well as to Python, with my limited knowledge I wrote these snippets; if you have a better way, I would love to learn.

My next assignment will be to store the data in Mysql database and to add charting features with my HMA-BB MDTS. I will be doing the charting with Matplotlib and Numpy.


import urllib.request
import datetime
import time

class Quote(object):
    DATE_FMT = '%Y-%m-%d'
    TIME_FMT = '%H:%M:%S'

    def __init__(self):
        self.symbol = ''
        self.date,self.time,self.open_,self.high,self.low,self.close,self.volume = ([] for _ in range(7))

    def append(self,dt,open_,high,low,close,volume):

    def to_csv(self):
        return ''.join(["{0},{1},{2},{3:.2f},{4:.2f},{5:.2f},{6:.2f},{7}\n".format(self.symbol,
              for bar in range(len(self.close))])

    def write_csv(self,filename):
        with open(filename, 'w') as f:

    def read_csv(self,filename):
        self.symbol = ''
        self.date,self.time,self.open_,self.high,self.low,self.close,self.volume = ([] for _ in range(7))
        for line in open(filename, 'r'):
            symbol,ds,ts,open_,high,low,close,volume = line.rstrip().split(',')
            self.symbol = symbol
            dt = datetime.datetime.strptime(ds+' '+ts,self.DATE_FMT+''+self.TIME_FMT)
        return True

    def __repr__(self):
        return self.to_csv()

class GoogleIntradayQuote(Quote):

    ''' Intraday quotes from Google. Specify interval seconds and number of days '''
    def __init__(self,symbol,interval_seconds=300,num_days=5):
        self.symbol = symbol.upper()
        url_string = "http://www.google.com/finance/getprices?q={0}".format(self.symbol)
        url_string += "&i={0}&p={1}d&f=d,o,h,l,c,v".format(interval_seconds,num_days)
        csv = urllib.request.urlopen(url_string).readlines()
        for bar in range(7,len(csv)):
            if csv[bar].decode().count(',')!=5: continue
            offset,close,high,low,open_,volume = csv[bar].decode().split(',')
            if offset[0]=='a':
                day = float(offset[1:])
                offset = 0
                offset = float(offset)
            open_,high,low,close = [float(x) for x in [open_,high,low,close]]
            dt = datetime.datetime.fromtimestamp(day+(interval_seconds*offset))

if __name__ == '__main__':
#    q = GoogleIntradayQuote('sbin', 300, 6)
    q = GoogleIntradayQuote(input('symbol: '))
    q.write_csv(input('csv: '))
#    q.write_csv('sbin.csv')

Healthy And Wealthy Trading
Healthy And Wealthy Coding

  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: