Column

Amazon vs Google vs Apple

Column

Amazon

Google

Apple

---
title: "ANLY 512 - Dashboarding Lab"
author: "Varun Ashok Bhagat & Varun Vincent James"
date: "September 5, 2017"
output: 
  flexdashboard::flex_dashboard:
    orientation: columns
    theme: cosmo
    social: menu
    source_code: embed
    
---

```{r message = FALSE}
library(flexdashboard, lib.loc="C:/software/Rpackages")
library(knitr, lib.loc="C:/software/Rpackages")
library(scales, lib.loc="C:/software/Rpackages")
library(dplyr, lib.loc="C:/software/Rpackages")
library(ggplot2, lib.loc="C:/software/Rpackages")
library(reshape2, lib.loc="C:/software/Rpackages")
library(plyr, lib.loc="C:/software/Rpackages")
library(grid, lib.loc="C:/software/Rpackages")
library(formatR, lib.loc="C:/software/Rpackages")
library(highr, lib.loc="C:/software/Rpackages")
library(ggthemes, lib.loc="C:/software/Rpackages")
library(xtable, lib.loc="C:/software/Rpackages")
library(devtools, lib.loc="C:/software/Rpackages")
library(lubridate, lib.loc="C:/software/Rpackages")
library(quantmod, lib.loc="C:/software/Rpackages")
library(tidyquant, lib.loc="C:/software/Rpackages")
library(cowplot, lib.loc="C:/software/Rpackages")
library(PerformanceAnalytics, lib.loc="C:/software/Rpackages")
library(dygraphs, lib.loc="C:/software/Rpackages")
library(data.table, lib.loc="C:/software/Rpackages")
library(knitr, lib.loc="C:/software/Rpackages")
library(stringr, lib.loc="C:/software/Rpackages")
library(XML, lib.loc="C:/software/Rpackages")

fn_get_news <- function(stock, number)
{
  url.b1 = 'http://www.google.com/finance/company_news?q='
  url    = paste(url.b1, stock, '&output=rss', "&start=", 1,
                 "&num=", number, sep = '')
  url    = URLencode(url)
  
  # parse xml tree, get item nodes, extract data and return data frame
  doc   = xmlTreeParse(url, useInternalNodes = T);
  nodes = getNodeSet(doc, "//item");
  vDF  = ldply(nodes, as.data.frame(xmlToList))
  News=vDF$value.description
  vNews=data.frame(News)
  colnames(vNews)=c(paste(stock,"-News",sep = ""))
  return(vNews)    
}

fn_get_stock_data <- function(vStockName)
{
  vGetData = getSymbols(vStockName,from=Sys.Date()-days(90), to=Sys.Date(),auto.assign = FALSE)
    vReturnData=periodReturn(vGetData, period='daily', subset=paste(Sys.Date() - days(90), "::", sep = ""), type ='log')
  colnames(vReturnData) = as.character(vStockName)
  assign(paste("v",vStockName,sep = ""), data.frame(Date=index(vGetData), coredata(vGetData)), .GlobalEnv)
  assign(paste("vReturns",vStockName,sep = ""), vReturnData, .GlobalEnv)
  
}



#Get Data
#-----------------------------------------------------------------------
fn_get_stock_data('AMZN')
fn_get_stock_data('GOOG')
fn_get_stock_data('AAPL')

colnames(vAMZN)=c("Date","Open", "High","Low","Close", "Volume", "Adjusted")
colnames(vGOOG)=c("Date","Open", "High","Low","Close", "Volume", "Adjusted")
colnames(vAAPL)=c("Date","Open", "High","Low","Close", "Volume", "Adjusted")

vMergedData = merge.xts(vReturnsAMZN,vReturnsGOOG,vReturnsAAPL)
```


Column {.sidebar data-with=550}
-------------------------------------

```{r message = FALSE}
kable(fn_get_news('AMZN',1), format = "markdown")
kable(fn_get_news('GOOG',1), format = "markdown")
kable(fn_get_news('AAPL',1), format = "markdown")
```

Column {data-with=550}
-------------------------------------

### Amazon vs Google vs Apple

```{r message = FALSE}
dygraph(vMergedData, main = "Daily Returns over 90 days") %>%
  dyAxis("y", label = "Retruns") %>%
  dyOptions(colors = RColorBrewer::brewer.pal(5, "Set1")) %>%
  dyHighlight(highlightSeriesBackgroundAlpha = 0.2,
              highlightSeriesOpts = list(strokeWidth = 3)) %>%
  dyRangeSelector(height = 75)

```

Column {.tabset data-with=450}
-------------------------------------

### Amazon

```{r message = FALSE}
vAMZN %>%
  ggplot(aes(x = Date, y = Close)) +
  geom_candlestick(aes(open = Open, high = High, low = Low, close = Close)) +
  labs(title = "Daily Closing Prices Trend", 
       subtitle = "Last 90 days",
       y = "Closing Price", x = "") +geom_smooth(method="loess")+theme_tq()+theme(plot.title = element_text(face="bold", size =22))+theme(axis.title = element_text(face="bold",size=12))

```

### Google

```{r message = FALSE}
vGOOG %>%
  ggplot(aes(x = Date, y = Close)) +
  geom_candlestick(aes(open = Open, high = High, low = Low, close = Close)) +
  labs(title = "Daily Closing Prices Trend", 
       subtitle = "Last 90 days",
       y = "Closing Price", x = "") +geom_smooth(method="loess")+theme_tq()+theme(plot.title = element_text(face="bold", size =22))+theme(axis.title = element_text(face="bold",size=12))
``` 

### Apple

```{r message = FALSE}
vAAPL %>%
  ggplot(aes(x = Date, y = Close)) +
  geom_candlestick(aes(open = Open, high = High, low = Low, close = Close)) +
  labs(title = "Daily Closing Prices Trends", 
       subtitle = "Last 90 days",
       y = "Closing Price", x = "") +geom_smooth(method="loess")+theme_tq()+theme(plot.title = element_text(face="bold", size =22))+theme(axis.title = element_text(face="bold",size=12))
```