O'Reilly logo

Python for Finance by Yuxing Yan

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Retrieving historical price data from Yahoo! Finance

The function called quotes_historical_yahoo() in the matplotlib module could be used to download historical price data from Yahoo! Finance. For example, we want to download daily price data for IBM over the period from January 1, 2012 to December 31, 2012, we have the following four-line Python code:

>>>from matplotlib.finance import quotes_historical_yahoo
>>>date1=(2012, 1, 1)
>>>date2=(2012, 12,31)
>>>price=quotes_historical_yahoo('IBM', date1, date2)

To download IBM's historical price data up to today, we could use the datetime.date.today() function as follows:

>>>import datetime
>>>import matplotlib.finance as finance
>>>import matplotlib.mlab as mlab
>>>ticker = 'IBM'
>>>begdate = datetime.date(2013,1,1) ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required