0
library(quantmod)
library(xts)


getSymbols("SY1.DE", from = "2019-4-10", to = "2019-4-19", auto.assign = TRUE)
getSymbols("PEP", from = "2019-4-9", to = "2019-4-19", auto.assign = TRUE)


calcreturn <- function(data, amount = 24) {
  start <- as.numeric(data[,4][1])
  end <- as.numeric(data[,4][nrow(data)])
  difference <- end - start
  winning <- difference * amount
  return(winning)
}



allstocks <- list(SY1.DE, PEP)
amount <- list(24, 23)

lapply(allstocks, calcreturn)

Hello everbody!

This is my code to calculate my returns for my stocks. However, the amount of stocks i bought differ, so lapply does only work when the amount argument does not change. Is there a day to deal with changing arguments?

Thank you!

1 Answers1

0

You can modify your lapply to run over an index pairing one by one stock with amount:

lapply(1:length(allstocks), function(x) calcreturn(allstocks[[x]], amount[[x]]))
LocoGris
  • 4,432
  • 3
  • 15
  • 30