Search code examples
rpercentagextsquantmod

Function to calculate daily % change from yesterday close and apply it to multiple tickers


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.


Solution

  • Creating the function like below does the trick

    percentage_change <- function(x) {
      stock_name <- stringi::stri_extract(names(x)[1], regex = "^[A-Z]+")
      stock_name <- paste0(stock_name, ".%change")
      column_names <- c(names(x), stock_name)
      x$change <- (quantmod::Cl(x) - quantmod::Lag(quantmod::Cl(x)))/quantmod::Lag(quantmod::Cl(x)) * 100
      x <- setNames(x, column_names)
      return(x)
    }
    
    all_stocks <- lapply(all_stocks, percentage_change)
    
    head(all_stocks$MSFT)
               MSFT.Open MSFT.High MSFT.Low MSFT.Close MSFT.Volume MSFT.Adjusted MSFT.%change
    2007-01-03     29.91     30.25    29.40      29.86    76935100      22.67236           NA
    2007-01-04     29.70     29.97    29.44      29.81    45774500      22.63439   -0.1674548
    2007-01-05     29.63     29.75    29.45      29.64    44607200      22.50531   -0.5702784
    2007-01-08     29.65     30.10    29.53      29.93    50220200      22.72550    0.9784110
    2007-01-09     30.00     30.18    29.73      29.96    44636600      22.74828    0.1002305
    2007-01-10     29.80     29.89    29.43      29.66    55017400      22.52049   -1.0013318
    

    You could also use the following, but then the header is daily.returns for every stock in your list.

    all_stocks  <- lapply(all_stocks , function(x) merge(x, dailyReturn(x, leading = FALSE) * 100))
    
    head(all_stocks$MSFT)
               MSFT.Open MSFT.High MSFT.Low MSFT.Close MSFT.Volume MSFT.Adjusted daily.returns
    2007-01-03     29.91     30.25    29.40      29.86    76935100      22.67236            NA
    2007-01-04     29.70     29.97    29.44      29.81    45774500      22.63439    -0.1674548
    2007-01-05     29.63     29.75    29.45      29.64    44607200      22.50531    -0.5702784
    2007-01-08     29.65     30.10    29.53      29.93    50220200      22.72550     0.9784110
    2007-01-09     30.00     30.18    29.73      29.96    44636600      22.74828     0.1002305
    2007-01-10     29.80     29.89    29.43      29.66    55017400      22.52049    -1.0013318