Tuesday, December 29, 2009

Getting Historical Stock Price Data from Yahoo (Quickly and into Excel)

Quite a bit of information on the internet will tell you how to access the historical data from Yahoo. Comparing to paid sources, the stock and option prices from Yahoo are quite good, but the bond prices are not. I will outline for reference how to get historical stock data. Unfortunately, this does not appear to work for other prices like futures, options, and bonds.

(Photo by Dannels)

1. Some basics
Stock data can be download using the interface from Yahoo. But a quicker way is to access the stock data directly using web URLs. Typing the following in your web browser and you will be prompted to download a CSV file containing historical price data for Apple Inc. (and a few other things).

http://ichart.finance.yahoo.com/table.csv?s=AAPL

To get other companies, you will need to replace AAPL with the stock ticker you want. For Microsoft, it would be MSFT.

2. From Firefox to Excel

In Firefox, create a new bookmark, and input the following information:



The location text box should contain the following, which replaces AAPL with %s.

http://ichart.finance.yahoo.com/table.csv?s=%s

After you save the changes, you can now enter the following in the URL box to get the historical apple inc prices.

yfh AAPL

Pictorially,



When you hit enter, you should have something similar to the following. Firefox replaces your string with the one needed to get the historical data.



Open the files, and the historical data of Apple Inc. is now available for data analysis.

3. Other Possibilities

Using the same idea, you can also write Perl scripts to get the data for multiple companies for analysis. For example, I wrote a script to download a list of stocks and calculate moving averages. Alternatively, you can do the same thing with a bit of VBA for Excel.