The following post shows you how to check for any stock splits and ex-dividends happening. It'll spit out a list of symbols. These can be used as alerts or piped into another script for updating your database as needed.
I want to thank pcavatore for suggesting this topic! Comments and suggestions help drive the content of this site. Now, onto the explanation. If you're just interested in the code, skip to the bottom or check out the github repo.
1. Requirements. We'll be using the "XML" library. In R, it makes it easy to extract tabular data from webpages. Running the following code will install "XML" if it isn't already installed. Then it will load the library for use.
if (!require(XML)) install.packages('XML')
library(XML)
2. URL selection. We'll need some online location from which to pull the data. Yahoo! has easy to grab stock split info; Nasdaq.com has easy to grab dividend info. So we'll use those.
#for stock split
url <- "http://biz.yahoo.com/c/s.html"
#or, for dividend information
url <- "http://www.nasdaq.com/dividend-stocks/dividend-calendar.aspx"
3. Download the data. We use the readHTMLTable function in order to grab the data.
tables <- readHTMLTable(url)
4. Extract desired data. The previous command will return more tables than you're interested in. Within those tables is also more data than we're interested in. So, we'll print out just the specific table and data column we'd like, now that the tables are stored in memory.
#for stock split data you only want the 6th table and the 3rd data column
tables[[6]][3]
#for the ex-dividend data you only want the "Table1" table and its 1st data column
tables[['Table1']][1]
5. The complete code. It can also be found on github, here.
if (!require(XML)) install.packages('XML')
library(XML)
# Check for stock splits
url <- "http://biz.yahoo.com/c/s.html"
tables <- readHTMLTable(url)
tables[[6]][3]
# Check for ex-dividend
url <- "http://www.nasdaq.com/dividend-stocks/dividend-calendar.aspx"
tables <- readHTMLTable(url)
tables[['Table1']][1]
6. Conclusion. You should now be able to get a printout of any stock splits or ex-dividend dates occurring. This can be used to help update your database or keep you alerted to potential trading opportunities.
7. Further. The above outline can be used to download other tabular data. The basic outline is: find a site with tabular date, download that data, and then extract the tables and columns you need.
(If you've enjoyed the above article, consider signing up for the Gödel's Market Newsletter. Every signup encourages me to write more. Thank you!)