Search code examples
rlistquantmod

Create a list to calculate consecutive days in a function for multiple tickers


The following code works fine for single stock (AAPL)

 library("quantmod")
 library("reshape")
 library("gplots")

 #Control Parameters
 dataStartDate = as.Date("2020-04-01")
 symbol<-  "AAPL"

#Function to turn a boolean vector into a vector containing the consecutive num of trues or    falses seen
#Will be used to calculate the consecutive number of up and down days
 
consecutiveTruesExtractor <- function(data){
genNumOfConsecutiveTrues <- function(x, y) { (x+y)*y  } #Y is either 0 or 1
upDaysCount <- Reduce(genNumOfConsecutiveTrues,data,accumulate=TRUE)
upDaysCount <- as.vector(Lag(upDaysCount))
upDaysCount[is.na(upDaysCount)] <- 0

downDaysCount <- Reduce(genNumOfConsecutiveTrues,!data,accumulate=TRUE)
downDaysCount <- as.vector(Lag(downDaysCount))
downDaysCount[is.na(downDaysCount)] <- 0
consecutiveTruesExtractor <- upDaysCount-downDaysCount
}


#Download the data
symbolData <- new.env() #Make a new environment for quantmod to store data in
getSymbols(symbol, env = symbolData, src = "yahoo", from = dataStartDate)
mktdata <- eval(parse(text=paste("symbolData$",sub("^","",symbol,fixed=TRUE))))
opClRet <- (Cl(mktdata)/Op(mktdata))-1
consecutiveDir <- consecutiveTruesExtractor(as.matrix(opClRet>0))
completeData<- cbind(opClRet,consecutiveDir)
colnames(completeData) <- c("OpClRet","ConsecutiveDir")

mktdata$Consecutive <- completeData$ConsecutiveDir

View(mktdata)

I'd like to have a list of tickers instead of a single ticker.Tried to create a list but getting some errors.

Symbols <- c("AMD","A","AAL","ABBV","ABT","ACAD","ADBE","AAPL")

My question is: how to make it work for a list of stocks? for example: ("AMD","A","AAL","ABBV","ABT","ACAD","ADBE", "AAPL")


Solution

  • Create a function and Map over Symbols with it.

    library(quantmod)
    
    f <- function(symbol, dataStartDate = as.Date("2020-04-01")) {
    
      consecutiveTruesExtractor <- function(data){
        genNumOfConsecutiveTrues <- function(x, y) { (x+y)*y  } #Y is either 0 or 1
        upDaysCount <- Reduce( genNumOfConsecutiveTrues, data, accumulate=TRUE)
        upDaysCount <- as.vector(Lag(upDaysCount))
        upDaysCount[is.na(upDaysCount)] <- 0
    
        downDaysCount <- Reduce( genNumOfConsecutiveTrues, !data, accumulate=TRUE)
        downDaysCount <- as.vector(Lag(downDaysCount))
        downDaysCount[is.na(downDaysCount)] <- 0
        consecutiveTruesExtractor <- upDaysCount-downDaysCount
      }
      
      symbolData <- new.env() #Make a new environment for quantmod to store data in
      getSymbols(symbol, env = symbolData, src = "yahoo", from = dataStartDate)
      mktdata <- eval( parse(text = paste("symbolData$", sub("^", "", symbol, fixed=TRUE))))
      opClRet <- (Cl(mktdata)/Op(mktdata))-1
      consecutiveDir <- consecutiveTruesExtractor(as.matrix(opClRet>0))
      completeData<- cbind(opClRet,consecutiveDir)
      colnames(completeData) <- c("OpClRet","ConsecutiveDir")
    
      mktdata$Consecutive <- completeData$ConsecutiveDir
    
      mktdata
    }
    
    Symbols <- c("AMD","A","AAL","ABBV","ABT","ACAD","ADBE","AAPL")
    L <- Map(f, Symbols)