Saturday, February 11, 2012

Getting Yahoo Finance Dividend Data in CSV Format and Plotting the Dividend Data in Stata

Here is some code to create a Stata program to get dividend data from Yahoo Finance and plotting it:

program define sdchart
    preserve
    clear
    insheet using "http://ichart.finance.yahoo.com/table.csv?s=`0'&a=00&b=01&c=1900&d=11&e=31&f=2099&g=v&ignore=.csv"
    gen tempDate = date(date, "YMD")
    drop date
    gen date = tempDate
    format date  %td
    drop tempDate
    line dividend date
    drop dividend date
   restore
end

If you put the http URL in your web browser, and replaced `0’ with a stock ticker (like "INTC"), you will be returned with a CSV file with Intel’s dividend. For example, click the following:

http://ichart.finance.yahoo.com/table.csv?s=INTC&a=00&b=01&c=1900&d=11&e=31&f=2099&g=v&ignore=.csv 

The rest of the code just takes the csv file data and plots it in Stata. I like using Stata because

  1. I have been using Stata a lot lately for my work, and
  2. that it can download web data (like those from Yahoo Finance) directly within the program.

So if you run sdchart INTC, you will get:

Graph

I have the code in my profile.do file in the Stata startup directory.

Reference: How to download the history of dividend payments for stocks in the US market [Quantshare]