Book Image

Python for Finance

By : Yuxing Yan
Book Image

Python for Finance

By: Yuxing Yan

Overview of this book

Table of Contents (20 chapters)
Python for Finance
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
Index

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)
>>>enddate = datetime.date.today()
>>>price = finance.fetch_historical_yahoo(ticker, begdate, enddate)
>>>r = mlab.csv2rec...