Column

Chart A: Prices of Three Stocks

Chart B: Return of Three Stocks

Column

Table A: Sharpe Ratio for Three Stocks

  AAPL_ret   MSFT_ret   TWTR_ret 
-0.1236674 -0.2128341 -0.1237455 

Chart C: Rolling Sharpe-Ratio for Three Stocks

Column

Chart D: Price Series Description of TWTR

Chart E: Return Series Description of TWTR

---
title: "ANLY 512-Lab 1: Financial Data"
author: "Shan Huang, Langsheng Lin, Hao Zhang"
Date: "July 3 2017"
output: 
  flexdashboard::flex_dashboard:
    orientation: columns
    vertical_layout: fill
    source_code: embed
---

```{r message=FALSE}
library(flexdashboard)
library(quantmod)
library(ggplot2)
library(tidyr)
library(plyr)
```

```{r, message=FALSE, include=FALSE}
# Data Preparation
getSymbols(c('AAPL', 'MSFT', 'TWTR'), from = '2014-06-01', to = '2017-05-31')
getSymbols('USD1MTD156N', src = 'FRED')
```


```{r, message = FALSE, warning= FALSE, ecol = FALSE}

AAPL.ret <- periodReturn(AAPL, period = 'daily')
MSFT.ret <- periodReturn(MSFT, period = 'daily')
TWTR.ret <- periodReturn(TWTR, period = 'daily')
ret <- merge(AAPL.ret, MSFT.ret, TWTR.ret)
colnames(ret) <- c('AAPL_ret', 'MSFT_ret', 'TWTR_ret')

rf <- mean(USD1MTD156N['2014-06/2017-05'], na.rm = T) / 100
# Average 1-M LIBOR as Risk-Free Rate for simplification
```

Sidebar {.sidebar}
-----------------------------------------------------------------------

### Financial Data Lab

1. From Chart A, we find that :
  + APPL's stock price steadily rised in the past 3 years. 
  + The most volatile stock is MSFT. 
  + TWTR has the lowest price, but it has a heathly growing trend.
  
2. From Chart B, we find that :
  + The return of AAPL and MSFT are getting closer to each other recently.
  + As a relative new company, TWTR's returns is more fluctuate, and getting lower in recent period, which might be a good buying signal at this point.

3. From Table A and Chart C:
  + Our strategy is to buy the stock with highest Sharpe Ratio as well as most days of positive rolling Sharpe Ratio
  + We found that Sharpe Ratio for TWTR and AAPL is close, but TWTR has more days of positive rolling Sharpe Ratio
  
4. Hence our decision is to choose TWTR. 
  + In Chart D and Chart E, we showed out-of-time (2017.06) price and return for TWTR.
  
Column {.tabset data-width=500}
-----------------------------------------------------------------------

### Chart A: Prices of Three Stocks

```{r, message=FALSE, warning=FALSE}
stocks <- as.xts(data.frame(AAPL = AAPL[, "AAPL.Adjusted"], MSFT = MSFT[, "MSFT.Adjusted"], 
                            TWTR = TWTR[, "TWTR.Adjusted"]))
plot(as.zoo(stocks[, c("AAPL.Adjusted", "MSFT.Adjusted")]), screens = 1, lty = 1:2, 
     xlab = "Date", ylab = "Price")
par(new = TRUE)
plot(as.zoo(stocks[, "TWTR.Adjusted"]), screens = 1, lty = 3, xaxt = "n", yaxt = "n", 
     xlab = "", ylab = "")
axis(4)
mtext("Price", side = 4, line = 3)
legend("topleft", c("AAPL (left)", "MSFT (left)", "TWTR"), lty = 1:3, cex = 0.5)
```

### Chart B: Return of Three Stocks

```{r, message=FALSE, warning=FALSE}
stock_return = apply(stocks, 1, function(x) {x / stocks[1,]}) %>% 
  t %>% as.xts

plot(as.zoo(stock_return), screens = 1, lty = 1:3, xlab = "Date", ylab = "Return")
legend("bottomleft", c("AAPL", "MSFT", "TWTR"), lty = 1:3, cex = 0.5)
```

Column {.tabset data-width=500}
-----------------------------------------------------------------------

### Table A: Sharpe Ratio for Three Stocks

```{r, echo=FALSE}
(apply(ret, 2, mean, na.rm = T) - rf) / apply(ret, 2, sd, na.rm = T)
```

### Chart C: Rolling Sharpe-Ratio for Three Stocks

```{r, message=FALSE, warning=FALSE}
mean_roll_30 <- rollapply(ret, 30, FUN = mean)
mean_roll_30 <- mean_roll_30[complete.cases(mean_roll_30), ]

sd_roll_30 <- rollapply(ret, 30, FUN = sd)
sd_roll_30 <- sd_roll_30[complete.cases(sd_roll_30), ]

sharp.ratio <- (mean_roll_30 - rf) / sd_roll_30
colnames(sharp.ratio) <- c('AAPL_SR', 'MSFT_SR', 'TWTR_SR')
sharp.ratio.df <- data.frame(date=index(sharp.ratio), coredata(sharp.ratio))
sharp.ratio.melt <- gather(sharp.ratio.df, key = 'company', value = 'Sharp_Ratio', -date)

ggplot(data = sharp.ratio.melt, aes(x = date, y = Sharp_Ratio)) + 
  geom_line(alpha = 0.3) + 
  geom_hline(yintercept = 0) +
  facet_grid(company ~.) + 
  ggtitle('30-Day Rolling Sharp Ratio for AAPL, MSFT, and TWTR \n (2014.07-2017.05)') + 
  xlab('Time') + 
  ylab('Shart Ratio')

count.sharp <- apply(sharp.ratio > 0, 2, sum)
# From the result we found that TWTR has the most days with positive sharpe ratio
```

Column {.tabset data-width=500}
-----------------------------------------------------------------------

### Chart D: Price Series Description of TWTR
```{r, message=FALSE, include=FALSE}
getSymbols("TWTR", from="2017-06-01",to="2017-06-30",auto.assign = TRUE)
```

```{r, message=FALSE, warning=FALSE}
plot(TWTR$TWTR.Adjust, xlab="Time",ylab = "Adjusted close", main = "Daily closing price of Twitter in June 2017")
```

### Chart E: Return Series Description of TWTR
```{r, message=FALSE, warning=FALSE}
TWTR_return<-dailyReturn(TWTR$TWTR.Adjusted)
plot(TWTR_return, xlab="Time",ylab = "Return",main = "TWTR Daily Return in June 2017")
```