With the help of community I've created the code to download multiple tickers from a .csv file list and create a daily range function to all of them. Now I would like to create a similar function to calculate the daily percentage change (%) of each ticker and apply to all of them.
Here is my approach:
Load my tickers list from a .csv file, create a list with all of them.
library(quantmod)
Tickers <- read.csv("nasdaq_tickers_list.csv", stringsAsFactors = FALSE)
getSymbols(Tickers$Tickers,from="2018-01-01", src="yahoo" )
stock_data = sapply(.GlobalEnv, is.xts)
all_stocks <- do.call(list, mget(names(stock_data)[stock_data]))
Percentage function?
Percentage <- function(x) {
stock_name <- stringi::stri_extract(names(x)[1], regex = "^[A-Z]+")
stock_name <- paste0(stock_name, ".percentage")
column_names <- c(names(x), stock_name)
x$percentage <- quantmod::Close(today) - quantmod::Close(yesterday)/100
x <- setNames(x, column_names)
return(x)
}
calculate percentages and add them to the data
all_stocks <- lapply(all_stocks, percentage_change)
Any help on how to create the %function?
Thanks a lot.