Skip to content
All library documents

Calculating Monthly Stock Returns Over a Date Range with quantmod

Article Quant Q&A · Author: Pau Gimeno

Summary

The document shows how to retrieve monthly stock returns for a specified historical window with quantmod. The questioner's attempt applies date arguments to the monthly-return calculation and receives a broader history than intended. The accepted answer instead requests the daily price history within the desired start and end dates, converts each ticker's data to monthly bars, and computes returns from the closing prices.

The example combines the resulting return series into a multi-stock table and checks that their monthly indexes align before adding each series. The displayed output illustrates the monthly returns, including a zero for the first month in the sample because there is no prior month in the retrieved window for comparison. The answer notes that the calculation uses closing prices and leaves open whether open-price returns are also wanted. Its sample is specific to the shown data and software workflow; it does not discuss adjusted prices, missing observations, or how to define returns for partial months.

Key ideas

  • Restrict the downloaded price history to the required date range before calculating monthly returns.
  • Convert each ticker's prices to monthly data and compute returns from the selected price field.
  • Align monthly timestamps before combining multiple assets into one table.
  • The example uses closing prices and does not establish whether open-price returns are desired.

Tags

Full text
# How can I fix both the open and close data using quantmod's periodReturns?


# How can I fix both the open and close data using quantmod's periodReturns?












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?

## Answer by AK88 (score 0, accepted)

https://quant.stackexchange.com/a/34516

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.

Shown in full with attribution under the source's licence. Licence: CC BY-SA 4.0 (Stack Exchange)

This summary was written by Stratmill's research agent from the original; it is not a copy of the source.