Getting Symbols: Downloading Data for Multiple Symbols and Calculating Returns
In this article, we will explore the process of downloading stock data using GetSymbols from the Quantmod package in R. We’ll cover how to download data for multiple symbols, calculate daily returns, and combine the data into a dataframe.
Introduction
GetSymbols is a function provided by the Quantmod package that allows us to download stock data for various tickers. The function takes several arguments such as the ticker symbol, date range, and environment where the data should be loaded into. In this article, we’ll delve into how to use GetSymbols to download data for multiple symbols and calculate daily returns.
Using For Loop or Apply Function
The original poster is trying to download data for a large set of stock symbols using a for loop or apply function. However, there’s an alternative way to achieve this using the eapply function from the base R package.
library(quantmod)
Symbols <- c("XOM", "MSFT", "JNJ", "GE", "CVX", "WFC", "PG", "JPM",
"VZ", "PFE", "T", "IBM", "MRK", "BAC", "DIS", "ORCL",
"PM", "INTC", "SLB")
# create environment to load data into
Data <- new.env()
getSymbols(c("^GSPC", Symbols), from="2007-01-01", env=Data)
# calculate returns, merge, and create data.frame (eapply loops over all
# objects in an environment, applies a function, and returns a list)
Returns <- eapply(Data, function(s) ROC(Ad(s), type="discrete"))
ReturnsDF <- as.data.frame(do.call(merge, Returns))
Using eapply Function
The eapply function is a powerful tool that allows us to apply a function to all objects in an environment. In this case, we’re using it to calculate daily returns for each symbol.
# calculate returns, merge, and create data.frame (eapply loops over all
# objects in an environment, applies a function, and returns a list)
Returns <- eapply(Data, function(s) {
ROC(Ad(s), type="discrete")
})
Using lapply Function
Another alternative is to use the lapply function instead of eapply. The main difference between the two functions is that lapply only returns a list, while eapply returns a list with named elements.
# calculate returns, merge, and create data.frame (lapply loops over all
# objects in an environment, applies a function, and returns a list)
Returns <- lapply(Data, function(s) {
ROC(Ad(s), type="discrete")
})
Using Map Function
We can also use the map function from the purrr package to calculate daily returns for each symbol.
library(purrr)
# calculate returns, merge, and create data.frame (map loops over all
# objects in an environment, applies a function, and returns a list)
Returns <- mapl(ROC, Ad, Data)[^"GSPC"]
Downloading Data for Multiple Symbols
To download data for multiple symbols, we can use the getSymbols function with the c() function to pass a vector of ticker symbols.
# create environment to load data into
Data <- new.env()
getSymbols(c("^GSPC", "XOM", "MSFT"), from="2007-01-01", env=Data)
Calculating Daily Returns
To calculate daily returns, we can use the ROC() function provided by the quantmod package. This function takes two arguments: the adjusted data and the type of return.
# calculate returns
ROC(Ad(s), type="discrete")
Combining Data
To combine the data for multiple symbols into a single dataframe, we can use the do.call() function to merge the dataframes together.
ReturnsDF <- as.data.frame(do.call(merge, Returns))
Adjusting Column Names
After merging the dataframes, we need to adjust the column names by removing the “Adjusted” suffix.
# adjust column names are re-order columns
colnames(ReturnsDF) <- gsub(".Adjusted", "", colnames(ReturnsDF))
ReturnsDF <- ReturnsDF[, c("GSPC", Symbols)]
Conclusion
In this article, we’ve explored how to download stock data for multiple symbols using GetSymbols from the Quantmod package. We’ve also covered how to calculate daily returns and combine the data into a single dataframe. By utilizing the eapply, lapply, or map functions, we can simplify the process of downloading and processing large amounts of data.
Example Use Cases
- Downloading stock data for multiple symbols:
library(quantmod)
Symbols <- c("XOM", "MSFT", "JNJ")
getSymbols(c("^GSPC", Symbols), from="2007-01-01")
- Calculating daily returns for a single symbol:
library(quantmod)
s <- Symbol("XOM")
ROC(Ad(s), type="discrete")
- Combining data for multiple symbols into a single dataframe:
library(purrr)
Returns <- mapl(ROC, Ad, getSymbols(c("^GSPC", "XOM"), from="2007-01-01"))
ReturnsDF <- as.data.frame(do.call(merge, Returns))
- Adjusting column names for multiple symbols:
# adjust column names are re-order columns
colnames(ReturnsDF) <- gsub(".Adjusted", "", colnames(ReturnsDF))
ReturnsDF <- ReturnsDF[, c("GSPC", Symbols)]
Last modified on 2023-10-13