library(tidyquant)
library(tidyverse)

# Import data
from = today() - years(4)
Stocks <- tq_get("TSLA", get = "stock.prices", from = from)
Stocks
## # A tibble: 1,006 x 7
##    date        open  high   low close  volume adjusted
##    <date>     <dbl> <dbl> <dbl> <dbl>   <dbl>    <dbl>
##  1 2015-03-09  194.  194.  188.  191. 6736700     191.
##  2 2015-03-10  188.  194.  188.  190. 5530900     190.
##  3 2015-03-11  191.  196.  191.  194. 4974900     194.
##  4 2015-03-12  194.  194.  190.  191. 4149300     191.
##  5 2015-03-13  189.  192.  187.  189. 5434300     189.
##  6 2015-03-16  192   196.  190.  196. 5628800     196.
##  7 2015-03-17  195.  199.  194.  195. 4883200     195.
##  8 2015-03-18  195.  201.  193.  201. 4756300     201.
##  9 2015-03-19  202   205.  195.  196. 8475200     196.
## 10 2015-03-20  197.  199.  196.  198. 4269500     198.
## # ... with 996 more rows
# Calculate daily returns. Do not save the result.
Stocks %>%
    tq_mutate(select = adjusted, mutate_fun = periodReturn, period = "daily") 
## # A tibble: 1,006 x 8
##    date        open  high   low close  volume adjusted daily.returns
##    <date>     <dbl> <dbl> <dbl> <dbl>   <dbl>    <dbl>         <dbl>
##  1 2015-03-09  194.  194.  188.  191. 6736700     191.       0      
##  2 2015-03-10  188.  194.  188.  190. 5530900     190.      -0.00293
##  3 2015-03-11  191.  196.  191.  194. 4974900     194.       0.0180 
##  4 2015-03-12  194.  194.  190.  191. 4149300     191.      -0.0138 
##  5 2015-03-13  189.  192.  187.  189. 5434300     189.      -0.0125 
##  6 2015-03-16  192   196.  190.  196. 5628800     196.       0.0372 
##  7 2015-03-17  195.  199.  194.  195. 4883200     195.      -0.00496
##  8 2015-03-18  195.  201.  193.  201. 4756300     201.       0.0307 
##  9 2015-03-19  202   205.  195.  196. 8475200     196.      -0.0252 
## 10 2015-03-20  197.  199.  196.  198. 4269500     198.       0.0124 
## # ... with 996 more rows

You are evaluating the performance of two stocks, Microsoft and Apple, for future investment.

Q1 Import stock prices of Microsoft and Apple for the last 20 years.

Hint: Add group_by(symbol) at the end of the code so that calculations below will be done per stock.

library(tidyquant)
library(tidyverse)

# Import data
from = today() - years(20)
Stocks <- tq_get(c("MICR","AAPL"), get = "stock.prices", from = from) %>%
    group_by(symbol)
Stocks %>% View()

Q2 Describe the first observation (first row) using all variables.

The first row is the Microsoft stock on the date of March 8, 1993. The opening price was 1.2500, the high price was 1.3125, the low price was 1.2500, and the close price was 1.3125. The volume of 1200 shares were traded and the adjusted price to divindend payments is 1.207519.

Q3 Create a line chart for adjusted closing prices of both stocks.

Hint: Use ggplot2::facet_wrap. Refer to the ggplot2 cheatsheet. See the section for Faceting.

Stocks%>%
  ggplot(aes(x=date,y=adjusted)) + geom_line()+facet_wrap(~ symbol)

Q4 Calculate yearly returns, and save the result under returns_yearly.

Hint: Take the adjusted variable from Stocks, and calculate yearly returns using tq_transmute(), instead of tq_mutate(), which is used when periodicity changes. Another difference between the two is that tq_transmute() returns only newly-created columns while tq_mutate() adds new columns to existing variables.


returns_yearly <-
  Stocks %>%
   tq_transmute(select = adjusted, mutate_fun = periodReturn, period = "yearly")


returns_yearly
## # A tibble: 42 x 3
## # Groups:   symbol [2]
##    symbol date       yearly.returns
##    <chr>  <date>              <dbl>
##  1 MICR   1999-12-31         0.238 
##  2 MICR   2000-12-29         0     
##  3 MICR   2001-12-31         0.538 
##  4 MICR   2002-12-31         0.0200
##  5 MICR   2003-12-31        11.4   
##  6 MICR   2004-12-31        -0.325 
##  7 MICR   2005-12-30        -0.579 
##  8 MICR   2006-12-29         1.78  
##  9 MICR   2007-12-31        -0.717 
## 10 MICR   2008-12-31        -0.709 
## # ... with 32 more rows

Q5 Create a boxplot for returns of both stocks.

Hint: Refer to the ggplot2 cheatsheet. Look for geom_boxplot under Two Variables. Note that the discrete variable should be mapped to the x-axis and the continuous variable to the y-axis. ]

returns_yearly%>%
  ggplot(aes(x=date,y=yearly.returns,fill=symbol))+geom_boxplot(alpha=.5)

## Q6 For Which of the two stocks is the typical yearly return expected to be higher? Hint: Discuss your answer in terms of the median in the boxplot you created in Q5. Google “Interpreting boxplots in R” to find the information you need.

Apple has a higher typical expected return than Microsoft. The median of Apple stock is higher than the median of the Microsoft stock which means its median average return is higer.

Q7 Which of the two stocks is expected to be riskier?

Hint: Discuss your answer in terms of the interquartile range (the middle 50%) of the boxplot you created in Q5. Apple is the more risky stock. Apple’s interquartile range is smaller than Microsoft. Also, the higher the expected the return, the higher the risk a stock is.

Q8. Hide both the code and the results of the code on the webpage.

Hint: Change echo and results in the chunk options. The published webpage should display charts.

# Set the global option
knitr::opts_chunk$set(message = F, warning = F, collapse = T, echo = F, results = 'hide')

Q9. Display the title and your name correctly at the top of the webpage.

Q10. Use the correct slug.