R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

library(quantmod)
## Loading required package: xts
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
## Loading required package: TTR
## Version 0.4-0 included new data defaults. See ?getSymbols.
library(plyr)
library(xts)
library(zoo)
library(dygraphs)
library(DT)
what_metrics <- yahooQF(c("Price/Sales", 
                          "P/E Ratio",
                          "Price/EPS Estimate Next Year",
                          "PEG Ratio",
                          "Dividend Yield",
                          "Return On Assets",
                          "Return On Equity",
                          "Market Capitalization"))

tickers <- c("AAPL", "TWTR", "MSFT")
# Not all the metrics are returned by Yahoo.
metrics <- getQuote(paste(tickers, sep="", collapse=";"), what=what_metrics)

#Add tickers as the first column and remove the first column which had date stamps
metrics <- data.frame(Symbol=tickers, metrics[,2:length(metrics)]) 

#Change colnames
colnames(metrics) <- c("Symbol", "Revenue Multiple", "Earnings Multiple","Earnings Multiple (Forward)", "Price-to-Earnings-Growth", "Div Yield", "Market Cap")
#Persist this to the csv file
#write.csv(metrics, "FinancialMetrics.csv", row.names=FALSE)
DT::datatable(metrics)
#AAPL stock graph
start <- as.Date("2017-01-01")
end <- as.Date("2017-07-01")
getSymbols("AAPL", src = "yahoo", from = start, to = end)
## 'getSymbols' currently uses auto.assign=TRUE by default, but will
## use auto.assign=FALSE in 0.5-0. You will still be able to use
## 'loadSymbols' to automatically load data. getOption("getSymbols.env")
## and getOption("getSymbols.auto.assign") will still be checked for
## alternate defaults.
## 
## This message is shown once per session and may be disabled by setting 
## options("getSymbols.warning4.0"=FALSE). See ?getSymbols for details.
## 
## WARNING: There have been significant changes to Yahoo Finance data.
## Please see the Warning section of '?getSymbols.yahoo' for details.
## 
## This message is shown once per session and may be disabled by setting
## options("getSymbols.yahoo.warning"=FALSE).
## Warning in sample.int(length(x), size, replace, prob): '.Random.seed' is
## not an integer vector but of type 'NULL', so ignored
## [1] "AAPL"
plot(AAPL[, "AAPL.Close"], main = "AAPL")

#TWTR stock graph
start <- as.Date("2017-01-01")
end <- as.Date("2017-07-01")
getSymbols("TWTR", src = "yahoo", from = start, to = end)
## [1] "TWTR"
plot(TWTR[, "TWTR.Close"], main = "TWTR")

#MSFT stock graph
start <- as.Date("2017-01-01")
end <- as.Date("2017-07-01")
getSymbols("MSFT", src = "yahoo", from = start, to = end)
## [1] "MSFT"
plot(MSFT[, "MSFT.Close"], main = "MSFT")

dygraph(TWTR[,2:4], group = "stocks") %>% 
  dySeries(c("TWTR.Low", "TWTR.Close", "TWTR.High"), label = "TWTR")

Microsoft

dygraph(MSFT[,2:4], group = "stocks") %>% 
  dySeries(c("MSFT.Low", "MSFT.Close", "MSFT.High"), label = "MSFT")

Apple

dygraph(AAPL[,2:4], group = "stocks") %>% 
  dySeries(c("AAPL.Low", "AAPL.Close", "AAPL.High"), label = "AAPL")