Fetching data off Google Finance need help

This is the place for queries that don't fit in any of the other categories.

Fetching data off Google Finance need help

Postby joe141 » Sun Dec 22, 2013 9:55 pm

Hi Guys i wrote a code to fetch data off google finance and the code is below.The issue is that this code is built to fetch data for only 1 symbol at a time.Can you guys point me as to how I can do the same for multiple stock codes.....I would be thankful to the extreme:

Code: Select all
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):
        self.date.append(dt.date())
        self.time.append(dt.time())
        self.open_.append(float(open_))
        self.high.append(float(high))
        self.low.append(float(low))
        self.close.append(float(close))
        self.volume.append(int(volume))
 
    def to_csv(self):
        return ''.join(["{0},{1},{2},{3:.2f},{4:.2f},{5:.2f},{6:.2f},{7}\n".format(self.symbol,
              self.date[bar].strftime('%Y-%m-%d'),self.time[bar].strftime('%H:%M:%S'),
              self.open_[bar],self.high[bar],self.low[bar],self.close[bar],self.volume[bar])
              for bar in range(len(self.close))])
 
    def write_csv(self,filename):
        with open(filename, 'w') as f:
            f.write(self.to_csv())
 
    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)
            self.append(dt,open_,high,low,close,volume)
        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=60,num_days=15):
        super(GoogleIntradayQuote,self).__init__()
        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
            else:
                offset = float(offset)
            open_,high,low,close = [float(x) for x in [open_,high,low,close]]
            dt = datetime.datetime.fromtimestamp(day+(interval_seconds*offset))
            self.append(dt,open_,high,low,close,volume)
 
if __name__ == '__main__':
#    q = GoogleIntradayQuote('sbin', 300, 6)
    q = GoogleIntradayQuote(input('symbol: '))
    q.write_csv(input('csv: '))
    print(q)
#    q.write_csv('sbin.csv')
Last edited by stranac on Sun Dec 22, 2013 10:09 pm, edited 1 time in total.
Reason: Added code tags. Locked.
joe141
 
Posts: 3
Joined: Sun Dec 22, 2013 9:48 pm

Re: Fetching data off Google Finance need help

Postby micseydel » Mon Dec 23, 2013 11:20 pm

Are you a programmer looking for help, or someone who wants this done for you? Because this looks simple, but if you do want to learn you need to first spend some time learning programming and understand control flow and such before tackling something like this, and then be more specific about why you're having trouble doing it yourself. And if you want it done for you, then we should move your post to the Jobs section and you might want to explain as specifically as you can what it is you want and possibly what/how you're willing to pay. Someone might offer to do it for free anyway, but we still prefer to put non-educational posts in the Jobs section.
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1443
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Fetching data off Google Finance need help

Postby joe141 » Tue Dec 24, 2013 3:21 pm

I want to learn sir
joe141
 
Posts: 3
Joined: Sun Dec 22, 2013 9:48 pm

Re: Fetching data off Google Finance need help

Postby micseydel » Tue Dec 24, 2013 6:14 pm

I recommend you start here and then this can be helpful for learning to form technical questions that will get answered promptly.
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1443
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Fetching data off Google Finance need help

Postby runawaykinms » Wed Dec 25, 2013 3:10 am

Hey joe,

I see we have similar interests. I am actually trying to do something very similar. I am a complete newbie, so not sure how much help I can be in the coding area.

Anyway, for finance I have found these two other projects that are interesting that might help a little bit:

1. http://wiki.quantsoftware.org/index.php ... re_ToolKit

2. https://github.com/valisc/ultra-finance

The ultra finance has a cool module called ystockquote.py that seems similar to what you are trying to do except with yahoo finance.

As for your code:I can't help much with my lack of knowledge, but when you call for the stock symbols it looks like you have them set to a string. What if you set them equal to a list and then would be able to pass through a list of symbols instead of just one.
runawaykinms
 
Posts: 7
Joined: Tue Dec 24, 2013 9:57 am

Re: Fetching data off Google Finance need help

Postby joe141 » Wed Dec 25, 2013 12:32 pm

thanks both of you big help
joe141
 
Posts: 3
Joined: Sun Dec 22, 2013 9:48 pm


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 5 guests