library(tidyquant)
library(tidyverse)

Suppose that you consider investing in two stocks: S&P500 and NASDAQ. As a prudent investor, you analyze the historical performance of the stocks for the past 20 years.

Q1 Import stock prices of Dow Jones Industrial Avarege and NASDAQ Compsite Index for the last 20 years.

from = today() - years(20)
Stocks <- 
  tq_get(c("^GSPC", "^IXIC"), get = "stock.prices", from = from) %>%
  group_by(symbol)
Stocks
## # A tibble: 10,064 x 8
## # Groups:   symbol [2]
##    symbol date        open  high   low close     volume adjusted
##    <chr>  <date>     <dbl> <dbl> <dbl> <dbl>      <dbl>    <dbl>
##  1 ^GSPC  1999-11-01 1363. 1367. 1354. 1354.  861000000    1354.
##  2 ^GSPC  1999-11-02 1354. 1369. 1346. 1348.  904500000    1348.
##  3 ^GSPC  1999-11-03 1348. 1360. 1348. 1355.  914400000    1355.
##  4 ^GSPC  1999-11-04 1355. 1369. 1355. 1363.  981700000    1363.
##  5 ^GSPC  1999-11-05 1363. 1387. 1363. 1370. 1007300000    1370.
##  6 ^GSPC  1999-11-08 1370. 1381. 1366. 1377.  806800000    1377.
##  7 ^GSPC  1999-11-09 1377. 1384. 1361. 1365.  854300000    1365.
##  8 ^GSPC  1999-11-10 1365. 1379. 1360. 1373.  984700000    1373.
##  9 ^GSPC  1999-11-11 1373. 1382. 1372. 1381.  891300000    1381.
## 10 ^GSPC  1999-11-12 1381. 1396. 1369. 1396.  900200000    1396.
## # … with 10,054 more rows

Q2 Calculate monthly returns, and save the result under returns_monthly.

returns_monthly <- 
  Stocks %>%
    tq_transmute(select = adjusted, mutate_fun = periodReturn, period = "monthly")
returns_monthly
## # A tibble: 480 x 3
## # Groups:   symbol [2]
##    symbol date       monthly.returns
##    <chr>  <date>               <dbl>
##  1 ^GSPC  1999-11-30          0.0257
##  2 ^GSPC  1999-12-31          0.0578
##  3 ^GSPC  2000-01-31         -0.0509
##  4 ^GSPC  2000-02-29         -0.0201
##  5 ^GSPC  2000-03-31          0.0967
##  6 ^GSPC  2000-04-28         -0.0308
##  7 ^GSPC  2000-05-31         -0.0219
##  8 ^GSPC  2000-06-30          0.0239
##  9 ^GSPC  2000-07-31         -0.0163
## 10 ^GSPC  2000-08-31          0.0607
## # … with 470 more rows

Q3 Which of the two stocks have higher expected monthly return?

returns_monthly %>%
  ggplot(aes(x = monthly.returns, fill = symbol)) +
  geom_density(alpha = 0.3)

NASDAQ has higher monthly return, acoording to the average.

Q4 Which of the two stocks is riskier?

returns_monthly %>%
    tq_performance(Ra = monthly.returns,
                   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.0419
## 2 ^IXIC  0.0649

NASDAQ because it has lower monthly return.

Q5 Are the returns normally distributed?

returns_monthly %>%
    tq_performance(Ra = monthly.returns,
                   Rb = NULL, # Calculataing downside risk measures doesn't require Rb
                   performance_fun = skewness)
## # A tibble: 2 x 2
## # Groups:   symbol [2]
##   symbol skewness.1
##   <chr>       <dbl>
## 1 ^GSPC      -0.595
## 2 ^IXIC      -0.365

# Compute kurtosis
returns_monthly %>%
    tq_performance(Ra = monthly.returns,
                   Rb = NULL, # Calculataing downside risk measures doesn't require Rb
                   performance_fun = kurtosis)
## # A tibble: 2 x 2
## # Groups:   symbol [2]
##   symbol kurtosis.1
##   <chr>       <dbl>
## 1 ^GSPC        1.08
## 2 ^IXIC        1.55

Both Dow Jones and NASDAQ have a negative skewness and a positive kurtusis, indicating a large negative return is likelier than a large positive return.

Q6 Calculate the downside risk measures by revising the code below. Which of the two stocks has greater downside risks? Discuss HistoricalES(95%), HistoricalVaR(95%), and SemiDeviation.

returns_monthly %>%
    tq_performance(Ra = monthly.returns,
                   Rb = NULL, # Calculataing downside risk measures doesn't require Rb
                   performance_fun = table.DownsideRisk) %>%
  t()
##                            [,1]      [,2]     
## symbol                     "^GSPC"   "^IXIC"  
## DownsideDeviation(0%)      "0.0299"  "0.0450" 
## DownsideDeviation(MAR=10%) "0.034"   "0.049"  
## DownsideDeviation(Rf=0%)   "0.0299"  "0.0450" 
## GainDeviation              "0.0236"  "0.0396" 
## HistoricalES(95%)          "-0.0966" "-0.1491"
## HistoricalVaR(95%)         "-0.0751" "-0.1049"
## LossDeviation              "0.0318"  "0.0482" 
## MaximumDrawdown            "0.5256"  "0.7504" 
## ModifiedES(95%)            "-0.1012" "-0.1572"
## ModifiedVaR(95%)           "-0.0704" "-0.1046"
## SemiDeviation              "0.0319"  "0.0481"

NASDAQ us riskier downside values. ## Q7 Which of the two stocks would you choose?

returns_monthly %>%
    tq_performance(Ra = monthly.returns,
                   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%,p=… `VaRSharpe(Rf=0%,p=…
##   <chr>                  <dbl>                   <dbl>                <dbl>
## 1 ^GSPC                 0.0422                  0.102                0.0607
## 2 ^IXIC                 0.0409                  0.0991               0.0615

Dow Jones because it has a higher sharp ratio, higher sharp ratio means greater expected return per unit of risk.

Q8 Hide the messages and the code, but display results of the code from the webpage.

Hint: Use message, echo and results in the chunk options. Refer to the RMarkdown Reference Guide.

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

Q10 Use the correct slug.