0
$\begingroup$

I am retrieving data from yahoo using the getSymbols to get then monthly returns. But I want to subset the data, I want to have monthly returns from January 2016 until May 2017. I am running this:

library(quantmod)
list <- c("AAL","AAPL","ADBE","AMZN", "FB", "GOOGL", "MSFT", "NFLX", "TSLA", "VOD")

df <- data.frame()

##get monthly returns from january 2016 until today

for (stock in list){

 data <- monthlyReturn(to.monthly(getSymbols(stock, auto.assign = FALSE)),from='2016-01-01', to='2017-05-31' )

      colnames(data)[1] <- paste('r_', stock, sep = "")

      df <- cbind(df, data)

}

df <- as.data.frame(df)
attach(df)

And it is not working as I get values from the entire dataset. Instead if I use this:

for (stock in list){

 data <- monthlyReturn(to.monthly(getSymbols(stock, auto.assign = FALSE)),subset = "2016::")

 colnames(data)[1] <- paste('r_', stock, sep = "")

  df <- cbind(df, data)

}

df <- as.data.frame(df)

I get monthly data from January 2016 until today, but that's not what I want as I want to fix the close date too. Any ideas on how to solve that?

$\endgroup$
0
$\begingroup$

Is this what you would like to get:

lst <- c("AAL","AAPL","ADBE","AMZN", "FB", "GOOGL", "MSFT", "NFLX", "TSLA", "VOD")

getSymbols(lst, from = "2016-01-01", to = "2017-05-31")
P <- NULL
seltickers <- NULL

for(stock in lst) {     
  tmp = monthlyReturn(Cl(to.monthly(eval(parse(text = stock))))) ## Ad from quantmod
  if(is.null(P)){ timeP = time(tmp) }
  if(any(time(tmp)!=timeP)) next
  else P = cbind(P, as.numeric(tmp))
  seltickers = c(seltickers, stock)
  P = xts(P, order.by = timeP)
  colnames(P) = seltickers
}

head(P)

                   AAL         AAPL        ADBE         AMZN          FB
Jan 2016  0.0000000000  0.000000000  0.00000000  0.000000000  0.00000000
Feb 2016  0.0515516260 -0.006677563 -0.04465382 -0.058739319 -0.04714376
Mar 2016  0.0002438537  0.127210629  0.10158545  0.074422634  0.06715301
Apr 2016 -0.1541087371 -0.139921096  0.00447759  0.111094283  0.03049960
May 2016 -0.0801383419  0.065286997  0.05572065  0.095817020  0.01046093
Jun 2016 -0.1128173300 -0.042659753 -0.03699608 -0.009919871 -0.03812810
               GOOGL        MSFT        NFLX         TSLA           AAL
Jan 2016  0.00000000  0.00000000  0.00000000  0.000000000  0.0000000000
Feb 2016 -0.05796284 -0.07642038  0.01709504  0.003817971  0.0515516260
Mar 2016  0.06369044  0.08549526  0.09442242  0.197155277  0.0002438537
Apr 2016 -0.07211957 -0.09704872 -0.11933878  0.047830399 -0.1541087371
May 2016  0.05787700  0.06276321  0.13928692 -0.072811096 -0.0801383419
Jun 2016 -0.06051939 -0.03452834 -0.10812125 -0.049052534 -0.1128173300
                 AAPL        ADBE         AMZN          FB       GOOGL
Jan 2016  0.000000000  0.00000000  0.000000000  0.00000000  0.00000000
Feb 2016 -0.006677563 -0.04465382 -0.058739319 -0.04714376 -0.05796284
Mar 2016  0.127210629  0.10158545  0.074422634  0.06715301  0.06369044
Apr 2016 -0.139921096  0.00447759  0.111094283  0.03049960 -0.07211957
May 2016  0.065286997  0.05572065  0.095817020  0.01046093  0.05787700
Jun 2016 -0.042659753 -0.03699608 -0.009919871 -0.03812810 -0.06051939
                MSFT        NFLX         TSLA           AAL         AAPL
Jan 2016  0.00000000  0.00000000  0.000000000  0.0000000000  0.000000000
Feb 2016 -0.07642038  0.01709504  0.003817971  0.0515516260 -0.006677563
Mar 2016  0.08549526  0.09442242  0.197155277  0.0002438537  0.127210629
Apr 2016 -0.09704872 -0.11933878  0.047830399 -0.1541087371 -0.139921096
May 2016  0.06276321  0.13928692 -0.072811096 -0.0801383419  0.065286997
Jun 2016 -0.03452834 -0.10812125 -0.049052534 -0.1128173300 -0.042659753
                ADBE         AMZN          FB       GOOGL        MSFT
Jan 2016  0.00000000  0.000000000  0.00000000  0.00000000  0.00000000
Feb 2016 -0.04465382 -0.058739319 -0.04714376 -0.05796284 -0.07642038
Mar 2016  0.10158545  0.074422634  0.06715301  0.06369044  0.08549526
Apr 2016  0.00447759  0.111094283  0.03049960 -0.07211957 -0.09704872
May 2016  0.05572065  0.095817020  0.01046093  0.05787700  0.06276321
Jun 2016 -0.03699608 -0.009919871 -0.03812810 -0.06051939 -0.03452834
                NFLX         TSLA
Jan 2016  0.00000000  0.000000000
Feb 2016  0.01709504  0.003817971
Mar 2016  0.09442242  0.197155277
Apr 2016 -0.11933878  0.047830399
May 2016  0.13928692 -0.072811096
Jun 2016 -0.10812125 -0.049052534

These are monthly returns on Close price. I am not sure if you want returns on Open price as well.

$\endgroup$
  • 1
    $\begingroup$ Thanks, that's what I was looking for! Instead, I used getSymbols(lst, from = "2015-12-31" ...) since I want to have returns for Jan. 2016 too $\endgroup$ – Pau Gimeno Jun 2 '17 at 19:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.