# Load packages

# Core
library(tidyverse)
library(tidyquant)

Goal

Visualize expected returns and risk to make it easier to compare the performance of multiple assets and portfolios.

Choose your stocks.

from 2012-12-31 to 2017-12-31

1 Import stock prices

# Choose stocks

symbols <- c("AMZN", "AAPL", "TSLA", "SPY", "EFA")

# Using tq_get() ----
prices <- tq_get(x = symbols,
                 get = "stock.prices",
                 from = "2012-12-31",
                 to = "2017-12-31")

2 Convert prices to returns (quarterly)

asset_returns_tbl <- prices %>%

    # Calculate monthly returns
    group_by(symbol) %>%
    tq_transmute(select = adjusted,
                 mutate_fun = periodReturn,
                 period = "quarterly",
                 type = "log") %>%
    slice(-1) %>%
    ungroup() %>%

    # remane
    set_names(c("asset", "date", "returns"))

# period_returns = c("yearly", "quarterly", "monthly", "weekly")

3 Assign a weight to each asset (change the weigting scheme)

symbols <- asset_returns_tbl %>% distinct(asset) %>% pull()

w <- c(0.25,
       0.25,
       0.20,
       0.20,
       0.10)

w_tbl <- tibble(symbols, w)

4 Build a portfolio

portfolio_returns_rebalanced_monthly_tbl <- asset_returns_tbl %>%
    
    tq_portfolio(assets_col   = asset,
                 returns_col  = returns,
                 weights      = w_tbl,
                 col_rename   = "returns",
                 rebalance_on = "quarters")

portfolio_returns_rebalanced_monthly_tbl
## # A tibble: 20 × 2
##    date        returns
##    <date>        <dbl>
##  1 2013-03-28  0.00902
##  2 2013-06-28  0.0926 
##  3 2013-09-30  0.168  
##  4 2013-12-31  0.110  
##  5 2014-03-31 -0.0158 
##  6 2014-06-30  0.0732 
##  7 2014-09-30  0.0101 
##  8 2014-12-31  0.00664
##  9 2015-03-31  0.0723 
## 10 2015-06-30  0.0783 
## 11 2015-09-30 -0.0312 
## 12 2015-12-31  0.0756 
## 13 2016-03-31 -0.0295 
## 14 2016-06-30  0.0117 
## 15 2016-09-30  0.0975 
## 16 2016-12-30 -0.0106 
## 17 2017-03-31  0.150  
## 18 2017-06-30  0.0682 
## 19 2017-09-29  0.0288 
## 20 2017-12-29  0.0846
# write_rds(portfolio_returns_rebalanced_monthly_tbl,
#           "00_data/Ch03_portfolio_returns_rebalanced_monthly_tbl.rds")

5 Calculate Standard Deviation

portfolio_sd_tidyquant_builtin_percent <- portfolio_returns_rebalanced_monthly_tbl %>%

tq_performance(Ra = returns,
    Rb = NULL, 
        performance_fun = table.Stats) %>%

select(Stdev) %>%
mutate(tq_sd = round(Stdev, 4))

portfolio_sd_tidyquant_builtin_percent
## # A tibble: 1 × 2
##    Stdev  tq_sd
##    <dbl>  <dbl>
## 1 0.0578 0.0578
# mean of portfolio returns
portfolio_mean_tidyquant_builtin_percent <- mean(portfolio_returns_rebalanced_monthly_tbl$returns)

portfolio_mean_tidyquant_builtin_percent
## [1] 0.05245307

6 Plot

# Expected Returns versus Risk asset_returns_tbl %>%
asset_returns_sd_mean_tbl <- asset_returns_tbl %>%

group_by(asset) %>%
tq_performance(Ra = returns,
    performance_fun = table.Stats) %>%
select(Mean = ArithmeticMean, Stdev) %>%
ungroup() %>%

# Add portfolio sd
add_row(tibble(asset = "Portfolio",
    Mean = mean(portfolio_mean_tidyquant_builtin_percent ),
    Stdev = portfolio_sd_tidyquant_builtin_percent$tq_sd))

asset_returns_sd_mean_tbl
## # A tibble: 6 × 3
##   asset       Mean  Stdev
##   <chr>      <dbl>  <dbl>
## 1 AAPL      0.045  0.119 
## 2 AMZN      0.077  0.129 
## 3 EFA       0.0179 0.0512
## 4 SPY       0.0364 0.0365
## 5 TSLA      0.111  0.300 
## 6 Portfolio 0.0525 0.0578
asset_returns_sd_mean_tbl %>%

ggplot(aes(x = Stdev, y = Mean, color = asset)) +
geom_point() +
ggrepel::geom_text_repel(aes(label = asset))