library(rvest)
## Warning: package 'rvest' was built under R version 3.3.3
## Loading required package: xml2
## Warning: package 'xml2' was built under R version 3.3.3
library(pbapply)
library(TTR)
## Warning: package 'TTR' was built under R version 3.3.3
library(dygraphs)
## Warning: package 'dygraphs' was built under R version 3.3.3
library(lubridate)
## Warning: package 'lubridate' was built under R version 3.3.3
## 
## Attaching package: 'lubridate'
## The following object is masked from 'package:base':
## 
##     date
stock.list<-'https://www.loyal3.com/stocks'
stocks<-read_html(stock.list)
stocks.names<-html_nodes(stocks,'.company-name')
stocks.names<-html_text(stocks.names)

stocks.names
##  [1] "21st Century Fox"                "AMC Theatres"                   
##  [3] "Abercrombie & Fitch"             "Alibaba "                       
##  [5] "Alphabet"                        "Amazon.com"                     
##  [7] "American Eagle Outfitters"       "American Express"               
##  [9] "Anheuser-Busch InBev"            "Apple"                          
## [11] "At Home"                         "Berkshire Hathaway"             
## [13] "Best Buy"                        "Blizzard"                       
## [15] "Blue Buffalo"                    "Buffalo Wild Wings"             
## [17] "Coca-Cola"                       "Dave & Buster's"                
## [19] "Discovery Communications"        "Disney"                         
## [21] "Dr Pepper Snapple"               "Dunkin"                         
## [23] "Electronic Arts"                 "Endurance"                      
## [25] "Facebook"                        "First Data"                     
## [27] "Frontier Communications"         "Gap"                            
## [29] "Globant"                         "GoDaddy"                        
## [31] "GoPro"                           "Hasbro"                         
## [33] "Hershey"                         "HubSpot"                        
## [35] "Intel"                           "Kate Spade"                     
## [37] "Kellogg's "                      "Kohl's"                         
## [39] "Kraft Heinz"                     "L Brands"                       
## [41] "Macy's"                          "Mattel"                         
## [43] "McDonald's"                      "Microsoft "                     
## [45] "Mondelez"                        "Monster"                        
## [47] "Netflix"                         "Nike"                           
## [49] "Nokia"                           "PVH "                           
## [51] "Pepsi"                           "Ralph Lauren"                   
## [53] "Restaurant Brands International" "STORE Capital"                  
## [55] "Santander Consumer USA"          "Square"                         
## [57] "Starbucks"                       "Target"                         
## [59] "Teladoc"                         "Tesla Motors"                   
## [61] "Time Warner"                     "Trupanion"                      
## [63] "Twitter"                         "Unilever"                       
## [65] "VF Corp"                         "Viacom"                         
## [67] "Wal-Mart"                        "World Wrestling Entertainment"  
## [69] "YUM"                             "Yahoo"
loyal.links<-html_nodes(stocks, "a")
loyal.links<-html_attr(loyal.links, "href")
stock.links<-paste0('http://www.loyal3.com',loyal.links[54:123])

get.ticker<-function(url){
  x<-read_html(url)
  x<-html_node(x,'.ticker-price')
  x<-html_text(x)
  x<-sub("^([[:alpha:]]*).*", "\\1", x)
  return(x)
  }

stock.tickers<-pblapply(stock.links[2],get.ticker)

stock.ticks<-do.call(rbind,stock.tickers)
stock.ticks<-data.frame(symbol=stock.ticks,name=stocks.names)

start.date<-Sys.Date()
end.date<-Sys.Date()-years(3)

start.date<-gsub('-','', start.date)
end.date<-gsub('-','', end.date)

stocks.ts<-pblapply(stock.ticks$symbol,getYahooData,end.date, start.date)

names(stocks.ts)<-stock.ticks$symbol
head(stocks.ts$KO)
##                Open     High      Low    Close   Volume Unadj.Close Div
## 2014-05-01 37.08827 37.09736 36.60636 37.07917 16921438       40.78  NA
## 2014-05-02 37.08827 37.24284 36.94279 37.23375 15613986       40.95  NA
## 2014-05-05 36.96097 37.21556 36.87004 37.06099 11177907       40.76  NA
## 2014-05-06 36.84277 36.97006 36.77912 36.81549 10294100       40.49  NA
## 2014-05-07 37.00643 37.25193 36.94279 37.19737 12225035       40.91  NA
## 2014-05-08 37.14282 37.24284 36.96097 37.03371  9390937       40.73  NA
##            Split Adj.Div
## 2014-05-01    NA      NA
## 2014-05-02    NA      NA
## 2014-05-05    NA      NA
## 2014-05-06    NA      NA
## 2014-05-07    NA      NA
## 2014-05-08    NA      NA
plot(stocks.ts$KO$Close)

dygraph(stocks.ts$KO$Close, main = "KO Stock Price") %>%
  dyRangeSelector(dateWindow = c("2013-12-18", "2016-12-30"))
head(SMA(stocks.ts$KO$Close, 200))
##            SMA
## 2014-05-01  NA
## 2014-05-02  NA
## 2014-05-05  NA
## 2014-05-06  NA
## 2014-05-07  NA
## 2014-05-08  NA
head(SMA(stocks.ts$KO$Close, 50))
##            SMA
## 2014-05-01  NA
## 2014-05-02  NA
## 2014-05-05  NA
## 2014-05-06  NA
## 2014-05-07  NA
## 2014-05-08  NA
mov.avgs<-function(stock.df){
  stock.close<-stock.df[,4]
  ifelse((nrow(stock.df)<(2*260)),
         x<-data.frame(stock.df, 'NA', 'NA'),
         x<-data.frame(stock.df, SMA(stock.close, 200), SMA(stock.close, 50)))
  colnames(x)<-c(names(stock.df), 'sma_200','sma_50')
  x<-x[complete.cases(x$sma_200),]
  return(x)
  }

stocks.ts<-pblapply(stocks.ts, mov.avgs)

dygraph(stocks.ts$KO[,c('sma_200','sma_50')],main = 'KO Moving Averages') %>%
  dySeries('sma_50', label = 'sma 50') %>%
  dySeries('sma_200', label = 'sma 200') %>%
  dyRangeSelector(height = 30) %>%
  dyShading(from = '2016-4-28', to = '2016-7-27', color = '#CCEBD6') %>%
  dyShading(from = '2016-7-28', to = '2016-12-30', color = '#FFE6E6')