library(tidyquant)
## Loading required package: lubridate
##
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
##
## date, intersect, setdiff, union
## Loading required package: PerformanceAnalytics
## Loading required package: xts
## Loading required package: zoo
##
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
##
## Attaching package: 'PerformanceAnalytics'
## The following object is masked from 'package:graphics':
##
## legend
## Loading required package: quantmod
## Loading required package: TTR
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
## Version 0.4-0 included new data defaults. See ?getSymbols.
## == Need to Learn tidyquant? =============================================
## Business Science offers a 1-hour course - Learning Lab #9: Performance Analysis & Portfolio Optimization with tidyquant!
## </> Learn more at: https://university.business-science.io/p/learning-labs-pro </>
library(tidyverse)
## -- Attaching packages -------------------------------- tidyverse 1.3.0 --
## v ggplot2 3.3.2 v purrr 0.3.4
## v tibble 3.0.3 v dplyr 1.0.2
## v tidyr 1.1.2 v stringr 1.4.0
## v readr 1.3.1 v forcats 0.5.0
## -- Conflicts ----------------------------------- tidyverse_conflicts() --
## x lubridate::as.difftime() masks base::as.difftime()
## x lubridate::date() masks base::date()
## x dplyr::filter() masks stats::filter()
## x dplyr::first() masks xts::first()
## x lubridate::intersect() masks base::intersect()
## x dplyr::lag() masks stats::lag()
## x dplyr::last() masks xts::last()
## x lubridate::setdiff() masks base::setdiff()
## x lubridate::union() masks base::union()
from <- today() - years(5)
stock_prices <- c("AMZN", "NFLX", "FB") %>%
tq_get(get = "stock.prices",
from = from)
stock_prices
## # A tibble: 3,777 x 8
## symbol date open high low close volume adjusted
## <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 AMZN 2015-11-18 647. 665. 646. 664. 4469800 664.
## 2 AMZN 2015-11-19 665. 673. 659 661. 4705200 661.
## 3 AMZN 2015-11-20 668. 669. 658. 668. 3896100 668.
## 4 AMZN 2015-11-23 672. 683. 670. 679. 4385100 679.
## 5 AMZN 2015-11-24 674. 676. 661. 671. 4543400 671.
## 6 AMZN 2015-11-25 675 680. 671. 675. 2697900 675.
## 7 AMZN 2015-11-27 681. 681. 672. 673. 1966800 673.
## 8 AMZN 2015-11-30 676. 681. 665. 665. 5693200 665.
## 9 AMZN 2015-12-01 674. 681 668. 679. 4751200 679.
## 10 AMZN 2015-12-02 681 685. 674. 676. 4273500 676.
## # ... with 3,767 more rows
stock_returns_monthly <- stock_prices %>%
group_by(symbol) %>%
tq_transmute(select = adjusted,
mutate_fun = periodReturn,
period = "monthly",
col_rename = "Ra")
stock_returns_monthly
## # A tibble: 183 x 3
## # Groups: symbol [3]
## symbol date Ra
## <chr> <date> <dbl>
## 1 AMZN 2015-11-30 0.00190
## 2 AMZN 2015-12-31 0.0167
## 3 AMZN 2016-01-29 -0.132
## 4 AMZN 2016-02-29 -0.0587
## 5 AMZN 2016-03-31 0.0744
## 6 AMZN 2016-04-29 0.111
## 7 AMZN 2016-05-31 0.0958
## 8 AMZN 2016-06-30 -0.00992
## 9 AMZN 2016-07-29 0.0604
## 10 AMZN 2016-08-31 0.0136
## # ... with 173 more rows
# scaling a single portfolio to num_port number
num_port = 100
stock_returns_monthly_multi <- stock_returns_monthly %>%
tq_repeat_df(n = num_port)
## Ungrouping data frame groups: symbol
stock_returns_monthly_multi
## # A tibble: 18,300 x 4
## # Groups: portfolio [100]
## portfolio symbol date Ra
## <int> <chr> <date> <dbl>
## 1 1 AMZN 2015-11-30 0.00190
## 2 1 AMZN 2015-12-31 0.0167
## 3 1 AMZN 2016-01-29 -0.132
## 4 1 AMZN 2016-02-29 -0.0587
## 5 1 AMZN 2016-03-31 0.0744
## 6 1 AMZN 2016-04-29 0.111
## 7 1 AMZN 2016-05-31 0.0958
## 8 1 AMZN 2016-06-30 -0.00992
## 9 1 AMZN 2016-07-29 0.0604
## 10 1 AMZN 2016-08-31 0.0136
## # ... with 18,290 more rows
# Retrieve performance metrics
stock_returns_monthly %>%
tq_performance(Ra = Ra,
Rb = NULL, # Calculataing downside risk measures doesn't require Rb
performance_fun = SharpeRatio, p=.99, Rf=.02)
## # A tibble: 3 x 4
## # Groups: symbol [3]
## symbol `ESSharpe(Rf=2%,p=99%~ `StdDevSharpe(Rf=2%,p=99~ `VaRSharpe(Rf=2%,p=99~
## <chr> <dbl> <dbl> <dbl>
## 1 AMZN 0.0395 0.110 0.0535
## 2 NFLX 0.0426 0.0771 0.0426
## 3 FB -0.0115 -0.0186 -0.0115
Hint: Use dplyr::arrange().
Hint: Make your argument using the calculated Sharpe Sharpe ratio
Hint: Calculate Beta from the Capital Asset Pricing Model. Make your argument based on the calculated Beta. Aggregate
Hint: Use message, echo and results in the chunk options. Refer to the RMarkdown Reference Guide.