0

How do I extract the list of all stocks that were part of the S&P 500 index during the last N years (e.g., 20 years) in R? (or perhaps just get this list).

I know how to get the list of the current stocks:

url <- "https://en.wikipedia.org/wiki/List_of_S%26P_500_companies"
tickers <- url %>%
read_html() %>%
html_nodes(xpath = '//*[@id="constituents"]') %>% 
html_table()
sp500tickers <- tickers[[1]]
sp500tickers = sp500tickers %>% mutate(Symbol = case_when(Symbol == "BRK.B" ~ "BRK-B",
                                                          Symbol == "BF.B" ~ "BF-B",
                                                          TRUE ~ as.character(Symbol)))
symbols = sp500tickers$Symbol

print(symbols) 

But how to get all the symbols during the last N years?

YefR
  • 369
  • 3
  • 12
  • 1
    This site keeps an updated list of changes since 2006 in a Google doc: https://newportquant.com/historical-components-of-sp-500-index/ – Brian Montgomery Nov 28 '21 at 23:45
  • @BrianMontgomery This is very helpful! Thanks! Are you aware of a list for a longer period? – YefR Nov 29 '21 at 11:56
  • 1
    I found this: https://github.com/leosmigel/analyzingalpha/tree/master/2019-09-18-sp500-historical-components-and-changes the `sp500_history.csv` goes back farther. Please read the associated material and disclaimer. – Brian Montgomery Nov 29 '21 at 19:53

0 Answers0