2

Given stocks in S&P500, how can I find which sector each stock belongs to, e.g. financial, energy ...., using R package, or other sources?

GSee
  • 48,880
  • 13
  • 125
  • 145
Kun
  • 41
  • 1
  • 4
  • Have you looked at http://cran.r-project.org/web/views/Finance.html? – Victor K. May 08 '13 at 05:06
  • You can get the data from Wikipedia: `http://en.wikipedia.org/wiki/Special:Export/List_of_S%26P_500_companies` (you need to parse the file, though). – Vincent Zoonekynd May 08 '13 at 06:11
  • Thank you guys! I will use just wiki then. This is probably faster than trying to find the specific function.. – Kun May 08 '13 at 18:58

1 Answers1

5

The term "sector" is, by itself, an ambiguous term. What one data provider calls "consumer services" may be called "restaurants" by another. That said, TTR provides a function called stockSymbols that returns some information including Sector, from NASDAQ, for ~6400 NMS stocks.

library(TTR)
ss <- stockSymbols()
#Fetching AMEX symbols...
#Fetching NASDAQ symbols...
#Fetching NYSE symbols...
head(ss)
#  Symbol                      Name LastSale MarketCap IPOyear           Sector                        Industry Exchange
#1   AA-P                Alcoa Inc.   92.300         0      NA    Capital Goods              Metal Fabrications     AMEX
#2    AAU    Almaden Minerals, Ltd.    1.620  97228060      NA Basic Industries                 Precious Metals     AMEX
#3    ACU  Acme United Corporation.   12.984  40798351    1988    Capital Goods Industrial Machinery/Components     AMEX
#4    ACY         AeroCentury Corp.   20.280  31297252      NA       Technology Diversified Commercial Services     AMEX
#5   ADGE   American DG Energy Inc.    1.720  83404061      NA           Energy     Electric Utilities: Central     AMEX
#6    ADK Adcare Health Systems Inc    5.800  85018494      NA      Health Care     Hospital/Nursing Management     AMEX

If you just want stocks that are in the S&P 500, you can cheat and use the holdings of SPY (or there are tons of places that you can find the holdings of the S&P 500, including the Standard & Poors website)

#install.packages("qmao", repos="http://r-forge.r-project.org")
library(qmao)
spyh <- getHoldings("SPY", auto.assign=FALSE)
head(ss[ss$Symbol %in% rownames(spyh), ])
#    Symbol                            Name LastSale    MarketCap IPOyear        Sector
#455   AAPL                      Apple Inc.   452.97 425179837530    1980    Technology
#490   ADBE      Adobe Systems Incorporated    44.02  22095230291    1986    Technology
#493    ADI            Analog Devices, Inc.    46.79  14317018779      NA    Technology
#495    ADP Automatic Data Processing, Inc.    70.03  33980125863      NA    Technology
#500   ADSK                  Autodesk, Inc.    39.75   8896050000      NA    Technology
#535   AKAM       Akamai Technologies, Inc.    46.70   8333728621    1999 Miscellaneous
#                                   Industry Exchange
#455                  Computer Manufacturing   NASDAQ
#490 Computer Software: Prepackaged Software   NASDAQ
#493                          Semiconductors   NASDAQ
#495                            EDP Services   NASDAQ
#500 Computer Software: Prepackaged Software   NASDAQ
#535                       Business Services   NASDAQ
GSee
  • 48,880
  • 13
  • 125
  • 145
  • actually, if you use `getHoldings()` (which calls `getHoldings.SPDR` when you give it "SPY"), then you don't even need `stockSymbols()`. From the code above, you can see that `spyh` actually has values for "Sector" for all the stocks. In fact, you can compare the values for Sector that `stockSymbols()` returns with those that `getHoldings.SPDR()` returns to see what I mean about the term "sector" being ambiguous. – GSee May 13 '13 at 18:02
  • THanks for your code, that's exactly what I was looking for. However I get an error `Error in out[[1L]] : subscript out of bounds` after `spyh <- getHoldings("SPY", auto.assign=FALSE)` – user1627466 Feb 04 '14 at 15:38
  • got just what I needed from sp500.components() in https://github.com/systematicinvestor/SIT/blob/master/R/data.r – user1627466 Feb 04 '14 at 16:02
  • GSee, I am also getting `Error in out[[1L]]` when running the command above, and a warning: `In readLines(tmp, 1) : line 1 appears to contain an embedded nul`. If I turn `auto.assign` to `TRUE`, the error disappears but the warning is still there. And `SPY.h` is a data frame with 0 columns and 512 rows. I hope you can help. – flodel Oct 28 '14 at 03:13
  • 1
    @flodel thanks for the report. I just fixed `getHoldings.SPDR()` (which is what `getHoldings()` dispatches to if the symbol is "SPY"). Unfortunately, all of these getHoldings.*() functions rely on the websites of the fund family not changing. The iShares site has given me the most trouble, changing many times. I used to patch ever month or two to keep up, but then they changed the website so it is hard to find a holdings file based on Symbol. I wish I had the time to maintain these that I had when I wrote them. I would love to accept patches. ;-) – GSee Oct 28 '14 at 12:05
  • @user1627466 sorry I missed your comment. This should work now for "SPY" (you'll need to [checkout and build](http://stackoverflow.com/questions/11105131/cannot-install-r-forge-package-using-install-packages/11105132#11105132) the source code which can be found [here](https://r-forge.r-project.org/scm/?group_id=1113) until R-Forge builds it) – GSee Oct 28 '14 at 12:12