rtidyquant

How to calculate investment values of multiple stocks using tidyquant?


I want to get the investment values for each stock, but I am getting, I think, for the overall dataset.

library(tidyquant)
library(dplyr)
data(FANG)
monthly_returns_stocks <- FANG %>%
  group_by(symbol) %>%
  tq_transmute(adjusted, periodReturn, period = "monthly")

weights <- c(0.50, 0.25, 0.25, 0)

monthly_returns_stocks %>%
  tq_portfolio(assets_col   = symbol,
               returns_col  = monthly.returns, 
               weights = weights,
               col_rename   = "investment.growth",
               wealth.index = TRUE) %>%
mutate(investment.growth = investment.growth*100)

What am I doing wrong?

I want to get the investment values over time by each symbol assuming, say, $100 investment at time 0. So the desired output would look something like...

desired_output <-
  tibble::tribble(
    ~symbol,       ~date, ~investment.growth,
       "FB", "1/31/2013",               100L,
       "FB", "2/28/2013",               103L,
       "FB", "3/28/2013",               106L,
       "FB", "4/30/2013",               101L,
       "FB", "5/31/2013",                99L,
     "AMZN", "1/31/2013",               100L,
     "AMZN", "2/28/2013",               105L,
     "AMZN", "3/28/2013",               109L,
     "AMZN", "4/30/2013",               123L,
     "AMZN", "5/31/2013",               112L,
     "GOOG", "1/31/2013",               100L,
     "GOOG", "2/28/2013",                98L,
     "GOOG", "3/28/2013",                96L,
     "GOOG", "4/30/2013",               102L,
     "GOOG", "5/31/2013",               106L)

Solution

  • Looking at your expected output, the following lines of code get you there. STarting point is your monthly_returns_stocks data.frame. Basically a cumulative sum of the montly returns + 1 times the investment of $100. Change the 100 into 1000 if you want to see the growth of a $1000 investment for each stock.

    monthly_returns_stocks %>% 
      mutate(investment.growth = 100 * (1 + cumsum(monthly.returns)))
    
    # A tibble: 192 x 4
    # Groups:   symbol [4]
       symbol date       monthly.returns investment.growth
       <chr>  <date>               <dbl>             <dbl>
     1 FB     2013-01-31        0.106                111. 
     2 FB     2013-02-28       -0.120                 98.6
     3 FB     2013-03-28       -0.0613                92.5
     4 FB     2013-04-30        0.0856               101. 
     5 FB     2013-05-31       -0.123                 88.7
     6 FB     2013-06-28        0.0218                90.9
     7 FB     2013-07-31        0.479                139. 
     8 FB     2013-08-30        0.122                151. 
     9 FB     2013-09-30        0.217                173. 
    10 FB     2013-10-31       -0.000398             173. 
    # ... with 182 more rows
    

    And as an extra: the weighted portfolio return in dollars:

    monthly_returns_stocks %>% 
      mutate(investment.growth = 100 * (1 + cumsum(monthly.returns))) %>% 
      # get the last records per symbol
      filter(row_number() == max(row_number())) %>%  
      ungroup() %>% 
      # weigths in order of the stock order: FB, AMZN, NFLX, GOOG
      summarise(portfolio_return = sum(investment.growth * weights)) 
    
    # A tibble: 1 x 1
      portfolio_return
                 <dbl>
    1            2111.