Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Finance with R Tutorial Code - S&P 100 Scenario Data
##### Finance with R Tutorial Code - S&P 100 Scenario Data
##### Ronald Hochreiter <ronald@algorithmic.finance>
##### http://www.finance-r.com/
##### Last Update: August 5th, 2019
### 0. Setup
# Libraries
library(quantmod)
library(rvest)
### 1. Get the current components of the index from Wikipedia
# Parse the third table into a data.frame (last checked: August 5th, 2019)
table_id <- 3
url <- "https://en.wikipedia.org/wiki/S%26P_100"
sp100 <- url %>%
read_html() %>%
html_nodes("table") %>%
.[[table_id]] %>%
html_table()
# Extract parsed data into vectors and save this information
sp100_ticker <- sp100$Symbol
sp100_company <- sp100$Name
save(sp100_ticker, sp100_company, file="oex-info.rda")
### 2. Download data from Yahoo! Finance
# Remove problematic companies (note: this can be done better)
sp100_company <- sp100_company[-which(sp100_ticker == "BRK.B")]
sp100_ticker <- sp100_ticker[-which(sp100_ticker == "BRK.B")]
# Get data from Yahoo! Finance
quantmod::getSymbols(sp100_ticker)
# Create a list with all data sets
sp100_data_complete <- list()
for(current_ticker in sp100_ticker) {
sp100_data_complete[[length(sp100_data_complete) + 1]] <- get(current_ticker)
}
# Save this list (when reloading, do not forget to remove BRK.B from ticker list)
save(sp100_data_complete, file="oex-data.rda")
### 3. Create a data.frame with Adjusted Close values
# Remove companies where not enough data (starting e.g. after 2013) is available
nrows <- sapply(sp100_data_complete, function(x) nrow(x))
nrow.cutoff <- 1500
pos <- which(nrows < nrow.cutoff)
sp100_ticker[pos]
sp100_data_complete <- sp100_data_complete[-pos]
sp100_company <- sp100_company[-pos]
sp100_ticker <- sp100_ticker[-pos]
# Merge adjusted prices into one data.frame
current_pos <- 1
sp100_adjusted <- Ad(sp100_data_complete[[current_pos]])
for(current_pos in 2:length(sp100_ticker)) {
sp100_adjusted <- merge(sp100_adjusted, Ad(sp100_data_complete[[current_pos]]))
}
names(sp100_adjusted) <- sp100_ticker
### 4. Create a return scenario set for a certain time frame
# Select time frame and compute returns
timeframe <- "2013/2019"
sp100_returns <- dailyReturn(sp100_adjusted[,1])[timeframe]
for(current_pos in 2:length(sp100_ticker)) {
sp100_returns <- merge(sp100_returns, dailyReturn(sp100_adjusted[,current_pos])[timeframe])
}
names(sp100_returns) <- sp100_ticker
### 4b. Post-process the return scenario set (very simplistic approach)
# Check for NAs
sapply(sp100_returns, function(x) length(which(is.na(x) == TRUE)))
# Remove NAs
sp100_returns <- na.spline(sp100_returns)
### 5. Create and save the final scenario set
scenario.set <- sp100_returns
save(scenario.set, sp100_ticker, sp100_company, file="oex-returns-1908.rda")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.