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.

4 comments:

  1. Hey, I found a very small error in your code. When I typed

    print msft["Adjusted Close"][-100:]

    I receive an error message. However, when I typed

    print msft["Adj Close"][-100:]

    it works fine. Basically, the header name of the column is "Adj Close" and not "Adjusted Close". I hope this is useful to anyone who experienced the same problem.

    By the way, fine job on the blog!! Bookmarked =) Fellow econ student here.

    ReplyDelete
  2. Nice catch, I've fixed the typo in the post. Thanks for catching that and for the kind words Graeme. Cheers!

    ReplyDelete
  3. is there any way to get prices for stocks at once multiple, im doing my analysis on > 3000 stocks and it's very time consuming

    ReplyDelete
  4. http://stackoverflow.com/questions/28174193/add-new-column-based-on-a-list-and-sort-date-by-newest/28210920#28210920 code for multiple stocks from yahoo finance

    ReplyDelete