# Load packages
# Core
library(tidyverse)
library(tidyquant)
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
symbols <- c("XOM", "QQQ", "SPY", "TSLA","CGC")
prices <- tq_get(x = symbols,
get = "stock.prices",
from = "2012-12-31",
to = "2017-12-31")
asset_returns_tbl <- prices %>%
group_by(symbol) %>%
tq_transmute(select = adjusted,
mutate_fun = periodReturn,
period = "quarterly",
type = "log" ) %>%
slice(-1) %>%
ungroup() %>%
set_names(c("asset", "date", "returns"))
# symbols
symbols <- asset_returns_tbl %>% distinct(asset) %>% pull()
symbols
## [1] "CGC" "QQQ" "SPY" "TSLA" "XOM"
# weights
weights <- c(0.25,0.25,0.2,0.2,0.1)
weights
## [1] 0.25 0.25 0.20 0.20 0.10
w_tbl <- tibble(symbols, weights)
w_tbl
## # A tibble: 5 × 2
## symbols weights
## <chr> <dbl>
## 1 CGC 0.25
## 2 QQQ 0.25
## 3 SPY 0.2
## 4 TSLA 0.2
## 5 XOM 0.1
# ?tq_portfolio
portfolio_returns_tbl <- asset_returns_tbl %>%
tq_portfolio(assets_col = asset,
returns_col = returns,
weights = w_tbl,
rebalance_on = "quarters" )
portfolio_returns_tbl
## # A tibble: 20 × 2
## date portfolio.returns
## <date> <dbl>
## 1 2013-03-28 0.0620
## 2 2013-06-28 0.224
## 3 2013-09-30 0.150
## 4 2013-12-31 0.0147
## 5 2014-03-31 0.0666
## 6 2014-06-30 0.0599
## 7 2014-09-30 -0.0626
## 8 2014-12-31 -0.0338
## 9 2015-03-31 -0.0487
## 10 2015-06-30 0.0595
## 11 2015-09-30 -0.114
## 12 2015-12-31 0.181
## 13 2016-03-31 -0.0218
## 14 2016-06-30 0.0114
## 15 2016-09-30 0.112
## 16 2016-12-30 0.221
## 17 2017-03-31 0.124
## 18 2017-06-30 0.00157
## 19 2017-09-29 0.0985
## 20 2017-12-29 0.268
portfolio_sd_tidyquant_builtin_percent <- portfolio_returns_tbl %>%
tq_performance(Ra = portfolio.returns,
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.104 0.104
# Mean of portfolio returns
portfolio_mean_tidyquant_builtin_percent <- mean(portfolio_returns_tbl$portfolio.returns)
portfolio_mean_tidyquant_builtin_percent
## [1] 0.06867569
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 = portfolio_mean_tidyquant_builtin_percent,
Stdev = portfolio_sd_tidyquant_builtin_percent$tq_sd))
sd_mean_tbl
## # A tibble: 6 × 3
## asset Mean Stdev
## <chr> <dbl> <dbl>
## 1 CGC 0.154 0.412
## 2 QQQ 0.0465 0.0469
## 3 SPY 0.0364 0.0365
## 4 TSLA 0.111 0.300
## 5 XOM 0.0063 0.0703
## 6 Portfolio 0.0687 0.104
sd_mean_tbl %>%
ggplot(aes(x = Stdev, y = Mean, color = asset)) +
geom_point() +
ggrepel::geom_text_repel(aes(label = asset))
How should you expect your portfolio to perform relative to its assets in the portfolio? Would you invest all your money in any of the individual stocks instead of the portfolio? Discuss both in terms of expected return and risk.
I would never invest in all of my money in any one individual stock unless I was the CEO of that company and had total control over it. It’s a very bold and one could call it “interesting” to be polite. If you have your portfolio for the long term you to be diverse and have some invested in little bit in all sectors, it can favor one sector over another if you prefer, but never keep your eggs in basket unless you control that basket. However if you are treating the stock market as the roulette table and don’t care if you loose it all well hell invest in the most volatile stock you can find typically in the tech or health care sectors those usually are the most volatile. Overall SPY and QQQ are very solid but they are index funds. So at the Time GCG was quite the stock it was on a trajectory of almost 500% perfect growth then in October of 2018 and since it has lost nearly 99% of its value. Tesla was a very solid stock at the time with a a lot of hype behind it and great growth. My portfolio would not have out performed TSLA or CGC during this time. During this time I would invest in my current portfolio has a not