library(tidyverse)
library(tidyquant)
Hint: Add group_by(symbol) at the end of the code so that calculations below will be done per stock.
from = today() - years(30)
Stocks <-
tq_get(c("^IXIC", "WMT", "MSFT"), get = "stock.prices", from = from) %>%
group_by(symbol)
Stocks
## # A tibble: 22,674 x 8
## # Groups: symbol [3]
## symbol date open high low close volume adjusted
## <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 ^IXIC 1990-05-07 429. 432. 429. 431. 122690000 431.
## 2 ^IXIC 1990-05-08 431. 432. 431. 432. 133840000 432.
## 3 ^IXIC 1990-05-09 431. 432. 430. 431. 146630000 431.
## 4 ^IXIC 1990-05-10 432. 434. 431. 433. 142490000 433.
## 5 ^IXIC 1990-05-11 434. 438. 434. 438. 167560000 438.
## 6 ^IXIC 1990-05-14 440. 443. 439. 442. 177170000 442.
## 7 ^IXIC 1990-05-15 441 443. 440 442. 162330000 442.
## 8 ^IXIC 1990-05-16 442. 443. 440. 443. 152490000 443.
## 9 ^IXIC 1990-05-17 445 446. 443. 446. 171410000 446.
## 10 ^IXIC 1990-05-18 446 448. 445 448. 191410000 448.
## # … with 22,664 more rows
Hint: Take the adjusted variable from Stocks, and calculate yearly returns using ***tq_transmute().
returns_yearly <-
Stocks %>%
tq_transmute(select = adjusted, mutate_fun = periodReturn, period = "yearly")
returns_yearly
## # A tibble: 93 x 3
## # Groups: symbol [3]
## symbol date yearly.returns
## <chr> <date> <dbl>
## 1 ^IXIC 1990-12-31 -0.133
## 2 ^IXIC 1991-12-31 0.569
## 3 ^IXIC 1992-12-31 0.155
## 4 ^IXIC 1993-12-31 0.147
## 5 ^IXIC 1994-12-30 -0.0320
## 6 ^IXIC 1995-12-29 0.399
## 7 ^IXIC 1996-12-31 0.227
## 8 ^IXIC 1997-12-31 0.216
## 9 ^IXIC 1998-12-31 0.396
## 10 ^IXIC 1999-12-31 0.856
## # … with 83 more rows
Hint: Take returns_yearly and pipe it to summarise. Calculate the mean yearly returns.
returns_yearly %>%
summarise(returns_avg = mean(yearly.returns))
## # A tibble: 3 x 2
## symbol returns_avg
## <chr> <dbl>
## 1 ^IXIC 0.137
## 2 MSFT 0.273
## 3 WMT 0.156
returns_yearly
## # A tibble: 93 x 3
## # Groups: symbol [3]
## symbol date yearly.returns
## <chr> <date> <dbl>
## 1 ^IXIC 1990-12-31 -0.133
## 2 ^IXIC 1991-12-31 0.569
## 3 ^IXIC 1992-12-31 0.155
## 4 ^IXIC 1993-12-31 0.147
## 5 ^IXIC 1994-12-30 -0.0320
## 6 ^IXIC 1995-12-29 0.399
## 7 ^IXIC 1996-12-31 0.227
## 8 ^IXIC 1997-12-31 0.216
## 9 ^IXIC 1998-12-31 0.396
## 10 ^IXIC 1999-12-31 0.856
## # … with 83 more rows
Hint: Take returns_yearly and pipe it to tidyquant::tq_performance. Use the performance_fun argument to compute sd (standard deviation).
returns_yearly %>%
tq_performance(Ra = yearly.returns,
Rb = NULL,
performance_fun = sd)
## # A tibble: 3 x 2
## # Groups: symbol [3]
## symbol sd.1
## <chr> <dbl>
## 1 ^IXIC 0.278
## 2 WMT 0.327
## 3 MSFT 0.402
Hint: when the return distribution is not normal, the standard deviation is not an appropriate measure of risk. One can use skewness and kurtosis to detect non-normal returns. Take returns_yearly and pipe it to tidyquant::tq_performance. Use the performance_fun argument to compute skewness. Do the same for kurtosis.
yearly_skewness <- returns_yearly %>%
tq_performance(Ra = yearly.returns,
Rb = NULL,
performance_fun = skewness)
yearly_kurtosis <- returns_yearly %>%
tq_performance(Ra = yearly.returns,
Rb = NULL,
performance_fun = kurtosis)
Hint: Take returns_yearly and pipe it to tidyquant::tq_performance. Use the performance_fun argument to compute table.DownsideRisk.
Downside_risk <- returns_yearly %>%
tq_performance(Ra = yearly.returns,
Rb = NULL,
performance_fun = table.DownsideRisk) %>%
t()
Hint: Make your argument based on the three Sharpe Ratios.
returns_yearly %>%
tq_performance(Ra = yearly.returns,
Rb = NULL,
performance_fun = SharpeRatio, Rf = .02) %>%
t()
## [,1] [,2] [,3]
## symbol "^IXIC" "WMT" "MSFT"
## ESSharpe(Rf=2%,p=95%) "0.2867333" "0.2501061" "0.5185779"
## StdDevSharpe(Rf=2%,p=95%) "0.4206751" "0.4160441" "0.6288367"
## VaRSharpe(Rf=2%,p=95%) "0.3928930" "0.6271157" "0.7412996"
Hint: Make your argument based on the three Sharpe Ratios.
returns_yearly %>%
tq_performance(Ra = yearly.returns,
Rb = NULL,
performance_fun = SharpeRatio, p= .01, Rf = .02) %>%
t()
## [,1] [,2] [,3]
## symbol "^IXIC" "WMT" "MSFT"
## ESSharpe(Rf=2%,p=1%) "0.2040602" "0.1361574" "0.3671338"
## StdDevSharpe(Rf=2%,p=1%) "0.4206751" "0.4160441" "0.6288367"
## VaRSharpe(Rf=2%,p=1%) "0.2430265" "1.2119986" "0.4274725"
Hint: Use message, echo and results in the chunk options. Refer to the RMarkdown Reference Guide.