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.

7 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
  5. In the past few decades there has been a revolution in computing and communications, and all indications are that technological progress and use of information technology will continue at a rapid pace. Accompanying and supporting the dramatic increases in the power and use of new information technologies has been the declining cost of communications as a result of both technological improvements and increased competition. According to Moore's law the processing power of microchips is doubling every 18 months. These advances present many significant opportunities but also pose major challenges. Today, innovations in information technology are having wide-ranging effects across numerous domains of society, and policy makers are acting on issues involving economic productivity, intellectual property rights, privacy protection, and affordability of and access to information. Choices made now will have long lasting consequences, and attention must be paid to their social and economic impacts. see here

    ReplyDelete
  6. Steve Wozniak thinks that education technology of the future will be game-alike, but even as it is today, technology is changing the face of the old analog school. Can computers really be tutors? get redirected here

    ReplyDelete
  7. Information technology is essential for both the administrative and operational activities of every enterprise, especially for building links with constituencies. Because knowledge management and connectivity are essential to building competitive, collaborative, and cooperative advantage, every enterprise is heavily dependent upon data arteries - its information supply, value, and demand chains. Information technology strategy is an enabler of business strategy, and is delivered through programs and projects on a phased basis. spy phone

    ReplyDelete