Suppose that you consider investing in two stocks: Apple and Facebook. As a prudent investor, you analyze the historical performance of the stocks during the period of 2013-01-01 to 2017-12-31.
-Facebook would have been the better preformer
library(tidyquant)
library(ggplot2)
# Import stock prices
stock_prices <- c("AAPL", "FB") %>%
tq_get(get = "stock.prices",
from = "2013-01-01",
to = "2017-12-31") %>%
group_by(symbol)
stock_prices
## # A tibble: 2,518 x 8
## # Groups: symbol [2]
## symbol date open high low close volume adjusted
## <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 AAPL 2013-01-02 79.1 79.3 77.4 78.4 140129500. 56.3
## 2 AAPL 2013-01-03 78.3 78.5 77.3 77.4 88241300. 55.6
## 3 AAPL 2013-01-04 76.7 76.9 75.1 75.3 148583400. 54.1
## 4 AAPL 2013-01-07 74.6 75.6 73.6 74.8 121039100. 53.7
## 5 AAPL 2013-01-08 75.6 76.0 74.5 75.0 114676800. 53.9
## 6 AAPL 2013-01-09 74.6 75.0 73.7 73.9 101901100. 53.0
## 7 AAPL 2013-01-10 75.5 75.5 73.6 74.8 150286500. 53.7
## 8 AAPL 2013-01-11 74.4 75.0 74.1 74.3 87626700. 53.4
## 9 AAPL 2013-01-14 71.8 72.5 71.2 71.7 183551900. 51.5
## 10 AAPL 2013-01-15 71.2 71.3 69.1 69.4 219193100. 49.8
## # ... with 2,508 more rows
stock_prices %>%
ggplot(aes(x = date, y = adjusted)) +
geom_line() +
facet_wrap(~symbol)
stock_prices %>%
group_by(symbol) %>%
mutate(close = close / close[1]) %>%
ungroup() %>%
ggplot(aes(x = date, y = close, col = symbol)) +
geom_line()
stock_prices %>%
group_by(symbol) %>%
mutate(close = close / close[1]) %>%
summarise(last = last(close))
## # A tibble: 2 x 2
## symbol last
## <chr> <dbl>
## 1 AAPL 2.16
## 2 FB 6.30
-I would expect Apple to be the most stable returns quarter after quarter, with the exceptions of the far right side of the distribution. ## Q4 Which of the two stocks would you expect the largest positive quarterly returns? -Facebook would have the bigger positive returns quarterly
stock_returns_quarterly <- stock_prices %>%
tq_transmute(select = adjusted,
mutate_fun = periodReturn,
period = "quarterly",
col_rename = "Ra")
stock_returns_quarterly
## # A tibble: 40 x 3
## # Groups: symbol [2]
## symbol date Ra
## <chr> <date> <dbl>
## 1 AAPL 2013-03-28 -0.160
## 2 AAPL 2013-06-28 -0.0610
## 3 AAPL 2013-09-30 0.260
## 4 AAPL 2013-12-31 0.227
## 5 AAPL 2014-03-31 -0.00170
## 6 AAPL 2014-06-30 0.261
## 7 AAPL 2014-09-30 0.0895
## 8 AAPL 2014-12-31 0.100
## 9 AAPL 2015-03-31 0.132
## 10 AAPL 2015-06-30 0.0122
## # ... with 30 more rows
# line chart
stock_returns_quarterly %>%
ggplot(aes(x = date, y = Ra)) +
geom_line() +
facet_wrap(~symbol)
# density plot
stock_returns_quarterly %>%
ggplot(aes(x = Ra, fill = symbol)) +
geom_density(alpha = 0.3)
-Apple would have the biggest returns adjusted to risk
# Retrieve performance metrics
stock_returns_quarterly %>%
tq_performance(Ra = Ra,
Rb = NULL, # Calculataing downside risk measures doesn't require Rb
performance_fun = SharpeRatio)
## # A tibble: 2 x 4
## # Groups: symbol [2]
## symbol `ESSharpe(Rf=0%,p=95%)` `StdDevSharpe(Rf=0%,… `VaRSharpe(Rf=0%,p…
## <chr> <dbl> <dbl> <dbl>
## 1 AAPL 0.365 0.494 0.442
## 2 FB 0.113 0.492 NA
library(tidyr)
# Select
stock_returns_quarterly %>%
tq_performance(Ra = Ra,
Rb = NULL, # Calculataing downside risk measures doesn't require Rb
performance_fun = SharpeRatio) %>%
gather(SharpRatio, measure, 2:4) %>%
ggplot(aes(x = symbol, y = measure, fill = SharpRatio)) +
geom_col(show.legend = FALSE) +
facet_wrap(~ SharpRatio) +
coord_flip() +
labs(title = "SharpRatio",
x = NULL,
y = NULL)
# Retrieve performance metrics
stock_returns_quarterly %>%
tq_performance(Ra = Ra,
Rb = NULL, # Calculataing downside risk measures doesn't require Rb
performance_fun = table.DownsideRisk) %>%
select(contains("histo"))
## # A tibble: 2 x 3
## # Groups: symbol [2]
## symbol `HistoricalES(95%)` `HistoricalVaR(95%)`
## <chr> <dbl> <dbl>
## 1 AAPL -0.160 -0.120
## 2 FB -0.103 -0.0873