# Getting data of sub-sector indexes of an S&P 500 index sector using QuantMod in R

Using the quantmod package in R, one can download the S&P500 index using the following command:

getSymbols("^GSPC", env = sp500, src = "yahoo",from = as.Date("1960-01-04"), to = as.Date("2009-01-01"))

The S&P is further broken down in sectors (with their yahoo symbols in brackets): Health care (XLV), industrials (XLI), consumer staples (XLP), consumer discretionary (XLY), materials (XLB), utilities (XLU), financials (XLF or XLFS), energy (XLE), technology (XLK), and Real Estate (XLRE).

These sectors are further broken down in subsectors, but I do not know how to get these indexes. For example, Healtcare is has Managed care, Pharmaceuticals, equipment, as subsectors.
For example,

http://finance.yahoo.com/quote/%5ESP500-3510?p=%5ESP500-3510

is a subsector of the Health care index, but using the symbol given there (^SP500-3510) does not seem to work in the above getSymbol R command. Any suggestion on how to get this index?

This is a Yahoo data issue - you should contact them regarding their symbols and availability of data.
It might just be that the index is not covered by them or they are restricted from providing historical data.

Also note that you have referenced ETFs (XLV, XLI, etc.) that trade on NYSE Arca as being sector-based subsets of the S&P 500. These ETFs are based upon the Select Sector indexes. The "Select" indexes are a little different from the actual S&P 500 sector indexes in that they have more rules associated with index composition (to minimize churn) and also include other GICS classifications.

For example, XLK (Technology Select Sector SPDR ETF) is not a GICS sector - it comprises both Information Technology and Telecommunications companies. From time-to-time it has a different set of components comapred to the S&P 500 Information Technology Sector Index and the S&P 500 Telecommunications Sector Index.

There is something wrong with the web page for that index. I tried viewing historical data and there was none. The link to download historical data is also broken. I suggest testing another index.

getSymbols("^GSPC", env = sp500, src = "yahoo",from = as.Date("1960-01-04"), to = as.Date("2009-01-01"))


this code not gonna work until you create a new environment which you used, sp500. you need to create this new environment using sp500 = new.env().

or you can not creat it but you need to change your code to

getSymbols("^GSPC", src = "yahoo",from = as.Date("1960-01-04"), to = as.Date("2009-01-01"))


Alternatively you could check this link:

getSymbols("^GSPC", src = "yahoo", from = as.Date("2019-05-20"),