Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

r - Find which sector a stock belongs to

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...