library(tidyquant)
## Warning: package 'tidyquant' was built under R version 3.3.3
## Loading required package: lubridate
## Warning: package 'lubridate' was built under R version 3.3.3
## 
## Attaching package: 'lubridate'
## The following object is masked from 'package:base':
## 
##     date
## Loading required package: PerformanceAnalytics
## Warning: package 'PerformanceAnalytics' was built under R version 3.3.3
## Loading required package: xts
## Warning: package 'xts' was built under R version 3.3.3
## Loading required package: zoo
## Warning: package 'zoo' was built under R version 3.3.3
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
## 
## Attaching package: 'PerformanceAnalytics'
## The following object is masked from 'package:graphics':
## 
##     legend
## Loading required package: quantmod
## Warning: package 'quantmod' was built under R version 3.3.3
## Loading required package: TTR
## Warning: package 'TTR' was built under R version 3.3.3
## Version 0.4-0 included new data defaults. See ?getSymbols.
## Loading required package: tidyverse
## Warning: package 'tidyverse' was built under R version 3.3.3
## Loading tidyverse: ggplot2
## Loading tidyverse: tibble
## Loading tidyverse: tidyr
## Loading tidyverse: readr
## Loading tidyverse: purrr
## Loading tidyverse: dplyr
## Warning: package 'ggplot2' was built under R version 3.3.3
## Warning: package 'tibble' was built under R version 3.3.3
## Warning: package 'tidyr' was built under R version 3.3.3
## Warning: package 'readr' was built under R version 3.3.3
## Warning: package 'purrr' was built under R version 3.3.3
## Warning: package 'dplyr' was built under R version 3.3.3
## Conflicts with tidy packages ----------------------------------------------
## as.difftime(): lubridate, base
## date():        lubridate, base
## filter():      dplyr, stats
## first():       dplyr, xts
## intersect():   lubridate, base
## lag():         dplyr, stats
## last():        dplyr, xts
## setdiff():     lubridate, base
## union():       lubridate, base
## 
## Attaching package: 'tidyquant'
## The following object is masked from 'package:dplyr':
## 
##     as_tibble
## The following object is masked from 'package:tibble':
## 
##     as_tibble
library(mdsr)
## Warning: package 'mdsr' was built under R version 3.3.3
## Warning: package 'mosaic' was built under R version 3.3.3
## Loading required package: lattice
## Loading required package: ggformula
## Warning: package 'ggformula' was built under R version 3.3.3
## 
## New to ggformula?  Try the tutorials: 
##  learnr::run_tutorial("introduction", package = "ggformula")
##  learnr::run_tutorial("refining", package = "ggformula")
## Loading required package: mosaicData
## Warning: package 'mosaicData' was built under R version 3.3.3
## 
## The 'mosaic' package masks several functions from core packages in order to add 
## additional features.  The original behavior of these functions should not be affected by this.
## 
## Note: If you use the Matrix package, be sure to load it BEFORE loading mosaic.
library(ggplot2)
library(stringr)
library(lubridate)
library(reshape2)
## Warning: package 'reshape2' was built under R version 3.3.3
## 
## Attaching package: 'reshape2'
## The following object is masked from 'package:tidyr':
## 
##     smiths
library(dplyr)

1. Enhanced Financial Data Visualizations

AMZN = tq_get("AMZN", get = "stock.prices",from="2007-01-01", to = "2017-01-01")
## Warning: package 'bindrcpp' was built under R version 3.3.3
FANG = c("GOOG","AMZN","AAPL","MSFT") %>%
    tq_get(get="stock.prices", from = "2007-01-01", to = "2017-01-01")

Set up dates for zoom window and create a chart

end = ymd("2017-01-01")
start = end-weeks(20)
AMZN %>%
    ggplot(aes(x=date, y=close)) + geom_line() +
    labs(title= "AMZN: Line Chart the Previous Way", 
         subtitle = "This is good, but doesn't give us open,high,low, or 
         direction information", x = "", y = "Closing Price") + 
    coord_x_date(xlim = c(start, end), ylim = c(700, 870) )

Create a candlestick chart for Amazon data:

AMZN %>%
    ggplot(aes(x=date, y=close)) + geom_candlestick(aes(open=open, close=close,high=high, low=low)) + labs(title="AMZN:New Candlestick Geom!",
                                                                                                           subtitle = "Visually shows open, high, low, and close information, along with direction", 
                                                                                                           x="", y="Closing Price")+
    coord_x_date(xlim = c(start, end), ylim = c(700, 870))

Create a candlestick chart for Google data:

library(dplyr)
GOOG = tq_get("GOOG", get = "stock.prices",from="2007-01-01", to = "2017-01-01")
FANG = c("GOOG","AMZN","AAPL","MSFT") %>%
    tq_get(get="stock.prices", from = "2007-01-01", to = "2017-01-01")
GOOG %>%
    ggplot(aes(x=date, y=close)) + geom_candlestick(aes(open=open, close=close, high=high, low=low)) + labs(title = "GOOG: New Candlestick Geom!", 
                                                                            subtitle = "Visually shows open, high, low, and close information, along with direction", x ="", y= "Closing Price") + coord_x_date(xlim = c(start, end), ylim = c(700, 870))

Show line chart for Google:

GOOG = tq_get("GOOG", get = "stock.prices",from="2007-01-01", to = "2017-01-01")
FANG = c("GOOG","AMZN","AAPL","MSFT") %>%
    tq_get(get="stock.prices", from = "2007-01-01", to = "2017-01-01")
end = ymd("2017-01-01")
start = end-weeks(20)
GOOG %>%
    ggplot(aes(x=date, y=close)) + geom_line() +
    labs(title= "GOOG: Line Chart the Previous Way", 
         subtitle = "This is good, but doesn't give us open,high,low, or 
         direction information", x = "", y = "Closing Price") + 
    coord_x_date(xlim = c(start, end), ylim = c(700, 870) )

Set up dates (52 weeks) for zoom window for Coke (“KO”):

library(dplyr)
KO = tq_get("KO", get = "stock.prices", from = "2007-01-01", to = "2017-01-01")
FANG = c("FB", "CRM", "NFLX", "GOOG") %>%
  tq_get(get="stock.prices", from = "2007-01-01", to = "2017-01-01")
end = ymd("2017-01-01")
start = end - weeks(52)
KO %>%
  ggplot(aes(x=date, y=close)) + geom_line() + labs(title = "KO: Line Chart the Previous Way", subtitle = "This doesn't give us open, high, low, or Direction information", x="", y="Closing Price") + coord_x_date(xlim = c(start, end), ylim = c(25, 70))

Set up dates (52 weeks) for zoom window for Apple (“AAPL”):

AAPL = tq_get("AAPL", get = "stock.prices", from = "2007-01-01", to = "2017-01-01")
FANG = c("FB", "CRM", "NFLX", "GOOG") %>%
  tq_get(get="stock.prices", from = "2007-01-01", to = "2017-01-01")
end = ymd("2017-01-01")
start = end - weeks(52)
AAPL %>%
  ggplot(aes(x=date, y=close)) + geom_line() + labs(title = "AAPL: Line Chart the Previous Way", subtitle = "This doesn't give us open, high, low, or Direction information", x="", y="Closing Price") + coord_x_date(xlim = c(start, end), ylim = c(25, 200))

Set up dates (52 weeks) for zoom window for Salesforce (“CRM”):

CRM = tq_get("CRM", get = "stock.prices", from = "2007-01-01", to = "2017-01-01")
FANG = c("FB", "CRM", "NFLX", "GOOG") %>%
  tq_get(get="stock.prices", from = "2007-01-01", to = "2017-01-01")
end = ymd("2017-01-01")
start = end - weeks(52)
CRM %>%
  ggplot(aes(x=date, y=close)) + geom_line() + labs(title = "CRM: Line Chart the Previous Way", subtitle = "This doesn't give us open, high, low, or Direction information", x="", y="Closing Price") + coord_x_date(xlim = c(start, end), ylim = c(25, 200))