---
title: "Stock Return and Candlestick Chart"
output:
flexdashboard::flex_dashboard:
orientation: columns
vertical_layout: fill
social: menu
source_code: embed
---
```{r setup, include=FALSE}
library(flexdashboard)
```
```{r message=FALSE}
library(PerformanceAnalytics)
library(quantmod)
library(dygraphs)
library(lubridate)
# Function to calculate monthly returns on a stock
daily_stock_returns <- function(ticker, start_year) {
# Download the data from Yahoo finance
symbol <- getSymbols(ticker, src = 'yahoo', auto.assign = FALSE, warnings = FALSE)
# Tranform it to monthly returns using the periodReturn function from quantmod
data <- periodReturn(symbol, period = 'daily', subset=paste(start_year, "::", sep = ""), type = 'log')
# Let's rename the column of returns to something intuitive because the column name is what
# will eventually be displayed on the time series graph.
colnames(data) <- as.character(ticker)
# We want to be able to work with the xts objects that result from this function
# so let's explicitly put them to the global environment with an easy to use
# name, the stock ticker.
assign(ticker, data, .GlobalEnv)
}
```
Column {.tabset data-width=550}
-----------------------------------------------------------------------
### Daily Return
```{r message = FALSE}
# Choose the starting year and assign it to the 'year' variable. How about 2016?
year <- ymd("2017-01-03")
# Use the function the monthly returns on 5 stocks, and pass in the 'year' value
# Let's choose Apple, Alibaba, Canopy Growth Corporation, iShares NASDAQ 100 Index ETF and Aurora Cannabis Inc.; after you run these functions, have
# a look at the global environment and make sure my five xts objects are there
daily_stock_returns('AAPL', year)
daily_stock_returns('BABA', year)
daily_stock_returns('WEED.TO', year)
daily_stock_returns('XQQ.TO', year)
daily_stock_returns('ACB.V', year)
# Merge the 5 daily return xts objects into 1 xts object.
merged_returns <- merge.xts(AAPL, BABA, WEED.TO, XQQ.TO, ACB.V)
# In order to correct percentage on the chart
merged_returns_percent <- merged_returns * 100
# Before we combine these into a portfolio, graph the individual returns and
# see if anything jumps out as unusual.
dygraph(merged_returns_percent, main = "Daily Return") %>%
dyAxis("y", label = "Return(%)") %>%
dyOptions(colors = RColorBrewer::brewer.pal(5, "Set1")) %>%
dyHighlight(highlightSeriesBackgroundAlpha = 0.2,
highlightSeriesOpts = list(strokeWidth = 2)) %>%
dyRangeSelector(height = 65)
```
Column {.tabset data-width=450}
-----------------------------------------------------------------------
### AAPL
```{r}
# invisible string out so that it will not show on dashboard.
invisible(getSymbols("AAPL", src = "yahoo", from = year))
AAPL_xts <- AAPL
# Plot candlestick by dyCandlestick funciton
dygraph(AAPL_xts[, -5], main = "Apple Inc.") %>%
dyCandlestick() %>%
dyAxis("y", label = "Price") %>%
dyOptions(colors = RColorBrewer::brewer.pal(5, "Set1")) %>%
dyHighlight(highlightCircleSize = 4,
highlightSeriesOpts = list(strokeWidth = 3),
highlightSeriesBackgroundAlpha = 1) %>%
dyRangeSelector(height = 65)
```
### BABA
```{r}
invisible(getSymbols("BABA", src = "yahoo", from = year))
BABA_xts <- BABA
dygraph(BABA_xts[, -5], main = "Alibaba Group Holding Limited") %>%
dyCandlestick() %>%
dyAxis("y", label = "Price") %>%
dyOptions(colors = RColorBrewer::brewer.pal(5, "Set1")) %>%
dyHighlight(highlightCircleSize = 4,
highlightSeriesOpts = list(strokeWidth = 3),
highlightSeriesBackgroundAlpha = 1) %>%
dyRangeSelector(height = 65)
```
### WEED.TO
```{r}
invisible(getSymbols("WEED.TO", src = "yahoo", from = year))
WEED_xts <- WEED.TO
dygraph(WEED_xts[, -5], main = "Canopy Growth Corporation") %>%
dyCandlestick() %>%
dyAxis("y", label = "Price") %>%
dyOptions(colors = RColorBrewer::brewer.pal(5, "Set1")) %>%
dyHighlight(highlightCircleSize = 4,
highlightSeriesOpts = list(strokeWidth = 3),
highlightSeriesBackgroundAlpha = 1) %>%
dyRangeSelector(height = 65)
```
### XQQ.TO
```{r}
invisible(getSymbols("XQQ.TO", src = "yahoo", from = year))
XQQ_xts <- XQQ.TO
dygraph(XQQ_xts[, -5], main = "iShares NASDAQ 100 Index ETF (CAD-Hedged)") %>%
dyCandlestick() %>%
dyAxis("y", label = "Price") %>%
dyOptions(colors = RColorBrewer::brewer.pal(5, "Set1")) %>%
dyHighlight(highlightCircleSize = 4,
highlightSeriesOpts = list(strokeWidth = 3),
highlightSeriesBackgroundAlpha = 1) %>%
dyRangeSelector(height = 65)
```
### ACB.V
```{r}
invisible(getSymbols("ACB.V", src = "yahoo", from = year))
ACB_xts <- ACB.V
dygraph(ACB_xts[, -5], main = "Aurora Cannabis Inc.") %>%
dyCandlestick() %>%
dyAxis("y", label = "Price") %>%
dyOptions(colors = RColorBrewer::brewer.pal(5, "Set1")) %>%
dyHighlight(highlightCircleSize = 4,
highlightSeriesOpts = list(strokeWidth = 3),
highlightSeriesBackgroundAlpha = 1) %>%
dyRangeSelector(height = 65)
```