May 29, 2008

Python - Yahoo Stock Quotes - Historical Pricing

I recently received a patch to my ystockquote.py module for retrieving historical stock prices. It takes a start and end date (YYYMMDD) and a ticker symbol, and returns pricing data (a nested list) for the time period specified. For example:
import ystockquote

ticker = 'GOOG'
start = '20080520'
end = '20080523'

data = ystockquote.get_historical_prices(ticker, start, end)

for dat in data:
    print dat
Output:
['Date', 'Open', 'High', 'Low', 'Close', 'Volume', 'Adj Clos']
['2008-05-23', '546.96', '553.00', '537.81', '544.62', '4431500', '544.6']
['2008-05-22', '551.95', '554.21', '540.25', '549.46', '5076300', '549.4']
['2008-05-21', '578.52', '581.41', '547.89', '549.99', '6468100', '549.9']
['2008-05-20', '574.63', '582.48', '572.91', '578.60', '3313600', '578.6']

13 comments:

Anonymous said...

You can also use this project: . Still alpha, still command line, but downloading current and historical data from Yahoo! Finance works fine. Requires PostgreSQL database though. Also has portfolio download and some basic analytic functions.

Anonymous said...

like the ystockquotes. i was about to write one myself. thanks.

Unknown said...

Corey,
I did something similar for google finance.

You may be interested in checking it out;
http://blog.thecapacity.org/2008/07/07/what-if-stocks-were-movies/

my next step is to modify my class so I can select the historical range (right now it's just google's default).

jay

Anonymous said...

I think it strips one char too many at line 161,

data = [day[:-2].split(',') for day in days]

At least on unix the line ending is '\n' not '\r\n' so you might want to use a len(os.linesep) to be x-plat.

Thanks for making the code available BTW.

tim said...

good job, but it seems like yahoo does not provide the complete historical data? At least there are lots of gaps and the the range is quite limited.

tim said...

Of course, there are weekends, thats why there are gaps, stupid. However, the euro-dollar exchange rate with symbol "EURUSD=X" is a little buggy.

Anonymous said...

Hey,

I'm new to python, and this may be a really stupid question, but I'm getting the following error when I try to use any of the functions in this module:

print ystockquote.get_price('GOOG')
Traceback (most recent call last):
File "interactive input", line 1, in module
AttributeError: 'module' object has no attribute 'get_price'

Can you let me know what I'm doing wrong? I've imported the module.

Thanks.

Anonymous said...

Thanks for posting this; it makes my life so much easier!

Since I'm using this within a Python program I've added a routine that accepts start/end dates as standard datetime objects. Not much code, but may be useful to others.

import datetime
def get_past_prices(symbol, start_date, end_date):
"""
Get historical prices for the given ticker symbol.
start_date and end_date are Python datetime objects.

Returns a nested list.
"""
return get_historical_prices(symbol, start_date.strftime("%Y%m%d"), end_date.strftime("%Y%m%d"))

BlueSpirit said...

Would be nice if your module can do the download from yahoo finance key statistics page:

http://finance.yahoo.com/q/ks?s=MSFT

Anonymous said...

In the get_historical_prices function a parameter is missing from the complete url - 'c' that stands for the start_date year

fix:

url = 'http://ichart.yahoo.com/table.csv?s=%s&' % symbol + \
'd=%s&' % str(int(end_date[4:6]) - 1) + \
'e=%s&' % str(int(end_date[6:8])) + \
'f=%s&' % str(int(end_date[0:4])) + \
'g=d&' + \
'a=%s&' % str(int(start_date[4:6]) - 1) + \
'b=%s&' % str(int(start_date[6:8])) + \
'c=%s&' % str(int(start_date[0:4])) + 'ignore=.csv'

Anonymous said...

In the introduction the format for the start and end date is wrong it should be (YYYYMMDD) rather than(YYYMMDD).

Vicente Soler said...

Do you have the same code but for Python 3?

Thank you

Sharad said...

I added Python 3 support for this script:

https://github.com/sharadk/mango/blob/master/ystockquote.py