Hint: What is the assignment? What class is this assignment from? Who is the instructor? If it’s not a class assignment, describe your research topic.
The topic we chose is from Professor Bradbury’s Financial Management course. The assignment is exploring a company’s stock performance and comparing stock performance considering both returns and risk.
#1 Calculate the stock prices
# Load packages
library(tidyquant)
library(tidyverse)
from <- today() - years(5)
stock_prices <- c("^GSPC", "TGT") %>%
tq_get(get = "stock.prices",
from = from)
stock_prices
## # A tibble: 2,516 x 8
## symbol date open high low close volume adjusted
## <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 ^GSPC 2014-12-05 2073. 2079. 2071. 2075. 3419620000 2075.
## 2 ^GSPC 2014-12-08 2075. 2076. 2054. 2060. 3800990000 2060.
## 3 ^GSPC 2014-12-09 2057. 2061. 2034. 2060. 3970150000 2060.
## 4 ^GSPC 2014-12-10 2059. 2059. 2024. 2026. 4114440000 2026.
## 5 ^GSPC 2014-12-11 2028. 2056. 2028. 2035. 3917950000 2035.
## 6 ^GSPC 2014-12-12 2030. 2032. 2002. 2002. 4157650000 2002.
## 7 ^GSPC 2014-12-15 2005. 2019. 1982. 1990. 4361990000 1990.
## 8 ^GSPC 2014-12-16 1987. 2017. 1973. 1973. 4958680000 1973.
## 9 ^GSPC 2014-12-17 1974. 2017. 1974. 2013. 4942370000 2013.
## 10 ^GSPC 2014-12-18 2019. 2061. 2019. 2061. 4703380000 2061.
## # … with 2,506 more rows
#2 Calculate yearly returns
stock_returns_yearly <- stock_prices %>%
group_by(symbol) %>%
tq_transmute(select = adjusted,
mutate_fun = periodReturn,
period = "yearly",
col_rename = "Ra")
stock_returns_yearly
## # A tibble: 12 x 3
## # Groups: symbol [2]
## symbol date Ra
## <chr> <date> <dbl>
## 1 ^GSPC 2014-12-31 -0.00794
## 2 ^GSPC 2015-12-31 -0.00727
## 3 ^GSPC 2016-12-30 0.0954
## 4 ^GSPC 2017-12-29 0.194
## 5 ^GSPC 2018-12-31 -0.0624
## 6 ^GSPC 2019-12-04 0.242
## 7 TGT 2014-12-31 0.0305
## 8 TGT 2015-12-31 -0.0160
## 9 TGT 2016-12-30 0.0274
## 10 TGT 2017-12-29 -0.0584
## 11 TGT 2018-12-31 0.0467
## 12 TGT 2019-12-04 0.944
# Baseline Period Returns
baseline_returns_yearly <- "^GSPC" %>%
tq_get(get = "stock.prices",
from = today() - years(5)) %>%
tq_transmute(select = adjusted,
mutate_fun = periodReturn,
period = "yearly",
col_rename = "Rb")
baseline_returns_yearly
## # A tibble: 6 x 2
## date Rb
## <date> <dbl>
## 1 2014-12-31 -0.00794
## 2 2015-12-31 -0.00727
## 3 2016-12-30 0.0954
## 4 2017-12-29 0.194
## 5 2018-12-31 -0.0624
## 6 2019-12-04 0.242
stock_prices %>%
group_by(symbol)%>%
mutate(close_index = close / close[1])%>%
ungroup() %>%
ggplot(aes(date, close_index, col = symbol)) +
geom_line()
# Compute standard deviation
stock_returns_yearly %>%
tq_performance(Ra = Ra,
Rb = NULL, # Calculataing downside risk measures doesn't require Rb
performance_fun = sd)
## # A tibble: 2 x 2
## # Groups: symbol [2]
## symbol sd.1
## <chr> <dbl>
## 1 ^GSPC 0.122
## 2 TGT 0.385
# See options for the `performance_fun` argument
#tq_performance_fun_options()
# Retrieve performance metrics
stock_returns_yearly %>%
tq_performance(Ra = Ra,
Rb = NULL, # Calculataing downside risk measures doesn't require Rb
performance_fun = SharpeRatio, p=.99, Rf=.02)
## # A tibble: 2 x 4
## # Groups: symbol [2]
## symbol `ESSharpe(Rf=2%,p=99… `StdDevSharpe(Rf=2%,p=… `VaRSharpe(Rf=2%,p=…
## <chr> <dbl> <dbl> <dbl>
## 1 ^GSPC 0.107 0.454 0.473
## 2 TGT 0.142 0.370 NA