Sunday, November 20, 2011

Retrieve Stock Price Data from Yahoo! Finance using Pandas

Here is a simple example of how to use the Python package pandas to retrieve stock price data from Yahoo! Finance (hereby YF). Assuming pandas is already installed on your system, open up a Python shell and make the following imports:
from pandas.io.data import DataReader
from datetime import datetime
DataReader is what we'll use to retrieve YF stock price data. The DataReader class can also retrieve economic data from two other remote sources, namely the St. Louis Federal Reserve's FRED database and Kenneth French's Data Library, both of which will be the topic of future posts.

For now, let's say we want to grab historical stock price data on Microsoft (MSFT). We'll do so by defining an object msft that contains daily prices for Microsoft as so:
msft = DataReader("MSFT""yahoo")
The first input of DataReader is the name of the dataset (e.g. the stock ticker for YF) and the second is the location of the dataset ("yahoo" for YF). The msft object is a DataFrame object whose rows are days of the year and columns are the prices for each day, labelled Open, High, Low, Close, Volume and Adj Close, respectively. 

By default, the data contains observations from the past year, but that can be changed by providing a datetime object as the third input to DataReader:
msft = DataReader("MSFT""yahoo", datetime(2009,1,1))
The msft object now contains daily price data from the start of 2009 up to today's date. To print a particular column of msft, such as the stock's daily volume, enter:
print msft["Volume"]
As another example, to print the adjusted closing price of MSFT, but only for the last 100 days, enter:
print msft["Adj Close"][-100:]
That's it for now. Expect a follow-up post soon that'll include a more involved example using the FRED database.