Stock Price Performance


Relative Rebase: Stock Price vs. Nasdaq


Technical Analysis: Candlestick Chart with Bollinger Bands


Technical Analysis: Moving Average Convergence Divergence (MACD)


---
title: "Stock Recommendation Dashboard"
output: 
  flexdashboard::flex_dashboard:
    storyboard: true
    orientation: rows
    vertical_layout: fill
    source_code: embed
    social: menu
---


```{r setup, include=FALSE}
library(flexdashboard)
library(quantmod)
library(dygraphs)
library(TTR)
options(getSymbols.warning4.0 = FALSE,
        getSymbols.yahoo.warning = FALSE)
```



```{r Technical_Analysis, include = FALSE}

invisible(getSymbols("AAPL", from = "2015-01-01", auto.assign=TRUE))
AAPL <- as.data.frame(AAPL)


# Simple Moving Averages (SMA)
# 20-day SMA
sma20 <- SMA(AAPL$AAPL.Close, n=20)
# 50-day SMA
sma50 <- SMA(AAPL$AAPL.Close, n=50)
# 200-day SMA
sma200 <- SMA(AAPL$AAPL.Close, n=200)

# Exponential Moving Average (EMA)
# 14-day EMA
ema14 <- EMA(AAPL$AAPL.Close, n=14)

# Bollinger Bands 
bb20 <- BBands(AAPL$AAPL.Close, sd=2.0, n=14, maType=EMA)
# Overall data frame
AAPLplusBB <- data.frame(AAPL,bb20)

# Relative Strength Indicator
rsi14 <- RSI(AAPLplusBB$AAPL.Close, n=14)

#MACD
macd <- MACD(AAPLplusBB$AAPL.Close, nFast = 12, nSlow = 26,
             nSig = 9, maType = SMA)

# allData
allData <- data.frame(AAPL,sma20,sma50,sma200,ema14,bb20,rsi14,macd)

```



###Stock Price Performance

```{r Stock_Price}
invisible(getSymbols("AAPL", from = "2015-01-01", auto.assign=TRUE))
difference <- AAPL[, "AAPL.Open"] - AAPL[, "AAPL.Close"]
decreasing <- which(difference < 0)
increasing <- which(difference > 0)

dyData <- AAPL[, "AAPL.Close"]

ribbonData <- rep(0, nrow(dyData))
ribbonData[decreasing] <- 0.5
ribbonData[increasing] <- 1

dygraph(dyData, main = "Stock Price Movement") %>%
  dyRibbon(data = ribbonData, top = 0.1, bottom = 0.02) %>%
  dyRangeSelector(height = 50)
```

***

- The trend of closing price is always regarded as one of the most straightforward indications of a stock's demand and performance, so the time series price plot is supposed to be a key component in a dashboard for investment guidance.

- The graph shows the stock price performance for Apple over the last 3 years.

- Up and down closing prices over previous day is shown through green and red colored ribbon beneath the stock price performance chart.

- Date range selector is enabled.



###Relative Rebase: Stock Price vs. Nasdaq 

```{r Relative_Rebase}
tickers <- c("AAPL", "NDAQ")
invisible(getSymbols(tickers))
closePrices <- do.call(merge, lapply(tickers, function(x) Cl(get(x))))
dateWindow <- c("2015-01-01", "2017-10-31")

dygraph(closePrices, main = "Prcie-to-Index Performance", group = "stock") %>%
  dyRebase(value = 100) %>%
  dyRangeSelector(dateWindow = dateWindow, height = 50) %>%
  dyAxis("y", label = "Price Indexed over a period")

```

***

 - The stock price to index performance shows how the stock price has been performing relative to its peer companies and the composite index in general over a span of time.

 - The graph displays the current valuation and return on invetment of the stock vs. the NASDAQ index if $100 were invested in each of them on the same day for any period of time chosen through the date range selector.


###Technical Analysis: Candlestick Chart with Bollinger Bands

```{r Bollinger_Bands}

m <- cbind(allData[,1:4], allData[,11], allData[,12], allData[,13])
colnames(m)[5]  <- "dn"
colnames(m)[6]  <- "mavg"
colnames(m)[7]  <- "up"

dygraph(m, main = "Bollinger Bands" ) %>%
  dyCandlestick() %>%
  dyRangeSelector(height = 50)

```

***

 - A Bollinger Band, developed by famous technical trader John Bollinger, is plotted two standard deviations away from a simple moving average. 
 
 - The price of the stock is bracketed by an upper and lower band along with a 21-day simple moving average.

 - The graph shows candlestick chart with Bollinger Bands with an additional date range selector.
 
 - The **up**, **mavg** and **down** symbols show the Bollinger Bands.


###Technical Analysis: Moving Average Convergence Divergence (MACD)

```{r MACD}

m <- cbind(allData[,1:4], allData[,16], allData[,17])
colnames(m)[5] <- "MACD"
colnames(m)[6] <- "MACD Signal Line"
dygraph(m, main = 'MACD') %>%
  dyAxis("y", valueRange = c(-8, 8), label = "Rate of Convergence/ Divergence")%>%
  dyRangeSelector(height = 50)

```

***

 - Moving average convergence divergence (MACD) is a trend-following momentum indicator that shows the relationship between two moving averages of prices. 

 - The MACD is calculated by subtracting the 26-day exponential moving average (EMA) from the 12-day EMA. 

 - A nine-day EMA of the MACD, called the "signal line", is then plotted on top of the MACD, functioning as a trigger for buy and sell signals.

 - Traders can look for signal line crossovers, centerline crossovers and divergences to generate signals.