```
pacman::p_load(tidyverse,tidyquant,PortfolioAnalytics,quantmod,PerformanceAnalytics,zoo,
plotly,timekit,ggthemes,timetk)
```

Portfolio optimization is the process of selecting proportions of various assets to include in a portfolio, in such a way as to make the portfolio better than any other according to some criterion. The criterion will combine, directly or indirectly, considerations of the expected value of the portfolioâ€™s rate of return as well as of the returnâ€™s dispersion and possibly other measures of financial risk. The objective of porrtfolio optimization is to maximize a measure of gain per unit measure of risk and minimize a measure of risk.

Modern portfolio theory was introduced by Harry Markowitz in 1952. It assumes that in general, an investor wants to maximize a portfolioâ€™s expected return contingent on any given amount of risk, with risk measured by the standard deviation of the portfolioâ€™s rate of return. For portfolios that meet this criterion, known as efficient portfolios, achieving a higher expected return requires taking on more risk, so investors are faced with a trade-off between risk and expected return. This risk-expected return relationship of efficient portfolios is graphically represented by a curve known as the efficient frontier. All efficient portfolios, each represented by a point on the efficient frontier, are well-diversified. For the specific formulas for efficient portfolios,[3] see Portfolio separation in mean-variance analysis. While ignoring higher moments can lead to significant over-investment in risky securities, especially when volatility is high[4], the development of portfolios for non-Gaussian economies is mathematically challenging.

Some of the best performing stocks year to date(November,2017) include Dynavax Tech(DVAX). Most of the best performing stocks were all from the Biotech space. Dynavax Tech (NASDAQ:DVAX) ranks first with a YTD gain of 444%. Straight Path (NYSEMKT:STRP) ranks second at +432.79%. Sangamo Therapeutics (NASDAQ:SGMO), Calithera Biosciences (NASDAQ:CALA), and Pieris Pharma (NASDAQ:PIRS) round out the top five. Eight of the top nine are Biotech stocks, and then Weight Watchers (NYSE:WTW) ranks tenth with a gain of 280%.

```
# Get data of the stock
Symbols=c("DVAX","STRP","SGMO","CALA","PIRS","KITE")
#Adjusted_price <- merge.zoo(SWX[,6], SPY[,6], ICUI[,6], MSFT[,6])
stock_prices <- tq_get(Symbols, get = "stock.prices", from = " 2016-01-01")
stock_prices=stock_prices%>%select(symbol,date,adjusted)%>%rename(Stock=symbol,Price=adjusted)
stock_prices%>%head()
```

```
#remove duplicated rows
stock_prices=stock_prices[!duplicated(stock_prices), ]
#Alternatively
#stock_prices[unique(stock_prices), ]
stock_prices2=stock_prices%>%spread(Stock,Price)
stock_prices2%>%head()
```

Use quantmod periodReturn to Convert daily Prices to daily Returns.

```
# Get stock pairs
stock_prices2 <- Symbols %>%
tq_get(get = "stock.prices",
from = "2016-01-01",
to = today()) %>%
group_by(symbol)
stock_prices2%>%head()
```

```
stock_returns <- stock_prices2 %>%
tq_transmute(select = adjusted,
mutate_fun = periodReturn,
period = "daily",
type = "arithmetic",
col_rename = "returns") %>%
spread(key = symbol, value = returns)
stock_returns%>%head()
```

```
stock_prices2 %>%
tq_transmute(select = adjusted,
mutate_fun = periodReturn,
period = "monthly",
type = "arithmetic",
col_rename = "returns") %>%
ggplot(aes(x = date, y = returns, fill = symbol)) +
geom_bar(stat = "identity", width = 30) +
geom_hline(yintercept = 0, color = palette_light()[[1]]) +
scale_y_continuous(labels = scales::percent) +
labs(title = " Monthly Returns",
subtitle = "",
y = "Monthly Returns", x = "") +
facet_wrap(~ symbol, ncol = 2,scales = "free") +
theme_tq() +
scale_fill_tq()
```

```
stock_prices_daily <- stock_prices2 %>%
group_by(symbol)
stock_prices_daily %>%
ggplot(aes(x = date, y = adjusted, color = symbol)) +
geom_line(size = 1) +
labs(title = "Daily Stock Prices",
x = "", y = "Adjusted Prices", color = "") +
facet_wrap(~ symbol, ncol = 2, scales = "free") +
scale_y_continuous(labels = scales::dollar) +
theme_tq() +
scale_color_tq()
```

```
stock_prices2 %>%
tq_transmute(select = adjusted,
mutate_fun = periodReturn,
period = "monthly",
type = "arithmetic",
col_rename = "returns")%>%
ggplot(aes(x = date, y = returns, fill = symbol)) +
geom_line(aes(color=symbol)) +
geom_hline(yintercept = 0, color = palette_light()[[1]]) +
scale_y_continuous(labels = scales::percent) +
labs(title = " Monthly Returns",
subtitle = "",
y = "Monthly Returns", x = "") +
facet_wrap(~ symbol, ncol = 2,scales = "free") +
theme_tq() +
scale_fill_tq()
```