954,510 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Obtaining quotes from the stock market

I was planning on writing code that would get stock data from the exchange and store it for use and analysis later. I'm new to programming and Python so I don't really know where to get started. Are there functions, modules, or techniques I should be aware of? So far I'm only familiar with conditional programming, defining functions/objects, recursive loops, working with strings, reading from and writing to text files, etc. Just the basics really. What else would I have to learn to get started on a project like this? All help is appreciated. Thanks!

-- ThisIsNotAnId.

Thisisnotanid
Junior Poster in Training
60 posts since Dec 2010
Reputation Points: 10
Solved Threads: 0
 

Use an online resource that makes it easy to retrieve data. Many sites have an API (Application Programming Interface) that allow programs to retrieve data with a few function calls.

For instance, Yahoo lets you retrieve a csv file of stock quotes with a url.
You can use the urllib.retrieve module to retrieve the data, and the csv module to process it.

#Written for and tested on Python 3.1.
import csv
import urllib.request

def yahoo_quotes_csv_url(tickers):
    return ''.join(('http://finance.yahoo.com/d/quotes.csv?s=',
                    '+'.join(tickers),
                    '&f=snl1'))

if __name__ == '__main__':
    tickers = ('GOOG', 'AAPL')
    url = yahoo_quotes_csv_url(tickers)
    print(url)
    urllib.request.urlretrieve(url, 'quotes.csv')
    reader = csv.reader(open('quotes.csv'))
    for row in reader:
        #row[0]=ticker, row[1]=company, float(row[2])=value
        print(row)


Different sites will provide different interfaces and have more data, but if you remember to read the documentation about retrieving data and if you remember to use the right modules you can use it.

lrh9
Posting Whiz in Training
243 posts since Oct 2009
Reputation Points: 119
Solved Threads: 36
 

As postet over using site API can help.

For webscraping are BeautifulSoup and lxml good parser.
Just a quick example with BeautifulSoup,take out price of bean.

#Python 2.x
from BeautifulSoup import BeautifulSoup
import urllib2

url = urllib2.urlopen('http://beans.itcarlow.ie/prices.html')
soup = BeautifulSoup(url)
#print soup #site conteds

tag = soup.find('strong')
print tag  #[<strong>$6.36</strong>]
print tag.string  #$6.36
snippsat
Practically a Posting Shark
808 posts since Aug 2008
Reputation Points: 353
Solved Threads: 294
 

Thanks fellas! Now to test the code!

Thisisnotanid
Junior Poster in Training
60 posts since Dec 2010
Reputation Points: 10
Solved Threads: 0
 

Google finance has a "Get quotes" function.
Here is a demo how we can use this function with python.
To get data here i use regular expression.
Regular expression and html can be hard,thats why parser often are better to use.
Will see if i write a BeautifulSoup to.

import re, urllib

def get_quote(symbol):
    base_url = 'http://finance.google.com/finance?q='
    content = urllib.urlopen(base_url + symbol).read()
    find_q = re.search(r'\<span\sid="ref_\d+.*">(.+)<', content)
    if find_q:
        quote = find_q.group(1)
    else:
        quote = 'no quote available for: %s' % symbol
    return quote

def main():
    #print get_quote('ibm') #168.28

    #Test of 5 companys
    Companys = ['google', 'ibm', 'microsoft', 'apple', 'nvidia']
    for i,value in enumerate(Companys):
        print '%s --> %s' %  (Companys[i],get_quote(value))

        '''Output-->
        google --> 525.10
        ibm --> 168.28
        microsoft --> 25.52
        apple --> 350.70
        nvidia --> 18.52
        '''

if __name__ == "__main__":
    main()
snippsat
Practically a Posting Shark
808 posts since Aug 2008
Reputation Points: 353
Solved Threads: 294
 

Snippsat: cleaned little your code, as loop was little strange:

import re, urllib

def get_quote(symbol):
    base_url = 'http://finance.google.com/finance?q='
    content = urllib.urlopen(base_url + symbol).read()
    find_q = re.search(r'\<span\sid="ref_\d+.*">(.+)<', content)
    return find_q.group(1) if find_q else 'no quote available for: %s' % symbol

def print_quotes(companys):
    for company in companys:
        print '%s --> %s' %  (company,get_quote(company))

if __name__ == "__main__":
    #Test of 5 companys
    print_quotes(['google', 'ibm', 'microsoft', 'apple', 'nvidia'])


Best would be to check Google's Python API for access.

pyTony
pyMod
Moderator
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
 
Best would be to check Google's Python API for access.

http://code.google.com/p/gdata-python-client/

pyTony
pyMod
Moderator
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
 

Thanks for all the info everyone! I didn't know about urllib or re before, but it turns out they can be used for a lot!

Thisisnotanid
Junior Poster in Training
60 posts since Dec 2010
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: