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.

# Import stock prices
from = today() - years(20)
Stocks <- 
  tq_get(c("^DJI", "^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 ^DJI   1999-11-01 10731. 10746. 10649. 10649. 150060000   10649.
##  2 ^DJI   1999-11-02 10654. 10752. 10573. 10582. 143460000   10582.
##  3 ^DJI   1999-11-03 10583. 10650. 10563. 10609. 153890000   10609.
##  4 ^DJI   1999-11-04 10612. 10712. 10603. 10640. 195700000   10640.
##  5 ^DJI   1999-11-05 10640. 10843. 10638. 10704. 171660000   10704.
##  6 ^DJI   1999-11-08 10668. 10776. 10650. 10719. 213120000   10719.
##  7 ^DJI   1999-11-09 10715. 10766. 10585. 10617. 168950000   10617.
##  8 ^DJI   1999-11-10 10612. 10655. 10536. 10598. 148240000   10598.
##  9 ^DJI   1999-11-11 10603. 10644. 10544. 10595. 130060000   10595.
## 10 ^DJI   1999-11-12 10594. 10772. 10542. 10769. 158320000   10769.
## # … with 10,054 more rows

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

# Calculate returns. 
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 ^DJI   1999-11-30         0.0215 
##  2 ^DJI   1999-12-31         0.0569 
##  3 ^DJI   2000-01-31        -0.0484 
##  4 ^DJI   2000-02-29        -0.0742 
##  5 ^DJI   2000-03-31         0.0784 
##  6 ^DJI   2000-04-28        -0.0172 
##  7 ^DJI   2000-05-31        -0.0197 
##  8 ^DJI   2000-06-30        -0.00707
##  9 ^DJI   2000-07-31         0.00709
## 10 ^DJI   2000-08-31         0.0659 
## # … with 470 more rows

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

^IXIC has a higher monthly returne with .64% where ^DJI has a lower monthly return of .47%.

returns_monthly %>% 
  summarise(returns_avg = mean(monthly.returns))
## # A tibble: 2 x 2
##   symbol returns_avg
##   <chr>        <dbl>
## 1 ^DJI       0.00474
## 2 ^IXIC      0.00643

Q4 Which of the two stocks is riskier?

^IXIC is the riskier of the two stocks. They have a standard deviation of 6.5% where ^DJI has a standard deviation of 4.1%. ^IXIC has a larger standard deviation than ^DJI.

# Compute standard deviation
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 ^DJI   0.0406
## 2 ^IXIC  0.0649

# See options for the `performance_fun` argument
#tq_performance_fun_options()

Q5 Are the returns normally distributed?

Both S&P500 and NASDAQ have a negative skewness, long tail to the left, and a positive kurtusis, meaning thick tails, indicating a large negative return is likelier than a large positive return.

# Compute skewness
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 ^DJI       -0.515
## 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 ^DJI        0.912
## 2 ^IXIC       1.55

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.

# Retrieve performance metrics
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                     "^DJI"    "^IXIC"  
## DownsideDeviation(0%)      "0.0282"  "0.0450" 
## DownsideDeviation(MAR=10%) "0.0323"  "0.0490" 
## DownsideDeviation(Rf=0%)   "0.0282"  "0.0450" 
## GainDeviation              "0.0237"  "0.0396" 
## HistoricalES(95%)          "-0.0937" "-0.1491"
## HistoricalVaR(95%)         "-0.0625" "-0.1049"
## LossDeviation              "0.0301"  "0.0482" 
## MaximumDrawdown            "0.4930"  "0.7504" 
## ModifiedES(95%)            "-0.0950" "-0.1572"
## ModifiedVaR(95%)           "-0.0668" "-0.1046"
## SemiDeviation              "0.0305"  "0.0481"

Q7 Which of the two stocks would you choose?

Hint: Calculate Sharep Ratio and discuss your answer based on calculated Sharp Ratios.

^DJI would be the stock we choose becuase their shart ratio is greater than IXIC.DJI’s sharpe ratio is 4.99% where ^IXIC has a sharpe ratio is .409%. The greater the sharpe ratio is the one we would choose because it has a greater extpected return per unit of risk.

# Retrieve performance metrics
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 ^DJI                  0.0499                  0.117                0.0710
## 2 ^IXIC                 0.0409                  0.0991               0.0615

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.